034c05e382f452c19cf41074bc5f2d93880b9bf1
[occt.git] / src / StlAPI / StlAPI_Reader.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <BRep_Builder.hxx>
16 #include <BRepBuilderAPI_MakeFace.hxx>
17 #include <BRepBuilderAPI_MakePolygon.hxx>
18 #include <BRepBuilderAPI_MakeVertex.hxx>
19 #include <BRepBuilderAPI_Sewing.hxx>
20 #include <gp_Pnt.hxx>
21 #include <OSD_Path.hxx>
22 #include <RWStl.hxx>
23 #include <StlAPI_Reader.hxx>
24 #include <StlMesh_Mesh.hxx>
25 #include <StlMesh_MeshExplorer.hxx>
26 #include <TopoDS_Compound.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Shell.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Wire.hxx>
33
34 StlAPI_Reader::StlAPI_Reader() {}
35
36 void StlAPI_Reader::Read(TopoDS_Shape& aShape, const Standard_CString aFileName) 
37 {
38   OSD_Path aFile(aFileName);
39   
40   Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile(aFile);
41   Standard_Integer NumberDomains = aSTLMesh->NbDomains();
42   Standard_Integer iND;
43   gp_XYZ p1, p2, p3;
44   TopoDS_Vertex Vertex1, Vertex2, Vertex3;
45   TopoDS_Face AktFace;
46   TopoDS_Wire AktWire;
47   BRepBuilderAPI_Sewing aSewingTool;
48   Standard_Real x1, y1, z1;
49   Standard_Real x2, y2, z2;
50   Standard_Real x3, y3, z3;
51   
52   aSewingTool.Init(1.0e-06,Standard_True);
53   
54   TopoDS_Compound aComp;
55   BRep_Builder BuildTool;
56   BuildTool.MakeCompound( aComp );
57
58   StlMesh_MeshExplorer aMExp (aSTLMesh);
59   
60   for (iND=1;iND<=NumberDomains;iND++) 
61   {
62     for (aMExp.InitTriangle (iND); aMExp.MoreTriangle (); aMExp.NextTriangle ()) 
63     {
64       aMExp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
65       p1.SetCoord(x1,y1,z1);
66       p2.SetCoord(x2,y2,z2);
67       p3.SetCoord(x3,y3,z3);
68       
69       if ((!(p1.IsEqual(p2,0.0))) && (!(p1.IsEqual(p3,0.0))))
70       {
71         Vertex1 = BRepBuilderAPI_MakeVertex(p1);
72         Vertex2 = BRepBuilderAPI_MakeVertex(p2);
73         Vertex3 = BRepBuilderAPI_MakeVertex(p3);
74         
75         AktWire = BRepBuilderAPI_MakePolygon( Vertex1, Vertex2, Vertex3, Standard_True);
76         
77         if( !AktWire.IsNull())
78         {
79           AktFace = BRepBuilderAPI_MakeFace( AktWire);
80           if(!AktFace.IsNull())
81             BuildTool.Add( aComp, AktFace );
82         }
83       }
84     }
85   }
86   aSTLMesh->Clear();
87
88   aSewingTool.Load( aComp );
89   aSewingTool.Perform();
90   aShape = aSewingTool.SewedShape();
91   if ( aShape.IsNull() )
92     aShape = aComp;
93 }
94