1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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
23 #include "DriverSTL_W_SMDS_Mesh.h"
29 #include <Basics_Utils.hxx>
31 #include "SMDS_FaceOfNodes.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_PolygonalFaceOfNodes.hxx"
35 #include "SMDS_SetIterator.hxx"
36 #include "SMDS_VolumeTool.hxx"
37 #include "SMESH_File.hxx"
38 #include "SMESH_MeshAlgos.hxx"
39 #include "SMESH_TypeDefs.hxx"
44 // definition des constantes
45 static const int LABEL_SIZE = 80;
47 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
52 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
54 myIsAscii = theIsAscii;
57 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
59 Kernel_Utils::Localizer loc;
61 Status aResult = DRS_OK;
64 fprintf(stderr, ">> ERROR : Mesh is null \n");
67 findVolumeTriangles();
69 aResult = writeAscii();
71 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 < myVolumeFacets.size(); ++i )
85 delete myVolumeFacets[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()
98 SMDS_VolumeTool theVolume;
99 SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
100 std::vector< const SMDS_MeshNode*> nodes;
101 while ( vIt->more() )
103 theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false );
104 for ( int iF = 0; iF < theVolume.NbFaces(); ++iF )
105 if ( theVolume.IsFreeFace( iF ))
107 const SMDS_MeshNode** n = theVolume.GetFaceNodes(iF);
108 int nbN = theVolume.NbFaceNodes(iF);
109 nodes.assign( n, n+nbN );
110 if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*noMedium=*/false))
112 if (( nbN == 9 || nbN == 7 ) &&
113 ( !theVolume.IsPoly() )) // facet is bi-quaratic
115 int nbTria = nbN - 1;
116 for ( int iT = 0; iT < nbTria; ++iT )
117 myVolumeFacets.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] ));
118 myNbVolumeTrias += nbTria;
122 myVolumeFacets.push_back( new SMDS_PolygonalFaceOfNodes( nodes ));
123 myNbVolumeTrias += nbN - 2;
130 //================================================================================
132 * \brief Return iterator on both faces in the mesh and on temporary faces
134 //================================================================================
136 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
138 SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
139 SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeFacets.begin(),
140 myVolumeFacets.end()));
141 typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
142 TElemIterVector iters(2);
143 iters[0] = facesIter;
144 iters[1] = tmpTriaIter;
146 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
147 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
152 static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile )
161 Standard_Integer entier;
162 entier = u.c[0] & 0xFF;
163 entier |= (u.c[1] & 0xFF) << 0x08;
164 entier |= (u.c[2] & 0xFF) << 0x10;
165 entier |= (u.c[3] & 0xFF) << 0x18;
167 ofile.write( entier );
170 static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
173 Standard_ShortReal f;
179 Standard_Integer entier;
181 entier = u.c[0] & 0xFF;
182 entier |= (u.c[1] & 0xFF) << 0x08;
183 entier |= (u.c[2] & 0xFF) << 0x10;
184 entier |= (u.c[3] & 0xFF) << 0x18;
186 ofile.write( entier );
189 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
190 const SMDS_MeshNode* n2,
191 const SMDS_MeshNode* n3)
193 SMESH_TNodeXYZ xyz1( n1 );
194 SMESH_TNodeXYZ xyz2( n2 );
195 SMESH_TNodeXYZ xyz3( n3 );
196 gp_XYZ q1 = xyz2 - xyz1;
197 gp_XYZ q2 = xyz3 - xyz1;
199 double len = n.Modulus();
200 if ( len > std::numeric_limits<double>::min() )
208 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
210 Status aResult = DRS_OK;
211 if ( myFile.empty() ) {
212 fprintf(stderr, ">> ERREOR : invalid file name \n");
216 SMESH_File aFile( myFile, /*openForReading=*/false );
217 aFile.openForWriting();
219 std::string buf("solid ");
220 buf += myName + "\n";
221 aFile.writeRaw( buf.c_str(), buf.size() );
224 std::vector< const SMDS_MeshNode* > triaNodes;
226 SMESH_MeshAlgos::Triangulate triangulator;
228 SMDS_ElemIteratorPtr itFaces = getFaces();
229 while ( itFaces->more() )
231 const SMDS_MeshElement* aFace = itFaces->next();
232 int nbTria = triangulator.GetTriangles( aFace, triaNodes );
234 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
236 gp_XYZ normale = getNormale( triaNodes[iN],
240 " facet normal % 12e % 12e % 12e\n"
242 normale.X(), normale.Y(), normale.Z());
243 aFile.writeRaw ( sval, 70 + strlen( sval + 70 )); // at least 70 but can be more (WIN)
245 for ( int jN = 0; jN < 3; ++jN, ++iN )
247 SMESH_TNodeXYZ node = triaNodes[iN];
249 " vertex % 12e % 12e % 12e\n",
250 node.X(), node.Y(), node.Z() );
251 aFile.writeRaw ( sval, 54 + strlen( sval + 54 ));
253 aFile.writeRaw (" endloop\n"
257 buf = "endsolid " + myName + "\n";
258 aFile.writeRaw( buf.c_str(), buf.size() );
263 //================================================================================
265 * \brief Writes all triangles in binary format
266 * \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
268 //================================================================================
270 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
272 Status aResult = DRS_OK;
274 if ( myFile.empty() ) {
275 fprintf(stderr, ">> ERREOR : invalid filename \n");
279 SMESH_File aFile( myFile );
280 aFile.openForWriting();
282 SMESH_MeshAlgos::Triangulate triangulator;
284 // we first count the number of triangles
285 int nbTri = myNbVolumeTrias;
287 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
288 while ( itFaces->more() ) {
289 const SMDS_MeshElement* aFace = itFaces->next();
290 nbTri += triangulator.GetNbTriangles( aFace );
293 std::string sval( LABEL_SIZE, ' ' );
294 if ( !myName.empty() )
296 sval = "name: " + myName;
297 sval.resize( LABEL_SIZE, ' ' );
299 aFile.write( sval.c_str(), LABEL_SIZE );
301 // write number of triangles
302 writeInteger( nbTri, aFile );
304 // Loop writing nodes
308 std::vector< const SMDS_MeshNode* > triaNodes;
310 SMDS_ElemIteratorPtr itFaces = getFaces();
311 while ( itFaces->more() )
313 const SMDS_MeshElement* aFace = itFaces->next();
314 int nbTria = triangulator.GetTriangles( aFace, triaNodes );
316 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
318 gp_XYZ normale = getNormale( triaNodes[iN],
321 writeFloat(normale.X(),aFile);
322 writeFloat(normale.Y(),aFile);
323 writeFloat(normale.Z(),aFile);
325 for ( int jN = 0; jN < 3; ++jN, ++iN )
327 const SMDS_MeshNode* node = triaNodes[iN];
328 writeFloat(node->X(),aFile);
329 writeFloat(node->Y(),aFile);
330 writeFloat(node->Z(),aFile);
332 aFile.writeRaw ( &dum, 2 );