1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 #include "DriverSTL_W_SMDS_Mesh.h"
28 #include "SMDS_FaceOfNodes.hxx"
29 #include "SMDS_IteratorOnIterators.hxx"
30 #include "SMDS_Mesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMDS_SetIterator.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESH_TypeDefs.hxx"
37 #include <OSD_File.hxx>
38 //#include <OSD_FromWhere.hxx>
39 #include <OSD_Path.hxx>
40 #include <OSD_Protection.hxx>
41 //#include <OSD_SingleProtection.hxx>
42 #include <TCollection_AsciiString.hxx>
45 #include "utilities.h"
47 // definition des constantes
48 static const int LABEL_SIZE = 80;
50 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
55 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
57 myIsAscii = theIsAscii;
60 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
62 Status aResult = DRS_OK;
65 fprintf(stderr, ">> ERROR : Mesh is null \n");
68 findVolumeTriangles();
70 aResult = writeAscii();
72 aResult = writeBinary();
76 //================================================================================
78 * \brief Destructor deletes temporary faces
80 //================================================================================
82 DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
84 for ( unsigned i = 0; i < myVolumeTrias.size(); ++i )
85 delete myVolumeTrias[i];
88 //================================================================================
90 * \brief Finds free facets of volumes for which faces are missing in the mesh
92 //================================================================================
94 void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
96 SMDS_VolumeTool myTool;
97 SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
100 myTool.Set( vIt->next() );
101 for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
102 if ( myTool.IsFreeFace( iF ))
104 const SMDS_MeshNode** n = myTool.GetFaceNodes(iF);
105 int nbN = myTool.NbFaceNodes(iF);
106 std::vector< const SMDS_MeshNode*> nodes( n, n+nbN );
107 if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
109 int nbTria = nbN - 2;
110 for ( int iT = 0; iT < nbTria; ++iT )
112 myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] ));
119 //================================================================================
121 * \brief Return iterator on both faces in the mesh and on temporary faces
123 //================================================================================
125 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
127 SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
128 SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(),
129 myVolumeTrias.end()));
130 typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
131 TElemIterVector iters(2);
132 iters[0] = facesIter;
133 iters[1] = tmpTriaIter;
135 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
136 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
141 static void writeInteger( const Standard_Integer& theVal,
151 Standard_Integer entier;
152 entier = u.c[0] & 0xFF;
153 entier |= (u.c[1] & 0xFF) << 0x08;
154 entier |= (u.c[2] & 0xFF) << 0x10;
155 entier |= (u.c[3] & 0xFF) << 0x18;
157 ofile.Write((char *)&entier,sizeof(u.c));
160 static void writeFloat ( const Standard_ShortReal& theVal,
164 Standard_ShortReal f;
170 Standard_Integer entier;
172 entier = u.c[0] & 0xFF;
173 entier |= (u.c[1] & 0xFF) << 0x08;
174 entier |= (u.c[2] & 0xFF) << 0x10;
175 entier |= (u.c[3] & 0xFF) << 0x18;
177 ofile.Write((char *)&entier,sizeof(u.c));
180 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
181 const SMDS_MeshNode* n2,
182 const SMDS_MeshNode* n3)
184 SMESH_TNodeXYZ xyz1( n1 );
185 SMESH_TNodeXYZ xyz2( n2 );
186 SMESH_TNodeXYZ xyz3( n3 );
187 gp_XYZ q1 = xyz2 - xyz1;
188 gp_XYZ q2 = xyz3 - xyz1;
190 double len = n.Modulus();
191 if ( len > std::numeric_limits<double>::min() )
197 //================================================================================
199 * \brief Decompose a mesh face into triangles
200 * \retval int - number of triangles
202 //================================================================================
204 static int getTriangles( const SMDS_MeshElement* face,
205 const SMDS_MeshNode** nodes)
207 // WARNING: implementation must be coherent with counting triangles in writeBinary()
208 int nbN = face->NbCornerNodes();
209 const int nbTria = nbN-2;
210 for ( int i = 0; nbN > 1; --nbN )
212 nodes[ i++ ] = face->GetNode( 0 );
213 nodes[ i++ ] = face->GetNode( nbN-2 );
214 nodes[ i++ ] = face->GetNode( nbN-1 );
221 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
223 Status aResult = DRS_OK;
224 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
225 if ( aFileName.IsEmpty() ) {
226 fprintf(stderr, ">> ERREOR : invalid file name \n");
230 OSD_File aFile = OSD_File(OSD_Path(aFileName));
231 aFile.Build(OSD_WriteOnly,OSD_Protection());
234 TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
235 aFile.Write (buf,buf.Length());buf.Clear();
237 const SMDS_MeshNode* triaNodes[2048];
239 SMDS_ElemIteratorPtr itFaces = getFaces();
240 while ( itFaces->more() )
242 const SMDS_MeshElement* aFace = itFaces->next();
243 int nbTria = getTriangles( aFace, triaNodes );
245 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
247 gp_XYZ normale = getNormale( triaNodes[iN],
251 buf += " facet normal ";
252 sprintf (sval,"% 12e",normale.X());
255 sprintf (sval,"% 12e",normale.Y());
258 sprintf (sval,"% 12e",normale.Z());
261 aFile.Write (buf,buf.Length());buf.Clear();
262 buf += " outer loop\n";
263 aFile.Write (buf,buf.Length());buf.Clear();
265 for ( int jN = 0; jN < 3; ++jN, ++iN ) {
266 const SMDS_MeshNode* node = triaNodes[iN];
268 sprintf (sval,"% 12e",node->X());
271 sprintf (sval,"% 12e",node->Y());
274 sprintf (sval,"% 12e",node->Z());
277 aFile.Write (buf,buf.Length());buf.Clear();
280 aFile.Write (buf,buf.Length());buf.Clear();
281 buf += " endfacet\n";
282 aFile.Write (buf,buf.Length());buf.Clear();
286 aFile.Write (buf,buf.Length());buf.Clear();
293 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
295 Status aResult = DRS_OK;
296 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
297 if ( aFileName.IsEmpty() ) {
298 fprintf(stderr, ">> ERREOR : invalid filename \n");
302 OSD_File aFile = OSD_File(OSD_Path(aFileName));
303 aFile.Build(OSD_WriteOnly,OSD_Protection());
305 // we first count the number of triangles
306 // WARNING: counting triangles must be coherent with getTriangles()
308 const SMDS_MeshInfo& info = myMesh->GetMeshInfo();
309 nbTri += info.NbTriangles();
310 nbTri += info.NbQuadrangles() * 2;
311 nbTri += myVolumeTrias.size();
312 if ( info.NbPolygons() > 0 )
314 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
315 while ( itFaces->more() )
317 const SMDS_MeshElement* aFace = itFaces->next();
318 if ( aFace->IsPoly() )
319 nbTri += aFace->NbNodes() - 2;
323 // char sval[80]; -- avoid writing not initialized memory
324 TCollection_AsciiString sval(LABEL_SIZE-1,' ');
325 aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE);
327 // write number of triangles
328 writeInteger(nbTri,aFile);
330 // Loop writing nodes
334 const SMDS_MeshNode* triaNodes[2048];
336 SMDS_ElemIteratorPtr itFaces = getFaces();
337 while ( itFaces->more() )
339 const SMDS_MeshElement* aFace = itFaces->next();
340 int nbTria = getTriangles( aFace, triaNodes );
342 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
344 gp_XYZ normale = getNormale( triaNodes[iN],
347 writeFloat(normale.X(),aFile);
348 writeFloat(normale.Y(),aFile);
349 writeFloat(normale.Z(),aFile);
351 for ( int jN = 0; jN < 3; ++jN, ++iN )
353 const SMDS_MeshNode* node = triaNodes[iN];
354 writeFloat(node->X(),aFile);
355 writeFloat(node->Y(),aFile);
356 writeFloat(node->Z(),aFile);
358 aFile.Write (&dum,2);