STL-Import

Hi eveybody,
I have a problem... I want to use OpenCASCADE in two different ways. Firstly to simply visualize motions of parts of a machine tool structure and secondly to calculate overlapping volumes between parts. This calculation cannot be done with boolean operations because I have to simulate a tool path through a workpiece, and lots of such operations have to be made during the simulation.
The 3D Data of the machine tool parts is given in STL-format and there is no way for me to get IGES or STEP representations of these parts which could be read in easyly with OpenCASCADE. So how can I import STL? Can you give me an example on how to do the import usig the OCC primitives and which works fastly. Or is there any library available which does the job?

Patrik Mueller's picture

Hi,

try this:

OSD_Path aFile(FileName);
Standard_Boolean ReturnValue = Standard_True;

Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile(aFile);

TopoDS_Compound ResultShape;
BRep_Builder CompoundBuilder;
CompoundBuilder.MakeCompound(ResultShape);

Standard_Integer NumberDomains = aSTLMesh->NbDomains();
Standard_Integer iND;
gp_XYZ p1, p2, p3;
TopoDS_Vertex Vertex1, Vertex2, Vertex3;
TopoDS_Face AktFace;
TopoDS_Wire AktWire;
BRep_Builder B;
Standard_Real x1, y1, z1;
Standard_Real x2, y2, z2;
Standard_Real x3, y3, z3;

StlMesh_MeshExplorer aMExp (aSTLMesh);

// BRepBuilderAPI_MakeWire WireCollector;
for (iND=1;iND<=NumberDomains;iND++)
{
for (aMExp.InitTriangle (iND); aMExp.MoreTriangle (); aMExp.NextTriangle ())
{
aMExp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
p1.SetCoord(x1,y1,z1);
p2.SetCoord(x2,y2,z2);
p3.SetCoord(x3,y3,z3);

if ((!(p1.IsEqual(p2,0.0))) && (!(p1.IsEqual(p3,0.0))))
{
Vertex1 = BRepBuilderAPI_MakeVertex(p1);
Vertex2 = BRepBuilderAPI_MakeVertex(p2);
Vertex3 = BRepBuilderAPI_MakeVertex(p3);

AktWire = BRepBuilderAPI_MakePolygon( Vertex1, Vertex2, Vertex3, Standard_True);

if( !AktWire.IsNull())
{
AktFace = BRepBuilderAPI_MakeFace( AktWire);
if(!AktFace.IsNull())
CompoundBuilder.Add(ResultShape,AktFace);
}
}
}
}
TopoDS_Shape aShape = ResultShape;

Best regards,

Patrik Müller

Stephane Routelous's picture

Take care,

the RWSTL package is buged in the release version 4.0.
You ahve to take the file from the repository to solve the problems.

HTH

Patrik Mueller's picture

My fault - I've forgotten saying that.

My routine in the repository also isn't perfect. I think it should use the source above for building Shapes instead of using sewing.

Best regards,

Patrik Müller

&amp;#30333;&amp;#26195;&amp;#20142;(Bai Xiaolian's picture

Hi,Patrik Müller
The listed code sames to construct a BREP model to represent a triangular mesh. Could you give me some advice about how to use TKMesh toolkit, which sames better for triangular mesh.

thanks

Patrik Mueller's picture

Hi,

what do you want to do?

Greets,

Patrik

&amp;#30333;&amp;#26195;&amp;#20142;(Bai Xiaolian's picture

Hi,

I'm do sth about reverse engineering, and firstly point clouds are imported, then triangulate it to construct a mesh, and then segmentation and surface fitting, after that Brep model could be constructed.

So, I need a data struct of triangular mesh which could be manipulated conveniently, e.g. find all edges and triangles associate to a vertex, and find all triangles associate to an edge, and ......, I mean a data structer with enough information about manipulating triangular mesh, for most of the algorithum is based on triangular mesh.

Greets,

白晓亮(Bai Xiaoliang)

Patrik Mueller's picture

Hi,

I think it would be easier for you if you build your own (for example half-edge based) mesh structure...

Greets,

Patrik

&amp;#30333;&amp;#26195;&amp;#20142;(Bai Xiaolian's picture

Hi,

Yes, my current work is just using half-edge based data structure. Did you mean OCC is not suit for the work?

Greets,

白晓亮(Bai Xiaoliang)

Patrik Mueller's picture

If you already has a data structure in use - why don't you use it inside of OCC instead of trying to map the data to an OCC structure?
OCC isn't so strong for mesh data - but it really depends on your needs...

Greetsm

Patrik

&amp;#30333;&amp;#26195;&amp;#20142;(Bai Xiaolian's picture

Hi,

Thanks for your advice.

白晓亮(Bai Xiaoliang).

Marcel Janer's picture

you can do:

for fast load and render, in shade mode:
------------------------------

Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile(aFile);
Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );

aMesh->SetDataSource(aDS);
aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh), Standard_True );//False -> No selection

aMesh->GetDrawer()->SetBoolean(MeshVS_DA_DisplayNodes,Standard_False); //MeshVS_DrawerAttribute
aMesh->GetDrawer()->SetBoolean(MeshVS_DA_ShowEdges,Standard_False);
aMesh->GetDrawer()->SetMaterial(MeshVS_DA_FrontMaterial,DEFAULT_MATERIAL);

aMesh->SetColor(Quantity_NOC_AZURE);
aMesh->SetDisplayMode( MeshVS_DMF_Shading ); // Mode as defaut
aMesh->SetHilightMode( MeshVS_DMF_WireFrame ); // Wireframe as default hilight mode

myDoc->m_AISContext->Display(aMesh);

-----------------------

Marcel Janer.

j-lin's picture

OCC is not good enough to do STL operation.There are some ways which are discussed above to import STL.But OCC does not provides enough methods to do mesh operation (just like Boolean).
It may be better to use VTK as your Toolkit.
http://public.kitware.com/VTK/
Good luck to you.

SunHongLei's picture

hello,I want to know that does VTK contains the alogrithm about triangle mesh simplification?
Thanks !
VTK这个函数库有没有三角网格的简化?谢谢!