Replies: 5 comments 2 replies
-
Hey, implementing a simple step to gltf converter is easy. Less than 20-30 lines of code, I think. Check out the starter template for nodejs (it's trivial). Then check out how the beginner example adds opencascade shapes to a document and then exports that as a glb file, which is used for rendering (via ObjectURL). Instead of creating an ObjectURL, you want to output the glb file to disk. For reading STEP files, there is STEPControl_Reader and STEPCAFControl_Reader - the latter is more powerful and can import colors and product structure. |
Beta Was this translation helpful? Give feedback.
-
STEPCAFControl_Reader does not have a function TransferRoots. Try something like this: const reader = new oc.STEPCAFControl_Reader_1();
reader.ReadFile("./file.stp");
oc.FS.unlink('/file.stp');
if(!reader.Transfer_1(new oc.Handle_TDocStd_Document_2(doc), new oc.Message_ProgressRange_1())) throw new Error();
// now, the STEP file is in your document |
Beta Was this translation helpful? Give feedback.
-
Hello again, For anybody needs something like what asked, I want to share my solution below. @donalffons this is working. But too slow for big assemblies. Do you have any recommendations for increasing conversion speed? import initOpenCascade from "opencascade.js/dist/node.js";
import fs from 'fs';
import path from 'path';
const loadSTEPorIGES = async (openCascade, file, fileName) => {
const fileText = fs.readFileSync(file.path, 'utf8');
const fileType = (() => {
switch (fileName.toLowerCase().split(".").pop()) {
case "step":
case "stp":
return "step";
case "iges":
case "igs":
return "iges";
default:
return undefined;
}
})();
// Writes the uploaded file to Emscripten's Virtual Filesystem
openCascade.FS.createDataFile("/", `file.${fileType}`, fileText, true, true);
// Choose the correct OpenCascade file parsers to read the CAD file
var reader = null;
if (fileType === "step") {
reader = new openCascade.STEPCAFControl_Reader_1();
} else if (fileType === "iges") {
reader = new openCascade.IGESCAFControl_Reader_1();
} else { console.error("opencascade.js can't parse this extension! (yet)"); }
const readResult = reader.ReadFile(`file.${fileType}`); // Read the file
if (readResult === openCascade.IFSelect_ReturnStatus.IFSelect_RetDone) {
console.log("file loaded successfully! Converting to OCC now...");
//const numRootsTransferred = reader.TransferRoots(new openCascade.Message_ProgressRange_1()); // Translate all transferable roots to OpenCascade
// Create a document add our shapes
const doc = new openCascade.TDocStd_Document(new openCascade.TCollection_ExtendedString_1());
if (!reader.Transfer_1(new openCascade.Handle_TDocStd_Document_2(doc), new openCascade.Message_ProgressRange_1())) throw new Error();
// Obtain the results of translation in one OCCT shape
console.log(fileName + " converted successfully! Triangulating now...");
return visualizeShapes(openCascade, doc, fileName, fileType);
} else {
console.error("Something in OCCT went wrong trying to read " + fileName);
}
};
function visualizeDoc(oc, doc, fileName, fileType) {
const justName = path.parse(fileName).name;
// Export a GLB file (this will also perform the meshing)
const cafWriter = new oc.RWGltf_CafWriter(new oc.TCollection_AsciiString_2(`./${justName}.glb`), true);
cafWriter.Perform_2(new oc.Handle_TDocStd_Document_2(doc), new oc.TColStd_IndexedDataMapOfStringString_1(), new oc.Message_ProgressRange_1());
// Out with the old, in with the new!
console.log(fileName + " triangulated and added to the scene!");
// Remove the file when we're done (otherwise we run into errors on reupload)
oc.FS.unlink(`/file.${fileType}`);
// Read the GLB file from the virtual file system
const glbFile = oc.FS.readFile(`./${justName}.glb`, { encoding: "binary" });
const fileObj = { file: glbFile, filename: fileName };
return fileObj;
}
function visualizeShapes(oc, doc, fileName, fileType) {
const aRootLabels = new oc.TDF_LabelSequence_1();
const aCompound = new oc.TopoDS_Compound();
const shapeTool = oc.XCAFDoc_DocumentTool.ShapeTool(doc.Main()).get();
shapeTool.GetFreeShapes(aRootLabels);
const BRepBuilder = new oc.BRep_Builder();
BRepBuilder.MakeCompound(aCompound);
console.log(aRootLabels.Length());
for (let i = 1; i <= aRootLabels.Length(); i++) {
const aRootShape = new oc.TopoDS_Shape();
const aRootLabel = aRootLabels.Value(i);
if (oc.XCAFDoc_ShapeTool.GetShape_1(aRootLabel, aRootShape)) {
BRepBuilder.Add(aCompound, aRootShape);
}
}
const aDrawer = new oc.Prs3d_Drawer();
const anAlgo = new oc.BRepMesh_IncrementalMesh_1();
anAlgo.ChangeParameters().Deflection = 0.2;
anAlgo.ChangeParameters().Angle = 20.0 * Math.PI / 180.0; // 20 degrees
anAlgo.ChangeParameters().InParallel = true;
anAlgo.SetShape(aCompound);
anAlgo.Perform_1(new oc.Message_ProgressRange_1());
console.log("hey");
// Return our visualized document
return visualizeDoc(oc, doc, fileName, fileType);
}
const convertSTEPorIGES = async (file) => {
const openCascade = await initOpenCascade();
console.log("inside opencascade");
return loadSTEPorIGES(openCascade, file, file.filename);
}
export default convertSTEPorIGES; |
Beta Was this translation helpful? Give feedback.
-
I tried to change c++ code in the URL I shared to js, I couldn't nail only this line of code:
So I just gave a value of 0.2 |
Beta Was this translation helpful? Give feedback.
-
Hello, I've tried running this code, but in the latest release (sept 2020), TDF_LabelSequence_1 is undefined (as well as Message_ProgressRange_1, but it runs without it). Until that line it runs. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I need a solution like literally in this link https://draw.sview.ru/xde-export-step2gltf/
It works perfect. And I want to implement it in a nodejs app. I am so new to OCC and have no knowledge on C++.
Do you guys think I am able to achieve same thing with opencascade.js?
Thanks everyone contributed to this project.
Beta Was this translation helpful? Give feedback.
All reactions