Salome HOME
332519b2e1a3989eda0d508082b921eb565d39f4
[modules/smesh.git] / src / DriverGMF / DriverGMF_Read.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : DriverGMF_Read.cxx
23 // Created   : Mon Sep 17 17:03:02 2012
24 // Author    : Edward AGAPOV (eap)
25
26 #include "DriverGMF_Read.hxx"
27 #include "DriverGMF_Write.hxx"
28
29 #include "SMESHDS_Group.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Comment.hxx"
32
33 extern "C"
34 {
35 #include "libmesh5.h"
36 }
37
38 #include <stdarg.h>
39
40 // --------------------------------------------------------------------------------
41 // Closing GMF mesh at destruction
42 DriverGMF_MeshCloser::~DriverGMF_MeshCloser()
43 {
44   if ( _gmfMeshID )
45     GmfCloseMesh( _gmfMeshID );
46 }
47 // --------------------------------------------------------------------------------
48 DriverGMF_Read::DriverGMF_Read():
49   Driver_SMESHDS_Mesh()
50 {
51 }
52 // --------------------------------------------------------------------------------
53 DriverGMF_Read::~DriverGMF_Read()
54 {
55 }
56
57 //================================================================================
58 /*!
59  * \brief Read a GMF file
60  */
61 //================================================================================
62
63 Driver_Mesh::Status DriverGMF_Read::Perform()
64 {
65   Status status = DRS_OK;
66
67   int dim, version;
68
69   // open the file
70   int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
71   if ( !meshID )
72     return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
73
74   DriverGMF_MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
75
76   // Read nodes
77
78   int nbNodes = GmfStatKwd(meshID, GmfVertices);
79   if ( nbNodes < 1 )
80     return addMessage( "No nodes in the mesh", /*fatal=*/true );
81
82   GmfGotoKwd(meshID, GmfVertices);
83
84   int ref;
85
86   const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
87   if ( version != GmfFloat )
88   {
89     double x, y, z;
90     for ( int i = 1; i <= nbNodes; ++i )
91     {
92       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
93       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
94     }
95   }
96   else
97   {
98     float x, y, z;
99     for ( int i = 1; i <= nbNodes; ++i )
100     {
101       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
102       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
103     }
104   }
105
106   // Read elements
107
108   int iN[28];
109
110   /* Read edges */
111   const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
112   if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
113   {
114     GmfGotoKwd(meshID, GmfEdges);
115     for ( int i = 1; i <= nbEdges; ++i )
116     {
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] );
120     }
121   }
122   /* Read quadratic edges */
123   const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
124   if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
125   {
126     GmfGotoKwd(meshID, GmfEdgesP2);
127     for ( int i = 1; i <= nbEdges; ++i )
128     {
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] );
132     }
133   }
134   /* Read triangles */
135   const int triaIDShift = myMesh->GetMeshInfo().NbElements();
136   if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
137   {
138     GmfGotoKwd(meshID, GmfTriangles);
139     for ( int i = 1; i <= nbTria; ++i )
140     {
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] );
144     }
145   }
146   /* Read quadratic triangles */
147   const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
148   if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
149   {
150     GmfGotoKwd(meshID, GmfTrianglesP2);
151     for ( int i = 1; i <= nbTria; ++i )
152     {
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],
156                                    tria2IDShift + i ))
157         status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
158     }
159   }
160   /* Read quadrangles */
161   const int quadIDShift = myMesh->GetMeshInfo().NbElements();
162   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
163   {
164     GmfGotoKwd(meshID, GmfQuadrilaterals);
165     for ( int i = 1; i <= nbQuad; ++i )
166     {
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] );
170     }
171   }
172   /* Read bi-quadratic quadrangles */
173   const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
174   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
175   {
176     GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
177     for ( int i = 1; i <= nbQuad; ++i )
178     {
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],
182                                    quad2IDShift + i ))
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] );
185     }
186   }
187   /* Read terahedra */
188   const int tetIDShift = myMesh->GetMeshInfo().NbElements();
189   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
190   {
191     GmfGotoKwd(meshID, GmfTetrahedra);
192     for ( int i = 1; i <= nbTet; ++i )
193     {
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] );
197     }
198   }
199   /* Read quadratic terahedra */
200   const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
201   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
202   {
203     GmfGotoKwd(meshID, GmfTetrahedraP2);
204     for ( int i = 1; i <= nbTet; ++i )
205     {
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],
209                                      iN[6],iN[5],iN[4],
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] );
213     }
214   }
215   /* Read pyramids */
216   const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
217   if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
218   {
219     GmfGotoKwd(meshID, GmfPyramids);
220     for ( int i = 1; i <= nbPyr; ++i )
221     {
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] );
225     }
226   }
227   /* Read hexahedra */
228   const int hexIDShift = myMesh->GetMeshInfo().NbElements();
229   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
230   {
231     GmfGotoKwd(meshID, GmfHexahedra);
232     for ( int i = 1; i <= nbHex; ++i )
233     {
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],
237                                      hexIDShift + i))
238         status = storeBadNodeIds( "GmfHexahedra",i,
239                                   8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
240     }
241   }
242   /* Read tri-quadratic hexahedra */
243   const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
244   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
245   {
246     GmfGotoKwd(meshID, GmfHexahedraQ2);
247     for ( int i = 1; i <= nbHex; ++i )
248     {
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],
256                                      hex2IDShift + i ))
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]);
261     }
262   }
263   /* Read prism */
264   const int prismIDShift = myMesh->GetMeshInfo().NbElements();
265   if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
266   {
267     GmfGotoKwd(meshID, GmfPrisms);
268     for ( int i = 1; i <= nbPrism; ++i )
269     {
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] );
275     }
276   }
277
278   // Read required entities into groups
279
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 );
287
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"      ,
293                            "_required_Edges"         ,
294                            "_required_Triangles"     ,
295                            "_required_Quadrilaterals" };
296   for ( int i = 0; i < 4; ++i )
297   {
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))
302     {
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 );
308
309       GmfGotoKwd(meshID, gmfKwd);
310       for ( int i = 0; i < nb; ++i )
311       {
312         GmfGetLin(meshID, gmfKwd, &iN[0] );
313         group->Add( shift + iN[0] );
314       }
315     }
316   }
317
318   return status;
319 }
320
321 //================================================================================
322 /*!
323  * \brief Store a message about invalid IDs of nodes
324  */
325 //================================================================================
326
327 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
328 {
329   if ( myStatus != DRS_OK )
330     return myStatus;
331
332   SMESH_Comment msg;
333
334   va_list VarArg;
335   va_start(VarArg, nb);
336
337   for ( int i = 0; i < nb; ++i )
338   {
339     int id = va_arg(VarArg, int );
340     if ( !myMesh->FindNode( id ))
341       msg << " " << id;
342   }
343   va_end(VarArg);
344
345   if ( !msg.empty() )
346   {
347     std::string nbStr;
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 ";
351
352     return addMessage
353       ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,
354         /*fatal=*/false );
355   }
356   return DRS_OK;
357 }