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);
113 const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
114 if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
116 GmfGotoKwd(meshID, GmfEdges);
117 for ( int i = 1; i <= nbEdges; ++i )
119 GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
120 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
121 status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
124 /* Read quadratic edges */
125 const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
126 if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
128 GmfGotoKwd(meshID, GmfEdgesP2);
129 for ( int i = 1; i <= nbEdges; ++i )
131 GmfGetLin(meshID, GmfEdgesP2, &iN[0], &iN[1], &iN[2], &ref);
132 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], iN[2], edge2IDShift + i ))
133 status = storeBadNodeIds( "GmfEdgesP2",i, 3, iN[0], iN[1], iN[2] );
137 const int triaIDShift = myMesh->GetMeshInfo().NbElements();
138 if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
140 GmfGotoKwd(meshID, GmfTriangles);
141 for ( int i = 1; i <= nbTria; ++i )
143 GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
144 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
145 status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
148 /* Read quadratic triangles */
149 const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
150 if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
152 GmfGotoKwd(meshID, GmfTrianglesP2);
153 for ( int i = 1; i <= nbTria; ++i )
155 GmfGetLin(meshID, GmfTrianglesP2,
156 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
157 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],
159 status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
162 /* Read quadrangles */
163 const int quadIDShift = myMesh->GetMeshInfo().NbElements();
164 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
166 GmfGotoKwd(meshID, GmfQuadrilaterals);
167 for ( int i = 1; i <= nbQuad; ++i )
169 GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
170 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
171 status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
174 /* Read bi-quadratic quadrangles */
175 const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
176 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
178 GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
179 for ( int i = 1; i <= nbQuad; ++i )
181 GmfGetLin(meshID, GmfQuadrilateralsQ2,
182 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &ref);
183 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8],
185 status = storeBadNodeIds( "GmfQuadrilateralsQ2",i,
186 9, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8] );
190 const int tetIDShift = myMesh->GetMeshInfo().NbElements();
191 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
193 GmfGotoKwd(meshID, GmfTetrahedra);
194 for ( int i = 1; i <= nbTet; ++i )
196 GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
197 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ))
198 status = storeBadNodeIds( "GmfTetrahedra",i, 4, iN[0], iN[1],iN[2], iN[3] );
201 /* Read quadratic terahedra */
202 const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
203 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
205 GmfGotoKwd(meshID, GmfTetrahedraP2);
206 for ( int i = 1; i <= nbTet; ++i )
208 GmfGetLin(meshID, GmfTetrahedraP2, &iN[0], &iN[1], &iN[2],
209 &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &iN[9], &ref);
210 if ( !myMesh->AddVolumeWithID( iN[0],iN[2],iN[1],iN[3],
212 iN[7],iN[9],iN[8], tet2IDShift + i ))
213 status = storeBadNodeIds( "GmfTetrahedraP2",i, 10, iN[0],iN[1],iN[2],iN[3],
214 iN[4],iN[5],iN[6],iN[7],iN[8],iN[9] );
218 const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
219 if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
221 GmfGotoKwd(meshID, GmfPyramids);
222 for ( int i = 1; i <= nbPyr; ++i )
224 GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
225 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[4], pyrIDShift + i ))
226 status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
230 const int hexIDShift = myMesh->GetMeshInfo().NbElements();
231 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
233 GmfGotoKwd(meshID, GmfHexahedra);
234 for ( int i = 1; i <= nbHex; ++i )
236 GmfGetLin(meshID, GmfHexahedra,
237 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &ref);
238 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1], iN[4], iN[7], iN[6], iN[5],
240 status = storeBadNodeIds( "GmfHexahedra",i,
241 8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
244 /* Read tri-quadratic hexahedra */
245 const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
246 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
248 GmfGotoKwd(meshID, GmfHexahedraQ2);
249 for ( int i = 1; i <= nbHex; ++i )
251 GmfGetLin(meshID, GmfHexahedraQ2, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5],
252 &iN[6], &iN[7], &iN[8],&iN[9],&iN[10],&iN[11],&iN[12],&iN[13],&iN[14],
253 &iN[15],&iN[16],&iN[17],&iN[18],&iN[19],&iN[20],&iN[21],&iN[22],&iN[23],
254 &iN[24],&iN[25],&iN[26], &ref);
255 if ( !myMesh->AddVolumeWithID( iN[0],iN[3],iN[2],iN[1],iN[4],iN[7],iN[6],iN[5],iN[11],iN[10],
256 iN[9],iN[8],iN[12],iN[15],iN[14], iN[13],iN[19],iN[18],iN[17],
257 iN[16],iN[20],iN[24],iN[23],iN[22],iN[21], iN[25],iN[26],
259 status = storeBadNodeIds( "GmfHexahedraQ2",i, 27,
260 iN[0],iN[3],iN[2],iN[1],iN[4], iN[7],iN[6],iN[5],iN[11],iN[10],
261 iN[9],iN[8],iN[12],iN[15],iN[14], iN[13],iN[19],iN[18],iN[17],
262 iN[16],iN[20],iN[24],iN[23],iN[22],iN[21], iN[25],iN[26]);
266 const int prismIDShift = myMesh->GetMeshInfo().NbElements();
267 if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
269 GmfGotoKwd(meshID, GmfPrisms);
270 for ( int i = 1; i <= nbPrism; ++i )
272 GmfGetLin(meshID, GmfPrisms,
273 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
274 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
275 status = storeBadNodeIds( "GmfPrisms",i,
276 6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
280 // Read required entities into groups
282 if ( _makeRequiredGroups )
284 // get ids of existing groups
285 std::set< int > groupIDs;
286 const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
287 std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
288 for ( ; grIter != groups.end(); ++grIter )
289 groupIDs.insert( (*grIter)->GetID() );
290 if ( groupIDs.empty() ) groupIDs.insert( 0 );
292 const int kes[4][3] = { { GmfRequiredVertices, SMDSAbs_Node, nodeIDShift },
293 { GmfRequiredEdges, SMDSAbs_Edge, edgeIDShift },
294 { GmfRequiredTriangles, SMDSAbs_Face, triaIDShift },
295 { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }};
296 const char* names[4] = { "_required_Vertices" ,
298 "_required_Triangles" ,
299 "_required_Quadrilaterals" };
300 for ( int i = 0; i < 4; ++i )
302 int gmfKwd = kes[i][0];
303 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
304 int shift = kes[i][2];
305 if ( int nb = GmfStatKwd(meshID, gmfKwd))
307 const int newID = *groupIDs.rbegin() + 1;
308 groupIDs.insert( newID );
309 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
310 group->SetStoreName( names[i] );
311 myMesh->AddGroup( group );
313 GmfGotoKwd(meshID, gmfKwd);
314 for ( int i = 0; i < nb; ++i )
316 GmfGetLin(meshID, gmfKwd, &iN[0] );
317 group->Add( shift + iN[0] );
326 //================================================================================
328 * \brief Store a message about invalid IDs of nodes
330 //================================================================================
332 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
334 if ( myStatus != DRS_OK )
340 va_start(VarArg, nb);
342 for ( int i = 0; i < nb; ++i )
344 int id = va_arg(VarArg, int );
345 if ( !myMesh->FindNode( id ))
353 const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
354 if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
355 else nbStr = SMESH_Comment(elemNb) << "-th ";
358 ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,