Salome HOME
9d5dcd1b686f8e0da439f76637db155acb09885f
[modules/smesh.git] / src / DriverMED / DriverMED_W_SMESHDS_Mesh.cxx
1 //  SMESH DriverMED : driver to read and write 'med' files
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
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 void DriverMED_W_SMESHDS_Mesh::SetMeshName(const std::string& theMeshName)
70 {
71   myMeshName = theMeshName;
72 }
73
74 void DriverMED_W_SMESHDS_Mesh::AddGroup(SMESHDS_GroupBase* theGroup)
75 {
76   myGroups.push_back(theGroup);
77 }
78
79 void DriverMED_W_SMESHDS_Mesh::AddAllSubMeshes()
80 {
81   myAllSubMeshes = true;
82 }
83
84 void DriverMED_W_SMESHDS_Mesh::AddSubMesh(SMESHDS_SubMesh* theSubMesh, int theID)
85 {
86   mySubMeshes[theID] = theSubMesh;
87 }
88
89 void DriverMED_W_SMESHDS_Mesh::AddGroupOfNodes()
90 {
91   myDoGroupOfNodes = true;
92 }
93
94 void DriverMED_W_SMESHDS_Mesh::AddGroupOfEdges()
95 {
96   myDoGroupOfEdges = true;
97 }
98
99 void DriverMED_W_SMESHDS_Mesh::AddGroupOfFaces()
100 {
101   myDoGroupOfFaces = true;
102 }
103
104 void DriverMED_W_SMESHDS_Mesh::AddGroupOfVolumes()
105 {
106   myDoGroupOfVolumes = true;
107 }
108
109 namespace{
110   typedef double (SMDS_MeshNode::* TGetCoord)() const;
111   typedef const char* TName;
112   typedef const char* TUnit;
113
114   // name length in a mesh must be equal to 16 :
115   //         1234567890123456
116   TName M = "m               ";
117   TName X = "x               ";
118   TName Y = "y               ";
119   TName Z = "z               ";
120
121   TUnit aUnit[3] = {M,M,M};
122
123   // 3 dim
124   TGetCoord aXYZGetCoord[3] = {
125     &SMDS_MeshNode::X, 
126     &SMDS_MeshNode::Y, 
127     &SMDS_MeshNode::Z
128   };
129   TName aXYZName[3] = {X,Y,Z};
130   
131   // 2 dim
132   TGetCoord aXYGetCoord[2] = {
133     &SMDS_MeshNode::X, 
134     &SMDS_MeshNode::Y
135   };
136   TName aXYName[2] = {X,Y};
137
138   TGetCoord aYZGetCoord[2] = {
139     &SMDS_MeshNode::Y, 
140     &SMDS_MeshNode::Z
141   };
142   TName aYZName[2] = {Y,Z};
143
144   TGetCoord aXZGetCoord[2] = {
145     &SMDS_MeshNode::X, 
146     &SMDS_MeshNode::Z
147   };
148   TName aXZName[2] = {X,Z};
149
150   // 1 dim
151   TGetCoord aXGetCoord[1] = {
152     &SMDS_MeshNode::X
153   };
154   TName aXName[1] = {X};
155
156   TGetCoord aYGetCoord[1] = {
157     &SMDS_MeshNode::Y
158   };
159   TName aYName[1] = {Y};
160
161   TGetCoord aZGetCoord[1] = {
162     &SMDS_MeshNode::Z
163   };
164   TName aZName[1] = {Z};
165
166
167   class TCoordHelper{
168     SMDS_NodeIteratorPtr myNodeIter;
169     const SMDS_MeshNode* myCurrentNode;
170     TGetCoord* myGetCoord;
171     TName* myName;
172     TUnit* myUnit;
173   public:
174     TCoordHelper(const SMDS_NodeIteratorPtr& theNodeIter,
175                  TGetCoord* theGetCoord,
176                  TName* theName,
177                  TUnit* theUnit = aUnit):
178       myNodeIter(theNodeIter),
179       myGetCoord(theGetCoord),
180       myName(theName),
181       myUnit(theUnit)
182     {}
183     virtual ~TCoordHelper(){}
184     bool Next(){ 
185       return myNodeIter->more() && 
186         (myCurrentNode = myNodeIter->next());
187     }
188     const SMDS_MeshNode* GetNode(){
189       return myCurrentNode;
190     }
191     MED::TIntVector::value_type GetID(){
192       return myCurrentNode->GetID();
193     }
194     MED::TFloatVector::value_type GetCoord(TInt theCoodId){
195       return (myCurrentNode->*myGetCoord[theCoodId])();
196     }
197     MED::TStringVector::value_type GetName(TInt theDimId){
198       return myName[theDimId];
199     }
200     MED::TStringVector::value_type GetUnit(TInt theDimId){
201       return myUnit[theDimId];
202     }
203   };
204   typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
205   
206 }
207
208   
209 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
210 {
211   Status aResult = DRS_OK;
212   if (myMesh->hasConstructionEdges() || myMesh->hasConstructionFaces()) {
213     INFOS("SMDS_MESH with hasConstructionEdges() or hasConstructionFaces() do not supports!!!");
214     return DRS_FAIL;
215   }
216   try{
217     MESSAGE("Perform - myFile : "<<myFile);
218
219     // Creating the MED mesh for corresponding SMDS structure
220     //-------------------------------------------------------
221     string aMeshName;
222     if (myMeshId != -1) {
223       ostringstream aMeshNameStr;
224       aMeshNameStr<<myMeshId;
225       aMeshName = aMeshNameStr.str();
226     } else {
227       aMeshName = myMeshName;
228     }
229
230     // Mesh dimension definition
231     TInt aMeshDimension;
232     TCoordHelperPtr aCoordHelperPtr;
233     {  
234       bool anIsXDimension = false;
235       bool anIsYDimension = false;
236       bool anIsZDimension = false;
237       {
238         SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
239         double aBounds[6];
240         if(aNodesIter->more()){
241           const SMDS_MeshNode* aNode = aNodesIter->next();
242           aBounds[0] = aBounds[1] = aNode->X();
243           aBounds[2] = aBounds[3] = aNode->Y();
244           aBounds[4] = aBounds[5] = aNode->Z();
245         }
246         while(aNodesIter->more()){
247           const SMDS_MeshNode* aNode = aNodesIter->next();
248           aBounds[0] = min(aBounds[0],aNode->X());
249           aBounds[1] = max(aBounds[1],aNode->X());
250           
251           aBounds[2] = min(aBounds[2],aNode->Y());
252           aBounds[3] = max(aBounds[3],aNode->Y());
253           
254           aBounds[4] = min(aBounds[4],aNode->Z());
255           aBounds[5] = max(aBounds[5],aNode->Z());
256         }
257
258         double EPS = 1.0E-7;
259         anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
260         anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
261         anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
262         aMeshDimension = anIsXDimension + anIsYDimension + anIsZDimension;
263         if(!aMeshDimension)
264           aMeshDimension = 3;
265       }
266
267       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
268       switch(aMeshDimension){
269       case 3:
270         aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
271         break;
272       case 2:
273         if(anIsXDimension && anIsYDimension)
274           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
275         if(anIsYDimension && anIsZDimension)
276           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
277         if(anIsXDimension && anIsZDimension)
278           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
279         break;
280       case 1:
281         if(anIsXDimension)
282           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
283         if(anIsYDimension)
284           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
285         if(anIsZDimension)
286           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
287         break;
288       }
289     }
290
291     
292     PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aMeshName);
293     MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
294     myMed->SetMeshInfo(aMeshInfo);
295
296     // Storing SMDS groups and sub-meshes
297     //-----------------------------------
298     int myNodesDefaultFamilyId = 0;
299     int myEdgesDefaultFamilyId = 0;
300     int myFacesDefaultFamilyId = 0;
301     int myVolumesDefaultFamilyId = 0;
302     if (myDoGroupOfNodes)
303       myNodesDefaultFamilyId = REST_NODES_FAMILY;
304     if (myDoGroupOfEdges)
305       myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
306     if (myDoGroupOfFaces)
307       myFacesDefaultFamilyId = REST_FACES_FAMILY;
308     if (myDoGroupOfVolumes)
309       myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
310
311     MESSAGE("Perform - aFamilyInfo");
312     map<const SMDS_MeshElement *, int> anElemFamMap;
313     list<DriverMED_FamilyPtr> aFamilies;
314     if (myAllSubMeshes) {
315       aFamilies = DriverMED_Family::MakeFamilies
316         (myMesh->SubMeshes(), myGroups,
317          myDoGroupOfNodes, myDoGroupOfEdges, myDoGroupOfFaces, myDoGroupOfVolumes);
318     } else {
319       aFamilies = DriverMED_Family::MakeFamilies
320         (mySubMeshes, myGroups,
321          myDoGroupOfNodes, myDoGroupOfEdges, myDoGroupOfFaces, myDoGroupOfVolumes);
322     }
323     list<DriverMED_FamilyPtr>::iterator aFamsIter = aFamilies.begin();
324
325     for (; aFamsIter != aFamilies.end(); aFamsIter++)
326     {
327       PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo(myMed,aMeshInfo);
328       myMed->SetFamilyInfo(aFamilyInfo);
329       int aFamId = (*aFamsIter)->GetId();
330
331       const set<const SMDS_MeshElement *>& anElems = (*aFamsIter)->GetElements();
332           set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElems.begin();
333       for (; anElemsIter != anElems.end(); anElemsIter++)
334       {
335         anElemFamMap[*anElemsIter] = aFamId;
336       }
337 //      delete (*aFamsIter);
338     }
339
340     // Storing SMDS nodes to the MED file for the MED mesh
341     //----------------------------------------------------
342 #ifdef _EDF_NODE_IDS_
343     typedef map<TInt,TInt> TNodeIdMap;
344     TNodeIdMap aNodeIdMap;
345 #endif
346     TInt aNbElems = myMesh->NbNodes();
347     MED::TIntVector anElemNums(aNbElems);
348     MED::TIntVector aFamilyNums(aNbElems);
349     MED::TFloatVector aCoordinates(aNbElems*aMeshDimension);
350     for(TInt iNode = 0, aStartId = 0; aCoordHelperPtr->Next(); iNode++, aStartId += aMeshDimension){
351       for(TInt iCoord = 0; iCoord < aMeshDimension; iCoord++){
352         aCoordinates[aStartId+iCoord] = aCoordHelperPtr->GetCoord(iCoord);
353       }
354       int aNodeID = aCoordHelperPtr->GetID();
355       anElemNums[iNode] = aNodeID;
356 #ifdef _EDF_NODE_IDS_
357       aNodeIdMap[aNodeID] = iNode+1;
358 #endif
359       const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
360       if (anElemFamMap.find(aNode) != anElemFamMap.end())
361         aFamilyNums[iNode] = anElemFamMap[aNode];
362       else
363         aFamilyNums[iNode] = myNodesDefaultFamilyId;
364     }
365
366     MED::TStringVector aCoordNames(aMeshDimension);
367     MED::TStringVector aCoordUnits(aMeshDimension);
368     for(TInt iCoord = 0; iCoord < aMeshDimension; iCoord++){
369       aCoordNames[iCoord] = aCoordHelperPtr->GetName(iCoord);
370       aCoordUnits[iCoord] = aCoordHelperPtr->GetUnit(iCoord);
371     }
372
373     const ERepere SMDS_COORDINATE_SYSTEM = eCART;
374
375     PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo,
376                                             SMDS_COORDINATE_SYSTEM,
377                                             aCoordinates,
378                                             aCoordNames,
379                                             aCoordUnits,
380                                             aFamilyNums,
381                                             anElemNums);
382     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems);
383     myMed->SetNodeInfo(aNodeInfo);
384
385
386     // Storing others SMDS elements to the MED file for the MED mesh
387     //--------------------------------------------------------------
388     EEntiteMaillage SMDS_MED_ENTITY = eMAILLE;
389     const EConnectivite SMDS_MED_CONNECTIVITY = eNOD;
390
391     // Storing SMDS Edges
392     if(TInt aNbElems = myMesh->NbEdges()){
393 #ifdef _ELEMENTS_BY_DIM_
394       SMDS_MED_ENTITY = eARETE;
395 #endif
396       SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
397
398 //      TInt aNbConnectivity = MED::GetNbNodes(eSEG2);
399 //      MED::TIntVector anElemNums(aNbElems);
400 //      MED::TIntVector aFamilyNums(aNbElems);
401 //      MED::TIntVector aConnectivity(aNbElems*aNbConnectivity);
402
403       TInt aNbSeg2Conn = MED::GetNbNodes(eSEG2);
404       MED::TIntVector aSeg2ElemNums(aNbElems);
405       MED::TIntVector aSeg2FamilyNums(aNbElems);
406       MED::TIntVector aSeg2Conn(aNbElems*aNbSeg2Conn);
407
408       TInt aNbSeg3Conn = MED::GetNbNodes(eSEG3);
409       MED::TIntVector aSeg3ElemNums(aNbElems);
410       MED::TIntVector aSeg3FamilyNums(aNbElems);
411       MED::TIntVector aSeg3Conn(aNbElems*aNbSeg3Conn);
412
413       //for(TInt iElem = 0, Conn = 0; anIter->more(); iElem++, iConn+=aNbConnectivity){
414       for(TInt iElem = 0; anIter->more(); iElem++) {
415         const SMDS_MeshEdge* anElem = anIter->next();
416         TInt aNbNodes = anElem->NbNodes();
417         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
418
419         TInt aNbConnectivity;
420         MED::TIntVector* anElemNums;
421         MED::TIntVector* aFamilyNums;
422         MED::TIntVector* aConnectivity;
423         switch(aNbNodes){
424         case 2:
425           aNbConnectivity = aNbSeg2Conn;
426           anElemNums = &aSeg2ElemNums;
427           aFamilyNums = &aSeg2FamilyNums;
428           aConnectivity = &aSeg2Conn;
429           break;
430         case 3:
431           aNbConnectivity = aNbSeg3Conn;
432           anElemNums = &aSeg3ElemNums;
433           aFamilyNums = &aSeg3FamilyNums;
434           aConnectivity = &aSeg3Conn;
435           break;
436         default:
437           break;
438         }
439
440         MED::TIntVector aVector(aNbNodes);
441         for(TInt iNode = 0; aNodesIter->more(); iNode++){
442           const SMDS_MeshElement* aNode = aNodesIter->next();
443 #ifdef _EDF_NODE_IDS_
444           aVector[iNode] = aNodeIdMap[aNode->GetID()];
445 #else
446           aVector[iNode] = aNode->GetID();
447 #endif
448         }
449
450         TInt aSize = aConnectivity->size();
451         aConnectivity->resize(aSize+aNbConnectivity);
452         // There is some differences between SMDS and MED in cells mapping
453         for(TInt iNode = 0; iNode < aNbNodes; iNode++) 
454           (*aConnectivity)[aSize+iNode] = aVector[iNode];
455
456         anElemNums->push_back(anElem->GetID());
457
458         if (anElemFamMap.find(anElem) != anElemFamMap.end())
459           aFamilyNums->push_back(anElemFamMap[anElem]);
460         else
461           aFamilyNums->push_back(myFacesDefaultFamilyId);
462
463       }
464       
465       if(TInt aNbElems = aSeg2ElemNums.size()){
466         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
467                                                 SMDS_MED_ENTITY,
468                                                 eSEG2,
469                                                 SMDS_MED_CONNECTIVITY,
470                                                 aSeg2Conn,
471                                                 aSeg2FamilyNums,
472                                                 aSeg2ElemNums);
473         myMed->SetCellInfo(aCellInfo);
474       }
475       if(TInt aNbElems = aSeg3ElemNums.size()){
476         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
477                                                 SMDS_MED_ENTITY,
478                                                 eSEG3,
479                                                 SMDS_MED_CONNECTIVITY,
480                                                 aSeg3Conn,
481                                                 aSeg3FamilyNums,
482                                                 aSeg3ElemNums);
483         myMed->SetCellInfo(aCellInfo);
484       }
485     }
486
487     // Storing SMDS Faces
488     if(TInt aNbElems = myMesh->NbFaces()){
489       SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
490 #ifdef _ELEMENTS_BY_DIM_
491       SMDS_MED_ENTITY = eFACE;
492 #endif
493       TInt aNbTriaConn = MED::GetNbNodes(eTRIA3);
494       MED::TIntVector anTriaElemNums; 
495       anTriaElemNums.reserve(aNbElems);
496       MED::TIntVector aTriaFamilyNums;
497       aTriaFamilyNums.reserve(aNbElems);
498       MED::TIntVector aTriaConn;
499       aTriaConn.reserve(aNbElems*aNbTriaConn);
500
501       TInt aNbTria6Conn = MED::GetNbNodes(eTRIA6);
502       MED::TIntVector anTria6ElemNums; 
503       anTria6ElemNums.reserve(aNbElems);
504       MED::TIntVector aTria6FamilyNums;
505       aTria6FamilyNums.reserve(aNbElems);
506       MED::TIntVector aTria6Conn;
507       aTria6Conn.reserve(aNbElems*aNbTria6Conn);
508
509       TInt aNbQuadConn = MED::GetNbNodes(eQUAD4);
510       MED::TIntVector aQuadElemNums;
511       aQuadElemNums.reserve(aNbElems);
512       MED::TIntVector aQuadFamilyNums;
513       aQuadFamilyNums.reserve(aNbElems);
514       MED::TIntVector aQuadConn;
515       aQuadConn.reserve(aNbElems*aNbQuadConn);
516
517       TInt aNbQuad8Conn = MED::GetNbNodes(eQUAD8);
518       MED::TIntVector aQuad8ElemNums;
519       aQuad8ElemNums.reserve(aNbElems);
520       MED::TIntVector aQuad8FamilyNums;
521       aQuad8FamilyNums.reserve(aNbElems);
522       MED::TIntVector aQuad8Conn;
523       aQuad8Conn.reserve(aNbElems*aNbQuad8Conn);
524
525       MED::TIntVector aPolygoneElemNums;
526       aPolygoneElemNums.reserve(aNbElems);
527       MED::TIntVector aPolygoneInds;
528       aPolygoneInds.reserve(aNbElems + 1);
529       aPolygoneInds.push_back(1); // reference on the first element in the connectivities
530       MED::TIntVector aPolygoneFamilyNums;
531       aPolygoneFamilyNums.reserve(aNbElems);
532       MED::TIntVector aPolygoneConn;
533       aPolygoneConn.reserve(aNbElems*aNbQuadConn);
534
535       for(TInt iElem = 0; iElem < aNbElems && anIter->more(); iElem++){
536         const SMDS_MeshFace* anElem = anIter->next();
537         TInt aNbNodes = anElem->NbNodes();
538         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
539         TInt aNbConnectivity;
540         MED::TIntVector* anElemNums;
541         MED::TIntVector* aFamilyNums;
542         MED::TIntVector* aConnectivity;
543         if (anElem->IsPoly()) {
544           aNbConnectivity = aNbNodes;
545           anElemNums = &aPolygoneElemNums;
546           aFamilyNums = &aPolygoneFamilyNums;
547           aConnectivity = &aPolygoneConn;
548         }
549         else {
550           switch(aNbNodes){
551           case 3:
552             aNbConnectivity = aNbTriaConn;
553             anElemNums = &anTriaElemNums;
554             aFamilyNums = &aTriaFamilyNums;
555             aConnectivity = &aTriaConn;
556             break;
557           case 4:
558             aNbConnectivity = aNbQuadConn;
559             anElemNums = &aQuadElemNums;
560             aFamilyNums = &aQuadFamilyNums;
561             aConnectivity = &aQuadConn;
562             break;
563           case 6:
564             aNbConnectivity = aNbTria6Conn;
565             anElemNums = &anTria6ElemNums;
566             aFamilyNums = &aTria6FamilyNums;
567             aConnectivity = &aTria6Conn;
568             break;
569           case 8:
570             aNbConnectivity = aNbQuad8Conn;
571             anElemNums = &aQuad8ElemNums;
572             aFamilyNums = &aQuad8FamilyNums;
573             aConnectivity = &aQuad8Conn;
574             break;
575           default:
576             break;
577           }
578         }
579         MED::TIntVector aVector(aNbNodes);
580         for(TInt iNode = 0; aNodesIter->more(); iNode++){
581           const SMDS_MeshElement* aNode = aNodesIter->next();
582 #ifdef _EDF_NODE_IDS_
583           aVector[iNode] = aNodeIdMap[aNode->GetID()];
584 #else
585           aVector[iNode] = aNode->GetID();
586 #endif
587         }
588
589         TInt aSize = aConnectivity->size();
590         aConnectivity->resize(aSize+aNbConnectivity);
591         // There is some differences between SMDS and MED in cells mapping
592         switch(aNbNodes){
593         case 4:
594           (*aConnectivity)[aSize+0] = aVector[0];
595           (*aConnectivity)[aSize+1] = aVector[1];
596           (*aConnectivity)[aSize+2] = aVector[3];  
597           (*aConnectivity)[aSize+3] = aVector[2];  
598         default:
599           for(TInt iNode = 0; iNode < aNbNodes; iNode++) 
600             (*aConnectivity)[aSize+iNode] = aVector[iNode];
601         }
602
603         if (anElem->IsPoly()) {
604           // fill indices for polygonal element
605           TInt aPrevPos = aPolygoneInds.back();
606           aPolygoneInds.push_back(aPrevPos + aNbNodes);
607         }
608
609         anElemNums->push_back(anElem->GetID());
610
611         if (anElemFamMap.find(anElem) != anElemFamMap.end())
612           aFamilyNums->push_back(anElemFamMap[anElem]);
613         else
614           aFamilyNums->push_back(myFacesDefaultFamilyId);
615       }
616       if(TInt aNbElems = anTriaElemNums.size()){
617         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
618                                                 SMDS_MED_ENTITY,
619                                                 eTRIA3,
620                                                 SMDS_MED_CONNECTIVITY,
621                                                 aTriaConn,
622                                                 aTriaFamilyNums,
623                                                 anTriaElemNums);
624         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTRIA3<<"; aNbElems = "<<aNbElems);
625         myMed->SetCellInfo(aCellInfo);
626       }
627       if(TInt aNbElems = aQuadElemNums.size()){
628         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
629                                                 SMDS_MED_ENTITY,
630                                                 eQUAD4,
631                                                 SMDS_MED_CONNECTIVITY,
632                                                 aQuadConn,
633                                                 aQuadFamilyNums,
634                                                 aQuadElemNums);
635         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eQUAD4<<"; aNbElems = "<<aNbElems);
636         myMed->SetCellInfo(aCellInfo);
637       }
638       if(TInt aNbElems = anTria6ElemNums.size()){
639         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
640                                                 SMDS_MED_ENTITY,
641                                                 eTRIA6,
642                                                 SMDS_MED_CONNECTIVITY,
643                                                 aTria6Conn,
644                                                 aTria6FamilyNums,
645                                                 anTria6ElemNums);
646         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTRIA6<<"; aNbElems = "<<aNbElems);
647         myMed->SetCellInfo(aCellInfo);
648       }
649       if(TInt aNbElems = aQuad8ElemNums.size()){
650         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
651                                                 SMDS_MED_ENTITY,
652                                                 eQUAD8,
653                                                 SMDS_MED_CONNECTIVITY,
654                                                 aQuad8Conn,
655                                                 aQuad8FamilyNums,
656                                                 aQuad8ElemNums);
657         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eQUAD8<<"; aNbElems = "<<aNbElems);
658         myMed->SetCellInfo(aCellInfo);
659       }
660       if(TInt aNbElems = aPolygoneElemNums.size()){
661         // add one element in connectivities,
662         // referenced by the last element in indices
663         aPolygoneConn.push_back(0);
664
665         PPolygoneInfo aCellInfo = myMed->CrPolygoneInfo(aMeshInfo,
666                                                         SMDS_MED_ENTITY,
667                                                         ePOLYGONE,
668                                                         SMDS_MED_CONNECTIVITY,
669                                                         aPolygoneConn,
670                                                         aPolygoneInds,
671                                                         aPolygoneFamilyNums,
672                                                         aPolygoneElemNums);
673         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePOLYGONE<<"; aNbElems = "<<aNbElems);
674         myMed->SetPolygoneInfo(aCellInfo);
675       }
676     }
677
678     // Storing SMDS Volumes
679     if(TInt aNbElems = myMesh->NbVolumes()){
680       SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
681 #ifdef _ELEMENTS_BY_DIM_
682       SMDS_MED_ENTITY = eMAILLE;
683 #endif
684       TInt aNbTetraConn = MED::GetNbNodes(eTETRA4);
685       MED::TIntVector anTetraElemNums; 
686       anTetraElemNums.reserve(aNbElems);
687       MED::TIntVector aTetraFamilyNums;
688       aTetraFamilyNums.reserve(aNbElems);
689       MED::TIntVector aTetraConn;
690       aTetraConn.reserve(aNbElems*aNbTetraConn);
691
692       TInt aNbPyraConn = MED::GetNbNodes(ePYRA5);
693       MED::TIntVector anPyraElemNums; 
694       anPyraElemNums.reserve(aNbElems);
695       MED::TIntVector aPyraFamilyNums;
696       aPyraFamilyNums.reserve(aNbElems);
697       MED::TIntVector aPyraConn;
698       aPyraConn.reserve(aNbElems*aNbPyraConn);
699
700       TInt aNbPentaConn = MED::GetNbNodes(ePENTA6);
701       MED::TIntVector anPentaElemNums; 
702       anPentaElemNums.reserve(aNbElems);
703       MED::TIntVector aPentaFamilyNums;
704       aPentaFamilyNums.reserve(aNbElems);
705       MED::TIntVector aPentaConn;
706       aPentaConn.reserve(aNbElems*aNbPentaConn);
707
708       TInt aNbHexaConn = MED::GetNbNodes(eHEXA8);
709       MED::TIntVector aHexaElemNums;
710       aHexaElemNums.reserve(aNbElems);
711       MED::TIntVector aHexaFamilyNums;
712       aHexaFamilyNums.reserve(aNbElems);
713       MED::TIntVector aHexaConn;
714       aHexaConn.reserve(aNbElems*aNbHexaConn);
715
716       TInt aNbTetra10Conn = MED::GetNbNodes(eTETRA10);
717       MED::TIntVector anTetra10ElemNums; 
718       anTetra10ElemNums.reserve(aNbElems);
719       MED::TIntVector aTetra10FamilyNums;
720       aTetra10FamilyNums.reserve(aNbElems);
721       MED::TIntVector aTetra10Conn;
722       aTetra10Conn.reserve(aNbElems*aNbTetra10Conn);
723
724       TInt aNbPyra13Conn = MED::GetNbNodes(ePYRA13);
725       MED::TIntVector anPyra13ElemNums; 
726       anPyra13ElemNums.reserve(aNbElems);
727       MED::TIntVector aPyra13FamilyNums;
728       aPyra13FamilyNums.reserve(aNbElems);
729       MED::TIntVector aPyra13Conn;
730       aPyra13Conn.reserve(aNbElems*aNbPyra13Conn);
731
732       TInt aNbPenta15Conn = MED::GetNbNodes(ePENTA15);
733       MED::TIntVector anPenta15ElemNums; 
734       anPenta15ElemNums.reserve(aNbElems);
735       MED::TIntVector aPenta15FamilyNums;
736       aPenta15FamilyNums.reserve(aNbElems);
737       MED::TIntVector aPenta15Conn;
738       aPenta15Conn.reserve(aNbElems*aNbPenta15Conn);
739
740       TInt aNbHexa20Conn = MED::GetNbNodes(eHEXA20);
741       MED::TIntVector aHexa20ElemNums;
742       aHexa20ElemNums.reserve(aNbElems);
743       MED::TIntVector aHexa20FamilyNums;
744       aHexa20FamilyNums.reserve(aNbElems);
745       MED::TIntVector aHexa20Conn;
746       aHexa20Conn.reserve(aNbElems*aNbHexa20Conn);
747
748       MED::TIntVector aPolyedreElemNums;
749       aPolyedreElemNums.reserve(aNbElems);
750       MED::TIntVector aPolyedreInds;
751       aPolyedreInds.reserve(aNbElems + 1);
752       aPolyedreInds.push_back(1); // reference on the first element in the faces
753       MED::TIntVector aPolyedreFaces;
754       aPolyedreFaces.reserve(aNbElems + 1);
755       aPolyedreFaces.push_back(1); // reference on the first element in the connectivities
756       MED::TIntVector aPolyedreFamilyNums;
757       aPolyedreFamilyNums.reserve(aNbElems);
758       MED::TIntVector aPolyedreConn;
759       aPolyedreConn.reserve(aNbElems*aNbHexaConn);
760
761       for(TInt iElem = 0; iElem < aNbElems && anIter->more(); iElem++){
762         const SMDS_MeshVolume* anElem = anIter->next();
763
764         MED::TIntVector* anElemNums;
765         MED::TIntVector* aFamilyNums;
766
767         if (anElem->IsPoly()) {
768           const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
769             (const SMDS_PolyhedralVolumeOfNodes*) anElem;
770           if (!aPolyedre) {
771             MESSAGE("Warning: bad volumic element");
772             continue;
773           }
774
775           anElemNums = &aPolyedreElemNums;
776           aFamilyNums = &aPolyedreFamilyNums;
777
778           TInt aNodeId, aNbFaces = aPolyedre->NbFaces();
779           for (int iface = 1; iface <= aNbFaces; iface++) {
780             int aNbFaceNodes = aPolyedre->NbFaceNodes(iface);
781             for (int inode = 1; inode <= aNbFaceNodes; inode++) {
782               aNodeId = aPolyedre->GetFaceNode(iface, inode)->GetID();
783 #ifdef _EDF_NODE_IDS_
784               aPolyedreConn.push_back(aNodeIdMap[aNodeId]);
785 #else
786               aPolyedreConn.push_back(aNodeId);
787 #endif
788             }
789             TInt aPrevPos = aPolyedreFaces.back();
790             aPolyedreFaces.push_back(aPrevPos + aNbFaceNodes);
791           }
792           TInt aPrevPos = aPolyedreInds.back();
793           aPolyedreInds.push_back(aPrevPos + aNbFaces);
794
795         }
796         else {
797           TInt aNbNodes = anElem->NbNodes();
798           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
799           TInt aNbConnectivity;
800           MED::TIntVector* aConnectivity;
801           switch(aNbNodes){
802           case 4:
803             aNbConnectivity = aNbTetraConn;
804             anElemNums = &anTetraElemNums;
805             aFamilyNums = &aTetraFamilyNums;
806             aConnectivity = &aTetraConn;
807             break;
808           case 5:
809             aNbConnectivity = aNbPyraConn;
810             anElemNums = &anPyraElemNums;
811             aFamilyNums = &aPyraFamilyNums;
812             aConnectivity = &aPyraConn;
813             break;
814           case 6:
815             aNbConnectivity = aNbPentaConn;
816             anElemNums = &anPentaElemNums;
817             aFamilyNums = &aPentaFamilyNums;
818             aConnectivity = &aPentaConn;
819             break;
820           case 8:
821             aNbConnectivity = aNbHexaConn;
822             anElemNums = &aHexaElemNums;
823             aFamilyNums = &aHexaFamilyNums;
824             aConnectivity = &aHexaConn;
825           case 10:
826             aNbConnectivity = aNbTetra10Conn;
827             anElemNums = &anTetra10ElemNums;
828             aFamilyNums = &aTetra10FamilyNums;
829             aConnectivity = &aTetra10Conn;
830             break;
831           case 13:
832             aNbConnectivity = aNbPyra13Conn;
833             anElemNums = &anPyra13ElemNums;
834             aFamilyNums = &aPyra13FamilyNums;
835             aConnectivity = &aPyra13Conn;
836             break;
837           case 15:
838             aNbConnectivity = aNbPenta15Conn;
839             anElemNums = &anPenta15ElemNums;
840             aFamilyNums = &aPenta15FamilyNums;
841             aConnectivity = &aPenta15Conn;
842             break;
843           case 20:
844             aNbConnectivity = aNbHexa20Conn;
845             anElemNums = &aHexa20ElemNums;
846             aFamilyNums = &aHexa20FamilyNums;
847             aConnectivity = &aHexa20Conn;
848           }
849
850           TInt aSize = aConnectivity->size();
851           aConnectivity->resize(aSize + aNbConnectivity);
852
853           MED::TIntVector aVector(aNbNodes);
854           for(TInt iNode = 0; aNodesIter->more(); iNode++){
855             const SMDS_MeshElement* aNode = aNodesIter->next();
856 #ifdef _EDF_NODE_IDS_
857             aVector[iNode] = aNodeIdMap[aNode->GetID()];
858 #else
859             aVector[iNode] = aNode->GetID();
860 #endif
861           }
862           // There is some difference between SMDS and MED in cells mapping
863           switch(aNbNodes){
864           case 5:
865             (*aConnectivity)[aSize+0] = aVector[0];
866             (*aConnectivity)[aSize+1] = aVector[3];
867             (*aConnectivity)[aSize+2] = aVector[2];  
868             (*aConnectivity)[aSize+3] = aVector[1];  
869             (*aConnectivity)[aSize+4] = aVector[4];  
870           default:
871             for(TInt iNode = 0; iNode < aNbNodes; iNode++) 
872               (*aConnectivity)[aSize+iNode] = aVector[iNode];
873           }
874         }
875
876         anElemNums->push_back(anElem->GetID());
877
878         if (anElemFamMap.find(anElem) != anElemFamMap.end())
879           aFamilyNums->push_back(anElemFamMap[anElem]);
880         else
881           aFamilyNums->push_back(myVolumesDefaultFamilyId);
882       }
883
884       if(TInt aNbElems = anTetraElemNums.size()){
885         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
886                                                 SMDS_MED_ENTITY,
887                                                 eTETRA4,
888                                                 SMDS_MED_CONNECTIVITY,
889                                                 aTetraConn,
890                                                 aTetraFamilyNums,
891                                                 anTetraElemNums);
892         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTETRA4<<"; aNbElems = "<<aNbElems);
893         myMed->SetCellInfo(aCellInfo);
894       }
895       if(TInt aNbElems = anPyraElemNums.size()){
896         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
897                                                 SMDS_MED_ENTITY,
898                                                 ePYRA5,
899                                                 SMDS_MED_CONNECTIVITY,
900                                                 aPyraConn,
901                                                 aPyraFamilyNums,
902                                                 anPyraElemNums);
903         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePYRA5<<"; aNbElems = "<<aNbElems);
904         myMed->SetCellInfo(aCellInfo);
905       }
906       if(TInt aNbElems = anPentaElemNums.size()){
907         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
908                                                 SMDS_MED_ENTITY,
909                                                 ePENTA6,
910                                                 SMDS_MED_CONNECTIVITY,
911                                                 aPentaConn,
912                                                 aPentaFamilyNums,
913                                                 anPentaElemNums);
914         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePENTA6<<"; aNbElems = "<<aNbElems);
915         myMed->SetCellInfo(aCellInfo);
916       }
917       if(TInt aNbElems = aHexaElemNums.size()){
918         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
919                                                 SMDS_MED_ENTITY,
920                                                 eHEXA8,
921                                                 SMDS_MED_CONNECTIVITY,
922                                                 aHexaConn,
923                                                 aHexaFamilyNums,
924                                                 aHexaElemNums);
925         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eHEXA8<<"; aNbElems = "<<aNbElems);
926         myMed->SetCellInfo(aCellInfo);
927       }
928       if(TInt aNbElems = anTetra10ElemNums.size()){
929         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
930                                                 SMDS_MED_ENTITY,
931                                                 eTETRA10,
932                                                 SMDS_MED_CONNECTIVITY,
933                                                 aTetra10Conn,
934                                                 aTetra10FamilyNums,
935                                                 anTetra10ElemNums);
936         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTETRA10<<"; aNbElems = "<<aNbElems);
937         myMed->SetCellInfo(aCellInfo);
938       }
939       if(TInt aNbElems = anPyra13ElemNums.size()){
940         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
941                                                 SMDS_MED_ENTITY,
942                                                 ePYRA13,
943                                                 SMDS_MED_CONNECTIVITY,
944                                                 aPyra13Conn,
945                                                 aPyra13FamilyNums,
946                                                 anPyra13ElemNums);
947         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePYRA13<<"; aNbElems = "<<aNbElems);
948         myMed->SetCellInfo(aCellInfo);
949       }
950       if(TInt aNbElems = anPenta15ElemNums.size()){
951         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
952                                                 SMDS_MED_ENTITY,
953                                                 ePENTA15,
954                                                 SMDS_MED_CONNECTIVITY,
955                                                 aPenta15Conn,
956                                                 aPenta15FamilyNums,
957                                                 anPenta15ElemNums);
958         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePENTA15<<"; aNbElems = "<<aNbElems);
959         myMed->SetCellInfo(aCellInfo);
960       }
961       if(TInt aNbElems = aHexa20ElemNums.size()){
962         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
963                                                 SMDS_MED_ENTITY,
964                                                 eHEXA20,
965                                                 SMDS_MED_CONNECTIVITY,
966                                                 aHexa20Conn,
967                                                 aHexa20FamilyNums,
968                                                 aHexa20ElemNums);
969         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eHEXA20<<"; aNbElems = "<<aNbElems);
970         myMed->SetCellInfo(aCellInfo);
971       }
972
973       if(TInt aNbElems = aPolyedreElemNums.size()){
974         // add one element in connectivities,
975         // referenced by the last element in faces
976         aPolyedreConn.push_back(0);
977
978         PPolyedreInfo aCellInfo = myMed->CrPolyedreInfo(aMeshInfo,
979                                                         SMDS_MED_ENTITY,
980                                                         ePOLYEDRE,
981                                                         SMDS_MED_CONNECTIVITY,
982                                                         aPolyedreConn,
983                                                         aPolyedreFaces,
984                                                         aPolyedreInds,
985                                                         aPolyedreFamilyNums,
986                                                         aPolyedreElemNums);
987         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePOLYEDRE<<"; aNbElems = "<<aNbElems);
988         myMed->SetPolyedreInfo(aCellInfo);
989       }
990     }
991   }
992   catch(const std::exception& exc) {
993     INFOS("Follow exception was cought:\n\t"<<exc.what());
994   }
995   catch(...) {
996     INFOS("Unknown exception was cought !!!");
997   }
998
999   myMeshId = -1;
1000   myGroups.clear();
1001   mySubMeshes.clear();
1002   return aResult;
1003 }