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"
33 #include <Basics_Utils.hxx>
42 // --------------------------------------------------------------------------------
43 // Closing GMF mesh at destruction
44 DriverGMF_MeshCloser::~DriverGMF_MeshCloser()
47 GmfCloseMesh( _gmfMeshID );
49 // --------------------------------------------------------------------------------
50 DriverGMF_Read::DriverGMF_Read():
51 Driver_SMESHDS_Mesh(),
52 _makeRequiredGroups( true )
55 // --------------------------------------------------------------------------------
56 DriverGMF_Read::~DriverGMF_Read()
60 //================================================================================
62 * \brief Read a GMF file
64 //================================================================================
66 Driver_Mesh::Status DriverGMF_Read::Perform()
68 Kernel_Utils::Localizer loc;
70 Status status = DRS_OK;
75 int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
77 return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
79 DriverGMF_MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
83 int nbNodes = GmfStatKwd(meshID, GmfVertices);
85 return addMessage( "No nodes in the mesh", /*fatal=*/true );
87 GmfGotoKwd(meshID, GmfVertices);
91 const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
92 if ( version != GmfFloat )
95 for ( int i = 1; i <= nbNodes; ++i )
97 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
98 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
104 for ( int i = 1; i <= nbNodes; ++i )
106 GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
107 myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
116 const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
117 if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
119 GmfGotoKwd(meshID, GmfEdges);
120 for ( int i = 1; i <= nbEdges; ++i )
122 GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
123 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
124 status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
127 /* Read quadratic edges */
128 const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
129 if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
131 GmfGotoKwd(meshID, GmfEdgesP2);
132 for ( int i = 1; i <= nbEdges; ++i )
134 GmfGetLin(meshID, GmfEdgesP2, &iN[0], &iN[1], &iN[2], &ref);
135 if ( !myMesh->AddEdgeWithID( iN[0], iN[1], iN[2], edge2IDShift + i ))
136 status = storeBadNodeIds( "GmfEdgesP2",i, 3, iN[0], iN[1], iN[2] );
140 const int triaIDShift = myMesh->GetMeshInfo().NbElements();
141 if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
143 GmfGotoKwd(meshID, GmfTriangles);
144 for ( int i = 1; i <= nbTria; ++i )
146 GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
147 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
148 status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
151 /* Read quadratic triangles */
152 const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
153 if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
155 GmfGotoKwd(meshID, GmfTrianglesP2);
156 for ( int i = 1; i <= nbTria; ++i )
158 GmfGetLin(meshID, GmfTrianglesP2,
159 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
160 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],
162 status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
165 /* Read quadrangles */
166 const int quadIDShift = myMesh->GetMeshInfo().NbElements();
167 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
169 GmfGotoKwd(meshID, GmfQuadrilaterals);
170 for ( int i = 1; i <= nbQuad; ++i )
172 GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
173 if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
174 status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
177 /* Read bi-quadratic quadrangles */
178 const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
179 if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
181 GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
182 for ( int i = 1; i <= nbQuad; ++i )
184 GmfGetLin(meshID, GmfQuadrilateralsQ2,
185 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &ref);
186 if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8],
188 status = storeBadNodeIds( "GmfQuadrilateralsQ2",i,
189 9, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5],iN[6],iN[7],iN[8] );
193 const int tetIDShift = myMesh->GetMeshInfo().NbElements();
194 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
196 GmfGotoKwd(meshID, GmfTetrahedra);
197 for ( int i = 1; i <= nbTet; ++i )
199 GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
200 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ))
201 status = storeBadNodeIds( "GmfTetrahedra",i, 4, iN[0], iN[1],iN[2], iN[3] );
204 /* Read quadratic terahedra */
205 const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
206 if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
208 GmfGotoKwd(meshID, GmfTetrahedraP2);
209 for ( int i = 1; i <= nbTet; ++i )
211 GmfGetLin(meshID, GmfTetrahedraP2, &iN[0], &iN[1], &iN[2],
212 &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &iN[8], &iN[9], &ref);
213 if ( !myMesh->AddVolumeWithID( iN[0],iN[2],iN[1],iN[3],
215 iN[7],iN[9],iN[8], tet2IDShift + i ))
216 status = storeBadNodeIds( "GmfTetrahedraP2",i, 10, iN[0],iN[1],iN[2],iN[3],
217 iN[4],iN[5],iN[6],iN[7],iN[8],iN[9] );
221 const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
222 if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
224 GmfGotoKwd(meshID, GmfPyramids);
225 for ( int i = 1; i <= nbPyr; ++i )
227 GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
228 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[4], pyrIDShift + i ))
229 status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
233 const int hexIDShift = myMesh->GetMeshInfo().NbElements();
234 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
236 GmfGotoKwd(meshID, GmfHexahedra);
237 for ( int i = 1; i <= nbHex; ++i )
239 GmfGetLin(meshID, GmfHexahedra,
240 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7], &ref);
241 if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1], iN[4], iN[7], iN[6], iN[5],
243 status = storeBadNodeIds( "GmfHexahedra",i,
244 8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
247 /* Read tri-quadratic hexahedra */
248 const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
249 if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
251 GmfGotoKwd(meshID, GmfHexahedraQ2);
252 for ( int i = 1; i <= nbHex; ++i )
254 GmfGetLin(meshID, GmfHexahedraQ2, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5],
255 &iN[6], &iN[7], &iN[8],&iN[9],&iN[10],&iN[11],&iN[12],&iN[13],&iN[14],
256 &iN[15],&iN[16],&iN[17],&iN[18],&iN[19],&iN[20],&iN[21],&iN[22],&iN[23],
257 &iN[24],&iN[25],&iN[26], &ref);
258 if ( !myMesh->AddVolumeWithID( 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],
262 status = storeBadNodeIds( "GmfHexahedraQ2",i, 27,
263 iN[0],iN[3],iN[2],iN[1],iN[4], iN[7],iN[6],iN[5],iN[11],iN[10],
264 iN[9],iN[8],iN[12],iN[15],iN[14], iN[13],iN[19],iN[18],iN[17],
265 iN[16],iN[20],iN[24],iN[23],iN[22],iN[21], iN[25],iN[26]);
269 const int prismIDShift = myMesh->GetMeshInfo().NbElements();
270 if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
272 GmfGotoKwd(meshID, GmfPrisms);
273 for ( int i = 1; i <= nbPrism; ++i )
275 GmfGetLin(meshID, GmfPrisms,
276 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
277 if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
278 status = storeBadNodeIds( "GmfPrisms",i,
279 6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
283 // Read required entities into groups
285 if ( _makeRequiredGroups )
287 // get ids of existing groups
288 std::set< int > groupIDs;
289 const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
290 std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
291 for ( ; grIter != groups.end(); ++grIter )
292 groupIDs.insert( (*grIter)->GetID() );
293 if ( groupIDs.empty() ) groupIDs.insert( 0 );
295 const int kes[4][3] = { { GmfRequiredVertices, SMDSAbs_Node, nodeIDShift },
296 { GmfRequiredEdges, SMDSAbs_Edge, edgeIDShift },
297 { GmfRequiredTriangles, SMDSAbs_Face, triaIDShift },
298 { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }};
299 const char* names[4] = { "_required_Vertices" ,
301 "_required_Triangles" ,
302 "_required_Quadrilaterals" };
303 for ( int i = 0; i < 4; ++i )
305 int gmfKwd = kes[i][0];
306 SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
307 int shift = kes[i][2];
308 if ( int nb = GmfStatKwd(meshID, gmfKwd))
310 const int newID = *groupIDs.rbegin() + 1;
311 groupIDs.insert( newID );
312 SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
313 group->SetStoreName( names[i] );
314 myMesh->AddGroup( group );
316 GmfGotoKwd(meshID, gmfKwd);
317 for ( int i = 0; i < nb; ++i )
319 GmfGetLin(meshID, gmfKwd, &iN[0] );
320 group->Add( shift + iN[0] );
329 //================================================================================
331 * \brief Store a message about invalid IDs of nodes
333 //================================================================================
335 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
337 if ( myStatus != DRS_OK )
343 va_start(VarArg, nb);
345 for ( int i = 0; i < nb; ++i )
347 int id = va_arg(VarArg, int );
348 if ( !myMesh->FindNode( id ))
356 const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
357 if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
358 else nbStr = SMESH_Comment(elemNb) << "-th ";
361 ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,