Salome HOME
Merge from V6_main 13/12/2012
[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.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 DriverGMF_Read::DriverGMF_Read():
44   Driver_SMESHDS_Mesh(),
45   _makeRequiredGroups( true )
46 {
47 }
48 // --------------------------------------------------------------------------------
49 DriverGMF_Read::~DriverGMF_Read()
50 {
51 }
52
53 //================================================================================
54 /*!
55  * \brief Read a GMF file
56  */
57 //================================================================================
58
59 Driver_Mesh::Status DriverGMF_Read::Perform()
60 {
61   Kernel_Utils::Localizer loc;
62
63   Status status = DRS_OK;
64
65   int dim, version;
66
67   // open the file
68   int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
69   if ( !meshID )
70   {
71     if ( DriverGMF::isExtensionCorrect( myFile ))
72       return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
73     else
74       return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile, /*fatal=*/true );
75   }
76   DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
77
78   // Read nodes
79
80   int nbNodes = GmfStatKwd(meshID, GmfVertices);
81   if ( nbNodes < 1 )
82     return addMessage( "No nodes in the mesh", /*fatal=*/true );
83
84   GmfGotoKwd(meshID, GmfVertices);
85
86   int ref;
87
88   const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
89   if ( version != GmfFloat )
90   {
91     double x, y, z;
92     for ( int i = 1; i <= nbNodes; ++i )
93     {
94       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
95       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
96     }
97   }
98   else
99   {
100     float x, y, z;
101     for ( int i = 1; i <= nbNodes; ++i )
102     {
103       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
104       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
105     }
106   }
107
108   // Read elements
109
110   int iN[28];
111
112   /* Read edges */
113   const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
114   if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
115   {
116     GmfGotoKwd(meshID, GmfEdges);
117     for ( int i = 1; i <= nbEdges; ++i )
118     {
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] );
122     }
123   }
124   /* Read quadratic edges */
125   const int edge2IDShift = myMesh->GetMeshInfo().NbElements();
126   if ( int nbEdges = GmfStatKwd(meshID, GmfEdgesP2))
127   {
128     GmfGotoKwd(meshID, GmfEdgesP2);
129     for ( int i = 1; i <= nbEdges; ++i )
130     {
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] );
134     }
135   }
136   /* Read triangles */
137   const int triaIDShift = myMesh->GetMeshInfo().NbElements();
138   if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
139   {
140     GmfGotoKwd(meshID, GmfTriangles);
141     for ( int i = 1; i <= nbTria; ++i )
142     {
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] );
146     }
147   }
148   /* Read quadratic triangles */
149   const int tria2IDShift = myMesh->GetMeshInfo().NbElements();
150   if ( int nbTria = GmfStatKwd(meshID, GmfTrianglesP2))
151   {
152     GmfGotoKwd(meshID, GmfTrianglesP2);
153     for ( int i = 1; i <= nbTria; ++i )
154     {
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],
158                                    tria2IDShift + i ))
159         status = storeBadNodeIds( "GmfTrianglesP2",i, 6, iN[0],iN[1],iN[2],iN[3],iN[4],iN[5] );
160     }
161   }
162   /* Read quadrangles */
163   const int quadIDShift = myMesh->GetMeshInfo().NbElements();
164   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
165   {
166     GmfGotoKwd(meshID, GmfQuadrilaterals);
167     for ( int i = 1; i <= nbQuad; ++i )
168     {
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] );
172     }
173   }
174   /* Read bi-quadratic quadrangles */
175   const int quad2IDShift = myMesh->GetMeshInfo().NbElements();
176   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilateralsQ2))
177   {
178     GmfGotoKwd(meshID, GmfQuadrilateralsQ2);
179     for ( int i = 1; i <= nbQuad; ++i )
180     {
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],
184                                    quad2IDShift + i ))
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] );
187     }
188   }
189   /* Read terahedra */
190   const int tetIDShift = myMesh->GetMeshInfo().NbElements();
191   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
192   {
193     GmfGotoKwd(meshID, GmfTetrahedra);
194     for ( int i = 1; i <= nbTet; ++i )
195     {
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] );
199     }
200   }
201   /* Read quadratic terahedra */
202   const int tet2IDShift = myMesh->GetMeshInfo().NbElements();
203   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedraP2))
204   {
205     GmfGotoKwd(meshID, GmfTetrahedraP2);
206     for ( int i = 1; i <= nbTet; ++i )
207     {
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],
211                                      iN[6],iN[5],iN[4],
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] );
215     }
216   }
217   /* Read pyramids */
218   const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
219   if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
220   {
221     GmfGotoKwd(meshID, GmfPyramids);
222     for ( int i = 1; i <= nbPyr; ++i )
223     {
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] );
227     }
228   }
229   /* Read hexahedra */
230   const int hexIDShift = myMesh->GetMeshInfo().NbElements();
231   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
232   {
233     GmfGotoKwd(meshID, GmfHexahedra);
234     for ( int i = 1; i <= nbHex; ++i )
235     {
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],
239                                      hexIDShift + i))
240         status = storeBadNodeIds( "GmfHexahedra",i,
241                                   8, iN[0], iN[1],iN[2], iN[3], iN[4], iN[7], iN[6], iN[5] );
242     }
243   }
244   /* Read tri-quadratic hexahedra */
245   const int hex2IDShift = myMesh->GetMeshInfo().NbElements();
246   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedraQ2))
247   {
248     GmfGotoKwd(meshID, GmfHexahedraQ2);
249     for ( int i = 1; i <= nbHex; ++i )
250     {
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],
258                                      hex2IDShift + i ))
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]);
263     }
264   }
265   /* Read prism */
266   const int prismIDShift = myMesh->GetMeshInfo().NbElements();
267   if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
268   {
269     GmfGotoKwd(meshID, GmfPrisms);
270     for ( int i = 1; i <= nbPrism; ++i )
271     {
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] );
277     }
278   }
279
280   // Read required entities into groups
281
282   if ( _makeRequiredGroups )
283   {
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 );
291
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"      ,
297                              "_required_Edges"         ,
298                              "_required_Triangles"     ,
299                              "_required_Quadrilaterals" };
300     for ( int i = 0; i < 4; ++i )
301     {
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))
306       {
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 );
312
313         GmfGotoKwd(meshID, gmfKwd);
314         for ( int i = 0; i < nb; ++i )
315         {
316           GmfGetLin(meshID, gmfKwd, &iN[0] );
317           group->Add( shift + iN[0] );
318         }
319       }
320     }
321   }
322
323   return status;
324 }
325
326 //================================================================================
327 /*!
328  * \brief Store a message about invalid IDs of nodes
329  */
330 //================================================================================
331
332 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
333 {
334   if ( myStatus != DRS_OK )
335     return myStatus;
336
337   SMESH_Comment msg;
338
339   va_list VarArg;
340   va_start(VarArg, nb);
341
342   for ( int i = 0; i < nb; ++i )
343   {
344     int id = va_arg(VarArg, int );
345     if ( !myMesh->FindNode( id ))
346       msg << " " << id;
347   }
348   va_end(VarArg);
349
350   if ( !msg.empty() )
351   {
352     std::string nbStr;
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 ";
356
357     return addMessage
358       ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,
359         /*fatal=*/false );
360   }
361   return DRS_OK;
362 }