1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <StlAPI_Reader.hxx>
16 #include <BRep_Builder.hxx>
17 #include <BRepBuilderAPI_MakeFace.hxx>
18 #include <BRepBuilderAPI_MakePolygon.hxx>
19 #include <BRepBuilderAPI_MakeVertex.hxx>
20 #include <BRepBuilderAPI_Sewing.hxx>
22 #include <OSD_Path.hxx>
24 #include <TopoDS_Compound.hxx>
25 #include <TopoDS_Edge.hxx>
26 #include <TopoDS_Face.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Shell.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Wire.hxx>
32 //=============================================================================
35 //=============================================================================
36 Standard_Boolean StlAPI_Reader::Read (TopoDS_Shape& theShape,
37 const Standard_CString theFileName)
39 Handle(Poly_Triangulation) aMesh = RWStl::ReadFile (theFileName);
42 return Standard_False;
45 TopoDS_Vertex aTriVertexes[3];
48 BRepBuilderAPI_Sewing aSewingTool;
49 aSewingTool.Init (1.0e-06, Standard_True);
51 TopoDS_Compound aComp;
52 BRep_Builder BuildTool;
53 BuildTool.MakeCompound (aComp);
55 for (Standard_Integer aTriIdx = 1; aTriIdx <= aMesh->NbTriangles(); ++aTriIdx)
57 const Poly_Triangle aTriangle = aMesh->Triangle (aTriIdx);
59 Standard_Integer anId[3];
60 aTriangle.Get(anId[0], anId[1], anId[2]);
62 const gp_Pnt aPnt1 = aMesh->Node (anId[0]);
63 const gp_Pnt aPnt2 = aMesh->Node (anId[1]);
64 const gp_Pnt aPnt3 = aMesh->Node (anId[2]);
65 if (!(aPnt1.IsEqual (aPnt2, 0.0))
66 && !(aPnt1.IsEqual (aPnt3, 0.0)))
68 aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
69 aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
70 aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
72 aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], Standard_True);
75 aFace = BRepBuilderAPI_MakeFace (aWire);
78 BuildTool.Add (aComp, aFace);
84 aSewingTool.Load (aComp);
85 aSewingTool.Perform();
86 theShape = aSewingTool.SewedShape();
87 if (theShape.IsNull())