0030389: Data Exchange - StlAPI_Writer does not check if the face has triangulation
[occt.git] / src / StlAPI / StlAPI_Writer.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_Writer.hxx>
42cf5bc1 15
7fd59977 16#include <Bnd_Box.hxx>
7fd59977 17#include <BRepBndLib.hxx>
44d5a096 18#include <Message.hxx>
19#include <Message_Messenger.hxx>
7fd59977 20#include <OSD_Path.hxx>
b508cbc5 21#include <OSD_OpenFile.hxx>
42cf5bc1 22#include <RWStl.hxx>
b508cbc5 23#include <BRep_Tool.hxx>
24#include <TopoDS.hxx>
25#include <TopoDS_Face.hxx>
26#include <TopExp_Explorer.hxx>
27#include <Poly_Triangulation.hxx>
7fd59977 28
4178b353 29//=============================================================================
30//function : StlAPI_Writer
31//purpose :
32//=============================================================================
7fd59977 33StlAPI_Writer::StlAPI_Writer()
4178b353 34: myASCIIMode (Standard_True)
7fd59977 35{
4178b353 36 //
7fd59977 37}
38
4178b353 39//=============================================================================
40//function : Write
41//purpose :
42//=============================================================================
43Standard_Boolean StlAPI_Writer::Write (const TopoDS_Shape& theShape,
44 const Standard_CString theFileName)
7fd59977 45{
4178b353 46 Standard_Integer aNbNodes = 0;
47 Standard_Integer aNbTriangles = 0;
b508cbc5 48
4178b353 49 // calculate total number of the nodes and triangles
50 for (TopExp_Explorer anExpSF (theShape, TopAbs_FACE); anExpSF.More(); anExpSF.Next())
b508cbc5 51 {
4178b353 52 TopLoc_Location aLoc;
53 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (anExpSF.Current()), aLoc);
22e70738 54 if (! aTriangulation.IsNull())
55 {
56 aNbNodes += aTriangulation->NbNodes ();
57 aNbTriangles += aTriangulation->NbTriangles ();
58 }
b508cbc5 59 }
b508cbc5 60
44d5a096 61 if (aNbTriangles == 0)
62 {
63 // No triangulation on the shape
64 return Standard_False;
65 }
66
4178b353 67 // create temporary triangulation
68 Handle(Poly_Triangulation) aMesh = new Poly_Triangulation (aNbNodes, aNbTriangles, Standard_False);
44d5a096 69 // count faces missing triangulation
70 Standard_Integer aNbFacesNoTri = 0;
4178b353 71 // fill temporary triangulation
72 Standard_Integer aNodeOffset = 0;
73 Standard_Integer aTriangleOffet = 0;
74 for (TopExp_Explorer anExpSF (theShape, TopAbs_FACE); anExpSF.More(); anExpSF.Next())
b508cbc5 75 {
44d5a096 76 const TopoDS_Shape& aFace = anExpSF.Current();
4178b353 77 TopLoc_Location aLoc;
44d5a096 78 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (TopoDS::Face (aFace), aLoc);
79 if (aTriangulation.IsNull())
80 {
81 ++aNbFacesNoTri;
82 continue;
83 }
b508cbc5 84
4178b353 85 const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes();
86 const Poly_Array1OfTriangle& aTriangles = aTriangulation->Triangles();
b508cbc5 87
4178b353 88 // copy nodes
89 gp_Trsf aTrsf = aLoc.Transformation();
90 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
b508cbc5 91 {
4178b353 92 gp_Pnt aPnt = aNodes (aNodeIter);
93 aPnt.Transform (aTrsf);
94 aMesh->ChangeNode (aNodeIter + aNodeOffset) = aPnt;
b508cbc5 95 }
b508cbc5 96
4178b353 97 // copy triangles
98 const TopAbs_Orientation anOrientation = anExpSF.Current().Orientation();
99 for (Standard_Integer aTriIter = aTriangles.Lower(); aTriIter <= aTriangles.Upper(); ++aTriIter)
b508cbc5 100 {
4178b353 101 Poly_Triangle aTri = aTriangles (aTriIter);
b508cbc5 102
4178b353 103 Standard_Integer anId[3];
104 aTri.Get (anId[0], anId[1], anId[2]);
105 if (anOrientation == TopAbs_REVERSED)
106 {
107 // Swap 1, 2.
108 Standard_Integer aTmpIdx = anId[1];
109 anId[1] = anId[2];
110 anId[2] = aTmpIdx;
111 }
b508cbc5 112
4178b353 113 // Update nodes according to the offset.
114 anId[0] += aNodeOffset;
115 anId[1] += aNodeOffset;
116 anId[2] += aNodeOffset;
b508cbc5 117
4178b353 118 aTri.Set (anId[0], anId[1], anId[2]);
119 aMesh->ChangeTriangle (aTriIter + aTriangleOffet) = aTri;
b508cbc5 120 }
4178b353 121
122 aNodeOffset += aNodes.Size();
123 aTriangleOffet += aTriangles.Size();
b508cbc5 124 }
125
4178b353 126 OSD_Path aPath (theFileName);
44d5a096 127 Standard_Boolean isDone = (myASCIIMode
4178b353 128 ? RWStl::WriteAscii (aMesh, aPath)
44d5a096 129 : RWStl::WriteBinary (aMesh, aPath));
130
131 if (isDone && (aNbFacesNoTri > 0))
132 {
133 // Print warning with number of faces missing triangulation
134 TCollection_AsciiString aWarningMsg =
135 TCollection_AsciiString ("Warning: ") +
136 TCollection_AsciiString (aNbFacesNoTri) +
137 TCollection_AsciiString ((aNbFacesNoTri == 1) ? " face has" : " faces have") +
138 TCollection_AsciiString (" been skipped due to null triangulation");
139 Message::DefaultMessenger()->Send (aWarningMsg, Message_Warning);
140 }
141
142 return isDone;
7fd59977 143}