Salome HOME
0021979]: [ CEA 709 ] Import/export GMF should not take into account the locale to...
[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 #include <Basics_Utils.hxx>
34
35 extern "C"
36 {
37 #include "libmesh5.h"
38 }
39
40 #include <stdarg.h>
41
42 // --------------------------------------------------------------------------------
43 // Closing GMF mesh at destruction
44 DriverGMF_MeshCloser::~DriverGMF_MeshCloser()
45 {
46   if ( _gmfMeshID )
47     GmfCloseMesh( _gmfMeshID );
48 }
49 // --------------------------------------------------------------------------------
50 DriverGMF_Read::DriverGMF_Read():
51   Driver_SMESHDS_Mesh(),
52   _makeRequiredGroups( true )
53 {
54 }
55 // --------------------------------------------------------------------------------
56 DriverGMF_Read::~DriverGMF_Read()
57 {
58 }
59
60 //================================================================================
61 /*!
62  * \brief Read a GMF file
63  */
64 //================================================================================
65
66 Driver_Mesh::Status DriverGMF_Read::Perform()
67 {
68   Kernel_Utils::Localizer loc;
69
70   Status status = DRS_OK;
71
72   int dim, version;
73
74   // open the file
75   int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
76   if ( !meshID )
77     return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
78
79   DriverGMF_MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
80
81   // Read nodes
82
83   int nbNodes = GmfStatKwd(meshID, GmfVertices);
84   if ( nbNodes < 1 )
85     return addMessage( "No nodes in the mesh", /*fatal=*/true );
86
87   GmfGotoKwd(meshID, GmfVertices);
88
89   int ref;
90
91   const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
92   if ( version != GmfFloat )
93   {
94     double x, y, z;
95     for ( int i = 1; i <= nbNodes; ++i )
96     {
97       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
98       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
99     }
100   }
101   else
102   {
103     float x, y, z;
104     for ( int i = 1; i <= nbNodes; ++i )
105     {
106       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
107       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
108     }
109   }
110
111   // Read elements
112
113   int iN[28];
114
115   /* Read edges */
116   const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
117   if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
118   {
119     GmfGotoKwd(meshID, GmfEdges);
120     for ( int i = 1; i <= nbEdges; ++i )
121     {
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] );
125     }
126   }
127   /* Read quadratic edges */
128   const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
129   if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
130   {
131     GmfGotoKwd(meshID, GmfEdgesP2);
132     for ( int i = 1; i <= nbEdges; ++i )
133     {
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] );
137     }
138   }
139   /* Read triangles */
140   const int triaIDShift = myMesh->GetMeshInfo().NbElements();
141   if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
142   {
143     GmfGotoKwd(meshID, GmfTriangles);
144     for ( int i = 1; i <= nbTria; ++i )
145     {
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] );
149     }
150   }
151   /* Read quadratic triangles */
152   const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
153   if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
154   {
155     GmfGotoKwd(meshID, GmfTrianglesP2);
156     for ( int i = 1; i <= nbTria; ++i )
157     {
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],
161                                    tria2IDShift + i ))
162         status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
163     }
164   }
165   /* Read quadrangles */
166   const int quadIDShift = myMesh->GetMeshInfo().NbElements();
167   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
168   {
169     GmfGotoKwd(meshID, GmfQuadrilaterals);
170     for ( int i = 1; i <= nbQuad; ++i )
171     {
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] );
175     }
176   }
177   /* Read bi-quadratic quadrangles */
178   const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
179   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
180   {
181     GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
182     for ( int i = 1; i <= nbQuad; ++i )
183     {
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],
187                                    quad2IDShift + i ))
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] );
190     }
191   }
192   /* Read terahedra */
193   const int tetIDShift = myMesh->GetMeshInfo().NbElements();
194   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
195   {
196     GmfGotoKwd(meshID, GmfTetrahedra);
197     for ( int i = 1; i <= nbTet; ++i )
198     {
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] );
202     }
203   }
204   /* Read quadratic terahedra */
205   const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
206   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
207   {
208     GmfGotoKwd(meshID, GmfTetrahedraP2);
209     for ( int i = 1; i <= nbTet; ++i )
210     {
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],
214                                      iN[6],iN[5],iN[4],
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] );
218     }
219   }
220   /* Read pyramids */
221   const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
222   if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
223   {
224     GmfGotoKwd(meshID, GmfPyramids);
225     for ( int i = 1; i <= nbPyr; ++i )
226     {
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] );
230     }
231   }
232   /* Read hexahedra */
233   const int hexIDShift = myMesh->GetMeshInfo().NbElements();
234   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
235   {
236     GmfGotoKwd(meshID, GmfHexahedra);
237     for ( int i = 1; i <= nbHex; ++i )
238     {
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],
242                                      hexIDShift + i))
243         status = storeBadNodeIds( "GmfHexahedra",i,
244                                   8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
245     }
246   }
247   /* Read tri-quadratic hexahedra */
248   const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
249   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
250   {
251     GmfGotoKwd(meshID, GmfHexahedraQ2);
252     for ( int i = 1; i <= nbHex; ++i )
253     {
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],
261                                      hex2IDShift + i ))
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]);
266     }
267   }
268   /* Read prism */
269   const int prismIDShift = myMesh->GetMeshInfo().NbElements();
270   if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
271   {
272     GmfGotoKwd(meshID, GmfPrisms);
273     for ( int i = 1; i <= nbPrism; ++i )
274     {
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] );
280     }
281   }
282
283   // Read required entities into groups
284
285   if ( _makeRequiredGroups )
286   {
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 );
294
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"      ,
300                              "_required_Edges"         ,
301                              "_required_Triangles"     ,
302                              "_required_Quadrilaterals" };
303     for ( int i = 0; i < 4; ++i )
304     {
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))
309       {
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 );
315
316         GmfGotoKwd(meshID, gmfKwd);
317         for ( int i = 0; i < nb; ++i )
318         {
319           GmfGetLin(meshID, gmfKwd, &iN[0] );
320           group->Add( shift + iN[0] );
321         }
322       }
323     }
324   }
325
326   return status;
327 }
328
329 //================================================================================
330 /*!
331  * \brief Store a message about invalid IDs of nodes
332  */
333 //================================================================================
334
335 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
336 {
337   if ( myStatus != DRS_OK )
338     return myStatus;
339
340   SMESH_Comment msg;
341
342   va_list VarArg;
343   va_start(VarArg, nb);
344
345   for ( int i = 0; i < nb; ++i )
346   {
347     int id = va_arg(VarArg, int );
348     if ( !myMesh->FindNode( id ))
349       msg << " " << id;
350   }
351   va_end(VarArg);
352
353   if ( !msg.empty() )
354   {
355     std::string nbStr;
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 ";
359
360     return addMessage
361       ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,
362         /*fatal=*/false );
363   }
364   return DRS_OK;
365 }