0028840: Data Exchange - rewrite the STL Reader/Writer
[occt.git] / src / StlAPI / StlAPI_Reader.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
4178b353 14#include <StlAPI_Reader.hxx>
42cf5bc1 15
7fd59977 16#include <BRep_Builder.hxx>
7fd59977 17#include <BRepBuilderAPI_MakeFace.hxx>
42cf5bc1 18#include <BRepBuilderAPI_MakePolygon.hxx>
19#include <BRepBuilderAPI_MakeVertex.hxx>
7fd59977 20#include <BRepBuilderAPI_Sewing.hxx>
21#include <gp_Pnt.hxx>
42cf5bc1 22#include <OSD_Path.hxx>
23#include <RWStl.hxx>
7fd59977 24#include <TopoDS_Compound.hxx>
7fd59977 25#include <TopoDS_Edge.hxx>
7fd59977 26#include <TopoDS_Face.hxx>
42cf5bc1 27#include <TopoDS_Shape.hxx>
7fd59977 28#include <TopoDS_Shell.hxx>
42cf5bc1 29#include <TopoDS_Vertex.hxx>
30#include <TopoDS_Wire.hxx>
7fd59977 31
4178b353 32//=============================================================================
33//function : Read
34//purpose :
35//=============================================================================
36Standard_Boolean StlAPI_Reader::Read (TopoDS_Shape& theShape,
37 const Standard_CString theFileName)
7fd59977 38{
4178b353 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;
7fd59977 48 BRepBuilderAPI_Sewing aSewingTool;
4178b353 49 aSewingTool.Init (1.0e-06, Standard_True);
50
7fd59977 51 TopoDS_Compound aComp;
52 BRep_Builder BuildTool;
4178b353 53 BuildTool.MakeCompound (aComp);
7fd59977 54
4178b353 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();
59 ++aTriIdx)
7fd59977 60 {
4178b353 61 const Poly_Triangle& aTriangle = aTriangles(aTriIdx);
62
63 Standard_Integer anId[3];
64 aTriangle.Get(anId[0], anId[1], anId[2]);
65
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))))
7fd59977 71 {
4178b353 72 aTriVertexes[0] = BRepBuilderAPI_MakeVertex (aPnt1);
73 aTriVertexes[1] = BRepBuilderAPI_MakeVertex (aPnt2);
74 aTriVertexes[2] = BRepBuilderAPI_MakeVertex (aPnt3);
75
76 aWire = BRepBuilderAPI_MakePolygon (aTriVertexes[0], aTriVertexes[1], aTriVertexes[2], Standard_True);
77 if (!aWire.IsNull())
7fd59977 78 {
4178b353 79 aFace = BRepBuilderAPI_MakeFace (aWire);
80 if (!aFace.IsNull())
7fd59977 81 {
4178b353 82 BuildTool.Add (aComp, aFace);
7fd59977 83 }
84 }
85 }
86 }
7fd59977 87
4178b353 88 aSewingTool.Load (aComp);
7fd59977 89 aSewingTool.Perform();
4178b353 90 theShape = aSewingTool.SewedShape();
91 if (theShape.IsNull())
92 {
93 theShape = aComp;
94 }
95 return Standard_True;
7fd59977 96}