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_Writer.hxx>
16 #include <Bnd_Box.hxx>
17 #include <BRepBndLib.hxx>
18 #include <OSD_Path.hxx>
19 #include <OSD_OpenFile.hxx>
21 #include <BRep_Tool.hxx>
23 #include <TopoDS_Face.hxx>
24 #include <TopExp_Explorer.hxx>
25 #include <Poly_Triangulation.hxx>
27 //=============================================================================
28 //function : StlAPI_Writer
30 //=============================================================================
31 StlAPI_Writer::StlAPI_Writer()
32 : myASCIIMode (Standard_True)
37 //=============================================================================
40 //=============================================================================
41 Standard_Boolean StlAPI_Writer::Write (const TopoDS_Shape& theShape,
42 const Standard_CString theFileName)
44 Standard_Integer aNbNodes = 0;
45 Standard_Integer aNbTriangles = 0;
47 // calculate total number of the nodes and triangles
48 for (TopExp_Explorer anExpSF (theShape, TopAbs_FACE); anExpSF.More(); anExpSF.Next())
51 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (anExpSF.Current()), aLoc);
52 aNbNodes += aTriangulation->NbNodes();
53 aNbTriangles += aTriangulation->NbTriangles();
56 // create temporary triangulation
57 Handle(Poly_Triangulation) aMesh = new Poly_Triangulation (aNbNodes, aNbTriangles, Standard_False);
59 // fill temporary triangulation
60 Standard_Integer aNodeOffset = 0;
61 Standard_Integer aTriangleOffet = 0;
62 for (TopExp_Explorer anExpSF (theShape, TopAbs_FACE); anExpSF.More(); anExpSF.Next())
65 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (anExpSF.Current()), aLoc);
67 const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes();
68 const Poly_Array1OfTriangle& aTriangles = aTriangulation->Triangles();
71 gp_Trsf aTrsf = aLoc.Transformation();
72 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
74 gp_Pnt aPnt = aNodes (aNodeIter);
75 aPnt.Transform (aTrsf);
76 aMesh->ChangeNode (aNodeIter + aNodeOffset) = aPnt;
80 const TopAbs_Orientation anOrientation = anExpSF.Current().Orientation();
81 for (Standard_Integer aTriIter = aTriangles.Lower(); aTriIter <= aTriangles.Upper(); ++aTriIter)
83 Poly_Triangle aTri = aTriangles (aTriIter);
85 Standard_Integer anId[3];
86 aTri.Get (anId[0], anId[1], anId[2]);
87 if (anOrientation == TopAbs_REVERSED)
90 Standard_Integer aTmpIdx = anId[1];
95 // Update nodes according to the offset.
96 anId[0] += aNodeOffset;
97 anId[1] += aNodeOffset;
98 anId[2] += aNodeOffset;
100 aTri.Set (anId[0], anId[1], anId[2]);
101 aMesh->ChangeTriangle (aTriIter + aTriangleOffet) = aTri;
104 aNodeOffset += aNodes.Size();
105 aTriangleOffet += aTriangles.Size();
108 OSD_Path aPath (theFileName);
110 ? RWStl::WriteAscii (aMesh, aPath)
111 : RWStl::WriteBinary (aMesh, aPath);