1 // Copyright (C) 2007-2013 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
22 // File : DriverGMF_Write.cxx
23 // Created : Mon Sep 17 17:03:02 2012
24 // Author : Edward AGAPOV (eap)
26 #include "DriverGMF_Write.hxx"
27 #include "DriverGMF.hxx"
29 #include "SMESHDS_GroupBase.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Comment.hxx"
33 #include <Basics_Utils.hxx>
42 #define BEGIN_ELEM_WRITE( SMDSEntity, GmfKwd, elem ) \
43 elemIt = elementIterator( SMDSEntity ); \
44 if ( elemIt->more() ) \
46 GmfSetKwd(meshID, GmfKwd, myMesh->GetMeshInfo().NbElements( SMDSEntity )); \
47 for ( int gmfID = 1; elemIt->more(); ++gmfID ) \
49 const SMDS_MeshElement* elem = elemIt->next(); \
50 GmfSetLin(meshID, GmfKwd,
52 #define BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom, LinType, GmfKwd, elem ) \
53 elemIt = elementIterator( SMDSGeom ); \
54 if ( elemIt->more() ) \
56 int totalNbElems = myMesh->GetMeshInfo().NbElements( SMDSGeom ); \
57 int nbLinearElems = myMesh->GetMeshInfo().NbElements( LinType ); \
58 if ( totalNbElems - nbLinearElems > 0 ) \
60 GmfSetKwd(meshID, GmfKwd, totalNbElems - nbLinearElems); \
61 for ( int gmfID = 1; elemIt->more(); ++gmfID ) \
63 const SMDS_MeshElement* elem = elemIt->next(); \
64 if ( elem->IsQuadratic() ) { \
65 GmfSetLin(meshID, GmfKwd, gmfID, elem->NbNodes() - elem->NbCornerNodes(),
67 #define END_ELEM_WRITE( elem ) \
68 elem->getshapeId() ); \
71 #define END_ELEM_WRITE_ADD_TO_MAP( elem, e2id ) \
72 elem->getshapeId() ); \
73 e2id.insert( e2id.end(), make_pair( elem, gmfID )); \
76 #define END_EXTRA_VERTICES_WRITE() \
81 DriverGMF_Write::DriverGMF_Write():
82 Driver_SMESHDS_Mesh(), _exportRequiredGroups( true )
85 DriverGMF_Write::~DriverGMF_Write()
89 //================================================================================
91 * \brief Reads a GMF file
93 //================================================================================
95 Driver_Mesh::Status DriverGMF_Write::Perform()
97 Kernel_Utils::Localizer loc;
99 const int dim = 3, version = sizeof(long) == 4 ? 2 : 3;
101 int meshID = GmfOpenMesh( myFile.c_str(), GmfWrite, version, dim );
104 if ( DriverGMF::isExtensionCorrect( myFile ))
105 return addMessage( SMESH_Comment("Can't open for writing ") << myFile, /*fatal=*/true );
107 return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile, /*fatal=*/true );
110 DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
113 std::map< const SMDS_MeshNode* , int > node2IdMap;
114 int iN = 0, nbNodes = myMesh->NbNodes();
115 GmfSetKwd( meshID, GmfVertices, nbNodes );
117 SMDS_NodeIteratorPtr nodeIt = myMesh->nodesIterator();
118 while ( nodeIt->more() )
120 const SMDS_MeshNode* n = nodeIt->next();
122 GmfSetLin( meshID, GmfVertices, xyz[0], xyz[1], xyz[2], n->getshapeId() );
123 node2IdMap.insert( node2IdMap.end(), make_pair( n, ++iN ));
126 return addMessage("Wrong nb of nodes returned by nodesIterator", /*fatal=*/true);
129 SMDS_ElemIteratorPtr elemIt;
130 typedef std::map< const SMDS_MeshElement*, size_t, TIDCompare > TElem2IDMap;
133 TElem2IDMap edge2IDMap;
134 BEGIN_ELEM_WRITE( SMDSGeom_EDGE, GmfEdges, edge )
135 node2IdMap[ edge->GetNode( 0 )],
136 node2IdMap[ edge->GetNode( 1 )],
137 END_ELEM_WRITE_ADD_TO_MAP( edge, edge2IDMap );
139 // nodes of quadratic edges
140 BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_EDGE, SMDSEntity_Edge,
141 GmfExtraVerticesAtEdges, edge )
142 node2IdMap[ edge->GetNode( 2 )]
143 END_EXTRA_VERTICES_WRITE();
146 TElem2IDMap tria2IDMap;
147 BEGIN_ELEM_WRITE( SMDSGeom_TRIANGLE, GmfTriangles, tria )
148 node2IdMap[ tria->GetNode( 0 )],
149 node2IdMap[ tria->GetNode( 1 )],
150 node2IdMap[ tria->GetNode( 2 )],
151 END_ELEM_WRITE_ADD_TO_MAP( tria, tria2IDMap );
153 // nodes of quadratic triangles
154 BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_TRIANGLE, SMDSEntity_Triangle,
155 GmfExtraVerticesAtTriangles, tria )
156 node2IdMap[ tria->GetNode( 3 )],
157 node2IdMap[ tria->GetNode( 4 )],
158 node2IdMap[ tria->GetNode( 5 )],
159 node2IdMap[ tria->GetNodeWrap( 6 )] // for TRIA7
160 END_EXTRA_VERTICES_WRITE();
163 TElem2IDMap quad2IDMap;
164 BEGIN_ELEM_WRITE( SMDSGeom_QUADRANGLE, GmfQuadrilaterals, quad )
165 node2IdMap[ quad->GetNode( 0 )],
166 node2IdMap[ quad->GetNode( 1 )],
167 node2IdMap[ quad->GetNode( 2 )],
168 node2IdMap[ quad->GetNode( 3 )],
169 END_ELEM_WRITE_ADD_TO_MAP( quad, quad2IDMap );
171 // nodes of quadratic quadrangles
172 BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_QUADRANGLE, SMDSEntity_Quadrangle,
173 GmfExtraVerticesAtQuadrilaterals, quad )
174 node2IdMap[ quad->GetNode( 4 )],
175 node2IdMap[ quad->GetNode( 5 )],
176 node2IdMap[ quad->GetNode( 6 )],
177 node2IdMap[ quad->GetNode( 7 )],
178 node2IdMap[ quad->GetNodeWrap( 8 )] // for QUAD9
179 END_EXTRA_VERTICES_WRITE();
182 BEGIN_ELEM_WRITE( SMDSGeom_TETRA, GmfTetrahedra, tetra )
183 node2IdMap[ tetra->GetNode( 0 )],
184 node2IdMap[ tetra->GetNode( 2 )],
185 node2IdMap[ tetra->GetNode( 1 )],
186 node2IdMap[ tetra->GetNode( 3 )],
187 END_ELEM_WRITE( tetra );
189 // nodes of quadratic terahedra
190 BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_TETRA, SMDSEntity_Tetra,
191 GmfExtraVerticesAtTetrahedra, tetra )
192 node2IdMap[ tetra->GetNode( 6 )],
193 node2IdMap[ tetra->GetNode( 5 )],
194 node2IdMap[ tetra->GetNode( 4 )],
195 node2IdMap[ tetra->GetNode( 7 )],
196 node2IdMap[ tetra->GetNode( 9 )],
197 node2IdMap[ tetra->GetNode( 8 )]
198 //node2IdMap[ tetra->GetNodeWrap( 10 )], // for TETRA11
199 END_EXTRA_VERTICES_WRITE();
202 BEGIN_ELEM_WRITE( SMDSEntity_Pyramid, GmfPyramids, pyra )
203 node2IdMap[ pyra->GetNode( 0 )],
204 node2IdMap[ pyra->GetNode( 2 )],
205 node2IdMap[ pyra->GetNode( 1 )],
206 node2IdMap[ pyra->GetNode( 3 )],
207 node2IdMap[ pyra->GetNode( 4 )],
208 END_ELEM_WRITE( pyra );
211 BEGIN_ELEM_WRITE( SMDSGeom_HEXA, GmfHexahedra, hexa )
212 node2IdMap[ hexa->GetNode( 0 )],
213 node2IdMap[ hexa->GetNode( 3 )],
214 node2IdMap[ hexa->GetNode( 2 )],
215 node2IdMap[ hexa->GetNode( 1 )],
216 node2IdMap[ hexa->GetNode( 4 )],
217 node2IdMap[ hexa->GetNode( 7 )],
218 node2IdMap[ hexa->GetNode( 6 )],
219 node2IdMap[ hexa->GetNode( 5 )],
220 END_ELEM_WRITE( hexa );
222 // nodes of quadratic hexahedra
223 BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_HEXA, SMDSEntity_Hexa,
224 GmfExtraVerticesAtHexahedra, hexa )
225 node2IdMap[ hexa->GetNode( 11 )], // HEXA20
226 node2IdMap[ hexa->GetNode( 10 )],
227 node2IdMap[ hexa->GetNode( 9 )],
228 node2IdMap[ hexa->GetNode( 8 )],
229 node2IdMap[ hexa->GetNode( 15 )],
230 node2IdMap[ hexa->GetNode( 14 )],
231 node2IdMap[ hexa->GetNode( 13 )],
232 node2IdMap[ hexa->GetNode( 12 )],
233 node2IdMap[ hexa->GetNode( 16 )],
234 node2IdMap[ hexa->GetNode( 19 )],
235 node2IdMap[ hexa->GetNodeWrap( 18 )], // + HEXA27
236 node2IdMap[ hexa->GetNodeWrap( 17 )],
237 node2IdMap[ hexa->GetNodeWrap( 20 )],
238 node2IdMap[ hexa->GetNodeWrap( 24 )],
239 node2IdMap[ hexa->GetNodeWrap( 23 )],
240 node2IdMap[ hexa->GetNodeWrap( 22 )],
241 node2IdMap[ hexa->GetNodeWrap( 21 )],
242 node2IdMap[ hexa->GetNodeWrap( 25 )],
243 node2IdMap[ hexa->GetNodeWrap( 26 )]
244 END_EXTRA_VERTICES_WRITE();
247 BEGIN_ELEM_WRITE( SMDSEntity_Penta, GmfPrisms, prism )
248 node2IdMap[ prism->GetNode( 0 )],
249 node2IdMap[ prism->GetNode( 2 )],
250 node2IdMap[ prism->GetNode( 1 )],
251 node2IdMap[ prism->GetNode( 3 )],
252 node2IdMap[ prism->GetNode( 5 )],
253 node2IdMap[ prism->GetNode( 4 )],
254 END_ELEM_WRITE( prism );
257 if ( _exportRequiredGroups )
260 SMESH_Comment badGroups;
261 const std::set<SMESHDS_GroupBase*>& groupSet = myMesh->GetGroups();
262 std::set<SMESHDS_GroupBase*>::const_iterator grIt = groupSet.begin();
263 for ( ; grIt != groupSet.end(); ++grIt )
265 const SMESHDS_GroupBase* group = *grIt;
266 std::string groupName = group->GetStoreName();
267 std::string::size_type pos = groupName.find( "_required_" );
268 if ( pos == std::string::npos ) continue;
271 SMDSAbs_EntityType smdsEntity;
272 std::string entity = groupName.substr( pos + strlen("_required_"));
273 if ( entity == "Vertices" ) {
274 gmfKwd = GmfRequiredVertices;
275 smdsEntity = SMDSEntity_Node;
277 else if ( entity == "Edges" ) {
278 gmfKwd = GmfRequiredEdges;
279 smdsEntity = SMDSEntity_Edge;
281 else if ( entity == "Triangles" ) {
282 gmfKwd = GmfRequiredTriangles;
283 smdsEntity = SMDSEntity_Triangle;
285 else if ( entity == "Quadrilaterals" ) {
286 gmfKwd = GmfRequiredQuadrilaterals;
287 smdsEntity = SMDSEntity_Quadrangle;
290 addMessage( SMESH_Comment("Invalig gmf entity name: ") << entity, /*fatal=*/false );
294 // check elem type in the group
296 SMDS_ElemIteratorPtr elemIt = group->GetElements();
297 while ( elemIt->more() )
298 nbOkElems += ( elemIt->next()->GetEntityType() == smdsEntity );
300 if ( nbOkElems != group->Extent() && nbOkElems == 0 )
302 badGroups << " " << groupName;
306 // choose a TElem2IDMap
307 TElem2IDMap* elem2IDMap = 0;
308 if ( smdsEntity == SMDSEntity_Quadrangle && nbOkElems != myMesh->NbFaces() )
309 elem2IDMap = & quad2IDMap;
310 else if ( smdsEntity == SMDSEntity_Triangle && nbOkElems != myMesh->NbFaces() )
311 elem2IDMap = & tria2IDMap;
312 else if ( smdsEntity == SMDSEntity_Edge && nbOkElems != myMesh->NbEdges() )
313 elem2IDMap = & edge2IDMap;
316 GmfSetKwd( meshID, gmfKwd, nbOkElems );
317 elemIt = group->GetElements();
319 for ( ; elemIt->more(); )
321 const SMDS_MeshElement* elem = elemIt->next();
322 if ( elem->GetEntityType() == smdsEntity )
323 GmfSetLin( meshID, gmfKwd, (*elem2IDMap)[ elem ] );
326 for ( int gmfID = 1; elemIt->more(); ++gmfID)
328 const SMDS_MeshElement* elem = elemIt->next();
329 if ( elem->GetEntityType() == smdsEntity )
330 GmfSetLin( meshID, gmfKwd, gmfID );
335 if ( !badGroups.empty() )
336 addMessage( SMESH_Comment("Groups of elements of inappropriate geometry:")
337 << badGroups, /*fatal=*/false );
343 //================================================================================
345 * \brief Returns an iterator on elements of a certain type
347 //================================================================================
349 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_ElementType type)
351 return myMesh->elementsIterator(type);
353 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_EntityType type)
355 return myMesh->elementEntityIterator(type);
357 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_GeometryType type)
359 return myMesh->elementGeomIterator(type);