tinytree
tinytree

Reputation: 1099

How to use STEPControl_Writer in openCascade.js?

I'm trying to write a stp file from from my shape.

I dowloaded the example to import a stp file, do something with it and then save it again as a stp file.

So far this is my reader and writer:

var reader = new openCascade.STEPControl_Reader_1();
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

  // return;
  const stepShape = reader.OneShape(); // Obtain the results of translation in one OCCT shape
  console.log(
    inputFile.name + " converted successfully!  Triangulating now..."
  );

  var writer = new openCascade.STEPControl_Writer_1();
  writer.Transfer(reader.OneShape(), openCascade.STEPControl_AsIs);
  writer.Write("test.step");

  // Remove the file when we're done (otherwise we run into errors on reupload)
  openCascade.FS.unlink(`/file.${fileType}`);
} else {
  console.error(
    "Something in OCCT went wrong trying to read " + inputFile.name
  );
}
});

I took the writer from OpenCascade and tried to adapt it to OpenCascade.js. Unfortunately the Transfer method requires four arguments:

https://dev.opencascade.org/doc/refman/html/class_s_t_e_p_control___writer.html

All examples I found only use 2 arguments.

I tried to use a boolean and a range like that, but this didnt work:

  var progressRange = new openCascade.Message_ProgressRange_1();
  writer.Transfer(reader.OneShape(), openCascade.STEPControl_AsIs, true, progressRange);

Didn't find a documentation on opencascade.js either.

Upvotes: 2

Views: 451

Answers (1)

Josh Watzman
Josh Watzman

Reputation: 7260

Here's how I got it to work. I did end up passing all 4 arguments; I'm not sure why other examples sometimes have only two. I think, at least at the C++ level, the last two arguments have default values -- maybe that didn't get properly translated to JS, or to the TS types? In any event, this worked for me:

import type { OpenCascadeInstance, TopoDS_Shape } from 'opencascade.js';

function toSTEP(oc: OpenCascadeInstance, shapes: TopoDS_Shape[]): string {
  const writer = new oc.STEPControl_Writer_1();
  for (const shape of shapes) {
    writer.Transfer(
      shape,
      // @ts-expect-error Problem with emscripten types?
      oc.STEPControl_StepModelType.STEPControl_AsIs,
      true,
      new oc.Message_ProgressRange_1(),
    );
  }

  const filename = 'export.stp';
  writer.Write(filename);

  return oc.FS.readFile('/' + filename, {
    encoding: 'utf8',
  });
}

Upvotes: 0

Related Questions