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