]> SALOME platform Git repositories - modules/smesh.git/blob - src/DriverGMF/DriverGMF_Write.cxx
Salome HOME
021980: [CEA 708] Import/export GMF does work only if the file extension is ".mesh"
[modules/smesh.git] / src / DriverGMF / DriverGMF_Write.cxx
1 // Copyright (C) 2007-2013  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, write 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_Write.cxx
23 // Created   : Mon Sep 17 17:03:02 2012
24 // Author    : Edward AGAPOV (eap)
25
26 #include "DriverGMF_Write.hxx"
27 #include "DriverGMF.hxx"
28
29 #include "SMESHDS_GroupBase.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 <vector>
41
42 #define BEGIN_ELEM_WRITE( SMDSEntity, GmfKwd, elem )                    \
43   elemIt = elementIterator( SMDSEntity );                               \
44   if ( elemIt->more() )                                                 \
45   {                                                                     \
46   GmfSetKwd(meshID, GmfKwd, myMesh->GetMeshInfo().NbElements( SMDSEntity )); \
47   for ( int gmfID = 1; elemIt->more(); ++gmfID )                        \
48   {                                                                     \
49   const SMDS_MeshElement* elem = elemIt->next();                        \
50   GmfSetLin(meshID, GmfKwd,
51
52 #define BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom, LinType, GmfKwd, elem )   \
53   elemIt = elementIterator( SMDSGeom );                                 \
54   if ( elemIt->more() )                                                 \
55   {                                                                     \
56   int totalNbElems  = myMesh->GetMeshInfo().NbElements( SMDSGeom );     \
57   int nbLinearElems = myMesh->GetMeshInfo().NbElements( LinType );      \
58   if ( totalNbElems - nbLinearElems > 0 )                               \
59   {                                                                     \
60   GmfSetKwd(meshID, GmfKwd, totalNbElems - nbLinearElems);              \
61   for ( int gmfID = 1; elemIt->more(); ++gmfID )                        \
62   {                                                                     \
63   const SMDS_MeshElement* elem = elemIt->next();                        \
64   if ( elem->IsQuadratic() ) {                                          \
65   GmfSetLin(meshID, GmfKwd, gmfID, elem->NbNodes() - elem->NbCornerNodes(),
66
67 #define END_ELEM_WRITE( elem )                  \
68   elem->getshapeId() );                         \
69   }}
70
71 #define END_ELEM_WRITE_ADD_TO_MAP( elem, e2id )         \
72   elem->getshapeId() );                                 \
73   e2id.insert( e2id.end(), make_pair( elem, gmfID ));   \
74   }}
75
76 #define END_EXTRA_VERTICES_WRITE()           \
77   );                                         \
78   }}}}
79
80
81 DriverGMF_Write::DriverGMF_Write():
82   Driver_SMESHDS_Mesh(), _exportRequiredGroups( true )
83 {
84 }
85 DriverGMF_Write::~DriverGMF_Write()
86 {
87 }
88
89 //================================================================================
90 /*!
91  * \brief Reads a GMF file
92  */
93 //================================================================================
94
95 Driver_Mesh::Status DriverGMF_Write::Perform()
96 {
97   Kernel_Utils::Localizer loc;
98
99   const int dim = 3, version = sizeof(long) == 4 ? 2 : 3;
100
101   int meshID = GmfOpenMesh( myFile.c_str(), GmfWrite, version, dim );
102   if ( !meshID )
103   {
104     if ( DriverGMF::isExtensionCorrect( myFile ))
105       return addMessage( SMESH_Comment("Can't open for writing ") << myFile, /*fatal=*/true );
106     else
107       return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile, /*fatal=*/true );
108   }
109
110   DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
111
112   // nodes
113   std::map< const SMDS_MeshNode* , int > node2IdMap;
114   int iN = 0, nbNodes = myMesh->NbNodes();
115   GmfSetKwd( meshID, GmfVertices, nbNodes );
116   double xyz[3];
117   SMDS_NodeIteratorPtr nodeIt = myMesh->nodesIterator();
118   while ( nodeIt->more() )
119   {
120     const SMDS_MeshNode* n = nodeIt->next();
121     n->GetXYZ( xyz );
122     GmfSetLin( meshID, GmfVertices, xyz[0], xyz[1], xyz[2], n->getshapeId() );
123     node2IdMap.insert( node2IdMap.end(), make_pair( n, ++iN ));
124   }
125   if ( iN != nbNodes )
126     return addMessage("Wrong nb of nodes returned by nodesIterator", /*fatal=*/true);
127
128
129   SMDS_ElemIteratorPtr elemIt;
130   typedef std::map< const SMDS_MeshElement*, size_t, TIDCompare > TElem2IDMap;
131
132   // edges
133   TElem2IDMap edge2IDMap;
134   BEGIN_ELEM_WRITE( SMDSGeom_EDGE, GmfEdges, edge )
135     node2IdMap[ edge->GetNode( 0 )],
136     node2IdMap[ edge->GetNode( 1 )],
137     END_ELEM_WRITE_ADD_TO_MAP( edge, edge2IDMap );
138
139   // nodes of quadratic edges
140   BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_EDGE, SMDSEntity_Edge,
141                               GmfExtraVerticesAtEdges, edge )
142     node2IdMap[ edge->GetNode( 2 )]
143     END_EXTRA_VERTICES_WRITE();
144
145   // triangles
146   TElem2IDMap tria2IDMap;
147   BEGIN_ELEM_WRITE( SMDSGeom_TRIANGLE, GmfTriangles, tria )
148     node2IdMap[ tria->GetNode( 0 )],
149     node2IdMap[ tria->GetNode( 1 )],
150     node2IdMap[ tria->GetNode( 2 )],
151     END_ELEM_WRITE_ADD_TO_MAP( tria, tria2IDMap );
152
153   // nodes of quadratic triangles
154   BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_TRIANGLE, SMDSEntity_Triangle,
155                               GmfExtraVerticesAtTriangles, tria )
156     node2IdMap[ tria->GetNode( 3 )],
157     node2IdMap[ tria->GetNode( 4 )],
158     node2IdMap[ tria->GetNode( 5 )],
159     node2IdMap[ tria->GetNodeWrap( 6 )] // for TRIA7
160     END_EXTRA_VERTICES_WRITE();
161
162   // quadrangles
163   TElem2IDMap quad2IDMap;
164   BEGIN_ELEM_WRITE( SMDSGeom_QUADRANGLE, GmfQuadrilaterals, quad )
165     node2IdMap[ quad->GetNode( 0 )],
166     node2IdMap[ quad->GetNode( 1 )],
167     node2IdMap[ quad->GetNode( 2 )],
168     node2IdMap[ quad->GetNode( 3 )],
169     END_ELEM_WRITE_ADD_TO_MAP( quad, quad2IDMap );
170
171   // nodes of quadratic quadrangles
172   BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_QUADRANGLE, SMDSEntity_Quadrangle,
173                               GmfExtraVerticesAtQuadrilaterals, quad )
174     node2IdMap[ quad->GetNode( 4 )],
175     node2IdMap[ quad->GetNode( 5 )],
176     node2IdMap[ quad->GetNode( 6 )],
177     node2IdMap[ quad->GetNode( 7 )],
178     node2IdMap[ quad->GetNodeWrap( 8 )] // for QUAD9
179     END_EXTRA_VERTICES_WRITE();
180
181   // terahedra
182   BEGIN_ELEM_WRITE( SMDSGeom_TETRA, GmfTetrahedra, tetra )
183     node2IdMap[ tetra->GetNode( 0 )],
184     node2IdMap[ tetra->GetNode( 2 )],
185     node2IdMap[ tetra->GetNode( 1 )],
186     node2IdMap[ tetra->GetNode( 3 )],
187     END_ELEM_WRITE( tetra );
188
189   // nodes of quadratic terahedra
190   BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_TETRA, SMDSEntity_Tetra,
191                               GmfExtraVerticesAtTetrahedra, tetra )
192     node2IdMap[ tetra->GetNode( 6 )],
193     node2IdMap[ tetra->GetNode( 5 )],
194     node2IdMap[ tetra->GetNode( 4 )],
195     node2IdMap[ tetra->GetNode( 7 )],
196     node2IdMap[ tetra->GetNode( 9 )],
197     node2IdMap[ tetra->GetNode( 8 )]
198     //node2IdMap[ tetra->GetNodeWrap( 10 )], // for TETRA11
199     END_EXTRA_VERTICES_WRITE();
200
201   // pyramids
202   BEGIN_ELEM_WRITE( SMDSEntity_Pyramid, GmfPyramids, pyra )
203     node2IdMap[ pyra->GetNode( 0 )],
204     node2IdMap[ pyra->GetNode( 2 )],
205     node2IdMap[ pyra->GetNode( 1 )],
206     node2IdMap[ pyra->GetNode( 3 )],
207     node2IdMap[ pyra->GetNode( 4 )],
208     END_ELEM_WRITE( pyra );
209
210   // hexahedra
211   BEGIN_ELEM_WRITE( SMDSGeom_HEXA, GmfHexahedra, hexa )
212     node2IdMap[ hexa->GetNode( 0 )],
213     node2IdMap[ hexa->GetNode( 3 )],
214     node2IdMap[ hexa->GetNode( 2 )],
215     node2IdMap[ hexa->GetNode( 1 )],
216     node2IdMap[ hexa->GetNode( 4 )],
217     node2IdMap[ hexa->GetNode( 7 )],
218     node2IdMap[ hexa->GetNode( 6 )],
219     node2IdMap[ hexa->GetNode( 5 )],
220     END_ELEM_WRITE( hexa );
221
222   // nodes of quadratic hexahedra
223   BEGIN_EXTRA_VERTICES_WRITE( SMDSGeom_HEXA, SMDSEntity_Hexa,
224                               GmfExtraVerticesAtHexahedra, hexa )
225     node2IdMap[ hexa->GetNode( 11 )], // HEXA20
226     node2IdMap[ hexa->GetNode( 10 )],
227     node2IdMap[ hexa->GetNode(  9 )],
228     node2IdMap[ hexa->GetNode(  8 )],
229     node2IdMap[ hexa->GetNode( 15 )],
230     node2IdMap[ hexa->GetNode( 14 )],
231     node2IdMap[ hexa->GetNode( 13 )],
232     node2IdMap[ hexa->GetNode( 12 )],
233     node2IdMap[ hexa->GetNode( 16 )],
234     node2IdMap[ hexa->GetNode( 19 )],
235     node2IdMap[ hexa->GetNodeWrap( 18 )], // + HEXA27
236     node2IdMap[ hexa->GetNodeWrap( 17 )],
237     node2IdMap[ hexa->GetNodeWrap( 20 )],
238     node2IdMap[ hexa->GetNodeWrap( 24 )],
239     node2IdMap[ hexa->GetNodeWrap( 23 )],
240     node2IdMap[ hexa->GetNodeWrap( 22 )],
241     node2IdMap[ hexa->GetNodeWrap( 21 )],
242     node2IdMap[ hexa->GetNodeWrap( 25 )],
243     node2IdMap[ hexa->GetNodeWrap( 26 )]
244     END_EXTRA_VERTICES_WRITE();
245
246   // prism
247   BEGIN_ELEM_WRITE( SMDSEntity_Penta, GmfPrisms, prism )
248     node2IdMap[ prism->GetNode( 0 )],
249     node2IdMap[ prism->GetNode( 2 )],
250     node2IdMap[ prism->GetNode( 1 )],
251     node2IdMap[ prism->GetNode( 3 )],
252     node2IdMap[ prism->GetNode( 5 )],
253     node2IdMap[ prism->GetNode( 4 )],
254     END_ELEM_WRITE( prism );
255
256
257   if ( _exportRequiredGroups )
258   {
259     // required entities
260     SMESH_Comment badGroups;
261     const std::set<SMESHDS_GroupBase*>&      groupSet = myMesh->GetGroups();
262     std::set<SMESHDS_GroupBase*>::const_iterator grIt = groupSet.begin();
263     for ( ; grIt != groupSet.end(); ++grIt )
264     {
265       const SMESHDS_GroupBase* group = *grIt;
266       std::string          groupName = group->GetStoreName();
267       std::string::size_type     pos = groupName.find( "_required_" );
268       if ( pos == std::string::npos ) continue;
269
270       int                    gmfKwd;
271       SMDSAbs_EntityType smdsEntity;
272       std::string entity = groupName.substr( pos + strlen("_required_"));
273       if      ( entity == "Vertices" ) {
274         gmfKwd   = GmfRequiredVertices;
275         smdsEntity = SMDSEntity_Node;
276       }
277       else if ( entity == "Edges" ) {
278         gmfKwd   = GmfRequiredEdges;
279         smdsEntity = SMDSEntity_Edge;
280       }
281       else if ( entity == "Triangles" ) {
282         gmfKwd   = GmfRequiredTriangles;
283         smdsEntity = SMDSEntity_Triangle;
284       }
285       else if ( entity == "Quadrilaterals" ) {
286         gmfKwd   = GmfRequiredQuadrilaterals;
287         smdsEntity = SMDSEntity_Quadrangle;
288       }
289       else {
290         addMessage( SMESH_Comment("Invalig gmf entity name: ") << entity, /*fatal=*/false );
291         continue;
292       }
293
294       // check elem type in the group
295       int nbOkElems = 0;
296       SMDS_ElemIteratorPtr elemIt = group->GetElements();
297       while ( elemIt->more() )
298         nbOkElems += ( elemIt->next()->GetEntityType() == smdsEntity );
299
300       if ( nbOkElems != group->Extent() && nbOkElems == 0 )
301       {
302         badGroups << " " << groupName;
303         continue;
304       }
305
306       // choose a TElem2IDMap
307       TElem2IDMap* elem2IDMap = 0;
308       if ( smdsEntity == SMDSEntity_Quadrangle && nbOkElems != myMesh->NbFaces() )
309         elem2IDMap = & quad2IDMap;
310       else if ( smdsEntity == SMDSEntity_Triangle && nbOkElems != myMesh->NbFaces() )
311         elem2IDMap = & tria2IDMap;
312       else if ( smdsEntity == SMDSEntity_Edge && nbOkElems != myMesh->NbEdges() )
313         elem2IDMap = & edge2IDMap;
314
315       // write the group
316       GmfSetKwd( meshID, gmfKwd, nbOkElems );
317       elemIt = group->GetElements();
318       if ( elem2IDMap )
319         for ( ; elemIt->more(); )
320         {
321           const SMDS_MeshElement* elem = elemIt->next();
322           if ( elem->GetEntityType() == smdsEntity )
323             GmfSetLin( meshID, gmfKwd, (*elem2IDMap)[ elem ] );
324         }
325       else
326         for ( int gmfID = 1; elemIt->more(); ++gmfID)
327         {
328           const SMDS_MeshElement* elem = elemIt->next();
329           if ( elem->GetEntityType() == smdsEntity )
330             GmfSetLin( meshID, gmfKwd, gmfID );
331         }
332
333     } // loop on groups
334
335     if ( !badGroups.empty() )
336       addMessage( SMESH_Comment("Groups of elements of inappropriate geometry:")
337                   << badGroups, /*fatal=*/false );
338   }
339
340   return DRS_OK;
341 }
342
343 //================================================================================
344 /*!
345  * \brief Returns an iterator on elements of a certain type
346  */
347 //================================================================================
348
349 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_ElementType type)
350 {
351   return myMesh->elementsIterator(type);
352 }
353 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_EntityType type) 
354 {
355   return myMesh->elementEntityIterator(type);
356 }
357 SMDS_ElemIteratorPtr DriverGMF_Write::elementIterator(SMDSAbs_GeometryType type)
358 {
359   return myMesh->elementGeomIterator(type);
360 }