Salome HOME
74475380dcfcdabafc6b776675f6ecf474f05274
[modules/smesh.git] / src / DriverMED / DriverMED_W_SMESHDS_Mesh.cxx
1 //  Copyright (C) 2007-2011  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
23 //  SMESH DriverMED : driver to read and write 'med' files
24 //  File   : DriverMED_W_SMESHDS_Mesh.cxx
25 //  Module : SMESH
26 //
27 #include <sstream>
28
29 #include "DriverMED_W_SMESHDS_Mesh.h"
30 #include "DriverMED_W_SMDS_Mesh.h"
31 #include "DriverMED_Family.h"
32
33 #include "SMESHDS_Mesh.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDS_MeshNode.hxx"
36 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
37
38 #include "utilities.h"
39
40 #include "MED_Utilities.hxx"
41
42 #define _EDF_NODE_IDS_
43 //#define _ELEMENTS_BY_DIM_
44
45 using namespace std;
46 using namespace MED;
47
48
49 DriverMED_W_SMESHDS_Mesh::DriverMED_W_SMESHDS_Mesh():
50   myAllSubMeshes (false),
51   myDoGroupOfNodes (false),
52   myDoGroupOfEdges (false),
53   myDoGroupOfFaces (false),
54   myDoGroupOfVolumes (false)
55 {}
56
57 void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName, 
58                                        MED::EVersion theId)
59 {
60   myMed = CrWrapper(theFileName,theId);
61   Driver_SMESHDS_Mesh::SetFile(theFileName);
62 }
63
64 void DriverMED_W_SMESHDS_Mesh::SetFile(const std::string& theFileName)
65 {
66   return SetFile(theFileName,MED::eV2_2);
67 }
68
69 string DriverMED_W_SMESHDS_Mesh::GetVersionString(const MED::EVersion theVersion, int theNbDigits)
70 {
71   TInt majeur, mineur, release;
72   majeur =  mineur = release = 0;
73   if ( theVersion == eV2_1 )
74     MED::GetVersionRelease<eV2_1>(majeur, mineur, release);
75   else
76     MED::GetVersionRelease<eV2_2>(majeur, mineur, release);
77   ostringstream name;
78   if ( theNbDigits > 0 )
79     name << majeur;
80   if ( theNbDigits > 1 )
81     name << "." << mineur;
82   if ( theNbDigits > 2 )
83     name << "." << release;
84   return name.str();
85 }
86
87 void DriverMED_W_SMESHDS_Mesh::SetMeshName(const std::string& theMeshName)
88 {
89   myMeshName = theMeshName;
90 }
91
92 void DriverMED_W_SMESHDS_Mesh::AddGroup(SMESHDS_GroupBase* theGroup)
93 {
94   myGroups.push_back(theGroup);
95 }
96
97 void DriverMED_W_SMESHDS_Mesh::AddAllSubMeshes()
98 {
99   myAllSubMeshes = true;
100 }
101
102 void DriverMED_W_SMESHDS_Mesh::AddSubMesh(SMESHDS_SubMesh* theSubMesh, int theID)
103 {
104   mySubMeshes[theID] = theSubMesh;
105 }
106
107 void DriverMED_W_SMESHDS_Mesh::AddGroupOfNodes()
108 {
109   myDoGroupOfNodes = true;
110 }
111
112 void DriverMED_W_SMESHDS_Mesh::AddGroupOfEdges()
113 {
114   myDoGroupOfEdges = true;
115 }
116
117 void DriverMED_W_SMESHDS_Mesh::AddGroupOfFaces()
118 {
119   myDoGroupOfFaces = true;
120 }
121
122 void DriverMED_W_SMESHDS_Mesh::AddGroupOfVolumes()
123 {
124   myDoGroupOfVolumes = true;
125 }
126
127 namespace{
128   typedef double (SMDS_MeshNode::* TGetCoord)() const;
129   typedef const char* TName;
130   typedef const char* TUnit;
131
132   // name length in a mesh must be equal to 16 :
133   //         1234567890123456
134   TName M = "m               ";
135   TName X = "x               ";
136   TName Y = "y               ";
137   TName Z = "z               ";
138
139   TUnit aUnit[3] = {M,M,M};
140
141   // 3 dim
142   TGetCoord aXYZGetCoord[3] = {
143     &SMDS_MeshNode::X, 
144     &SMDS_MeshNode::Y, 
145     &SMDS_MeshNode::Z
146   };
147   TName aXYZName[3] = {X,Y,Z};
148   
149   // 2 dim
150   TGetCoord aXYGetCoord[2] = {
151     &SMDS_MeshNode::X, 
152     &SMDS_MeshNode::Y
153   };
154   TName aXYName[2] = {X,Y};
155
156   TGetCoord aYZGetCoord[2] = {
157     &SMDS_MeshNode::Y, 
158     &SMDS_MeshNode::Z
159   };
160   TName aYZName[2] = {Y,Z};
161
162   TGetCoord aXZGetCoord[2] = {
163     &SMDS_MeshNode::X, 
164     &SMDS_MeshNode::Z
165   };
166   TName aXZName[2] = {X,Z};
167
168   // 1 dim
169   TGetCoord aXGetCoord[1] = {
170     &SMDS_MeshNode::X
171   };
172   TName aXName[1] = {X};
173
174   TGetCoord aYGetCoord[1] = {
175     &SMDS_MeshNode::Y
176   };
177   TName aYName[1] = {Y};
178
179   TGetCoord aZGetCoord[1] = {
180     &SMDS_MeshNode::Z
181   };
182   TName aZName[1] = {Z};
183
184
185   class TCoordHelper{
186     SMDS_NodeIteratorPtr myNodeIter;
187     const SMDS_MeshNode* myCurrentNode;
188     TGetCoord* myGetCoord;
189     TName* myName;
190     TUnit* myUnit;
191   public:
192     TCoordHelper(const SMDS_NodeIteratorPtr& theNodeIter,
193                  TGetCoord* theGetCoord,
194                  TName* theName,
195                  TUnit* theUnit = aUnit):
196       myNodeIter(theNodeIter),
197       myGetCoord(theGetCoord),
198       myName(theName),
199       myUnit(theUnit)
200     {}
201     virtual ~TCoordHelper(){}
202     bool Next(){ 
203       return myNodeIter->more() && 
204         (myCurrentNode = myNodeIter->next());
205     }
206     const SMDS_MeshNode* GetNode(){
207       return myCurrentNode;
208     }
209     MED::TIntVector::value_type GetID(){
210       return myCurrentNode->GetID();
211     }
212     MED::TFloatVector::value_type GetCoord(TInt theCoodId){
213       return (myCurrentNode->*myGetCoord[theCoodId])();
214     }
215     MED::TStringVector::value_type GetName(TInt theDimId){
216       return myName[theDimId];
217     }
218     MED::TStringVector::value_type GetUnit(TInt theDimId){
219       return myUnit[theDimId];
220     }
221   };
222   typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
223
224
225   //-------------------------------------------------------
226   /*!
227    * \brief Class helping to use either SMDS_0DElementIterator,
228    * SMDS_EdgeIterator, SMDS_FaceIterator or SMDS_VolumeIterator
229    * in the same code
230    */
231   //-------------------------------------------------------
232   struct TElemIterator
233   {
234     virtual const SMDS_MeshElement* next() = 0;
235     virtual ~TElemIterator() {}
236   };
237   typedef boost::shared_ptr<TElemIterator> PElemIterator;
238
239   template< class SMDSIteratorPtr > class TypedElemIterator: public TElemIterator
240   {
241     SMDSIteratorPtr myItPtr;
242   public:
243     TypedElemIterator(SMDSIteratorPtr it): myItPtr(it) {}
244     virtual const SMDS_MeshElement* next() {
245       if ( myItPtr->more() ) return myItPtr->next();
246       else                   return 0;
247     }
248   };
249   typedef TypedElemIterator< SMDS_0DElementIteratorPtr > T0DElementIterator;
250   typedef TypedElemIterator< SMDS_EdgeIteratorPtr > TEdgeIterator;
251   typedef TypedElemIterator< SMDS_FaceIteratorPtr > TFaceIterator;
252   typedef TypedElemIterator< SMDS_VolumeIteratorPtr > TVolumeIterator;
253
254   //-------------------------------------------------------
255   /*!
256    * \brief Structure describing element type
257    */
258   //-------------------------------------------------------
259   struct TElemTypeData
260   {
261     EEntiteMaillage     _entity;
262     EGeometrieElement   _geomType;
263     TInt                _nbElems;
264     SMDSAbs_ElementType _smdsType;
265
266     TElemTypeData (EEntiteMaillage entity, EGeometrieElement geom, TInt nb, SMDSAbs_ElementType type)
267       : _entity(entity), _geomType(geom), _nbElems( nb ), _smdsType( type ) {}
268   };
269
270
271   typedef NCollection_DataMap< Standard_Address, int > TElemFamilyMap;
272   //typedef map<const SMDS_MeshElement *, int> TElemFamilyMap;
273
274   //================================================================================
275   /*!
276    * \brief Fills element to famaly ID map for element type.
277    * Removes all families of anElemType
278    */
279   //================================================================================
280
281   void fillElemFamilyMap( TElemFamilyMap &            anElemFamMap,
282                           list<DriverMED_FamilyPtr> & aFamilies,
283                           const SMDSAbs_ElementType   anElemType)
284   {
285     anElemFamMap.Clear();
286     //anElemFamMap.clear();
287     list<DriverMED_FamilyPtr>::iterator aFamsIter = aFamilies.begin();
288     while ( aFamsIter != aFamilies.end() )
289     {
290       if ((*aFamsIter)->GetType() != anElemType) {
291         aFamsIter++;
292       }
293       else {
294         int aFamId = (*aFamsIter)->GetId();
295         const set<const SMDS_MeshElement *>& anElems = (*aFamsIter)->GetElements();
296         set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElems.begin();
297         for (; anElemsIter != anElems.end(); anElemsIter++)
298         {
299           anElemFamMap.Bind( (Standard_Address)*anElemsIter, aFamId );
300           //anElemFamMap[*anElemsIter] = aFamId;
301         }
302         // remove a family from the list
303         aFamilies.erase( aFamsIter++ );
304       }
305     }
306   }
307
308   //================================================================================
309   /*!
310    * \brief For an element, return family ID found in the map or a default one
311    */
312   //================================================================================
313
314   int getFamilyId( const TElemFamilyMap &  anElemFamMap,
315                    const SMDS_MeshElement* anElement,
316                    const int               aDefaultFamilyId)
317   {
318     if ( anElemFamMap.IsBound( (Standard_Address) anElement ))
319       return anElemFamMap( (Standard_Address) anElement );
320 //     TElemFamilyMap::iterator elem_famNum = anElemFamMap.find( anElement );
321 //     if ( elem_famNum != anElemFamMap.end() )
322 //       return elem_famNum->second;
323     return aDefaultFamilyId;
324   }
325 }
326
327 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
328 {
329   Status aResult = DRS_OK;
330   if (myMesh->hasConstructionEdges() || myMesh->hasConstructionFaces()) {
331     INFOS("SMDS_MESH with hasConstructionEdges() or hasConstructionFaces() do not supports!!!");
332     return DRS_FAIL;
333   }
334   try {
335     MESSAGE("Perform - myFile : "<<myFile);
336
337     // Creating the MED mesh for corresponding SMDS structure
338     //-------------------------------------------------------
339     string aMeshName;
340     if (myMeshId != -1) {
341       ostringstream aMeshNameStr;
342       aMeshNameStr<<myMeshId;
343       aMeshName = aMeshNameStr.str();
344     } else {
345       aMeshName = myMeshName;
346     }
347
348     // Space dimension definition
349     TInt aSpaceDim;
350     TCoordHelperPtr aCoordHelperPtr;
351     {  
352       bool anIsXDimension = false;
353       bool anIsYDimension = false;
354       bool anIsZDimension = false;
355       {
356         SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
357         double aBounds[6];
358         if(aNodesIter->more()){
359           const SMDS_MeshNode* aNode = aNodesIter->next();
360           aBounds[0] = aBounds[1] = aNode->X();
361           aBounds[2] = aBounds[3] = aNode->Y();
362           aBounds[4] = aBounds[5] = aNode->Z();
363         }
364         while(aNodesIter->more()){
365           const SMDS_MeshNode* aNode = aNodesIter->next();
366           aBounds[0] = min(aBounds[0],aNode->X());
367           aBounds[1] = max(aBounds[1],aNode->X());
368           
369           aBounds[2] = min(aBounds[2],aNode->Y());
370           aBounds[3] = max(aBounds[3],aNode->Y());
371           
372           aBounds[4] = min(aBounds[4],aNode->Z());
373           aBounds[5] = max(aBounds[5],aNode->Z());
374         }
375
376         double EPS = 1.0E-7;
377         anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
378         anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
379         anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
380         aSpaceDim = anIsXDimension + anIsYDimension + anIsZDimension;
381         if(!aSpaceDim)
382           aSpaceDim = 3;
383         // PAL16857(SMESH not conform to the MED convention):
384         if ( aSpaceDim == 2 && anIsZDimension ) // 2D only if mesh is in XOY plane
385           aSpaceDim = 3;
386         // PAL18941(a saved study with a mesh belong Z is opened and the mesh is belong X)
387         if ( aSpaceDim == 1 && !anIsXDimension ) // 1D only if mesh is along OX
388           if ( anIsYDimension ) {
389             aSpaceDim = 2;
390             anIsXDimension = true;
391           } else {
392             aSpaceDim = 3;
393           }
394       }
395
396       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator(/*idInceasingOrder=*/true);
397       switch(aSpaceDim){
398       case 3:
399         aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
400         break;
401       case 2:
402         if(anIsXDimension && anIsYDimension)
403           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
404         if(anIsYDimension && anIsZDimension)
405           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
406         if(anIsXDimension && anIsZDimension)
407           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
408         break;
409       case 1:
410         if(anIsXDimension)
411           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
412         if(anIsYDimension)
413           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
414         if(anIsZDimension)
415           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
416         break;
417       }
418     }
419     TInt aMeshDimension = 0;
420     if ( myMesh->NbEdges() > 0 )
421       aMeshDimension = 1;
422     if ( myMesh->NbFaces() > 0 )
423       aMeshDimension = 2;
424     if ( myMesh->NbVolumes() > 0 )
425       aMeshDimension = 3;
426
427     
428     PMeshInfo aMeshInfo = myMed->CrMeshInfo(aSpaceDim,aMeshName);
429     aMeshInfo->myMeshDim = aMeshDimension;
430     MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
431     myMed->SetMeshInfo(aMeshInfo);
432
433     // Storing SMDS groups and sub-meshes as med families
434     //----------------------------------------------------
435     int myNodesDefaultFamilyId   = 0;
436     int my0DElementsDefaultFamilyId = 0;
437     int myEdgesDefaultFamilyId   = 0;
438     int myFacesDefaultFamilyId   = 0;
439     int myVolumesDefaultFamilyId = 0;
440     int nbNodes   = myMesh->NbNodes();
441     int nb0DElements = myMesh->Nb0DElements();
442     int nbEdges   = myMesh->NbEdges();
443     int nbFaces   = myMesh->NbFaces();
444     int nbVolumes = myMesh->NbVolumes();
445     if (myDoGroupOfNodes && nbNodes)
446       myNodesDefaultFamilyId = REST_NODES_FAMILY;
447     if (myDoGroupOfEdges && nbEdges)
448       myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
449     if (myDoGroupOfFaces && nbFaces)
450       myFacesDefaultFamilyId = REST_FACES_FAMILY;
451     if (myDoGroupOfVolumes && nbVolumes)
452       myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
453
454     MESSAGE("Perform - aFamilyInfo");
455     //cout << " DriverMED_Family::MakeFamilies() " << endl;
456     list<DriverMED_FamilyPtr> aFamilies;
457     if (myAllSubMeshes) {
458       aFamilies = DriverMED_Family::MakeFamilies
459         (myMesh->SubMeshes(), myGroups,
460          myDoGroupOfNodes   && nbNodes,
461          myDoGroupOfEdges   && nbEdges,
462          myDoGroupOfFaces   && nbFaces,
463          myDoGroupOfVolumes && nbVolumes);
464     } else {
465       aFamilies = DriverMED_Family::MakeFamilies
466         (mySubMeshes, myGroups,
467          myDoGroupOfNodes   && nbNodes,
468          myDoGroupOfEdges   && nbEdges,
469          myDoGroupOfFaces   && nbFaces,
470          myDoGroupOfVolumes && nbVolumes);
471     }
472     //cout << " myMed->SetFamilyInfo() " << endl;
473     list<DriverMED_FamilyPtr>::iterator aFamsIter;
474     for (aFamsIter = aFamilies.begin(); aFamsIter != aFamilies.end(); aFamsIter++)
475     {
476       PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo(myMed,aMeshInfo);
477       myMed->SetFamilyInfo(aFamilyInfo);
478     }
479
480     // Storing SMDS nodes to the MED file for the MED mesh
481     //----------------------------------------------------
482 #ifdef _EDF_NODE_IDS_
483     typedef map<TInt,TInt> TNodeIdMap;
484     TNodeIdMap aNodeIdMap;
485 #endif
486     const EModeSwitch   theMode        = eFULL_INTERLACE;
487     const ERepere       theSystem      = eCART;
488     const EBooleen      theIsElemNum   = eVRAI;
489     const EBooleen      theIsElemNames = eFAUX;
490     const EConnectivite theConnMode    = eNOD;
491
492     TInt aNbNodes = myMesh->NbNodes();
493     //cout << " myMed->CrNodeInfo() aNbNodes = " << aNbNodes << endl;
494     PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo, aNbNodes,
495                                             theMode, theSystem, theIsElemNum, theIsElemNames);
496
497     //cout << " fillElemFamilyMap( SMDSAbs_Node )" << endl;
498     // find family numbers for nodes
499     TElemFamilyMap anElemFamMap;
500     fillElemFamilyMap( anElemFamMap, aFamilies, SMDSAbs_Node );
501
502     for (TInt iNode = 0; aCoordHelperPtr->Next(); iNode++)
503     {
504       // coordinates
505       TCoordSlice aTCoordSlice = aNodeInfo->GetCoordSlice( iNode );
506       for(TInt iCoord = 0; iCoord < aSpaceDim; iCoord++){
507         aTCoordSlice[iCoord] = aCoordHelperPtr->GetCoord(iCoord);
508       }
509       // node number
510       int aNodeID = aCoordHelperPtr->GetID();
511       aNodeInfo->SetElemNum( iNode, aNodeID );
512 #ifdef _EDF_NODE_IDS_
513       aNodeIdMap[aNodeID] = iNode+1;
514 #endif
515       // family number
516       const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
517       int famNum = getFamilyId( anElemFamMap, aNode, myNodesDefaultFamilyId );
518       aNodeInfo->SetFamNum( iNode, famNum );
519     }
520     anElemFamMap.Clear();
521
522     // coordinate names and units
523     for (TInt iCoord = 0; iCoord < aSpaceDim; iCoord++) {
524       aNodeInfo->SetCoordName( iCoord, aCoordHelperPtr->GetName(iCoord));
525       aNodeInfo->SetCoordUnit( iCoord, aCoordHelperPtr->GetUnit(iCoord));
526     }
527
528     //cout << " SetNodeInfo(aNodeInfo)" << endl;
529     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbNodes);
530     myMed->SetNodeInfo(aNodeInfo);
531     aNodeInfo.reset(); // free memory used for arrays
532
533
534     // Storing SMDS elements to the MED file for the MED mesh
535     //-------------------------------------------------------
536     // Write one element type at once in order to minimize memory usage (PAL19276)
537
538     const SMDS_MeshInfo& nbElemInfo = myMesh->GetMeshInfo();
539
540     // poly elements are not supported by med-2.1
541     bool polyTypesSupported = myMed->CrPolygoneInfo(aMeshInfo,eMAILLE,ePOLYGONE,0,0);
542     TInt nbPolygonNodes = 0, nbPolyhedronNodes = 0, nbPolyhedronFaces = 0;
543
544     // collect info on all geom types
545
546     list< TElemTypeData > aTElemTypeDatas;
547
548     EEntiteMaillage anEntity = eMAILLE;
549 #ifdef _ELEMENTS_BY_DIM_
550     anEntity = eNOEUD_ELEMENT;
551 #endif
552     aTElemTypeDatas.push_back(TElemTypeData(anEntity,
553                                             ePOINT1,
554                                             nbElemInfo.Nb0DElements(),
555                                             SMDSAbs_0DElement));
556 #ifdef _ELEMENTS_BY_DIM_
557     anEntity = eARETE;
558 #endif
559     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
560                                             eSEG2,
561                                             nbElemInfo.NbEdges( ORDER_LINEAR ),
562                                             SMDSAbs_Edge));
563     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
564                                             eSEG3,
565                                             nbElemInfo.NbEdges( ORDER_QUADRATIC ),
566                                             SMDSAbs_Edge));
567 #ifdef _ELEMENTS_BY_DIM_
568     anEntity = eFACE;
569 #endif
570     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
571                                             eTRIA3,
572                                             nbElemInfo.NbTriangles( ORDER_LINEAR ),
573                                             SMDSAbs_Face));
574     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
575                                             eTRIA6,
576                                             nbElemInfo.NbTriangles( ORDER_QUADRATIC ),
577                                             SMDSAbs_Face));
578     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
579                                             eQUAD4,
580                                             nbElemInfo.NbQuadrangles( ORDER_LINEAR ),
581                                             SMDSAbs_Face));
582     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
583                                             eQUAD8,
584                                             nbElemInfo.NbQuadrangles( ORDER_QUADRATIC ),
585                                             SMDSAbs_Face));
586     if ( polyTypesSupported ) {
587       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
588                                                ePOLYGONE,
589                                                nbElemInfo.NbPolygons(),
590                                                SMDSAbs_Face));
591       // we need one more loop on poly elements to count nb of their nodes
592       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
593                                                ePOLYGONE,
594                                                nbElemInfo.NbPolygons(),
595                                                SMDSAbs_Face));
596     }
597 #ifdef _ELEMENTS_BY_DIM_
598     anEntity = eMAILLE;
599 #endif
600     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
601                                             eTETRA4,
602                                             nbElemInfo.NbTetras( ORDER_LINEAR ),
603                                             SMDSAbs_Volume));
604     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
605                                             eTETRA10,
606                                             nbElemInfo.NbTetras( ORDER_QUADRATIC ),
607                                             SMDSAbs_Volume));
608     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
609                                             ePYRA5,
610                                             nbElemInfo.NbPyramids( ORDER_LINEAR ),
611                                             SMDSAbs_Volume));
612     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
613                                             ePYRA13,
614                                             nbElemInfo.NbPyramids( ORDER_QUADRATIC ),
615                                             SMDSAbs_Volume));
616     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
617                                             ePENTA6,
618                                             nbElemInfo.NbPrisms( ORDER_LINEAR ),
619                                             SMDSAbs_Volume));
620     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
621                                              ePENTA15,
622                                              nbElemInfo.NbPrisms( ORDER_QUADRATIC ),
623                                              SMDSAbs_Volume));
624     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
625                                              eHEXA8,
626                                              nbElemInfo.NbHexas( ORDER_LINEAR ),
627                                              SMDSAbs_Volume));
628     aTElemTypeDatas.push_back( TElemTypeData(anEntity,
629                                              eHEXA20,
630                                              nbElemInfo.NbHexas( ORDER_QUADRATIC ),
631                                              SMDSAbs_Volume));
632     if ( polyTypesSupported ) {
633       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
634                                                ePOLYEDRE,
635                                                nbElemInfo.NbPolyhedrons(),
636                                                SMDSAbs_Volume));
637       // we need one more loop on poly elements to count nb of their nodes
638       aTElemTypeDatas.push_back( TElemTypeData(anEntity,
639                                                ePOLYEDRE,
640                                                nbElemInfo.NbPolyhedrons(),
641                                                SMDSAbs_Volume));
642     }
643
644     vector< bool > isElemFamMapBuilt( SMDSAbs_NbElementTypes, false );
645
646     // loop on all geom types of elements
647
648     list< TElemTypeData >::iterator aElemTypeData = aTElemTypeDatas.begin();
649     for ( ; aElemTypeData != aTElemTypeDatas.end(); ++aElemTypeData )
650     {
651       if ( aElemTypeData->_nbElems == 0 )
652         continue;
653
654       // iterator on elements of a current type
655       PElemIterator elemIterator;
656       int defaultFamilyId = 0;
657       switch ( aElemTypeData->_smdsType ) {
658       case SMDSAbs_0DElement:
659         elemIterator = PElemIterator( new T0DElementIterator( myMesh->elements0dIterator() ));
660         defaultFamilyId = my0DElementsDefaultFamilyId;
661         break;
662       case SMDSAbs_Edge:
663         elemIterator = PElemIterator( new TEdgeIterator( myMesh->edgesIterator() ));
664         defaultFamilyId = myEdgesDefaultFamilyId;
665         break;
666       case SMDSAbs_Face:
667         elemIterator = PElemIterator( new TFaceIterator( myMesh->facesIterator() ));
668         defaultFamilyId = myFacesDefaultFamilyId;
669         break;
670       case SMDSAbs_Volume:
671         elemIterator = PElemIterator( new TVolumeIterator( myMesh->volumesIterator() ));
672         defaultFamilyId = myVolumesDefaultFamilyId;
673         break;
674       default:
675         continue;
676       }
677       int iElem = 0;
678
679       //cout << " Treat type " << aElemTypeData->_geomType << " nb = " <<aElemTypeData->_nbElems<< endl;
680       // Treat POLYGONs
681       // ---------------
682       if ( aElemTypeData->_geomType == ePOLYGONE )
683       {
684         if ( nbPolygonNodes == 0 ) {
685           // Count nb of nodes
686           while ( const SMDS_MeshElement* anElem = elemIterator->next() ) {
687             if ( anElem->IsPoly() ) {
688               nbPolygonNodes += anElem->NbNodes();
689               if ( ++iElem == aElemTypeData->_nbElems )
690                 break;
691             }
692           }
693         }
694         else {
695           // Store in med file
696           PPolygoneInfo aPolygoneInfo = myMed->CrPolygoneInfo(aMeshInfo,
697                                                               aElemTypeData->_entity,
698                                                               aElemTypeData->_geomType,
699                                                               aElemTypeData->_nbElems,
700                                                               nbPolygonNodes,
701                                                               theConnMode, theIsElemNum,
702                                                               theIsElemNames);
703           TElemNum & index = *(aPolygoneInfo->myIndex.get());
704           index[0] = 1;
705
706           while ( const SMDS_MeshElement* anElem = elemIterator->next() )
707           {
708             if ( !anElem->IsPoly() )
709               continue;
710
711             // index
712             TInt aNbNodes = anElem->NbNodes();
713             index[ iElem+1 ] = index[ iElem ] + aNbNodes;
714
715             // connectivity
716             TConnSlice aTConnSlice = aPolygoneInfo->GetConnSlice( iElem );
717             for(TInt iNode = 0; iNode < aNbNodes; iNode++) {
718               const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
719 #ifdef _EDF_NODE_IDS_
720               aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
721 #else
722               aTConnSlice[ iNode ] = aNode->GetID();
723 #endif
724             }
725             // element number
726             aPolygoneInfo->SetElemNum( iElem, anElem->GetID() );
727
728             // family number
729             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
730             aPolygoneInfo->SetFamNum( iElem, famNum );
731
732             if ( ++iElem == aPolygoneInfo->GetNbElem() )
733               break;
734           }
735           //       if(TInt aNbElems = aPolygoneElemNums.size())
736           //         // add one element in connectivities,
737           //         // referenced by the last element in indices
738           //         aPolygoneConn.push_back(0);
739           //cout << " SetPolygoneInfo(aPolygoneInfo)" << endl;
740           myMed->SetPolygoneInfo(aPolygoneInfo);
741         }
742
743       } 
744
745       // Treat POLYEDREs
746       // ----------------
747       else if (aElemTypeData->_geomType == ePOLYEDRE )
748       {
749         if ( nbPolyhedronNodes == 0 ) {
750           // Count nb of nodes
751           while ( const SMDS_MeshElement* anElem = elemIterator->next() ) {
752             const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
753               dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*>( anElem );
754             if ( aPolyedre ) {
755               nbPolyhedronNodes += aPolyedre->NbNodes();
756               nbPolyhedronFaces += aPolyedre->NbFaces();
757               if ( ++iElem == aElemTypeData->_nbElems )
758                 break;
759             }
760           }
761         }
762         else {
763           // Store in med file
764           PPolyedreInfo aPolyhInfo = myMed->CrPolyedreInfo(aMeshInfo,
765                                                            aElemTypeData->_entity,
766                                                            aElemTypeData->_geomType,
767                                                            aElemTypeData->_nbElems,
768                                                            nbPolyhedronFaces+1,
769                                                            nbPolyhedronNodes,
770                                                            theConnMode,
771                                                            theIsElemNum,
772                                                            theIsElemNames);
773           TElemNum & index = *(aPolyhInfo->myIndex.get());
774           TElemNum & faces = *(aPolyhInfo->myFaces.get());
775           TElemNum & conn  = *(aPolyhInfo->myConn.get());
776           index[0] = 1;
777           faces[0] = 1;
778
779           TInt iFace = 0, iNode = 0;
780           while ( const SMDS_MeshElement* anElem = elemIterator->next() )
781           {
782             const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
783               dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*>( anElem );
784             if ( !aPolyedre )
785               continue;
786
787             // index
788             TInt aNbFaces = aPolyedre->NbFaces();
789             index[ iElem+1 ] = index[ iElem ] + aNbFaces;
790
791             // face index
792             for (TInt f = 1; f <= aNbFaces; ++f, ++iFace ) {
793               int aNbFaceNodes = aPolyedre->NbFaceNodes( f );
794               faces[ iFace+1 ] = faces[ iFace ] + aNbFaceNodes;
795             }
796             // connectivity
797             SMDS_ElemIteratorPtr nodeIt = anElem->nodesIterator();
798             while ( nodeIt->more() ) {
799               const SMDS_MeshElement* aNode = nodeIt->next();
800 #ifdef _EDF_NODE_IDS_
801               conn[ iNode ] = aNodeIdMap[aNode->GetID()];
802 #else
803               conn[ iNode ] = aNode->GetID();
804 #endif
805               ++iNode;
806             }
807             // element number
808             aPolyhInfo->SetElemNum( iElem, anElem->GetID() );
809
810             // family number
811             int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
812             aPolyhInfo->SetFamNum( iElem, famNum );
813
814             if ( ++iElem == aPolyhInfo->GetNbElem() )
815               break;
816           }
817           //cout << " SetPolyedreInfo(aPolyhInfo )" << endl;
818           myMed->SetPolyedreInfo(aPolyhInfo);
819         }
820       } // if (aElemTypeData->_geomType == ePOLYEDRE )
821
822       else
823       {
824         // Treat standard types
825         // ---------------------
826
827         // allocate data arrays
828         PCellInfo aCellInfo = myMed->CrCellInfo( aMeshInfo,
829                                                  aElemTypeData->_entity,
830                                                  aElemTypeData->_geomType,
831                                                  aElemTypeData->_nbElems,
832                                                  theConnMode,
833                                                  theIsElemNum,
834                                                  theIsElemNames);
835         // build map of family numbers for this type
836         if ( !isElemFamMapBuilt[ aElemTypeData->_smdsType ])
837         {
838           //cout << " fillElemFamilyMap()" << endl;
839           fillElemFamilyMap( anElemFamMap, aFamilies, aElemTypeData->_smdsType );
840           isElemFamMapBuilt[ aElemTypeData->_smdsType ] = true;
841         }
842
843         TInt aNbNodes = MED::GetNbNodes(aElemTypeData->_geomType);
844         while ( const SMDS_MeshElement* anElem = elemIterator->next() )
845         {
846           if ( anElem->NbNodes() != aNbNodes || anElem->IsPoly() )
847             continue; // other geometry
848
849           // connectivity
850           TConnSlice aTConnSlice = aCellInfo->GetConnSlice( iElem );
851           for (TInt iNode = 0; iNode < aNbNodes; iNode++) {
852             const SMDS_MeshElement* aNode = anElem->GetNode( iNode );
853 #ifdef _EDF_NODE_IDS_
854             aTConnSlice[ iNode ] = aNodeIdMap[aNode->GetID()];
855 #else
856             aTConnSlice[ iNode ] = aNode->GetID();
857 #endif
858           }
859           // element number
860           aCellInfo->SetElemNum( iElem, anElem->GetID() );
861
862           // family number
863           int famNum = getFamilyId( anElemFamMap, anElem, defaultFamilyId );
864           aCellInfo->SetFamNum( iElem, famNum );
865
866           if ( ++iElem == aCellInfo->GetNbElem() )
867             break;
868         }
869         // store data in a file
870         //cout << " SetCellInfo(aCellInfo)" << endl;
871         myMed->SetCellInfo(aCellInfo);
872       }
873
874     } // loop on geom types
875
876
877   }
878   catch(const std::exception& exc) {
879     INFOS("Follow exception was cought:\n\t"<<exc.what());
880     throw;
881   }
882   catch(...) {
883     INFOS("Unknown exception was cought !!!");
884     throw;
885   }
886
887   myMeshId = -1;
888   myGroups.clear();
889   mySubMeshes.clear();
890   return aResult;
891 }