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_Write.hxx"
29 #include "SMESHDS_Group.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Comment.hxx"
40 // --------------------------------------------------------------------------------
41 // Closing GMF mesh at destruction
42 DriverGMF_MeshCloser::~DriverGMF_MeshCloser()
45 GmfCloseMesh( _gmfMeshID );
47 // --------------------------------------------------------------------------------
48 DriverGMF_Read::DriverGMF_Read():
52 // --------------------------------------------------------------------------------
53 DriverGMF_Read::~DriverGMF_Read()
57 //================================================================================
59 * \brief Read a GMF file
61 //================================================================================
63 Driver_Mesh::Status DriverGMF_Read::Perform()
65 Status status = DRS_OK;
70 int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
72 return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
74 DriverGMF_MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
78 int nbNodes = GmfStatKwd(meshID, GmfVertices);
80 return addMessage( "No nodes in the mesh", /*fatal=*/true );
82 GmfGotoKwd(meshID, GmfVertices);
86 const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
87 if ( version != GmfFloat )
90 for ( int i = 1; i <= nbNodes; ++i )
92 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
93 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
99 for ( int i = 1; i <= nbNodes; ++i )
101 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
102 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
111 const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
112 if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
114 GmfGotoKwd(meshID, GmfEdges);
115 for ( int i = 1; i <= nbEdges; ++i )
117 GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
118 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
119 status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
122 /* Read quadratic edges */
123 const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
124 if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
126 GmfGotoKwd(meshID, GmfEdgesP2);
127 for ( int i = 1; i <= nbEdges; ++i )
129 GmfGetLin(meshID, GmfEdgesP2, &iN[0], &iN[1], &iN[2], &ref);
130 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], iN[2], edge2IDShift + i ))
131 status = storeBadNodeIds( "GmfEdgesP2",i, 3, iN[0], iN[1], iN[2] );
135 const int triaIDShift = myMesh->GetMeshInfo().NbElements();
136 if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
138 GmfGotoKwd(meshID, GmfTriangles);
139 for ( int i = 1; i <= nbTria; ++i )
141 GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
142 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
143 status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
146 /* Read quadratic triangles */
147 const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
148 if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
150 GmfGotoKwd(meshID, GmfTrianglesP2);
151 for ( int i = 1; i <= nbTria; ++i )
153 GmfGetLin(meshID, GmfTrianglesP2,
154 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
155 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],
157 status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
160 /* Read quadrangles */
161 const int quadIDShift = myMesh->GetMeshInfo().NbElements();
162 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
164 GmfGotoKwd(meshID, GmfQuadrilaterals);
165 for ( int i = 1; i <= nbQuad; ++i )
167 GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
168 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
169 status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
172 /* Read bi-quadratic quadrangles */
173 const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
174 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
176 GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
177 for ( int i = 1; i <= nbQuad; ++i )
179 GmfGetLin(meshID, GmfQuadrilateralsQ2,
180 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &ref);
181 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8],
183 status = storeBadNodeIds( "GmfQuadrilateralsQ2",i,
184 9, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8] );
188 const int tetIDShift = myMesh->GetMeshInfo().NbElements();
189 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
191 GmfGotoKwd(meshID, GmfTetrahedra);
192 for ( int i = 1; i <= nbTet; ++i )
194 GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
195 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ))
196 status = storeBadNodeIds( "GmfTetrahedra",i, 4, iN[0], iN[1],iN[2], iN[3] );
199 /* Read quadratic terahedra */
200 const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
201 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
203 GmfGotoKwd(meshID, GmfTetrahedraP2);
204 for ( int i = 1; i <= nbTet; ++i )
206 GmfGetLin(meshID, GmfTetrahedraP2, &iN[0], &iN[1], &iN[2],
207 &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &iN[9], &ref);
208 if ( !myMesh->AddVolumeWithID( iN[0],iN[2],iN[1],iN[3],
210 iN[7],iN[9],iN[8], tet2IDShift + i ))
211 status = storeBadNodeIds( "GmfTetrahedraP2",i, 10, iN[0],iN[1],iN[2],iN[3],
212 iN[4],iN[5],iN[6],iN[7],iN[8],iN[9] );
216 const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
217 if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
219 GmfGotoKwd(meshID, GmfPyramids);
220 for ( int i = 1; i <= nbPyr; ++i )
222 GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
223 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[4], pyrIDShift + i ))
224 status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
228 const int hexIDShift = myMesh->GetMeshInfo().NbElements();
229 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
231 GmfGotoKwd(meshID, GmfHexahedra);
232 for ( int i = 1; i <= nbHex; ++i )
234 GmfGetLin(meshID, GmfHexahedra,
235 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &ref);
236 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1], iN[4], iN[7], iN[6], iN[5],
238 status = storeBadNodeIds( "GmfHexahedra",i,
239 8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
242 /* Read tri-quadratic hexahedra */
243 const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
244 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
246 GmfGotoKwd(meshID, GmfHexahedraQ2);
247 for ( int i = 1; i <= nbHex; ++i )
249 GmfGetLin(meshID, GmfHexahedraQ2, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5],
250 &iN[6], &iN[7], &iN[8],&iN[9],&iN[10],&iN[11],&iN[12],&iN[13],&iN[14],
251 &iN[15],&iN[16],&iN[17],&iN[18],&iN[19],&iN[20],&iN[21],&iN[22],&iN[23],
252 &iN[24],&iN[25],&iN[26], &ref);
253 if ( !myMesh->AddVolumeWithID( iN[0],iN[3],iN[2],iN[1],iN[4],iN[7],iN[6],iN[5],iN[11],iN[10],
254 iN[9],iN[8],iN[12],iN[15],iN[14], iN[13],iN[19],iN[18],iN[17],
255 iN[16],iN[20],iN[24],iN[23],iN[22],iN[21], iN[25],iN[26],
257 status = storeBadNodeIds( "GmfHexahedraQ2",i, 27,
258 iN[0],iN[3],iN[2],iN[1],iN[4], iN[7],iN[6],iN[5],iN[11],iN[10],
259 iN[9],iN[8],iN[12],iN[15],iN[14], iN[13],iN[19],iN[18],iN[17],
260 iN[16],iN[20],iN[24],iN[23],iN[22],iN[21], iN[25],iN[26]);
264 const int prismIDShift = myMesh->GetMeshInfo().NbElements();
265 if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
267 GmfGotoKwd(meshID, GmfPrisms);
268 for ( int i = 1; i <= nbPrism; ++i )
270 GmfGetLin(meshID, GmfPrisms,
271 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
272 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
273 status = storeBadNodeIds( "GmfPrisms",i,
274 6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
278 // Read required entities into groups
280 // get ids of existing groups
281 std::set< int > groupIDs;
282 const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
283 std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
284 for ( ; grIter != groups.end(); ++grIter )
285 groupIDs.insert( (*grIter)->GetID() );
286 if ( groupIDs.empty() ) groupIDs.insert( 0 );
288 const int kes[4][3] = { { GmfRequiredVertices, SMDSAbs_Node, nodeIDShift },
289 { GmfRequiredEdges, SMDSAbs_Edge, edgeIDShift },
290 { GmfRequiredTriangles, SMDSAbs_Face, triaIDShift },
291 { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }};
292 const char* names[4] = { "_required_Vertices" ,
294 "_required_Triangles" ,
295 "_required_Quadrilaterals" };
296 for ( int i = 0; i < 4; ++i )
298 int gmfKwd = kes[i][0];
299 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
300 int shift = kes[i][2];
301 if ( int nb = GmfStatKwd(meshID, gmfKwd))
303 const int newID = *groupIDs.rbegin() + 1;
304 groupIDs.insert( newID );
305 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
306 group->SetStoreName( names[i] );
307 myMesh->AddGroup( group );
309 GmfGotoKwd(meshID, gmfKwd);
310 for ( int i = 0; i < nb; ++i )
312 GmfGetLin(meshID, gmfKwd, &iN[0] );
313 group->Add( shift + iN[0] );
321 //================================================================================
323 * \brief Store a message about invalid IDs of nodes
325 //================================================================================
327 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
329 if ( myStatus != DRS_OK )
335 va_start(VarArg, nb);
337 for ( int i = 0; i < nb; ++i )
339 int id = va_arg(VarArg, int );
340 if ( !myMesh->FindNode( id ))
348 const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
349 if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
350 else nbStr = SMESH_Comment(elemNb) << "-th ";
353 ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,