Salome HOME
948b111b9332df38846ffd2e93e7243dab3ef3d5
[modules/smesh.git] / src / DriverCGNS / DriverCGNS_Write.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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      : DriverCGNS_Write.cxx
23 // Created   : Fri Aug  5 17:43:54 2011
24 // Author    : Edward AGAPOV (eap)
25
26 #include "DriverCGNS_Write.hxx"
27
28 #include "SMDS_IteratorOnIterators.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_VolumeTool.hxx"
31 #include "SMESHDS_GroupBase.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESH_Comment.hxx"
34
35 #include <limits>
36 #include <cgnslib.h>
37
38 #if CGNS_VERSION < 3100
39 # define cgsize_t int
40 #endif
41
42 using namespace std;
43
44 namespace
45 {
46   //================================================================================
47   /*!
48    * \brief Return interlace and type of CGNS element for the given SMDSAbs_EntityType
49    */
50   //================================================================================
51
52   const int* getInterlaceAndType( const SMDSAbs_EntityType      smType,
53                                   CGNS_ENUMT( ElementType_t ) & cgType )
54   {
55     static vector< const int* >                 interlaces;
56     static vector< CGNS_ENUMT( ElementType_t )> cgTypes; 
57     if ( interlaces.empty() )
58     {
59       interlaces.resize( SMDSEntity_Last, 0 );
60       cgTypes.resize( SMDSEntity_Last, CGNS_ENUMV( ElementTypeNull ));
61       {
62         static int ids[] = {0};
63         interlaces[SMDSEntity_0D] = ids;
64         cgTypes   [SMDSEntity_0D] = CGNS_ENUMV( NODE );
65       }
66       {
67         static int ids[] = { 0, 1 };
68         interlaces[SMDSEntity_Edge] = ids;
69         cgTypes   [SMDSEntity_Edge] = CGNS_ENUMV( BAR_2 );
70       }
71       {
72         static int ids[] = { 0, 1, 2 };
73         interlaces[SMDSEntity_Quad_Edge] = ids;
74         cgTypes   [SMDSEntity_Quad_Edge] = CGNS_ENUMV( BAR_3 );
75       }
76       {
77         static int ids[] = { 0, 2, 1 };
78         interlaces[SMDSEntity_Triangle] = ids;
79         cgTypes   [SMDSEntity_Triangle] = CGNS_ENUMV( TRI_3 );
80       }
81       {
82         static int ids[] = { 0, 2, 1, 5, 4, 3 };
83         interlaces[SMDSEntity_Quad_Triangle] = ids;
84         cgTypes   [SMDSEntity_Quad_Triangle] = CGNS_ENUMV( TRI_6 );
85         interlaces[SMDSEntity_BiQuad_Triangle] = ids;
86         cgTypes   [SMDSEntity_BiQuad_Triangle] = CGNS_ENUMV( TRI_6 );
87       }
88       {
89         static int ids[] = { 0, 3, 2, 1 };
90         interlaces[SMDSEntity_Quadrangle] = ids;
91         cgTypes   [SMDSEntity_Quadrangle] = CGNS_ENUMV( QUAD_4 );
92       }
93       {
94         static int ids[] = { 0,3,2,1,7,6,5,4 };
95         interlaces[SMDSEntity_Quad_Quadrangle] = ids;
96         cgTypes   [SMDSEntity_Quad_Quadrangle] = CGNS_ENUMV( QUAD_8 );
97       }
98       {
99         static int ids[] = { 0,3,2,1,7,6,5,4,8 };
100         interlaces[SMDSEntity_BiQuad_Quadrangle] = ids;
101         cgTypes   [SMDSEntity_BiQuad_Quadrangle] = CGNS_ENUMV( QUAD_9 );
102       }
103       {
104         static int ids[] = { 0, 2, 1, 3 };
105         interlaces[SMDSEntity_Tetra] = ids;
106         cgTypes   [SMDSEntity_Tetra] = CGNS_ENUMV( TETRA_4 );
107       }
108       {
109         static int ids[] = { 0,2,1,3,6,5,4,7,9,8 };
110         interlaces[SMDSEntity_Quad_Tetra] = ids;
111         cgTypes   [SMDSEntity_Quad_Tetra] = CGNS_ENUMV( TETRA_10 );
112       }
113       {
114         static int ids[] = { 0,3,2,1,4 };
115         interlaces[SMDSEntity_Pyramid] = ids;
116         cgTypes   [SMDSEntity_Pyramid] = CGNS_ENUMV( PYRA_5 );
117       }
118       {
119         static int ids[] = { 0,3,2,1,4,8,7,6,5,9,12,11,10 };
120         interlaces[SMDSEntity_Quad_Pyramid] = ids;
121         cgTypes   [SMDSEntity_Quad_Pyramid] = CGNS_ENUMV( PYRA_13 );
122       }
123       {
124         static int ids[] = { 0,2,1,3,5,4 };
125         interlaces[SMDSEntity_Penta] = ids;
126         cgTypes   [SMDSEntity_Penta] = CGNS_ENUMV( PENTA_6 );
127       }
128       {
129         static int ids[] = { 0,2,1,3,5,4,8,7,6,9,11,10,14,13,12 };
130         interlaces[SMDSEntity_Quad_Penta] = ids;
131         cgTypes   [SMDSEntity_Quad_Penta] = CGNS_ENUMV( PENTA_15 );
132       }
133       {
134         static int ids[] = { 0,3,2,1,4,7,6,5 };
135         interlaces[SMDSEntity_Hexa] = ids;
136         cgTypes   [SMDSEntity_Hexa] = CGNS_ENUMV( HEXA_8 );
137       }
138       {
139         static int ids[] = { 0,3,2,1,4,7,6,5,11,10,9,8,12,15,14,13,19,18,17,16 };
140         interlaces[SMDSEntity_Quad_Hexa] = ids;
141         cgTypes   [SMDSEntity_Quad_Hexa] = CGNS_ENUMV( HEXA_20 );
142       }
143       {
144         static int ids[] = { 0,3,2,1,4,7,6,5,11,10,9,8,12,15,14,13,19,18,17,16,
145                              20, 24,23,22,21, 25, 26};
146         interlaces[SMDSEntity_TriQuad_Hexa] = ids;
147         cgTypes   [SMDSEntity_TriQuad_Hexa] = CGNS_ENUMV( HEXA_27 );
148       }
149       {
150         cgTypes[SMDSEntity_Polygon]         = CGNS_ENUMV( NGON_n );
151         cgTypes[SMDSEntity_Quad_Polygon]    = CGNS_ENUMV( NGON_n );
152         cgTypes[SMDSEntity_Polyhedra]       = CGNS_ENUMV( NFACE_n );
153         cgTypes[SMDSEntity_Hexagonal_Prism] = CGNS_ENUMV( NFACE_n );
154       }
155     }
156     cgType  = cgTypes[ smType ];
157     return interlaces[ smType ];
158   }
159
160   //================================================================================
161   /*!
162    * \brief Cut off type of boundary condition from the group name
163    */
164   //================================================================================
165
166   CGNS_ENUMT( BCType_t ) getBCType( string& groupName )
167   {
168     CGNS_ENUMT( BCType_t ) bcType = CGNS_ENUMV( BCGeneral ); // default type
169
170     // boundary condition type starts from "BC"
171     size_t bcBeg = groupName.find("BC");
172     if ( bcBeg != string::npos )
173     {
174       for ( int t = 0; t < NofValidBCTypes; ++t )
175       {
176         CGNS_ENUMT( BCType_t ) type = CGNS_ENUMT( BCType_t )( t );
177         string typeName = cg_BCTypeName( type );
178         if ( typeName == &groupName[0] + bcBeg )
179         {
180           bcType = type;
181           while ( bcBeg > 0 && isspace( bcBeg-1 ))
182             --bcBeg;
183           if ( bcBeg == 0 )
184             groupName = "Group";
185           else
186             groupName = groupName.substr( 0, bcBeg-1 );
187         }
188       }
189     }
190     return bcType;
191   }
192
193   //================================================================================
194   /*!
195    * \brief Sortable face of a polyhedron
196    */
197   struct TPolyhedFace
198   {
199     int _id; // id of NGON_n
200     vector< int > _nodes; // lowest node IDs used for sorting
201
202     TPolyhedFace( const SMDS_MeshNode** nodes, const int nbNodes, int ID):_id(ID)
203     {
204       set< int > ids;
205       for ( int i = 0; i < nbNodes; ++i )
206         ids.insert( nodes[i]->GetID() );
207
208       _nodes.resize( 3 ); // std::min( nbNodes, 4 )); hope 3 nodes is enough
209       set< int >::iterator idIt = ids.begin();
210       for ( size_t j = 0; j < _nodes.size(); ++j, ++idIt )
211         _nodes[j] = *idIt;
212     }
213     bool operator< (const TPolyhedFace& o ) const
214     {
215       return _nodes < o._nodes;
216     }
217   };
218   //================================================================================
219   /*!
220    * \brief Return CGNS id of an element
221    */
222   //================================================================================
223
224   cgsize_t cgnsID( const SMDS_MeshElement*                         elem,
225                    const map< const SMDS_MeshElement*, cgsize_t >& elem2cgID )
226   {
227     map< const SMDS_MeshElement*, cgsize_t >::const_iterator e2id = elem2cgID.find( elem );
228     return ( e2id == elem2cgID.end() ? elem->GetID() : e2id->second );
229   }
230
231 } // namespace
232
233 //================================================================================
234 /*!
235  * \brief Write the mesh into the CGNS file
236  */
237 //================================================================================
238
239 Driver_Mesh::Status DriverCGNS_Write::Perform()
240 {
241   myErrorMessages.clear();
242
243   if ( !myMesh || myMesh->GetMeshInfo().NbElements() < 1 )
244     return addMessage( !myMesh ? "NULL mesh" : "Empty mesh (no elements)", /*fatal = */true );
245
246   // open the file
247   if ( cg_open(myFile.c_str(), CG_MODE_MODIFY, &_fn) != CG_OK &&
248        cg_open(myFile.c_str(), CG_MODE_WRITE,  &_fn) != CG_OK )
249     return addMessage( cg_get_error(), /*fatal = */true );
250
251   // create a Base
252   // --------------
253
254   const int spaceDim = 3;
255   int        meshDim = 1;
256   if ( myMesh->NbFaces()   > 0 ) meshDim = 2;
257   if ( myMesh->NbVolumes() > 0 ) meshDim = 3;
258
259   if ( myMeshName.empty() )
260   {
261     int nbases = 0;
262     if ( cg_nbases( _fn, &nbases) == CG_OK )
263       myMeshName = ( SMESH_Comment("Base_") << nbases+1 );
264     else
265       myMeshName = "Base_0";
266   }
267   int iBase;
268   if ( cg_base_write( _fn, myMeshName.c_str(), meshDim, spaceDim, &iBase ))
269     return addMessage( cg_get_error(), /*fatal = */true );
270
271   // create a Zone
272   // --------------
273
274   int nbCells = myMesh->NbEdges();
275   if ( meshDim == 3 )
276     nbCells = myMesh->NbVolumes();
277   else if ( meshDim == 2 )
278     nbCells = myMesh->NbFaces();
279
280   cgsize_t size[9] = { myMesh->NbNodes(), nbCells, /*NBoundVertex=*/0, 0,0,0,0,0,0 };
281   int iZone;
282   if ( cg_zone_write( _fn, iBase, "SMESH_Mesh", size,
283                       CGNS_ENUMV( Unstructured ), &iZone) != CG_OK )
284     return addMessage( cg_get_error(), /*fatal = */true );
285
286   // Map to store only elements whose an SMDS ID differs from a CGNS one
287   typedef map< const SMDS_MeshElement*, cgsize_t > TElem2cgIDMap;
288   vector< TElem2cgIDMap > elem2cgIDByEntity( SMDSEntity_Last );
289   TElem2cgIDMap::iterator elem2cgIDIter;
290
291   TElem2cgIDMap & n2cgID = elem2cgIDByEntity[ SMDSEntity_Node ];
292
293   // Write nodes
294   // ------------
295   {
296     vector< double > coords( myMesh->NbNodes() );
297     int iC;
298     // X
299     SMDS_NodeIteratorPtr nIt = myMesh->nodesIterator( /*idInceasingOrder=*/true );
300     for ( int i = 0; nIt->more(); ++i ) coords[i] = nIt->next()->X();
301     if ( cg_coord_write( _fn, iBase, iZone, CGNS_ENUMV(RealDouble),
302                           "CoordinateX", &coords[0], &iC) != CG_OK )
303       return addMessage( cg_get_error(), /*fatal = */true );
304     // Y
305     nIt = myMesh->nodesIterator( /*idInceasingOrder=*/true );
306     for ( int i = 0; nIt->more(); ++i ) coords[i] = nIt->next()->Y();
307     if ( cg_coord_write( _fn, iBase, iZone, CGNS_ENUMV(RealDouble),
308                           "CoordinateY", &coords[0], &iC) != CG_OK )
309       return addMessage( cg_get_error(), /*fatal = */true );
310     // Z
311     nIt = myMesh->nodesIterator( /*idInceasingOrder=*/true );
312     for ( int i = 0; nIt->more(); ++i ) coords[i] = nIt->next()->Z();
313     if ( cg_coord_write( _fn, iBase, iZone, CGNS_ENUMV(RealDouble),
314                           "CoordinateZ", &coords[0], &iC) != CG_OK )
315       return addMessage( cg_get_error(), /*fatal = */true );
316
317     // store CGNS ids of nodes
318     nIt = myMesh->nodesIterator( /*idInceasingOrder=*/true );
319     for ( int i = 0; nIt->more(); ++i )
320     {
321       const SMDS_MeshElement* n = nIt->next();
322       if ( n->GetID() != i+1 )
323         n2cgID.insert( n2cgID.end(), make_pair( n, i+1 ));
324     }
325   }
326   // Write elements
327   // ---------------
328   
329   cgsize_t cgID = 1, startID;
330
331   // write into a section all successive elements of one geom type
332   int iSec;
333   vector< cgsize_t > elemData;
334   SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator();
335   vector< SMDS_ElemIteratorPtr > elemItVec;
336   if ( _elementsByType )
337   {
338     // create an iterator returning all elements by type
339     for ( int type = SMDSEntity_Node + 1; type < SMDSEntity_Last; ++type )
340     {
341       if ( type == SMDSEntity_Ball )
342         continue; // not supported
343       elemIt = myMesh->elementEntityIterator( SMDSAbs_EntityType( type ));
344       if ( elemIt->more() )
345         elemItVec.push_back( elemIt );
346     }
347     typedef SMDS_IteratorOnIterators< const SMDS_MeshElement*,
348                                       vector< SMDS_ElemIteratorPtr > > TVecIterator;
349     elemIt.reset( new TVecIterator( elemItVec ));
350   }
351
352   const SMDS_MeshElement* elem = elemIt->next();
353   while ( elem )
354   {
355     const SMDSAbs_EntityType elemType = elem->GetEntityType();
356     CGNS_ENUMT( ElementType_t ) cgType;
357     const int* interlace = getInterlaceAndType( elemType, cgType );
358
359     TElem2cgIDMap & elem2cgID = elem2cgIDByEntity[ elemType ];
360
361     elemData.clear();
362     startID = cgID;
363
364     if ( interlace ) // STANDARD elements
365     {
366       int cgnsNbNodes; // get nb nodes by element type, that can be less that elem->NbNodes()
367       cg_npe( cgType, &cgnsNbNodes );
368       do
369       {
370         for ( int i = 0; i < cgnsNbNodes; ++i )
371           elemData.push_back( cgnsID( elem->GetNode( interlace[i] ), n2cgID ));
372         if ( elem->GetID() != cgID )
373           elem2cgID.insert( elem2cgID.end(), make_pair( elem, cgID ));
374         ++cgID;
375         elem = elemIt->more() ? elemIt->next() : 0;
376       }
377       while ( elem && elem->GetEntityType() == elemType );
378     }
379     else if ( elemType == SMDSEntity_Polygon ) // POLYGONS
380       do
381       {
382         elemData.push_back( elem->NbNodes() );
383         for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
384           elemData.push_back( cgnsID( elem->GetNode(i), n2cgID ));
385         if ( elem->GetID() != cgID )
386           elem2cgID.insert( elem2cgID.end(), make_pair( elem, cgID ));
387         ++cgID;
388         elem = elemIt->more() ? elemIt->next() : 0;
389       }
390       while ( elem && elem->GetEntityType() == elemType );
391
392     else if ( elemType == SMDSEntity_Quad_Polygon ) // QUADRATIC POLYGONS
393       do // write as linear NGON_n
394       {
395         elemData.push_back( elem->NbNodes() );
396         interlace = & SMDS_MeshCell::interlacedSmdsOrder( SMDSEntity_Quad_Polygon,
397                                                           elem->NbNodes() )[0];
398         for ( int i = 0, nb = elem->NbNodes(); i < nb; ++i )
399           elemData.push_back( cgnsID( elem->GetNode( interlace[i] ), n2cgID ));
400         if ( elem->GetID() != cgID )
401           elem2cgID.insert( elem2cgID.end(), make_pair( elem, cgID ));
402         ++cgID;
403         elem = elemIt->more() ? elemIt->next() : 0;
404       }
405       while ( elem && elem->GetEntityType() == elemType );
406
407     else if ( elemType == SMDSEntity_Polyhedra ||
408               elemType == SMDSEntity_Hexagonal_Prism) // POLYHEDRA
409     {
410       // to save polyhedrons after all
411       const SMDS_MeshInfo& meshInfo = myMesh->GetMeshInfo();
412       if ( meshInfo.NbPolyhedrons() == meshInfo.NbElements() - cgID + 1 )
413         break; // only polyhedrons remain
414       while ( elem && elem->GetEntityType() == elemType )
415         elem = elemIt->more() ? elemIt->next() : 0;
416       continue;
417     }
418     else // skip NOT SUPPORTED elements
419     {
420       while ( elemIt->more() )
421       {
422         elem = elemIt->next();
423         if ( elem->GetEntityType() != elemType )
424           break;
425       }
426     }
427
428     SMESH_Comment sectionName( cg_ElementTypeName( cgType ));
429     sectionName << " " << startID << " - " << cgID-1;
430
431     if ( cg_section_write(_fn, iBase, iZone, sectionName.c_str(), cgType, startID,
432                           cgID-1, /*nbndry=*/0, &elemData[0], &iSec) != CG_OK )
433       return addMessage( cg_get_error(), /*fatal = */true );
434   }
435
436   // Write polyhedral volumes
437   // -------------------------
438
439   if ( myMesh->GetMeshInfo().NbElements() > cgID-1 ) // polyhedra or hexagonal prisms remain
440   {
441     // the polyhedron (NFACE_n) is described as a set of signed face IDs,
442     // so first we are to write all polygones (NGON_n) bounding polyhedrons
443
444     vector< cgsize_t > faceData;
445     set< TPolyhedFace > faces;
446     set< TPolyhedFace >::iterator faceInSet;
447     vector<const SMDS_MeshNode *> faceNodesVec;
448     int nbPolygones = 0, faceID;
449
450     SMDS_VolumeTool vol;
451
452     elemData.clear();
453
454     int nbPolyhTreated = 0;
455
456     TElem2cgIDMap * elem2cgID = 0;
457     TElem2cgIDMap & n2cgID    = elem2cgIDByEntity[ SMDSEntity_Node ];
458
459     SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator();
460     while ( elemIt->more() )
461     {
462       elem = elemIt->next();
463       SMDSAbs_EntityType type = elem->GetEntityType();
464       if ( type == SMDSEntity_Polyhedra ||
465            type == SMDSEntity_Hexagonal_Prism )
466       {
467         ++nbPolyhTreated;
468         vol.Set( elem );
469         vol.SetExternalNormal();
470         const int nbFaces = vol.NbFaces();
471         elemData.push_back( nbFaces );
472         for ( int iF = 0; iF < nbFaces; ++iF )
473         {
474           const int nbNodes = vol.NbFaceNodes( iF );
475           const SMDS_MeshNode** faceNodes = vol.GetFaceNodes( iF );
476           faceNodesVec.assign( faceNodes, faceNodes + nbNodes );
477           if (( elem = myMesh->FindElement( faceNodesVec, SMDSAbs_Face, /*noMedium=*/false)))
478           {
479             // a face of the polyhedron is present in the mesh
480             faceID = cgnsID( elem, elem2cgIDByEntity[ elem->GetEntityType() ]);
481           }
482           else if ( vol.IsFreeFace( iF ))
483           {
484             // the face is not shared by volumes
485             faceID = cgID++;
486             ++nbPolygones;
487             faceData.push_back( nbNodes );
488             for ( int i = 0; i < nbNodes; ++i )
489               faceData.push_back( cgnsID( faceNodes[i], n2cgID ));
490           }
491           else
492           {
493             TPolyhedFace face( faceNodes, nbNodes, cgID );
494             faceInSet = faces.insert( faces.end(), face );
495             if ( faceInSet->_id == cgID ) // the face encounters for the 1st time
496             {
497               faceID = cgID++;
498               ++nbPolygones;
499               faceData.push_back( nbNodes );
500               for ( int i = 0; i < nbNodes; ++i )
501                 faceData.push_back( cgnsID( faceNodes[i], n2cgID ));
502             }
503             else
504             {
505               // the face encounters for the 2nd time; we hope it won't encounter once more,
506               // for that we can erase it from the set of faces
507               faceID = -faceInSet->_id;
508               faces.erase( faceInSet );
509             }
510           }
511           elemData.push_back( faceID );
512         }
513       }
514     }
515
516     if ( nbPolygones > 0 )
517     {
518       if ( cg_section_write(_fn, iBase, iZone, "Faces of Polyhedrons",
519                              CGNS_ENUMV( NGON_n ), cgID - nbPolygones, cgID-1,
520                              /*nbndry=*/0, &faceData[0], &iSec) != CG_OK )
521         return addMessage( cg_get_error(), /*fatal = */true );
522     }
523     
524     if ( cg_section_write(_fn, iBase, iZone, "Polyhedrons", 
525                              CGNS_ENUMV( NFACE_n ), cgID, cgID+nbPolyhTreated-1,
526                            /*nbndry=*/0, &elemData[0], &iSec) != CG_OK )
527       return addMessage( cg_get_error(), /*fatal = */true );
528
529     if ( !myMesh->GetGroups().empty() )
530     {
531       // store CGNS ids of polyhedrons
532       elem2cgID = &elem2cgIDByEntity[ SMDSEntity_Polyhedra ];
533       elemIt = myMesh->elementsIterator();
534       while ( elemIt->more() )
535       {
536         elem = elemIt->next();
537         if ( elem->GetEntityType() == SMDSEntity_Polyhedra )
538         {
539           if ( elem->GetID() != cgID )
540             elem2cgID->insert( elem2cgID->end(), make_pair( elem, cgID ));
541           ++cgID;
542         }
543       }
544     }
545   } // write polyhedral volumes
546
547
548   // Write groups as boundary conditions
549   // ------------------------------------
550
551   const set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
552   set<SMESHDS_GroupBase*>::const_iterator grpIt = groups.begin();
553   set< string > groupNames; groupNames.insert(""); // to avoid duplicated and empty names
554   for ( ; grpIt != groups.end(); ++grpIt )
555   {
556     const SMESHDS_GroupBase* group = *grpIt;
557
558     // write BC location (default is Vertex)
559     CGNS_ENUMT( GridLocation_t ) location = CGNS_ENUMV( Vertex );
560     if ( group->GetType() != SMDSAbs_Node )
561     {
562 #if CGNS_VERSION > 3130
563       location = CGNS_ENUMV( CellCenter );
564 #else
565       switch ( meshDim ) {
566       case 3:
567         switch ( group->GetType() ) {
568         case SMDSAbs_Volume: location = CGNS_ENUMV( FaceCenter ); break; // !!!
569         case SMDSAbs_Face:   location = CGNS_ENUMV( FaceCenter ); break; // OK
570         case SMDSAbs_Edge:   location = CGNS_ENUMV( EdgeCenter ); break; // OK
571         default:;
572         }
573         break;
574       case 2:
575         switch ( group->GetType() ) {
576         case SMDSAbs_Face: location = CGNS_ENUMV( FaceCenter ); break; // ???
577         case SMDSAbs_Edge: location = CGNS_ENUMV( EdgeCenter ); break; // OK
578         default:;
579         }
580         break;
581       case 1:
582         location = CGNS_ENUMV( EdgeCenter ); break; // ???
583         break;
584       }
585 #endif
586     }
587
588     // try to extract type of boundary condition from the group name
589     string name = group->GetStoreName();
590     CGNS_ENUMT( BCType_t ) bcType = getBCType( name );
591     if ( !groupNames.insert( name ).second ) // assure name uniqueness
592     {
593       int index = 1;
594       string newName;
595       do {
596         newName = SMESH_Comment( name ) << "_" << index++;
597       }
598       while ( !groupNames.insert( newName ).second );
599       name = newName;
600     }
601     // write IDs of elements
602     vector< cgsize_t > pnts;
603     pnts.reserve( group->Extent() );
604     SMDS_ElemIteratorPtr elemIt = group->GetElements();
605     while ( elemIt->more() )
606     {
607       const SMDS_MeshElement* elem = elemIt->next();
608       pnts.push_back( cgnsID( elem, elem2cgIDByEntity[ elem->GetEntityType() ]));
609     }
610     if ( pnts.size() == 0 )
611       continue; // can't store empty group
612     int iBC;
613     if ( cg_boco_write( _fn, iBase, iZone, name.c_str(), bcType,
614                         CGNS_ENUMV( PointList ), pnts.size(), &pnts[0], &iBC) != CG_OK )
615       return addMessage( cg_get_error(), /*fatal = */true);
616
617     // write BC location
618     if ( location != CGNS_ENUMV( Vertex ))
619     {
620       if ( cg_boco_gridlocation_write( _fn, iBase, iZone, iBC, location) != CG_OK )
621         return addMessage( cg_get_error(), /*fatal = */false);
622     }
623   }
624   return DRS_OK;
625 }
626
627 //================================================================================
628 /*!
629  * \brief Constructor
630  */
631 //================================================================================
632
633 DriverCGNS_Write::DriverCGNS_Write(): _fn(0), _elementsByType( false )
634 {
635 }
636
637 //================================================================================
638 /*!
639  * \brief Close the cgns file at destruction
640  */
641 //================================================================================
642
643 DriverCGNS_Write::~DriverCGNS_Write()
644 {
645   if ( _fn > 0 )
646     cg_close( _fn );
647 }