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
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"
32 #include "SMESH_TypeDefs.hxx"
34 #include <Basics_Utils.hxx>
43 // --------------------------------------------------------------------------------
44 DriverGMF_Read::DriverGMF_Read():
45 Driver_SMESHDS_Mesh(),
46 _makeRequiredGroups( true ),
47 _makeFaultGroups( true )
50 // --------------------------------------------------------------------------------
51 DriverGMF_Read::~DriverGMF_Read()
55 //================================================================================
57 * \brief Read a GMF file
59 //================================================================================
61 Driver_Mesh::Status DriverGMF_Read::Perform()
63 Kernel_Utils::Localizer loc;
65 Status status = DRS_OK;
70 int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
73 if ( DriverGMF::isExtensionCorrect( myFile ))
74 return addMessage( SMESH_Comment("Can't open for reading ") << myFile,
77 return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile,
80 DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
84 int nbNodes = GmfStatKwd(meshID, GmfVertices);
86 return addMessage( "No nodes in the mesh", /*fatal=*/true );
88 GmfGotoKwd(meshID, GmfVertices);
92 const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
93 if ( version != GmfFloat )
96 for ( int i = 1; i <= nbNodes; ++i )
98 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
99 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
105 for ( int i = 1; i <= nbNodes; ++i )
107 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
108 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
114 int iN[28]; // 28 - nb nodes in HEX27 (+ 1 for safety :)
117 const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
118 if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
120 // read extra vertices for quadratic edges
121 std::vector<int> quadNodesAtEdges( nbEdges + 1, -1 );
122 if ( int nbQuadEdges = GmfStatKwd(meshID, GmfExtraVerticesAtEdges))
124 GmfGotoKwd(meshID, GmfExtraVerticesAtEdges);
125 for ( int i = 1; i <= nbQuadEdges; ++i )
127 GmfGetLin(meshID, GmfExtraVerticesAtEdges, &iN[0], &iN[1], &iN[2]);
129 quadNodesAtEdges[ iN[0] ] = iN[2];
133 GmfGotoKwd(meshID, GmfEdges);
134 for ( int i = 1; i <= nbEdges; ++i )
136 GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
137 const int midN = quadNodesAtEdges[ i ];
140 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], midN, edgeIDShift + i ))
141 status = storeBadNodeIds( "GmfEdges + GmfExtraVerticesAtEdges",i,
142 3, iN[0], iN[1], midN);
146 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
147 status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
153 const int triaIDShift = myMesh->GetMeshInfo().NbElements();
154 if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
156 // read extra vertices for quadratic triangles
157 std::vector< std::vector<int> > quadNodesAtTriangles( nbTria + 1 );
158 if ( int nbQuadTria = GmfStatKwd(meshID, GmfExtraVerticesAtTriangles ))
160 GmfGotoKwd( meshID, GmfExtraVerticesAtTriangles );
161 for ( int i = 1; i <= nbQuadTria; ++i )
163 GmfGetLin(meshID, GmfExtraVerticesAtTriangles,
164 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4],
165 &iN[5]); // iN[5] - preview TRIA7
166 if ( iN[0] <= nbTria )
168 std::vector<int>& nodes = quadNodesAtTriangles[ iN[0] ];
169 nodes.insert( nodes.end(), & iN[2], & iN[5+1] );
170 nodes.resize( iN[1] );
175 GmfGotoKwd(meshID, GmfTriangles);
176 for ( int i = 1; i <= nbTria; ++i )
178 GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
179 std::vector<int>& midN = quadNodesAtTriangles[ i ];
180 if ( midN.size() >= 3 )
182 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2], midN[0],midN[1],midN[2],
184 status = storeBadNodeIds( "GmfTriangles + GmfExtraVerticesAtTriangles",i, 6,
185 iN[0],iN[1],iN[2], midN[0],midN[1],midN[2] );
189 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
190 status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
192 if ( !midN.empty() ) SMESHUtils::FreeVector( midN );
196 /* Read quadrangles */
197 const int quadIDShift = myMesh->GetMeshInfo().NbElements();
198 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
200 // read extra vertices for quadratic quadrangles
201 std::vector< std::vector<int> > quadNodesAtQuadrilaterals( nbQuad + 1 );
202 if ( int nbQuadQuad = GmfStatKwd( meshID, GmfExtraVerticesAtQuadrilaterals ))
204 GmfGotoKwd(meshID, GmfExtraVerticesAtQuadrilaterals);
205 for ( int i = 1; i <= nbQuadQuad; ++i )
207 GmfGetLin(meshID, GmfExtraVerticesAtQuadrilaterals,
208 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6]);
209 if ( iN[0] <= nbQuad )
211 std::vector<int>& nodes = quadNodesAtQuadrilaterals[ iN[0] ];
212 nodes.insert( nodes.end(), & iN[2], & iN[6+1] );
213 nodes.resize( iN[1] );
217 // create quadrangles
218 GmfGotoKwd(meshID, GmfQuadrilaterals);
219 for ( int i = 1; i <= nbQuad; ++i )
221 GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
222 std::vector<int>& midN = quadNodesAtQuadrilaterals[ i ];
223 if ( midN.size() == 8-4 ) // QUAD8
225 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
226 midN[0], midN[1], midN[2], midN[3],
228 status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 8,
229 iN[0], iN[1],iN[2], iN[3],
230 midN[0], midN[1], midN[2], midN[3]);
232 else if ( midN.size() > 8-4 ) // QUAD9
234 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
235 midN[0], midN[1], midN[2], midN[3], midN[4],
237 status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 9,
238 iN[0], iN[1],iN[2], iN[3],
239 midN[0], midN[1], midN[2], midN[3], midN[4]);
243 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
244 status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
246 if ( !midN.empty() ) SMESHUtils::FreeVector( midN );
251 const int tetIDShift = myMesh->GetMeshInfo().NbElements();
252 if ( int nbTet = GmfStatKwd( meshID, GmfTetrahedra ))
254 // read extra vertices for quadratic tetrahedra
255 std::vector< std::vector<int> > quadNodesAtTetrahedra( nbTet + 1 );
256 if ( int nbQuadTetra = GmfStatKwd( meshID, GmfExtraVerticesAtTetrahedra ))
258 GmfGotoKwd(meshID, GmfExtraVerticesAtTetrahedra);
259 for ( int i = 1; i <= nbQuadTetra; ++i )
261 GmfGetLin(meshID, GmfExtraVerticesAtTetrahedra,
262 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7]);
263 if ( iN[0] <= nbTet )
265 std::vector<int>& nodes = quadNodesAtTetrahedra[ iN[0] ];
266 nodes.insert( nodes.end(), & iN[2], & iN[7+1] );
267 nodes.resize( iN[1] );
272 GmfGotoKwd(meshID, GmfTetrahedra);
273 for ( int i = 1; i <= nbTet; ++i )
275 GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
276 std::vector<int>& midN = quadNodesAtTetrahedra[ i ];
277 if ( midN.size() >= 10-4 ) // TETRA10
279 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3],
280 midN[2], midN[1], midN[0], midN[3], midN[5], midN[4],
282 status = storeBadNodeIds( "GmfTetrahedra + GmfExtraVerticesAtTetrahedra",i, 10,
283 iN[0], iN[2], iN[1], iN[3],
284 midN[2], midN[1], midN[0], midN[3], midN[5], midN[4] );
288 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ))
289 status = storeBadNodeIds( "GmfTetrahedra" ,i, 4, iN[0], iN[2], iN[1], iN[3] );
291 if ( !midN.empty() ) SMESHUtils::FreeVector( midN );
296 const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
297 if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
299 GmfGotoKwd(meshID, GmfPyramids);
300 for ( int i = 1; i <= nbPyr; ++i )
302 GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
303 if ( !myMesh->AddVolumeWithID( iN[3], iN[2], iN[1], iN[0], iN[4], pyrIDShift + i ))
304 status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
309 const int hexIDShift = myMesh->GetMeshInfo().NbElements();
310 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
312 // read extra vertices for quadratic hexahedra
313 std::vector< std::vector<int> > quadNodesAtHexahedra( nbHex + 1 );
314 if ( int nbQuadHexa = GmfStatKwd( meshID, GmfExtraVerticesAtHexahedra ))
316 GmfGotoKwd(meshID, GmfExtraVerticesAtHexahedra);
317 for ( int i = 1; i <= nbQuadHexa; ++i )
319 GmfGetLin(meshID, GmfExtraVerticesAtHexahedra, &iN[0], &iN[1], // Hexa Id, Nb extra vertices
320 &iN[2], &iN[3], &iN[4], &iN[5],
321 &iN[6], &iN[7], &iN[8], &iN[9],
322 &iN[10], &iN[11], &iN[12], &iN[13], // HEXA20
324 &iN[15], &iN[16], &iN[17], &iN[18],
327 if ( iN[0] <= nbHex )
329 std::vector<int>& nodes = quadNodesAtHexahedra[ iN[0] ];
330 nodes.insert( nodes.end(), & iN[2], & iN[20+1] );
331 nodes.resize( iN[1] );
336 GmfGotoKwd(meshID, GmfHexahedra);
337 for ( int i = 1; i <= nbHex; ++i )
339 GmfGetLin(meshID, GmfHexahedra, &iN[0], &iN[1], &iN[2], &iN[3],
340 &iN[4], &iN[5], &iN[6], &iN[7], &ref);
341 std::vector<int>& midN = quadNodesAtHexahedra[ i ];
342 if ( midN.size() == 20-8 ) // HEXA20
344 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
345 iN[4], iN[7], iN[6], iN[5],
346 midN[3], midN[2], midN[1], midN[0],
347 midN[7], midN[6], midN[5], midN[4],
348 midN[8], midN[11], midN[10], midN[9],
350 status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 20,
351 iN[0], iN[3], iN[2], iN[1],
352 iN[4], iN[7], iN[6], iN[5],
353 midN[3], midN[2], midN[1], midN[0],
354 midN[7], midN[6], midN[5], midN[4],
355 midN[8], midN[11], midN[10], midN[9]);
357 else if ( midN.size() >= 27-8 ) // HEXA27
359 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
360 iN[4], iN[7], iN[6], iN[5],
361 midN[3], midN[2], midN[1], midN[0],
362 midN[7], midN[6], midN[5], midN[4],
363 midN[8], midN[11], midN[10], midN[9],
365 midN[16], midN[15], midN[14], midN[13],
369 status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 27,
370 iN[0], iN[3], iN[2], iN[1],
371 iN[4], iN[7], iN[6], iN[5],
372 midN[3], midN[2], midN[1], midN[0],
373 midN[7], midN[6], midN[5], midN[4],
374 midN[8], midN[11], midN[10], midN[9],
376 midN[16], midN[15], midN[14], midN[13],
382 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
383 iN[4], iN[7], iN[6], iN[5], hexIDShift + i ) )
384 status = storeBadNodeIds( "GmfHexahedra" ,i, 8, iN[0], iN[3], iN[2], iN[1],
385 iN[4], iN[7], iN[6], iN[5] );
387 if ( !midN.empty() ) SMESHUtils::FreeVector( midN );
392 const int prismIDShift = myMesh->GetMeshInfo().NbElements();
393 if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
395 GmfGotoKwd(meshID, GmfPrisms);
396 for ( int i = 1; i <= nbPrism; ++i )
398 GmfGetLin(meshID, GmfPrisms, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
399 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
400 status = storeBadNodeIds( "GmfPrisms",i,
401 6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
405 // Read some entities into groups
406 // see MeshGems/Docs/meshgems_formats_description.pdf
408 // get ids of existing groups
409 std::set< int > groupIDs;
410 const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
411 std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
412 for ( ; grIter != groups.end(); ++grIter )
413 groupIDs.insert( (*grIter)->GetID() );
414 if ( groupIDs.empty() ) groupIDs.insert( 0 );
416 // Read required entities into groups
417 if ( _makeRequiredGroups )
420 const int kes[4][3] = { { GmfRequiredVertices, SMDSAbs_Node, nodeIDShift },
421 { GmfRequiredEdges, SMDSAbs_Edge, edgeIDShift },
422 { GmfRequiredTriangles, SMDSAbs_Face, triaIDShift },
423 { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }
425 const char* names[4] = { "_required_Vertices" ,
427 "_required_Triangles" ,
428 "_required_Quadrilaterals"
430 for ( int i = 0; i < 4; ++i )
432 int gmfKwd = kes[i][0];
433 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
434 int shift = kes[i][2];
435 if ( int nb = GmfStatKwd(meshID, gmfKwd))
437 const int newID = *groupIDs.rbegin() + 1;
438 groupIDs.insert( newID );
439 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
440 group->SetStoreName( names[i] );
441 myMesh->AddGroup( group );
443 GmfGotoKwd(meshID, gmfKwd);
444 for ( int i = 0; i < nb; ++i )
446 GmfGetLin(meshID, gmfKwd, &iN[0] );
447 group->Add( shift + iN[0] );
453 // Read fault entities into groups
454 if ( _makeFaultGroups )
457 const int kes[7][3] = { { GmfFault_SmallTri, SMDSAbs_Face, triaIDShift },
458 { GmfFault_BadShape, SMDSAbs_Face, triaIDShift },
459 { GmfFault_Overlap, SMDSAbs_Face, triaIDShift },
460 { GmfFault_Inter, SMDSAbs_Face, triaIDShift },
461 { GmfFault_NearTri, SMDSAbs_Face, triaIDShift },
462 { GmfFault_FreeEdge, SMDSAbs_Face, triaIDShift },
463 { GmfFault_MultipleEdge, SMDSAbs_Face, triaIDShift }
465 const char* names[7] = { "Fault_SmallTri",
473 for ( int i = 0; i < 7; ++i )
475 int gmfKwd = kes[i][0];
476 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
477 int shift = kes[i][2];
478 if ( int nb = GmfStatKwd(meshID, gmfKwd))
480 const int newID = *groupIDs.rbegin() + 1;
481 groupIDs.insert( newID );
482 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
483 group->SetStoreName( names[i] );
484 myMesh->AddGroup( group );
486 GmfGotoKwd(meshID, gmfKwd);
487 for ( int i = 0; i < nb; ++i )
489 GmfGetLin(meshID, gmfKwd, &iN[0] );
490 group->Add( shift + iN[0] );
498 myMesh->CompactMesh();
503 //================================================================================
505 * \brief Store a message about invalid IDs of nodes
507 //================================================================================
509 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
511 if ( myStatus != DRS_OK )
517 va_start(VarArg, nb);
519 for ( int i = 0; i < nb; ++i )
521 int id = va_arg(VarArg, int );
522 if ( !myMesh->FindNode( id ))
530 const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
531 if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
532 else nbStr = SMESH_Comment(elemNb) << "-th ";
535 ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,