exporters_export-stl.js

import oc from "../opencascade/initializer";

/**
 * Export entities to a binary STL file.
 * @memberof exporters
 * @alias exportStl
 * @param {Object} parameters - Export parameters.
 * @param {Array} parameters.entities - The entities array can include `Solid`, `Face`, `Edge`, and `Wire` objects.
 * @param {number} [parameters.linearTolerance=0.01] - The linear tolerance for triangulation.
 * @param {number} [parameters.angularTolerance=0.05] - The angular tolerance for triangulation.
 * @returns {string|null} - The content of the STL file as a binary string, or null if the export failed.
 */
const exportStl = ({entities, linearTolerance = 0.01, angularTolerance = 0.05}) => {
  const filename = "temp";
  const compound = new oc.TopoDS_Compound();
  const builder = new oc.BRep_Builder();
  builder.MakeCompound(compound);
  entities.forEach((entity) => {
    builder.Add(compound, entity.wrapped);
  });
  new oc.BRepMesh_IncrementalMesh_2(compound, linearTolerance, false, angularTolerance, false);
  const writer = new oc.BinaryStlWriter();
  const result = writer.Write(compound, filename, new oc.Message_ProgressRange_1());
  if (!result) {
    return null;
  }
  const content = oc.FS.readFile(`/${filename}`, {encoding: "binary"});
  oc.FS.unlink(`/${filename}`);
  return content;
};

export {exportStl};