1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <StlAPI_Reader.ixx>
20 #include <StlMesh_Mesh.hxx>
21 #include <OSD_Path.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRepBuilderAPI_MakeVertex.hxx>
24 #include <BRepBuilderAPI_MakePolygon.hxx>
25 #include <BRepBuilderAPI_MakeFace.hxx>
26 #include <BRepBuilderAPI_Sewing.hxx>
28 #include <TopoDS_Compound.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <StlMesh_MeshExplorer.hxx>
38 StlAPI_Reader::StlAPI_Reader() {}
40 void StlAPI_Reader::Read(TopoDS_Shape& aShape, const Standard_CString aFileName)
42 OSD_Path aFile(aFileName);
44 Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile(aFile);
45 Standard_Integer NumberDomains = aSTLMesh->NbDomains();
48 TopoDS_Vertex Vertex1, Vertex2, Vertex3;
51 BRepBuilderAPI_Sewing aSewingTool;
52 Standard_Real x1, y1, z1;
53 Standard_Real x2, y2, z2;
54 Standard_Real x3, y3, z3;
56 aSewingTool.Init(1.0e-06,Standard_True);
58 TopoDS_Compound aComp;
59 BRep_Builder BuildTool;
60 BuildTool.MakeCompound( aComp );
62 StlMesh_MeshExplorer aMExp (aSTLMesh);
64 for (iND=1;iND<=NumberDomains;iND++)
66 for (aMExp.InitTriangle (iND); aMExp.MoreTriangle (); aMExp.NextTriangle ())
68 aMExp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
69 p1.SetCoord(x1,y1,z1);
70 p2.SetCoord(x2,y2,z2);
71 p3.SetCoord(x3,y3,z3);
73 if ((!(p1.IsEqual(p2,0.0))) && (!(p1.IsEqual(p3,0.0))))
75 Vertex1 = BRepBuilderAPI_MakeVertex(p1);
76 Vertex2 = BRepBuilderAPI_MakeVertex(p2);
77 Vertex3 = BRepBuilderAPI_MakeVertex(p3);
79 AktWire = BRepBuilderAPI_MakePolygon( Vertex1, Vertex2, Vertex3, Standard_True);
81 if( !AktWire.IsNull())
83 AktFace = BRepBuilderAPI_MakeFace( AktWire);
85 BuildTool.Add( aComp, AktFace );
92 aSewingTool.Load( aComp );
93 aSewingTool.Perform();
94 aShape = aSewingTool.SewedShape();
95 if ( aShape.IsNull() )