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 const TColgp_Array1OfPnt& aNodes = aMesh->Nodes();
56 const Poly_Array1OfTriangle& aTriangles = aMesh->Triangles();
57 for (Standard_Integer aTriIdx = aTriangles.Lower();
58 aTriIdx <= aTriangles.Upper();
61 const Poly_Triangle& aTriangle = aTriangles(aTriIdx);
63 Standard_Integer anId[3];
64 aTriangle.Get(anId[0], anId[1], anId[2]);
66 const gp_Pnt& aPnt1 = aNodes (anId[0]);
67 const gp_Pnt& aPnt2 = aNodes (anId[1]);
68 const gp_Pnt& aPnt3 = aNodes (anId[2]);
69 if ((!(aPnt1.IsEqual (aPnt2, 0.0)))
70 && (!(aPnt1.IsEqual (aPnt3, 0.0))))
72 aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
73 aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
74 aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
76 aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], Standard_True);
79 aFace = BRepBuilderAPI_MakeFace (aWire);
82 BuildTool.Add (aComp, aFace);
88 aSewingTool.Load (aComp);
89 aSewingTool.Perform();
90 theShape = aSewingTool.SewedShape();
91 if (theShape.IsNull())