Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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   TUnit aUnit[3] = {"m","m","m"};
115
116   TGetCoord aXYZGetCoord[3] = {
117     &SMDS_MeshNode::X, 
118     &SMDS_MeshNode::Y, 
119     &SMDS_MeshNode::Z
120   };
121   TName aXYZName[3] = {"x","y","z"};
122   
123   
124   TGetCoord aXYGetCoord[2] = {
125     &SMDS_MeshNode::X, 
126     &SMDS_MeshNode::Y
127   };
128   TName aXYName[2] = {"x","y"};
129
130   TGetCoord aYZGetCoord[2] = {
131     &SMDS_MeshNode::Y, 
132     &SMDS_MeshNode::Z
133   };
134   TName aYZName[2] = {"y","z"};
135
136   TGetCoord aXZGetCoord[2] = {
137     &SMDS_MeshNode::X, 
138     &SMDS_MeshNode::Z
139   };
140   TName aXZName[2] = {"x","z"};
141
142
143   TGetCoord aXGetCoord[1] = {
144     &SMDS_MeshNode::X
145   };
146   TName aXName[1] = {"x"};
147
148   TGetCoord aYGetCoord[1] = {
149     &SMDS_MeshNode::Y
150   };
151   TName aYName[1] = {"y"};
152
153   TGetCoord aZGetCoord[1] = {
154     &SMDS_MeshNode::Z
155   };
156   TName aZName[1] = {"z"};
157
158
159   class TCoordHelper{
160     SMDS_NodeIteratorPtr myNodeIter;
161     const SMDS_MeshNode* myCurrentNode;
162     TGetCoord* myGetCoord;
163     TName* myName;
164     TUnit* myUnit;
165   public:
166     TCoordHelper(const SMDS_NodeIteratorPtr& theNodeIter,
167                  TGetCoord* theGetCoord,
168                  TName* theName,
169                  TUnit* theUnit = aUnit):
170       myNodeIter(theNodeIter),
171       myGetCoord(theGetCoord),
172       myName(theName),
173       myUnit(theUnit)
174     {}
175     virtual ~TCoordHelper(){}
176     bool Next(){ 
177       return myNodeIter->more() && 
178         (myCurrentNode = myNodeIter->next());
179     }
180     const SMDS_MeshNode* GetNode(){
181       return myCurrentNode;
182     }
183     MED::TIntVector::value_type GetID(){
184       return myCurrentNode->GetID();
185     }
186     MED::TFloatVector::value_type GetCoord(TInt theCoodId){
187       return (myCurrentNode->*myGetCoord[theCoodId])();
188     }
189     MED::TStringVector::value_type GetName(TInt theDimId){
190       return myName[theDimId];
191     }
192     MED::TStringVector::value_type GetUnit(TInt theDimId){
193       return myUnit[theDimId];
194     }
195   };
196   typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
197   
198 }
199
200   
201 Driver_Mesh::Status DriverMED_W_SMESHDS_Mesh::Perform()
202 {
203   Status aResult = DRS_OK;
204   if (myMesh->hasConstructionEdges() || myMesh->hasConstructionFaces()) {
205     INFOS("SMDS_MESH with hasConstructionEdges() or hasConstructionFaces() do not supports!!!");
206     return DRS_FAIL;
207   }
208   try{
209     MESSAGE("Perform - myFile : "<<myFile);
210
211     // Creating the MED mesh for corresponding SMDS structure
212     //-------------------------------------------------------
213     string aMeshName;
214     if (myMeshId != -1) {
215       ostringstream aMeshNameStr;
216       aMeshNameStr<<myMeshId;
217       aMeshName = aMeshNameStr.str();
218     } else {
219       aMeshName = myMeshName;
220     }
221
222     // Mesh dimension definition
223     TInt aMeshDimension;
224     TCoordHelperPtr aCoordHelperPtr;
225     {  
226       bool anIsXDimension = false;
227       bool anIsYDimension = false;
228       bool anIsZDimension = false;
229       {
230         SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
231         double aBounds[6];
232         if(aNodesIter->more()){
233           const SMDS_MeshNode* aNode = aNodesIter->next();
234           aBounds[0] = aBounds[1] = aNode->X();
235           aBounds[2] = aBounds[3] = aNode->Y();
236           aBounds[4] = aBounds[5] = aNode->Z();
237         }
238         while(aNodesIter->more()){
239           const SMDS_MeshNode* aNode = aNodesIter->next();
240           aBounds[0] = min(aBounds[0],aNode->X());
241           aBounds[1] = max(aBounds[1],aNode->X());
242           
243           aBounds[2] = min(aBounds[2],aNode->Y());
244           aBounds[3] = max(aBounds[3],aNode->Y());
245           
246           aBounds[4] = min(aBounds[4],aNode->Z());
247           aBounds[5] = max(aBounds[5],aNode->Z());
248         }
249
250         double EPS = 1.0E-7;
251         anIsXDimension = (aBounds[1] - aBounds[0]) + abs(aBounds[1]) + abs(aBounds[0]) > EPS;
252         anIsYDimension = (aBounds[3] - aBounds[2]) + abs(aBounds[3]) + abs(aBounds[2]) > EPS;
253         anIsZDimension = (aBounds[5] - aBounds[4]) + abs(aBounds[5]) + abs(aBounds[4]) > EPS;
254         aMeshDimension = anIsXDimension + anIsYDimension + anIsZDimension;
255         if(!aMeshDimension)
256           aMeshDimension = 3;
257       }
258
259       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
260       switch(aMeshDimension){
261       case 3:
262         aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYZGetCoord,aXYZName));
263         break;
264       case 2:
265         if(anIsXDimension && anIsYDimension)
266           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXYGetCoord,aXYName));
267         if(anIsYDimension && anIsZDimension)
268           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYZGetCoord,aYZName));
269         if(anIsXDimension && anIsZDimension)
270           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXZGetCoord,aXZName));
271         break;
272       case 1:
273         if(anIsXDimension)
274           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aXGetCoord,aXName));
275         if(anIsYDimension)
276           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aYGetCoord,aYName));
277         if(anIsZDimension)
278           aCoordHelperPtr.reset(new TCoordHelper(aNodesIter,aZGetCoord,aZName));
279         break;
280       }
281     }
282
283     
284     PMeshInfo aMeshInfo = myMed->CrMeshInfo(aMeshDimension,aMeshName);
285     MESSAGE("Add - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
286     myMed->SetMeshInfo(aMeshInfo);
287
288     // Storing SMDS groups and sub-meshes
289     //-----------------------------------
290     int myNodesDefaultFamilyId = 0;
291     int myEdgesDefaultFamilyId = 0;
292     int myFacesDefaultFamilyId = 0;
293     int myVolumesDefaultFamilyId = 0;
294     if (myDoGroupOfNodes)
295       myNodesDefaultFamilyId = REST_NODES_FAMILY;
296     if (myDoGroupOfEdges)
297       myEdgesDefaultFamilyId = REST_EDGES_FAMILY;
298     if (myDoGroupOfFaces)
299       myFacesDefaultFamilyId = REST_FACES_FAMILY;
300     if (myDoGroupOfVolumes)
301       myVolumesDefaultFamilyId = REST_VOLUMES_FAMILY;
302
303     MESSAGE("Perform - aFamilyInfo");
304     map<const SMDS_MeshElement *, int> anElemFamMap;
305     list<DriverMED_FamilyPtr> aFamilies;
306     if (myAllSubMeshes) {
307       aFamilies = DriverMED_Family::MakeFamilies
308         (myMesh->SubMeshes(), myGroups,
309          myDoGroupOfNodes, myDoGroupOfEdges, myDoGroupOfFaces, myDoGroupOfVolumes);
310     } else {
311       aFamilies = DriverMED_Family::MakeFamilies
312         (mySubMeshes, myGroups,
313          myDoGroupOfNodes, myDoGroupOfEdges, myDoGroupOfFaces, myDoGroupOfVolumes);
314     }
315     list<DriverMED_FamilyPtr>::iterator aFamsIter = aFamilies.begin();
316
317     for (; aFamsIter != aFamilies.end(); aFamsIter++)
318     {
319       PFamilyInfo aFamilyInfo = (*aFamsIter)->GetFamilyInfo(myMed,aMeshInfo);
320       myMed->SetFamilyInfo(aFamilyInfo);
321       int aFamId = (*aFamsIter)->GetId();
322
323       const set<const SMDS_MeshElement *>& anElems = (*aFamsIter)->GetElements();
324       set<const SMDS_MeshElement *>::iterator anElemsIter = anElems.begin();
325       for (; anElemsIter != anElems.end(); anElemsIter++)
326       {
327         anElemFamMap[*anElemsIter] = aFamId;
328       }
329 //      delete (*aFamsIter);
330     }
331
332     // Storing SMDS nodes to the MED file for the MED mesh
333     //----------------------------------------------------
334 #ifdef _EDF_NODE_IDS_
335     typedef map<TInt,TInt> TNodeIdMap;
336     TNodeIdMap aNodeIdMap;
337 #endif
338     TInt aNbElems = myMesh->NbNodes();
339     MED::TIntVector anElemNums(aNbElems);
340     MED::TIntVector aFamilyNums(aNbElems);
341     MED::TFloatVector aCoordinates(aNbElems*aMeshDimension);
342     for(TInt iNode = 0, aStartId = 0; aCoordHelperPtr->Next(); iNode++, aStartId += aMeshDimension){
343       for(TInt iCoord = 0; iCoord < aMeshDimension; iCoord++){
344         aCoordinates[aStartId+iCoord] = aCoordHelperPtr->GetCoord(iCoord);
345       }
346       int aNodeID = aCoordHelperPtr->GetID();
347       anElemNums[iNode] = aNodeID;
348 #ifdef _EDF_NODE_IDS_
349       aNodeIdMap[aNodeID] = iNode+1;
350 #endif
351       const SMDS_MeshNode* aNode = aCoordHelperPtr->GetNode();
352       if (anElemFamMap.find(aNode) != anElemFamMap.end())
353         aFamilyNums[iNode] = anElemFamMap[aNode];
354       else
355         aFamilyNums[iNode] = myNodesDefaultFamilyId;
356     }
357
358     MED::TStringVector aCoordNames(aMeshDimension);
359     MED::TStringVector aCoordUnits(aMeshDimension);
360     for(TInt iCoord = 0; iCoord < aMeshDimension; iCoord++){
361       aCoordNames[iCoord] = aCoordHelperPtr->GetName(iCoord);
362       aCoordUnits[iCoord] = aCoordHelperPtr->GetUnit(iCoord);
363     }
364
365     const ERepere SMDS_COORDINATE_SYSTEM = eCART;
366
367     PNodeInfo aNodeInfo = myMed->CrNodeInfo(aMeshInfo,
368                                             SMDS_COORDINATE_SYSTEM,
369                                             aCoordinates,
370                                             aCoordNames,
371                                             aCoordUnits,
372                                             aFamilyNums,
373                                             anElemNums);
374     MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems);
375     myMed->SetNodeInfo(aNodeInfo);
376
377
378     // Storing others SMDS elements to the MED file for the MED mesh
379     //--------------------------------------------------------------
380     EEntiteMaillage SMDS_MED_ENTITY = eMAILLE;
381     const EConnectivite SMDS_MED_CONNECTIVITY = eNOD;
382
383     // Storing SMDS Edges
384     if(TInt aNbElems = myMesh->NbEdges()){
385 #ifdef _ELEMENTS_BY_DIM_
386       SMDS_MED_ENTITY = eARETE;
387 #endif
388       SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
389       TInt aNbConnectivity = MED::GetNbNodes(eSEG2);
390       MED::TIntVector anElemNums(aNbElems);
391       MED::TIntVector aFamilyNums(aNbElems);
392       MED::TIntVector aConnectivity(aNbElems*aNbConnectivity);
393
394       for(TInt iElem = 0, iConn = 0; anIter->more(); iElem++, iConn+=aNbConnectivity){
395         const SMDS_MeshEdge* anElem = anIter->next();
396         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
397         for(TInt iNode = 0; iNode < aNbConnectivity && aNodesIter->more(); iNode++){
398           const SMDS_MeshElement* aNode = aNodesIter->next();
399 #ifdef _EDF_NODE_IDS_
400           aConnectivity[iConn+iNode] = aNodeIdMap[aNode->GetID()];
401 #else
402           aConnectivity[iConn+iNode] = aNode->GetID();
403 #endif
404         }
405         anElemNums[iElem] = anElem->GetID();
406
407         if (anElemFamMap.find(anElem) != anElemFamMap.end())
408           aFamilyNums[iElem] = anElemFamMap[anElem];
409         else
410           aFamilyNums[iElem] = myEdgesDefaultFamilyId;
411       }
412       
413       PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
414                                               SMDS_MED_ENTITY,
415                                               eSEG2,
416                                               SMDS_MED_CONNECTIVITY,
417                                               aConnectivity,
418                                               aFamilyNums,
419                                               anElemNums);
420       myMed->SetCellInfo(aCellInfo);
421     }
422
423     // Storing SMDS Faces
424     if(TInt aNbElems = myMesh->NbFaces()){
425       SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
426 #ifdef _ELEMENTS_BY_DIM_
427       SMDS_MED_ENTITY = eFACE;
428 #endif
429       TInt aNbTriaConn = MED::GetNbNodes(eTRIA3);
430       MED::TIntVector anTriaElemNums; 
431       anTriaElemNums.reserve(aNbElems);
432       MED::TIntVector aTriaFamilyNums;
433       aTriaFamilyNums.reserve(aNbElems);
434       MED::TIntVector aTriaConn;
435       aTriaConn.reserve(aNbElems*aNbTriaConn);
436
437       TInt aNbQuadConn = MED::GetNbNodes(eQUAD4);
438       MED::TIntVector aQuadElemNums;
439       aQuadElemNums.reserve(aNbElems);
440       MED::TIntVector aQuadFamilyNums;
441       aQuadFamilyNums.reserve(aNbElems);
442       MED::TIntVector aQuadConn;
443       aQuadConn.reserve(aNbElems*aNbQuadConn);
444
445       MED::TIntVector aPolygoneElemNums;
446       aPolygoneElemNums.reserve(aNbElems);
447       MED::TIntVector aPolygoneInds;
448       aPolygoneInds.reserve(aNbElems + 1);
449       aPolygoneInds.push_back(1); // reference on the first element in the connectivities
450       MED::TIntVector aPolygoneFamilyNums;
451       aPolygoneFamilyNums.reserve(aNbElems);
452       MED::TIntVector aPolygoneConn;
453       aPolygoneConn.reserve(aNbElems*aNbQuadConn);
454
455       for(TInt iElem = 0; iElem < aNbElems && anIter->more(); iElem++){
456         const SMDS_MeshFace* anElem = anIter->next();
457         TInt aNbNodes = anElem->NbNodes();
458         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
459         TInt aNbConnectivity;
460         MED::TIntVector* anElemNums;
461         MED::TIntVector* aFamilyNums;
462         MED::TIntVector* aConnectivity;
463         if (anElem->IsPoly()) {
464           aNbConnectivity = aNbNodes;
465           anElemNums = &aPolygoneElemNums;
466           aFamilyNums = &aPolygoneFamilyNums;
467           aConnectivity = &aPolygoneConn;
468         } else {
469           switch(aNbNodes){
470           case 3:
471             aNbConnectivity = aNbTriaConn;
472             anElemNums = &anTriaElemNums;
473             aFamilyNums = &aTriaFamilyNums;
474             aConnectivity = &aTriaConn;
475             break;
476           case 4:
477             aNbConnectivity = aNbQuadConn;
478             anElemNums = &aQuadElemNums;
479             aFamilyNums = &aQuadFamilyNums;
480             aConnectivity = &aQuadConn;
481             break;
482           default:
483             break;
484           }
485         }
486         MED::TIntVector aVector(aNbNodes);
487         for(TInt iNode = 0; aNodesIter->more(); iNode++){
488           const SMDS_MeshElement* aNode = aNodesIter->next();
489 #ifdef _EDF_NODE_IDS_
490           aVector[iNode] = aNodeIdMap[aNode->GetID()];
491 #else
492           aVector[iNode] = aNode->GetID();
493 #endif
494         }
495
496         TInt aSize = aConnectivity->size();
497         aConnectivity->resize(aSize+aNbConnectivity);
498         // There is some differences between SMDS and MED in cells mapping
499         switch(aNbNodes){
500         case 4:
501           (*aConnectivity)[aSize+0] = aVector[0];
502           (*aConnectivity)[aSize+1] = aVector[1];
503           (*aConnectivity)[aSize+2] = aVector[3];  
504           (*aConnectivity)[aSize+3] = aVector[2];  
505         default:
506           for(TInt iNode = 0; iNode < aNbNodes; iNode++) 
507             (*aConnectivity)[aSize+iNode] = aVector[iNode];
508         }
509
510         if (anElem->IsPoly()) {
511           // fill indices for polygonal element
512           TInt aPrevPos = aPolygoneInds.back();
513           aPolygoneInds.push_back(aPrevPos + aNbNodes);
514         }
515
516         anElemNums->push_back(anElem->GetID());
517
518         if (anElemFamMap.find(anElem) != anElemFamMap.end())
519           aFamilyNums->push_back(anElemFamMap[anElem]);
520         else
521           aFamilyNums->push_back(myFacesDefaultFamilyId);
522       }
523       if(TInt aNbElems = anTriaElemNums.size()){
524         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
525                                                 SMDS_MED_ENTITY,
526                                                 eTRIA3,
527                                                 SMDS_MED_CONNECTIVITY,
528                                                 aTriaConn,
529                                                 aTriaFamilyNums,
530                                                 anTriaElemNums);
531         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTRIA3<<"; aNbElems = "<<aNbElems);
532         myMed->SetCellInfo(aCellInfo);
533       }
534       if(TInt aNbElems = aQuadElemNums.size()){
535         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
536                                                 SMDS_MED_ENTITY,
537                                                 eQUAD4,
538                                                 SMDS_MED_CONNECTIVITY,
539                                                 aQuadConn,
540                                                 aQuadFamilyNums,
541                                                 aQuadElemNums);
542         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eQUAD4<<"; aNbElems = "<<aNbElems);
543         myMed->SetCellInfo(aCellInfo);
544       }
545       if(TInt aNbElems = aPolygoneElemNums.size()){
546         // add one element in connectivities,
547         // referenced by the last element in indices
548         aPolygoneConn.push_back(0);
549
550         PPolygoneInfo aCellInfo = myMed->CrPolygoneInfo(aMeshInfo,
551                                                         SMDS_MED_ENTITY,
552                                                         ePOLYGONE,
553                                                         SMDS_MED_CONNECTIVITY,
554                                                         aPolygoneConn,
555                                                         aPolygoneInds,
556                                                         aPolygoneFamilyNums,
557                                                         aPolygoneElemNums);
558         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePOLYGONE<<"; aNbElems = "<<aNbElems);
559         myMed->SetPolygoneInfo(aCellInfo);
560       }
561     }
562
563     // Storing SMDS Volumes
564     if(TInt aNbElems = myMesh->NbVolumes()){
565       SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
566 #ifdef _ELEMENTS_BY_DIM_
567       SMDS_MED_ENTITY = eMAILLE;
568 #endif
569       TInt aNbTetraConn = MED::GetNbNodes(eTETRA4);
570       MED::TIntVector anTetraElemNums; 
571       anTetraElemNums.reserve(aNbElems);
572       MED::TIntVector aTetraFamilyNums;
573       aTetraFamilyNums.reserve(aNbElems);
574       MED::TIntVector aTetraConn;
575       aTetraConn.reserve(aNbElems*aNbTetraConn);
576
577       TInt aNbPyraConn = MED::GetNbNodes(ePYRA5);
578       MED::TIntVector anPyraElemNums; 
579       anPyraElemNums.reserve(aNbElems);
580       MED::TIntVector aPyraFamilyNums;
581       aPyraFamilyNums.reserve(aNbElems);
582       MED::TIntVector aPyraConn;
583       aPyraConn.reserve(aNbElems*aNbPyraConn);
584
585       TInt aNbPentaConn = MED::GetNbNodes(ePENTA6);
586       MED::TIntVector anPentaElemNums; 
587       anPentaElemNums.reserve(aNbElems);
588       MED::TIntVector aPentaFamilyNums;
589       aPentaFamilyNums.reserve(aNbElems);
590       MED::TIntVector aPentaConn;
591       aPentaConn.reserve(aNbElems*aNbPentaConn);
592
593       TInt aNbHexaConn = MED::GetNbNodes(eHEXA8);
594       MED::TIntVector aHexaElemNums;
595       aHexaElemNums.reserve(aNbElems);
596       MED::TIntVector aHexaFamilyNums;
597       aHexaFamilyNums.reserve(aNbElems);
598       MED::TIntVector aHexaConn;
599       aHexaConn.reserve(aNbElems*aNbHexaConn);
600
601       MED::TIntVector aPolyedreElemNums;
602       aPolyedreElemNums.reserve(aNbElems);
603       MED::TIntVector aPolyedreInds;
604       aPolyedreInds.reserve(aNbElems + 1);
605       aPolyedreInds.push_back(1); // reference on the first element in the faces
606       MED::TIntVector aPolyedreFaces;
607       aPolyedreFaces.reserve(aNbElems + 1);
608       aPolyedreFaces.push_back(1); // reference on the first element in the connectivities
609       MED::TIntVector aPolyedreFamilyNums;
610       aPolyedreFamilyNums.reserve(aNbElems);
611       MED::TIntVector aPolyedreConn;
612       aPolyedreConn.reserve(aNbElems*aNbHexaConn);
613
614       for(TInt iElem = 0; iElem < aNbElems && anIter->more(); iElem++){
615         const SMDS_MeshVolume* anElem = anIter->next();
616
617         MED::TIntVector* anElemNums;
618         MED::TIntVector* aFamilyNums;
619
620         if (anElem->IsPoly()) {
621           const SMDS_PolyhedralVolumeOfNodes* aPolyedre =
622             (const SMDS_PolyhedralVolumeOfNodes*) anElem;
623           if (!aPolyedre) {
624             MESSAGE("Warning: bad volumic element");
625             continue;
626           }
627
628           anElemNums = &aPolyedreElemNums;
629           aFamilyNums = &aPolyedreFamilyNums;
630
631           TInt aNodeId, aNbFaces = aPolyedre->NbFaces();
632           for (int iface = 1; iface <= aNbFaces; iface++) {
633             int aNbFaceNodes = aPolyedre->NbFaceNodes(iface);
634             for (int inode = 1; inode <= aNbFaceNodes; inode++) {
635               aNodeId = aPolyedre->GetFaceNode(iface, inode)->GetID();
636 #ifdef _EDF_NODE_IDS_
637               aPolyedreConn.push_back(aNodeIdMap[aNodeId]);
638 #else
639               aPolyedreConn.push_back(aNodeId);
640 #endif
641             }
642             TInt aPrevPos = aPolyedreFaces.back();
643             aPolyedreFaces.push_back(aPrevPos + aNbFaceNodes);
644           }
645           TInt aPrevPos = aPolyedreInds.back();
646           aPolyedreInds.push_back(aPrevPos + aNbFaces);
647
648         } else {
649           TInt aNbNodes = anElem->NbNodes();
650           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
651           TInt aNbConnectivity;
652           MED::TIntVector* aConnectivity;
653           switch(aNbNodes){
654           case 4:
655             aNbConnectivity = aNbTetraConn;
656             anElemNums = &anTetraElemNums;
657             aFamilyNums = &aTetraFamilyNums;
658             aConnectivity = &aTetraConn;
659             break;
660           case 5:
661             aNbConnectivity = aNbPyraConn;
662             anElemNums = &anPyraElemNums;
663             aFamilyNums = &aPyraFamilyNums;
664             aConnectivity = &aPyraConn;
665             break;
666           case 6:
667             aNbConnectivity = aNbPentaConn;
668             anElemNums = &anPentaElemNums;
669             aFamilyNums = &aPentaFamilyNums;
670             aConnectivity = &aPentaConn;
671             break;
672           case 8:
673             aNbConnectivity = aNbHexaConn;
674             anElemNums = &aHexaElemNums;
675             aFamilyNums = &aHexaFamilyNums;
676             aConnectivity = &aHexaConn;
677           }
678
679           TInt aSize = aConnectivity->size();
680           aConnectivity->resize(aSize + aNbConnectivity);
681
682           MED::TIntVector aVector(aNbNodes);
683           for(TInt iNode = 0; aNodesIter->more(); iNode++){
684             const SMDS_MeshElement* aNode = aNodesIter->next();
685 #ifdef _EDF_NODE_IDS_
686             aVector[iNode] = aNodeIdMap[aNode->GetID()];
687 #else
688             aVector[iNode] = aNode->GetID();
689 #endif
690           }
691           // There is some difference between SMDS and MED in cells mapping
692           switch(aNbNodes){
693           case 5:
694             (*aConnectivity)[aSize+0] = aVector[0];
695             (*aConnectivity)[aSize+1] = aVector[3];
696             (*aConnectivity)[aSize+2] = aVector[2];  
697             (*aConnectivity)[aSize+3] = aVector[1];  
698             (*aConnectivity)[aSize+4] = aVector[4];  
699           default:
700             for(TInt iNode = 0; iNode < aNbNodes; iNode++) 
701               (*aConnectivity)[aSize+iNode] = aVector[iNode];
702           }
703         }
704
705         anElemNums->push_back(anElem->GetID());
706
707         if (anElemFamMap.find(anElem) != anElemFamMap.end())
708           aFamilyNums->push_back(anElemFamMap[anElem]);
709         else
710           aFamilyNums->push_back(myVolumesDefaultFamilyId);
711       }
712
713       if(TInt aNbElems = anTetraElemNums.size()){
714         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
715                                                 SMDS_MED_ENTITY,
716                                                 eTETRA4,
717                                                 SMDS_MED_CONNECTIVITY,
718                                                 aTetraConn,
719                                                 aTetraFamilyNums,
720                                                 anTetraElemNums);
721         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eTETRA4<<"; aNbElems = "<<aNbElems);
722         myMed->SetCellInfo(aCellInfo);
723       }
724       if(TInt aNbElems = anPyraElemNums.size()){
725         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
726                                                 SMDS_MED_ENTITY,
727                                                 ePYRA5,
728                                                 SMDS_MED_CONNECTIVITY,
729                                                 aPyraConn,
730                                                 aPyraFamilyNums,
731                                                 anPyraElemNums);
732         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePYRA5<<"; aNbElems = "<<aNbElems);
733         myMed->SetCellInfo(aCellInfo);
734       }
735       if(TInt aNbElems = anPentaElemNums.size()){
736         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
737                                                 SMDS_MED_ENTITY,
738                                                 ePENTA6,
739                                                 SMDS_MED_CONNECTIVITY,
740                                                 aPentaConn,
741                                                 aPentaFamilyNums,
742                                                 anPentaElemNums);
743         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePENTA6<<"; aNbElems = "<<aNbElems);
744         myMed->SetCellInfo(aCellInfo);
745       }
746       if(TInt aNbElems = aHexaElemNums.size()){
747         PCellInfo aCellInfo = myMed->CrCellInfo(aMeshInfo,
748                                                 SMDS_MED_ENTITY,
749                                                 eHEXA8,
750                                                 SMDS_MED_CONNECTIVITY,
751                                                 aHexaConn,
752                                                 aHexaFamilyNums,
753                                                 aHexaElemNums);
754         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<eHEXA8<<"; aNbElems = "<<aNbElems);
755         myMed->SetCellInfo(aCellInfo);
756       }
757       if(TInt aNbElems = aPolyedreElemNums.size()){
758         // add one element in connectivities,
759         // referenced by the last element in faces
760         aPolyedreConn.push_back(0);
761
762         PPolyedreInfo aCellInfo = myMed->CrPolyedreInfo(aMeshInfo,
763                                                         SMDS_MED_ENTITY,
764                                                         ePOLYEDRE,
765                                                         SMDS_MED_CONNECTIVITY,
766                                                         aPolyedreConn,
767                                                         aPolyedreFaces,
768                                                         aPolyedreInds,
769                                                         aPolyedreFamilyNums,
770                                                         aPolyedreElemNums);
771         MESSAGE("Perform - anEntity = "<<SMDS_MED_ENTITY<<"; aGeom = "<<ePOLYEDRE<<"; aNbElems = "<<aNbElems);
772         myMed->SetPolyedreInfo(aCellInfo);
773       }
774     }
775   }catch(const std::exception& exc){
776     INFOS("Follow exception was cought:\n\t"<<exc.what());
777   }catch(...){
778     INFOS("Unknown exception was cought !!!");
779   }
780
781   myMeshId = -1;
782   myGroups.clear();
783   mySubMeshes.clear();
784   return aResult;
785 }