OpenJSCAD.org icon indicating copy to clipboard operation
OpenJSCAD.org copied to clipboard

Three.js scene export to dxf

Open Azure-Phoenix opened this issue 5 months ago • 2 comments

Could you help me to use this library to export my three.js scene to dxf?

function exportSceneToSTL(scene) {
  const exporter = new STLExporter();
  return exporter.parse(scene, { binary: false }); // ASCII STL
}

async function convertSTLtoDXF(stlString) {
  let deserializedStl = stlDeserializer.deserialize(
    {
      output: "csg", //jscad
    },
    stlString
  );
  console.log("CSG data were got");
  console.log(dxfSerializer);
  let dxfStringArray = dxfSerializer.serialize(deserializedStl);
  console.log("DXF data were got");
  return dxfStringArray;
}

function downloadDXF(dxfString, filename = "scene.dxf") {
  const blob = new Blob([dxfString], { type: "application/dxf" });
  const link = document.createElement("a");
  link.href = URL.createObjectURL(blob);
  link.download = filename;
  link.click();
}

/**
 * Action Listeners
 */
// Export on spacebar press
window.addEventListener("keydown", async (event) => {
  if (event.code === "Space") {
    const stlString = exportSceneToSTL(scene);
    const dxfString = await convertSTLtoDXF(stlString);
    downloadDXF(dxfString);
  }
});

I tried this steps, but it returns Uncaught (in promise) Error: only JSCAD geometries can be serialized to DXF error...

(The code is inspired from this discussion)

Azure-Phoenix avatar Jul 02 '25 19:07 Azure-Phoenix

Sounds like could be possible. Can you make scene with a simple cube, and paste ouput of console.log("CSG data were got",deserializedStl); here.

hrgdavor avatar Jul 03 '25 06:07 hrgdavor

The error means that the input to dxfSerializer.serialize() did not contain any JSCAD geometries. So, double check the results of stlDeserializer.deserialize(), or exporter.parse(). You are probably close.

z3dev avatar Nov 30 '25 00:11 z3dev