A Crash Occurs when BRepFeat_SplitShape's Build() method if Imported CAD File (Solved)

Dear OCCT Community,

First of all, my main purpose is to convert the data I read from an .STL file to TopoDS_Shape and then split this mesh.

For this purpose, Firstly, I did a PoC and created a cylinder as a simple shape. Then I split it successfully. BRepFeat_SplitShape's Build() method did not crash.

BRepPrimAPI_MakeCylinder aCylinderMaker(10.0, 100.0);
TopoDS_Shape aCylinder = aCylinderMaker.Shape();
BRepFeat_SplitShape shape_splitter(aCylinder);
BRepAlgoAPI_Section section = this->createSplitterWithGivenHeight(aCylinder, height);

for (TopExp_Explorer iter(section.Shape(), TopAbs_EDGE); iter.More(); iter.Next())
{
   TopoDS_Shape shape_of_edge = iter.Current();
   TopoDS_Shape shape_of_face;

   if (section.HasAncestorFaceOn1(shape_of_edge, shape_of_face))
     {
        TopoDS_Edge edge = TopoDS::Edge(shape_of_edge);
        TopoDS_Face face = TopoDS::Face(shape_of_face);

        shape_splitter.Add(edge, face);
      }
}

shape_splitter.Build();

However, if I read from .STEP or .STL and store it in TopoDS_Shape and try to split this mesh, Build() method crashes. 

Normally, when I read the STEP file or read from STL, I can visualize the mesh I get, I can choose vertex on it, I can convert the format, but unfortunately I cannot split it.

Splitter code blocks are here as well:


BRepAlgoAPI_Section OCCTShapeHealer::createSplitterWithGivenHeight(TopoDS_Shape shape, float height)
{
    BRepAlgoAPI_Section splitter_section (shape, gp_Pln(gp_Pnt(0.0, 0.0, height), gp::DZ()), Standard_False);
    splitter_section.ComputePCurveOn1(Standard_True);
    splitter_section.Approximation(Standard_True);
    splitter_section.Build();
    return splitter_section;
}
std::vector<TopoDS_Shape> OCCTShapeHealer::getSplittedMeshesAsTwoPart(TopoDS_Shape being_splitted_mesh, float height)
{
    std::vector<TopoDS_Shape> splitted_meshes;

    // Split the shape.
    //BRepPrimAPI_MakeCylinder aCylinderMaker(10.0, 100.0);
    //TopoDS_Shape aCylinder = aCylinderMaker.Shape();
    BRepFeat_SplitShape shape_splitter(being_splitted_mesh);
    BRepAlgoAPI_Section section = this->createSplitterWithGivenHeight(being_splitted_mesh, height);

    for (TopExp_Explorer iter(section.Shape(), TopAbs_EDGE); iter.More(); iter.Next())
    {
        TopoDS_Shape shape_of_edge = iter.Current();
        TopoDS_Shape shape_of_face;

        if (section.HasAncestorFaceOn1(shape_of_edge, shape_of_face))
        {
            TopoDS_Edge edge = TopoDS::Edge(shape_of_edge);
            TopoDS_Face face = TopoDS::Face(shape_of_face);

            shape_splitter.Add(edge, face);
        }
    }

    shape_splitter.Build();

    // Rebuild top and bottom shape.
    BRep_Builder brep_builder;
    TopoDS_Compound top_compound;
    TopoDS_Compound bottom_compound;

    brep_builder.MakeCompound(top_compound);
    brep_builder.MakeCompound(bottom_compound);

    // Top shape.
    TopTools_MapOfShape map_of_top_shape;
    const TopTools_ListOfShape& list_of_top_shapes = shape_splitter.Left();
    for (auto i = list_of_top_shapes.cbegin(); i != list_of_top_shapes.cend(); i++)
    {
        map_of_top_shape.Add(*i);
        brep_builder.Add(top_compound, *i);
    }

    // Bottom shape.
    TopTools_IndexedMapOfShape indexed_map_of_bottom_shape;
    TopExp::MapShapes(shape_splitter.Shape(), TopAbs_FACE, indexed_map_of_bottom_shape);

    for (auto i = indexed_map_of_bottom_shape.cbegin(); i != indexed_map_of_bottom_shape.cend(); i++)
    {
        if (!map_of_top_shape.Contains(*i))
        {
            brep_builder.Add(bottom_compound, *i);
        }
    }
    splitted_meshes.push_back(top_compound);
    splitted_meshes.push_back(bottom_compound);

    return splitted_meshes;
}

How can I overcome this crash problem? Do you have any suggestion?
Thank you in advance

NOTE: OCCT Version is: 7.6.0, Operating Systems are: Windows 10 & Debian 10

Kirill Gavrilov's picture

"Crash" is very unspecific definition. Have you tried to wrap your code with try/catch or to see in Debugger what exactly crashes in called algorithm? I may guess either, something is wrong with input parameters, or you observe some bug in the algorithm.

Nezihe Sözen's picture

Thank you for your reply. You're right, it is related with wrong input parameters. BRepFeat_SplitShape::Build() method works properly either import cad file or creating some shapes (cylinder, cube etc.).

Best regards