Do not write jsons to a file.
This commit is contained in:
parent
4574d698ec
commit
8c468ba00d
@ -2,46 +2,40 @@ let rhino3dm = require ('../../../libs/rhino3dm.min.js')
|
||||
let fs = require ('fs');
|
||||
let path = require ('path');
|
||||
|
||||
function Write3dmFileToJson (rhino, rhinoFileName, outputFolder)
|
||||
function GetThreeMeshesFrom3dm (rhino, rhinoFileName)
|
||||
{
|
||||
console.log ('Writing 3dm to json: ' + outputFolder);
|
||||
if (!fs.existsSync (outputFolder)){
|
||||
fs.mkdirSync (outputFolder);
|
||||
}
|
||||
let inputBuffer = fs.readFileSync (rhinoFileName, null).buffer;
|
||||
let inputArray = new Uint8Array (inputBuffer);
|
||||
let inputDoc = rhino.File3dm.fromByteArray (inputArray);
|
||||
let objects = inputDoc.objects ();
|
||||
let threeMeshes = [];
|
||||
for (let i = 0; i < objects.count; i++) {
|
||||
let mesh = objects.get (i).geometry ();
|
||||
if (mesh instanceof rhino.Mesh) {
|
||||
let threeJson = mesh.toThreejsJSON ();
|
||||
let jsonFileName = path.join (outputFolder, 'mesh' + i + '.json');
|
||||
fs.writeFileSync (jsonFileName, JSON.stringify (threeJson, null, 4));
|
||||
threeMeshes.push (mesh.toThreejsJSON ());
|
||||
}
|
||||
}
|
||||
return threeMeshes;
|
||||
}
|
||||
|
||||
function WriteJsonTo3dmFile (rhino, inputFolder)
|
||||
function WriteThreeMeshesTo3dm (rhino, threeMeshes, rhinoFileName)
|
||||
{
|
||||
console.log ('Writing json to 3dm: ' + inputFolder);
|
||||
let outputDoc = new rhino.File3dm ();
|
||||
let fileNames = fs.readdirSync (inputFolder);
|
||||
for (let i = 0; i < fileNames.length; i++) {
|
||||
let fileName = fileNames[i];
|
||||
if (path.extname (fileName) != '.json') {
|
||||
continue;
|
||||
}
|
||||
let filePath = path.join (inputFolder, fileName);
|
||||
let fileContent = fs.readFileSync (filePath);
|
||||
let jsonContent = JSON.parse (fileContent);
|
||||
let rhinoMesh = new rhino.Mesh.createFromThreejsJSON (jsonContent);
|
||||
for (let i = 0; i < threeMeshes.length; i++) {
|
||||
let rhinoMesh = new rhino.Mesh.createFromThreejsJSON (threeMeshes[i]);
|
||||
outputDoc.objects ().add (rhinoMesh, null);
|
||||
}
|
||||
let writeOptions = new rhino.File3dmWriteOptions ();
|
||||
writeOptions.version = 6;
|
||||
let outputBuffer = outputDoc.toByteArray (writeOptions);
|
||||
fs.writeFileSync (path.join (inputFolder, 'model.3dm'), outputBuffer);
|
||||
fs.writeFileSync (rhinoFileName, outputBuffer);
|
||||
}
|
||||
|
||||
function ReadWriteRhinoFile (rhinoIn, rhinoOut, inputRhinoFile, outputRhinoFile)
|
||||
{
|
||||
let threeMeshes = GetThreeMeshesFrom3dm (rhinoIn, inputRhinoFile);
|
||||
console.log ('Meshes in ' + inputRhinoFile + ': ' + threeMeshes.length);
|
||||
WriteThreeMeshesTo3dm (rhinoOut, threeMeshes, outputRhinoFile);
|
||||
}
|
||||
|
||||
if (!fs.existsSync ('output')){
|
||||
@ -49,14 +43,9 @@ if (!fs.existsSync ('output')){
|
||||
}
|
||||
|
||||
rhino3dm ().then (async function (rhinoIn) {
|
||||
Write3dmFileToJson (rhinoIn, 'input/hello_mesh.3dm', 'output/hello_mesh');
|
||||
Write3dmFileToJson (rhinoIn, 'input/one_cube.3dm', 'output/one_cube');
|
||||
Write3dmFileToJson (rhinoIn, 'input/two_cubes.3dm', 'output/two_cubes');
|
||||
Write3dmFileToJson (rhinoIn, 'input/three_cubes.3dm', 'output/three_cubes');
|
||||
rhino3dm ().then (async function (rhinoOut) {
|
||||
WriteJsonTo3dmFile (rhinoOut, 'output/hello_mesh');
|
||||
WriteJsonTo3dmFile (rhinoOut, 'output/one_cube');
|
||||
WriteJsonTo3dmFile (rhinoOut, 'output/two_cubes');
|
||||
WriteJsonTo3dmFile (rhinoOut, 'output/three_cubes');
|
||||
ReadWriteRhinoFile (rhinoIn, rhinoOut, 'input/one_cube.3dm', 'output/one_cube.3dm');
|
||||
ReadWriteRhinoFile (rhinoIn, rhinoOut, 'input/two_cubes.3dm', 'output/two_cubes.3dm');
|
||||
ReadWriteRhinoFile (rhinoIn, rhinoOut, 'input/three_cubes.3dm', 'output/three_cubes.3dm');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user