1 // Copyright (C) 2007-2012 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, Read 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_Read.cxx
23 // Created : Mon Sep 17 17:03:02 2012
24 // Author : Edward AGAPOV (eap)
26 #include "DriverGMF_Read.hxx"
27 #include "DriverGMF.hxx"
29 #include "SMESHDS_Group.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Comment.hxx"
33 #include <Basics_Utils.hxx>
42 // --------------------------------------------------------------------------------
43 DriverGMF_Read::DriverGMF_Read():
44 Driver_SMESHDS_Mesh(),
45 _makeRequiredGroups( true )
48 // --------------------------------------------------------------------------------
49 DriverGMF_Read::~DriverGMF_Read()
53 //================================================================================
55 * \brief Read a GMF file
57 //================================================================================
59 Driver_Mesh::Status DriverGMF_Read::Perform()
61 Kernel_Utils::Localizer loc;
63 Status status = DRS_OK;
68 int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
71 if ( DriverGMF::isExtensionCorrect( myFile ))
72 return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
74 return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile, /*fatal=*/true );
76 DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
80 int nbNodes = GmfStatKwd(meshID, GmfVertices);
82 return addMessage( "No nodes in the mesh", /*fatal=*/true );
84 GmfGotoKwd(meshID, GmfVertices);
88 const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
89 if ( version != GmfFloat )
92 for ( int i = 1; i <= nbNodes; ++i )
94 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
95 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
101 for ( int i = 1; i <= nbNodes; ++i )
103 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
104 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
110 int iN[28]; // 28 - nb nodes in HEX27 (+ 1 for safety :)
112 /* Read extra vertices for quadratic edges */
113 std::vector<int> quadNodesAtEdges;
115 if ( (nbQuadEdges = GmfStatKwd(meshID, GmfExtraVerticesAtEdges)) )
117 quadNodesAtEdges.reserve( nbQuadEdges );
118 GmfGotoKwd(meshID, GmfExtraVerticesAtEdges);
119 for ( int i = 1; i <= nbQuadEdges; ++i )
121 GmfGetLin(meshID, GmfExtraVerticesAtEdges, &iN[0], &iN[1], &iN[2]);
122 quadNodesAtEdges.push_back(iN[2]);
127 const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
128 if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
130 const bool readQuadNodes = ( nbQuadEdges == nbEdges );
131 GmfGotoKwd(meshID, GmfEdges);
132 for ( int i = 1; i <= nbEdges; ++i )
134 GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
137 const int midN = quadNodesAtEdges[i-1];
138 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], midN, edgeIDShift + i ))
139 status = storeBadNodeIds( "GmfEdges + GmfExtraVerticesAtEdges",i, 3, iN[0],iN[1],midN);
143 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
144 status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
148 // the vector of extra vertices at edges won't be used anymore so it is cleared
149 quadNodesAtEdges.clear();
151 /* Read extra vertices for quadratic triangles */
152 std::vector< std::vector<int> > quadNodesAtTriangles;
154 if ( (nbQuadTria = GmfStatKwd(meshID, GmfExtraVerticesAtTriangles)) )
156 GmfGotoKwd(meshID, GmfExtraVerticesAtTriangles);
157 quadNodesAtTriangles.reserve( nbQuadTria );
158 std::vector<int> nodes(4);
159 for ( int i = 1; i <= nbQuadTria; ++i )
161 GmfGetLin(meshID, GmfExtraVerticesAtTriangles,
162 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4],
163 &iN[5]); // iN[5] - preview TRIA7
165 nodes.push_back(iN[2]);
166 nodes.push_back(iN[3]);
167 nodes.push_back(iN[4]);
168 nodes.push_back(iN[5]);
169 nodes.resize( iN[1] );
171 quadNodesAtTriangles.push_back(nodes);
176 const int triaIDShift = myMesh->GetMeshInfo().NbElements();
177 if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
179 const bool readQuadNodes = (nbQuadTria == nbTria);
180 GmfGotoKwd(meshID, GmfTriangles);
181 for ( int i = 1; i <= nbTria; ++i )
183 GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
186 const std::vector<int>& midN = quadNodesAtTriangles[ i-1 ];
187 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2], midN[0],midN[1],midN[2], triaIDShift + i ))
188 status = storeBadNodeIds( "GmfTriangles + GmfExtraVerticesAtTriangles",i, 6,
189 iN[0],iN[1],iN[2], midN[0],midN[1],midN[2] );
193 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
194 status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
198 // the vector of extra vertices at triangles won't be used anymore so it is cleared
199 quadNodesAtTriangles.clear();
201 /* Read extra vertices for quadratic quadrangles */
202 std::vector< std::vector<int> > quadNodesAtQuadrilaterals;
204 if ( (nbQuadQuad = GmfStatKwd(meshID, GmfExtraVerticesAtQuadrilaterals)) )
206 GmfGotoKwd(meshID, GmfExtraVerticesAtQuadrilaterals);
207 quadNodesAtQuadrilaterals.reserve( nbQuadQuad );
208 std::vector<int> nodes( 5 );
209 for ( int i = 1; i <= nbQuadQuad; ++i )
211 GmfGetLin(meshID, GmfExtraVerticesAtQuadrilaterals,
212 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6]);
214 nodes.push_back(iN[2]);
215 nodes.push_back(iN[3]);
216 nodes.push_back(iN[4]);
217 nodes.push_back(iN[5]);
218 nodes.push_back(iN[6]);
219 nodes.resize( iN[1] );
221 quadNodesAtQuadrilaterals.push_back(nodes);
225 /* Read quadrangles */
226 const int quadIDShift = myMesh->GetMeshInfo().NbElements();
227 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
229 const bool readQuadNodes = (nbQuadQuad == nbQuad);
230 GmfGotoKwd(meshID, GmfQuadrilaterals);
231 for ( int i = 1; i <= nbQuad; ++i )
233 GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
236 const std::vector<int>& midN = quadNodesAtQuadrilaterals[ i-1 ];
237 if ( midN.size() == 4 )
239 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
240 midN[0], midN[1], midN[2], midN[3],
242 status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 8,
243 iN[0], iN[1],iN[2], iN[3],
244 midN[0], midN[1], midN[2], midN[3]);
248 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
249 midN[0], midN[1], midN[2], midN[3], midN[4],
251 status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 9,
252 iN[0], iN[1],iN[2], iN[3],
253 midN[0], midN[1], midN[2], midN[3], midN[4]);
258 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
259 status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
263 // the vector of extra vertices at quadrilaterals won't be used anymore so it is cleared
264 quadNodesAtQuadrilaterals.clear();
266 /* Read extra vertices for quadratic tetrahedra */
267 std::vector< std::vector<int> > quadNodesAtTetrahedra;
269 if ( (nbQuadTetra = GmfStatKwd(meshID, GmfExtraVerticesAtTetrahedra)) )
271 GmfGotoKwd(meshID, GmfExtraVerticesAtTetrahedra);
272 quadNodesAtTetrahedra.reserve( nbQuadTetra );
273 std::vector<int> nodes( 6 );
274 for ( int i = 1; i <= nbQuadTetra; ++i )
276 GmfGetLin(meshID, GmfExtraVerticesAtTetrahedra,
277 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7]);
279 nodes.push_back(iN[2]);
280 nodes.push_back(iN[3]);
281 nodes.push_back(iN[4]);
282 nodes.push_back(iN[5]);
283 nodes.push_back(iN[6]);
284 nodes.push_back(iN[7]);
285 nodes.resize( iN[1] );
287 quadNodesAtTetrahedra.push_back(nodes);
292 const int tetIDShift = myMesh->GetMeshInfo().NbElements();
293 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
295 const bool readQuadNodes = (nbQuadTetra == nbTet);
296 GmfGotoKwd(meshID, GmfTetrahedra);
297 for ( int i = 1; i <= nbTet; ++i )
299 GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
302 const std::vector<int>& midN = quadNodesAtTetrahedra[ i-1 ];
303 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3],
304 midN[2], midN[1], midN[0], midN[3], midN[5], midN[4], tetIDShift + i ))
305 status = storeBadNodeIds( "GmfTetrahedra + GmfExtraVerticesAtTetrahedra",i, 10, iN[0], iN[2], iN[1], iN[3],
306 midN[2], midN[1], midN[0], midN[3], midN[5], midN[4] );
310 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ) )
311 status = storeBadNodeIds( "GmfTetrahedra" ,i, 4, iN[0], iN[2], iN[1], iN[3] );
315 // the vector of extra vertices at tetrahedra won't be used anymore so it is cleared
316 quadNodesAtTetrahedra.clear();
319 const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
320 if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
322 GmfGotoKwd(meshID, GmfPyramids);
323 for ( int i = 1; i <= nbPyr; ++i )
325 GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
326 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[4], pyrIDShift + i ))
327 status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
331 /* Read extra vertices for quadratic hexahedra */
332 std::vector< std::vector<int> > quadNodesAtHexahedra;
334 if ( (nbQuadHexa = GmfStatKwd(meshID, GmfExtraVerticesAtHexahedra)) )
336 GmfGotoKwd(meshID, GmfExtraVerticesAtHexahedra);
337 quadNodesAtHexahedra.reserve( nbQuadHexa );
338 std::vector<int> nodes( 19 );
339 for ( int i = 1; i <= nbQuadHexa; ++i )
341 GmfGetLin(meshID, GmfExtraVerticesAtHexahedra, &iN[0], &iN[1], // Hexa Id, Nb of extra vertices
342 &iN[2], &iN[3], &iN[4], &iN[5],
343 &iN[6], &iN[7], &iN[8], &iN[9],
344 &iN[10], &iN[11], &iN[12], &iN[13], // HEXA20
346 &iN[15], &iN[16], &iN[17], &iN[18],
350 nodes.push_back(iN[2]);
351 nodes.push_back(iN[3]);
352 nodes.push_back(iN[4]);
353 nodes.push_back(iN[5]);
354 nodes.push_back(iN[6]);
355 nodes.push_back(iN[7]);
356 nodes.push_back(iN[8]);
357 nodes.push_back(iN[9]);
358 nodes.push_back(iN[10]);
359 nodes.push_back(iN[11]);
360 nodes.push_back(iN[12]);
361 nodes.push_back(iN[13]);
362 nodes.push_back(iN[14]);
363 nodes.push_back(iN[15]);
364 nodes.push_back(iN[16]);
365 nodes.push_back(iN[17]);
366 nodes.push_back(iN[18]);
367 nodes.push_back(iN[19]);
368 nodes.push_back(iN[20]);
369 nodes.resize( iN[1] );
371 quadNodesAtHexahedra.push_back(nodes);
376 const int hexIDShift = myMesh->GetMeshInfo().NbElements();
377 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
379 const bool readQuadNodes = (nbQuadHexa == nbHex);
380 GmfGotoKwd(meshID, GmfHexahedra);
381 for ( int i = 1; i <= nbHex; ++i )
383 GmfGetLin(meshID, GmfHexahedra, &iN[0], &iN[1], &iN[2], &iN[3],
384 &iN[4], &iN[5], &iN[6], &iN[7],&ref);
387 const std::vector<int>& midN = quadNodesAtHexahedra[ i-1 ];
388 if ( midN.size() == 12 ) // HEXA20
390 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
391 iN[4], iN[7], iN[6], iN[5],
392 midN[3], midN[2], midN[1], midN[0],
393 midN[7], midN[6], midN[5], midN[4],
394 midN[8], midN[11], midN[10], midN[9],
396 status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 20,
397 iN[0], iN[3], iN[2], iN[1],
398 iN[4], iN[7], iN[6], iN[5],
399 midN[3], midN[2], midN[1], midN[0],
400 midN[7], midN[6], midN[5], midN[4],
401 midN[8], midN[11], midN[10], midN[9]);
405 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
406 iN[4], iN[7], iN[6], iN[5],
407 midN[3], midN[2], midN[1], midN[0],
408 midN[7], midN[6], midN[5], midN[4],
409 midN[8], midN[11], midN[10], midN[9],
411 midN[16], midN[15], midN[14], midN[13],
415 status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 27,
416 iN[0], iN[3], iN[2], iN[1],
417 iN[4], iN[7], iN[6], iN[5],
418 midN[3], midN[2], midN[1], midN[0],
419 midN[7], midN[6], midN[5], midN[4],
420 midN[8], midN[11], midN[10], midN[9],
422 midN[16], midN[15], midN[14], midN[13],
429 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
430 iN[4], iN[7], iN[6], iN[5], hexIDShift + i ) )
431 status = storeBadNodeIds( "GmfHexahedra" ,i, 8, iN[0], iN[3], iN[2], iN[1],
432 iN[4], iN[7], iN[6], iN[5] );
436 // the vector of extra vertices at tetrahedra won't be used anymore so it is cleared
437 quadNodesAtHexahedra.clear();
440 const int prismIDShift = myMesh->GetMeshInfo().NbElements();
441 if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
443 GmfGotoKwd(meshID, GmfPrisms);
444 for ( int i = 1; i <= nbPrism; ++i )
446 GmfGetLin(meshID, GmfPrisms,
447 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
448 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
449 status = storeBadNodeIds( "GmfPrisms",i,
450 6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
454 // Read required entities into groups
456 if ( _makeRequiredGroups )
458 // get ids of existing groups
459 std::set< int > groupIDs;
460 const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
461 std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
462 for ( ; grIter != groups.end(); ++grIter )
463 groupIDs.insert( (*grIter)->GetID() );
464 if ( groupIDs.empty() ) groupIDs.insert( 0 );
466 const int kes[4][3] = { { GmfRequiredVertices, SMDSAbs_Node, nodeIDShift },
467 { GmfRequiredEdges, SMDSAbs_Edge, edgeIDShift },
468 { GmfRequiredTriangles, SMDSAbs_Face, triaIDShift },
469 { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }};
470 const char* names[4] = { "_required_Vertices" ,
472 "_required_Triangles" ,
473 "_required_Quadrilaterals" };
474 for ( int i = 0; i < 4; ++i )
476 int gmfKwd = kes[i][0];
477 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
478 int shift = kes[i][2];
479 if ( int nb = GmfStatKwd(meshID, gmfKwd))
481 const int newID = *groupIDs.rbegin() + 1;
482 groupIDs.insert( newID );
483 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
484 group->SetStoreName( names[i] );
485 myMesh->AddGroup( group );
487 GmfGotoKwd(meshID, gmfKwd);
488 for ( int i = 0; i < nb; ++i )
490 GmfGetLin(meshID, gmfKwd, &iN[0] );
491 group->Add( shift + iN[0] );
500 //================================================================================
502 * \brief Store a message about invalid IDs of nodes
504 //================================================================================
506 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
508 if ( myStatus != DRS_OK )
514 va_start(VarArg, nb);
516 for ( int i = 0; i < nb; ++i )
518 int id = va_arg(VarArg, int );
519 if ( !myMesh->FindNode( id ))
527 const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
528 if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
529 else nbStr = SMESH_Comment(elemNb) << "-th ";
532 ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,