0032133: Modeling Data - Restriction of access to internal arrays for Poly_Triangulat...
[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 #include <StlAPI_Reader.hxx>
15
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>
21 #include <gp_Pnt.hxx>
22 #include <OSD_Path.hxx>
23 #include <RWStl.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>
31
32 //=============================================================================
33 //function : Read
34 //purpose  :
35 //=============================================================================
36 Standard_Boolean StlAPI_Reader::Read (TopoDS_Shape&          theShape,
37                                       const Standard_CString theFileName)
38 {
39   Handle(Poly_Triangulation) aMesh = RWStl::ReadFile (theFileName);
40   if (aMesh.IsNull())
41   {
42     return Standard_False;
43   }
44
45   TopoDS_Vertex aTriVertexes[3];
46   TopoDS_Face aFace;
47   TopoDS_Wire aWire;
48   BRepBuilderAPI_Sewing aSewingTool;
49   aSewingTool.Init (1.0e-06, Standard_True);
50
51   TopoDS_Compound aComp;
52   BRep_Builder BuildTool;
53   BuildTool.MakeCompound (aComp);
54
55   for (Standard_Integer aTriIdx  = 1; aTriIdx <= aMesh->NbTriangles(); ++aTriIdx)
56   {
57     const Poly_Triangle aTriangle = aMesh->Triangle (aTriIdx);
58
59     Standard_Integer anId[3];
60     aTriangle.Get(anId[0], anId[1], anId[2]);
61
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)))
67     {
68       aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
69       aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
70       aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
71
72       aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], Standard_True);
73       if (!aWire.IsNull())
74       {
75         aFace = BRepBuilderAPI_MakeFace (aWire);
76         if (!aFace.IsNull())
77         {
78           BuildTool.Add (aComp, aFace);
79         }
80       }
81     }
82   }
83
84   aSewingTool.Load (aComp);
85   aSewingTool.Perform();
86   theShape = aSewingTool.SewedShape();
87   if (theShape.IsNull())
88   {
89     theShape = aComp;
90   }
91   return Standard_True;
92 }