0

How to convert JSModeller jsonData for uploaded file to JSModeller body to use in JSM.ExportBodyToStl()

Requirement: option to upload stl/obj file, render it and at the end give an option to export to either stl/obj file

https://3dviewer.net/ has the option to upload files but does not have option to export to stl/obj

Problem: cannot get the body of the uploaded file.

// this code is from 3dviewer.net **impoterapp.js**
var processorFunc = JSM.ConvertFileListToJsonData;
if (isUrl) {
    processorFunc = JSM.ConvertURLListToJsonData;
}
processorFunc (userFiles, {
    onError : function () {
        myThis.GenerateError ('No readable file found.');
        myThis.SetReadyForTest ();
        return;
    },
    onReady : function (fileNames, jsonData) {
        myThis.fileNames = fileNames
        // i get the jsonData here 
        // 
        // how do i convert jsonData to jsmodeller object

    }
});

JSModeller has an export Option here http://kovacsv.github.io/JSModeler/documentation/demo/demonstration.html but all the examples they used here are created with generator function like JSM.LegoBrickGenerator(), JSM.ShapeGenerator (), etc

how do we generate/get/convert an stl/obj file to JSModeller Body ? as we need to pass body to ExportBodyToStl

example http://kovacsv.github.io/JSModeler/documentation/jsmdoc/index.html#ExportBodyToStl

JSM.ExportBodyToStl (body, 'JSModelerBody', false);
JSM.ExportBodyToObj (body, 'JSModelerBody', false);

above example of exportBody is from http://kovacsv.github.io/JSModeler/documentation/demo/demonstration.html

Shantanu Terang
  • 208
  • 3
  • 6

1 Answers1

1

JSModeler has an own model format called JSM.Model. You can export this model to obj and stl, or to json format. When you import something it generates the json format directly, and there is no way to convert it back to JSM.Model.

By the way I have an unpublished code which converts the json format to stl, I hope it will help you:

function ConvertJsonDataToStl (model)
{
    function GetVertex (vertices, index)
    {
        var result = new JSM.Coord (
            parseFloat (vertices[parseInt (index) * 3 + 0]),
            parseFloat (vertices[parseInt (index) * 3 + 1]),
            parseFloat (vertices[parseInt (index) * 3 + 2])
        );
        return result;
    }

    var stl = '';
    stl += 'solid Model\n';
    var meshId, triangleId, paramId, mesh, vertices, triangle;
    var v0, v1, v2, normal;
    for (meshId = 0; meshId < model.meshes.length; meshId++) {
        mesh = model.meshes[meshId];
        vertices = mesh.vertices;
        for (triangleId = 0; triangleId < mesh.triangles.length; triangleId++) {
            triangle = mesh.triangles[triangleId];
            for (paramId = 0; paramId < triangle.parameters.length; paramId += 9) {
                mesh = model.meshes[meshId];
                v0 = GetVertex (vertices, triangle.parameters[paramId + 0]);
                v1 = GetVertex (vertices, triangle.parameters[paramId + 1]);
                v2 = GetVertex (vertices, triangle.parameters[paramId + 2]);
                normal = JSM.CalculateTriangleNormal (v0, v1, v2);
                stl += 'facet normal ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
                stl += '\touter loop\n';
                stl += '\t\tvertex '  + v0.x + ' ' + v0.y + ' ' + v0.z + '\n';
                stl += '\t\tvertex '  + v1.x + ' ' + v1.y + ' ' + v1.z + '\n';
                stl += '\t\tvertex '  + v2.x + ' ' + v2.y + ' ' + v2.z + '\n';
                stl += '\tendloop\n';
                stl += 'endfacet\n';
            }
        }
    }
    stl += 'endsolid Model\n';
    return stl;
};
kovacsv
  • 687
  • 4
  • 14