Segmentation fault when using STEPCAFControl_Reader

I am attempting to use the following code, along with the dev version of OCCT (7.4.1) to convert a step file via command line interface:

// STEP Read Methods
#include <STEPCAFControl_Reader.hxx>
#include <TopoDS_Compound.hxx>
#include <TDocStd_Document.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <TDF_LabelSequence.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS_Shape.hxx>
// Mesh Methods
#include <Prs3d_Drawer.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
// Gltf Methods
#include <Prs3d.hxx>
#include <Prs3d_Drawer.hxx>
#include <StdPrs_ToolTriangulatedShape.hxx>
#include <TColStd_IndexedDataMapOfStringString.hxx>
#include <RWGltf_CafWriter.hxx>
#include <RWMesh_CoordinateSystem.hxx>
#include <XCAFDoc_DocumentTool.hxx>

#include <iostream>

int step2glb(char *in, char *out) {

  // read / create / fill in the document
  Handle(TDocStd_Document) theXdeDoc; // created in advance

  STEPCAFControl_Reader aStepReader;
  if (!aStepReader.ReadFile (in) != IFSelect_RetDone) { std::cout << "parse error\n"; }
  if (!aStepReader.Transfer (theXdeDoc)) { std::cout << "translation error\n"; }

  // collect document roots into temporary compound
  Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool (theXdeDoc->Main());

  TDF_LabelSequence aRootLabels;
  aShapeTool->GetFreeShapes (aRootLabels);

  TopoDS_Compound aCompound;
  BRep_Builder    aBuildTool;
  aBuildTool.MakeCompound (aCompound);
  for (TDF_LabelSequence::Iterator aRootIter (aRootLabels); aRootIter.More(); aRootIter.Next())
    const TDF_Label& aRootLabel = aRootIter.Value();
    TopoDS_Shape aRootShape;
    if (XCAFDoc_ShapeTool::GetShape (aRootLabel, aRootShape))
      aBuildTool.Add (aCompound, aRootShape);

  // perform meshing
  Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
  BRepMesh_IncrementalMesh anAlgo;
  //anAlgo.ChangeParameters().Deflection = Prs3d::GetDeflection (aCompound, aDrawer);
  anAlgo.ChangeParameters().Deflection = StdPrs_ToolTriangulatedShape::GetDeflection (aCompound, aDrawer);
  anAlgo.ChangeParameters().Angle      = 20.0 * M_PI / 180.0; // 20 degrees
  anAlgo.ChangeParameters().InParallel = true;
  anAlgo.SetShape (aCompound);

  // write or export the document
  TColStd_IndexedDataMapOfStringString aMetadata;
  RWGltf_CafWriter aGltfWriter (out, true);
  // STEP reader translates into mm units by default
  aGltfWriter.ChangeCoordinateSystemConverter().SetInputLengthUnit (0.001);
  aGltfWriter.ChangeCoordinateSystemConverter().SetInputCoordinateSystem (RWMesh_CoordinateSystem_Zup);
  if (!aGltfWriter.Perform (theXdeDoc, aMetadata, Handle(Message_ProgressIndicator)())) { std::cout << "export error\n"; }

  return 0;

Standard_Integer main (int argc, char *argv[]) {
  if (argc != 3) {
      std::cerr << "Usage: " << argv[0] << " file.step file.glb" << std::endl;
      return 1;

  return step2glb(argv[1], argv[2]);

I was able to successfully compile this on Linux using g++:

g++ -I/usr/local/include/opencascade -o step2glb -L/usr/local/lib -lTKBRep -lTKXDESTEP -lTKLCAF -lTKMath -lTKernel -lTKXCAF -lTKSTEP -lTKV3d -lTKMesh -lTKXSBase -lTKRWMesh step2glb.cpp

Afterwards, I had to link the library location using the following command:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

However, when I run the command line program I get the following error:

>> ./step2glb test.stp output.glb
parse error
Segmentation fault

It appears to be coming from reading the step file with STEPCAFControl_Reader...  I've attached the step file for validation.  Thanks for any help!

Kirill Gavrilov's picture

There is no enough information to conclude what might be wrong, so I may share only basic advises:

  • Test file is opened fine in CAD Assistant, so that issue is unlikely in STEPCAFControl_Reader.
  • Do not ignore errors returned by ReadFile() and Transfer() methods - you should abort function rather than continue.
  • Raise compiler warning level - your code has misprints in condition statements which modern compilers are able to warn about:
    > if (!aStepReader.ReadFile (in) != IFSelect_RetDone)
  • Check that application uses proper OCCT libraries (with expected location) with help of ldd.
  • Make debug builds of OCCT and application to see meaningful call stack instead of "Segmentation fault" and to use gdb.