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