Triangulation of objects from STEP files

Hi!

I like to display STEP models with OGRE3d instead of OpenGL. I have a working function for reading STEP files and putting the shapes in a list (TopTools_SequenceOfShape m_lShapes). To create an ogre mesh I have to copy all tries from OCC to OGRE. Btw, I'm using OCC 6.5.4. I try to get access to the tris but it's not working:

TopoDS_Shape tShape = m_lShapes.Value(i);

// Iterate over all faces
for(TopExp_Explorer exp(tShape, TopAbs_FACE); exp.More(); exp.Next())
{
TopoDS_Face face = TopoDS::Face(exp.Current());

// get the triangulation
TopLoc_Location loc;
Handle(Poly_Triangulation) tri = BRep_Tool::Triangulation(face, loc);
if (tri.IsNull()) continue;

// create a transformation from the location
gp_Trsf xloc = loc;

// get the nodes
const TColgp_Array1OfPnt &nodes = tri->Nodes();
for (Standard_Integer iCount = nodes.Lower(); iCount {
Standard_Real x,y,z;
nodes(iCount).Coord(x,y,z);
xloc.Transforms(x,y,z);
// do something with the point
}

// copy the polygons
Standard_Integer i1, i2, i3;
const Poly_Array1OfTriangle &tris = tri->Triangles();
for (Standard_Integer iCount = tris.Lower(); iCount {
// get the node indexes for this triangle
Poly_Triangle tri = tris(iCount);

// reverse the triangle orientation if the face is reversed
if (face.Orientation() != TopAbs_FORWARD)
tri.Get(i3, i2, i1);
else
tri.Get(i1, i2, i3);
}
}

What's wrong with that code? tri is always NULL! There are a few threads with additional code like

BRepMesh::Mesh(tShape, 0.001f);

or

BRepMesh_FastDiscret* myMesh = new BRepMesh_FastDiscret(deflection, tShape, bb, angulardeflection, Standard_True, Standard_True, Standard_False, Standard_False);

But this codes are not working:

First-chance exception at 0x003af0d7 (TKMesh.dll) in test.exe: 0xC0000005: Access violation reading location 0xfefd0000.

I hope you can help me.

Regards,
S.

Göran Barz's picture

Hi,

the step import usually doesn't create a triangulation, so you have to create it yourself with BRepMesh. I don't know why the meshing failed for your faces, maybe you could try to lower the resolution:

Handle(Poly_Triangulation) tri = BRep_Tool::Triangulation(face, loc);
if (tri.IsNull())
{
BRepMesh::Mesh(face, 0.7f);
tri = BRep_Tool::Triangulation(face, loc);
}

Mr. S's picture

Hi,

thanks a lot! It's working at the moment.

Regards,
S.

Mr. S's picture

Hi,

I still have a triangulation problem. Now, most of the faces are working but a lot of faces are still not working. The model is correct because it's working in the MFC example. What's wrong now? I can see I've attached screenshots of the MFC example and the output of the following code.

I still receive

First-chance exception at 0x767bc41f in test.exe: Microsoft C++ exception: Standard_NullObject at memory location 0x003dccc0..

in VS debug output on non working faces, if I try to mesh the face with BRepMesh::Mesh.

Thank you for your help,
S.

TopoDS_Shape tShape = m_lShapes.Value(i);
Standard_Real aDeflection = 0.5;
Bnd_Box bb;
BRepBndLib::Add(tShape, bb, Standard_False);
Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
bb.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
aDeflection = std::max(aXmax-aXmin, std::max(aYmax-aYmin, aZmax-aZmin)) * 0.0004;

SharedVertextBuffer polyList;

// Iterate over all faces
for(TopExp_Explorer exp(tShape, TopAbs_FACE); exp.More(); exp.Next())
{
TopoDS_Face face = TopoDS::Face(exp.Current());

// get the triangulation
TopLoc_Location loc;
BRepMesh::Mesh(face, aDeflection);
Handle(Poly_Triangulation) tri = BRep_Tool::Triangulation(face, loc);
if(tri.IsNull())
{
BRepMesh::Mesh(face, 0.7f);
tri = BRep_Tool::Triangulation(face, loc);
}
if (tri.IsNull())
continue; // This means: First-chance exception at 0x767bc41f in test.exe: Microsoft C++ exception: Standard_NullObject at memory location 0x003dccc0..

// create a transformation from the location
gp_Trsf xloc = loc;

// get the nodes
std::vector vPoints;
const TColgp_Array1OfPnt &nodes = tri->Nodes();
for (Standard_Integer iCount = nodes.Lower(); iCount <= nodes.Upper(); iCount++)
{
Standard_Real x,y,z;
nodes(iCount).Coord(x,y,z);
xloc.Transforms(x,y,z);
vPoints.push_back(polyList.addVertex(x, y, z));
}

// copy the polygons
Standard_Integer i1, i2, i3;
const Poly_Array1OfTriangle &tris = tri->Triangles();
for(Standard_Integer iCount = tris.Lower(); iCount <= tris.Upper(); iCount++)
{
// get the node indexes for this triangle
Poly_Triangle tri = tris(iCount);

// reverse the triangle orientation if the face is reversed
if (face.Orientation() != TopAbs_FORWARD)
tri.Get(i3, i2, i1);
else
tri.Get(i1, i2, i3);

polyList.addPolygon(vPoints[i1 - 1], vPoints[i2 - 1], vPoints[i3 - 1]);
}
}

Catriel Carbonera's picture

Hi,

I am dealing with a problem similar to the one you posted. I cannot find the function BRepMesh::Mesh(). Did you mispell it in your post? I am testing OpenCascade 7.1. Is it possibilble that the interface doesn't' exist anymore?

Thanks,

Catriel Carbonera

Forum supervisor's picture

Dear Catriel,

In OCCT 6.8.0 BRepMesh algorithm had been was already refactored and optimized and the method RepMesh::Mesh was removed.

You should the methods of the class BRepMesh_IncrementalMesh.

What is domain of your project (CAD/CAM/CAE/...) ? 

You can communicate us via Contact Form to exchange information about our services and expertise and needs of your company projects.

Best regards, FSR

JinXu's picture

Hello,

This is normal with OCC. It can be problem of import of STEP files. OCC often cannot create correct faces or surfaces after import. if you get wrong face normals or surface properties, then you cannot get trianguation too.

Mr. S's picture

Hello,

no, it's not a problem of the model. Have a look at the attachment of my previous posting. The model is working well in MFC example.

Regards,
S.

Márton Kun's picture

Hello Mr. Sal,

I'm working on a similar problem than yours (loading STEP file to Ogre3D).
I've just found the OCC, I'm buliding it now. So I'm a newcomer here.

I'd like to ask you that have you got the solution now?
If so, what is that? And how does it work?

Thanks,
Márton

Mr. S's picture

Hello Márton,

Ogre3d and OCC doesn't like each other. My intension was loading the CAD models in a running Ogre scene. This is not working. Now, I have built a small converter programm which converts the CAD models to Ogre meshs and save them to a file. I could run this programm from my main programm and load the mesh in my running Ogre scene. You can use my code from 2013/04/05 10:39 to triangulate CAD models. But you have to work in a special order:
1. Init OCC
2. Load and triangulate your CAD file
3. Init Ogre
4. Create new mesh with the vertexes from the triangulated model (use vertexbuffer and indexbuffer)
5. Save mesh to file
6. Shutdown everything

If you init an Ogre render system before OCC the triangulation is not working! I'll have to check if this problem still exists with Ogre 2.0.0. You can look at the source code of ogrexmlconverter to create a converter.

Regards,
S.