Salome HOME
b9d84175f20f51c3678f9d20543e28c07520eb69
[modules/smesh.git] / src / DriverMED / DriverMED_R_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_R_SMESHDS_Mesh.cxx
25 //  Module : SMESH
26
27 #include "DriverMED_R_SMESHDS_Mesh.h"
28 #include "DriverMED_R_SMDS_Mesh.h"
29 #include "SMESHDS_Mesh.hxx"
30 #include "utilities.h"
31
32 #include "DriverMED_Family.h"
33
34 #include "SMESHDS_Group.hxx"
35
36 #include "MED_Factory.hxx"
37 #include "MED_Utilities.hxx"
38
39 #include <stdlib.h>
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 #define _EDF_NODE_IDS_
48
49 using namespace MED;
50
51 void DriverMED_R_SMESHDS_Mesh::SetMeshName(string theMeshName)
52 {
53   myMeshName = theMeshName;
54 }
55
56 static const SMDS_MeshNode* 
57 FindNode(const SMDS_Mesh* theMesh, TInt theId){
58   const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
59   if(aNode) return aNode;
60   EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
61 }
62
63
64 enum ECoordName{eX, eY, eZ, eNone};
65 typedef TFloat (*TGetCoord)(MED::PNodeInfo&, TInt);
66
67 template<ECoordName TheCoordId>
68 TFloat GetCoord(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
69   return thePNodeInfo->GetNodeCoord(theElemId,TheCoordId);
70 }
71
72 template<>
73 TFloat GetCoord<eNone>(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
74   return 0.0;
75 }
76
77
78 static TGetCoord aXYZGetCoord[3] = {
79   &GetCoord<eX>, 
80   &GetCoord<eY>, 
81   &GetCoord<eZ>
82 };
83
84
85 static TGetCoord aXYGetCoord[3] = {
86   &GetCoord<eX>, 
87   &GetCoord<eY>, 
88   &GetCoord<eNone>
89 };
90
91 static TGetCoord aYZGetCoord[3] = {
92   &GetCoord<eNone>,
93   &GetCoord<eX>, 
94   &GetCoord<eY>
95 };
96
97 static TGetCoord aXZGetCoord[3] = {
98   &GetCoord<eX>, 
99   &GetCoord<eNone>,
100   &GetCoord<eY>
101 };
102
103
104 static TGetCoord aXGetCoord[3] = {
105   &GetCoord<eX>, 
106   &GetCoord<eNone>,
107   &GetCoord<eNone>
108 };
109
110 static TGetCoord aYGetCoord[3] = {
111   &GetCoord<eNone>,
112   &GetCoord<eX>, 
113   &GetCoord<eNone>
114 };
115
116 static TGetCoord aZGetCoord[3] = {
117   &GetCoord<eNone>,
118   &GetCoord<eNone>,
119   &GetCoord<eX>
120 };
121
122
123 class TCoordHelper{
124   MED::PNodeInfo myPNodeInfo;
125   TGetCoord* myGetCoord;
126 public:
127   TCoordHelper(const MED::PNodeInfo& thePNodeInfo,
128                TGetCoord* theGetCoord):
129     myPNodeInfo(thePNodeInfo),
130     myGetCoord(theGetCoord)
131   {}
132   virtual ~TCoordHelper(){}
133   TFloat GetCoord(TInt theElemId, TInt theCoodId){
134     return (*myGetCoord[theCoodId])(myPNodeInfo,theElemId);
135   }
136 };
137 typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
138
139
140 Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
141 {
142   Status aResult = DRS_FAIL;
143   try{
144     myFamilies.clear();
145     if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
146     PWrapper aMed = CrWrapper(myFile);
147
148     aResult = DRS_EMPTY;
149     if(TInt aNbMeshes = aMed->GetNbMeshes()){
150       for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
151         // Reading the MED mesh
152         //---------------------
153         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
154         string aMeshName;
155         if (myMeshId != -1) {
156           ostringstream aMeshNameStr;
157           aMeshNameStr<<myMeshId;
158           aMeshName = aMeshNameStr.str();
159         } else {
160           aMeshName = myMeshName;
161         }
162         if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
163         if(aMeshName != aMeshInfo->GetName()) continue;
164         aResult = DRS_OK;
165         //TInt aMeshDim = aMeshInfo->GetDim();
166         
167         // Reading MED families to the temporary structure
168         //------------------------------------------------
169         TErr anErr;
170         TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
171         if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
172         for (TInt iFam = 0; iFam < aNbFams; iFam++) {
173           PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
174           if(anErr >= 0){
175             TInt aFamId = aFamilyInfo->GetId();
176             if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
177             
178             DriverMED_FamilyPtr aFamily (new DriverMED_Family);
179             
180             TInt aNbGrp = aFamilyInfo->GetNbGroup();
181             if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
182             for (TInt iGr = 0; iGr < aNbGrp; iGr++) {
183               string aGroupName = aFamilyInfo->GetGroupName(iGr);
184               if(MYDEBUG) MESSAGE(aGroupName);
185               aFamily->AddGroupName(aGroupName);
186             }
187             aFamily->SetId( aFamId );
188             myFamilies[aFamId] = aFamily;
189           }
190         }
191
192         // Reading MED nodes to the corresponding SMDS structure
193         //------------------------------------------------------
194         PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
195
196         TCoordHelperPtr aCoordHelperPtr;
197         {
198           TInt aMeshDimension = aMeshInfo->GetDim();
199           bool anIsDimPresent[3] = {false, false, false};
200           for(TInt iDim = 0; iDim < aMeshDimension; iDim++){
201             string aDimName = aNodeInfo->GetCoordName(iDim);
202             if(aDimName == "x" || aDimName == "X")
203               anIsDimPresent[eX] = true;
204             else if(aDimName == "y" || aDimName == "Y")
205               anIsDimPresent[eY] = true;
206             else if(aDimName == "z" || aDimName == "Z")
207               anIsDimPresent[eZ] = true;
208           }
209           switch(aMeshDimension){
210           case 3:
211             aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
212             break;
213           case 2:
214             if(anIsDimPresent[eY] && anIsDimPresent[eZ])
215               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
216             else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
217               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
218             else
219               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
220             break;
221           case 1:
222             if(anIsDimPresent[eY])
223               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
224             else if(anIsDimPresent[eZ])
225               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
226             else
227               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
228             break;
229           }
230         }
231
232         EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
233         TInt aNbElems = aNodeInfo->GetNbElem();
234         if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
235         DriverMED_FamilyPtr aFamily;
236         for(TInt iElem = 0; iElem < aNbElems; iElem++){
237           double aCoords[3] = {0.0, 0.0, 0.0};
238           for(TInt iDim = 0; iDim < 3; iDim++)
239             aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
240           const SMDS_MeshNode* aNode;
241           if(anIsNodeNum) {
242             aNode = myMesh->AddNodeWithID
243               (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
244           } else {
245             aNode = myMesh->AddNode
246               (aCoords[0],aCoords[1],aCoords[2]);
247           }
248           //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
249
250           // Save reference to this node from its family
251           TInt aFamNum = aNodeInfo->GetFamNum(iElem);
252           if ( checkFamilyID ( aFamily, aFamNum ))
253           {
254             aFamily->AddElement(aNode);
255             aFamily->SetType(SMDSAbs_Node);
256           }
257         }
258
259         // Reading pre information about all MED cells
260         //--------------------------------------------
261         bool takeNumbers = true;  // initially we trust the numbers from file
262         MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
263         MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
264         for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
265           const EEntiteMaillage& anEntity = anEntityIter->first;
266           if(anEntity == eNOEUD) continue;
267           // Reading MED cells to the corresponding SMDS structure
268           //------------------------------------------------------
269           const MED::TGeom& aTGeom = anEntityIter->second;
270           MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
271           for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
272             const EGeometrieElement& aGeom = anTGeomIter->first;
273
274             if (aGeom == ePOINT1) {
275               continue;
276
277             } else if (aGeom == ePOLYGONE) {
278               PPolygoneInfo aPolygoneInfo = aMed->GetPPolygoneInfo(aMeshInfo,anEntity,aGeom);
279               EBooleen anIsElemNum = takeNumbers ? aPolygoneInfo->IsElemNum() : eFAUX;
280
281               TElemNum aConn  = aPolygoneInfo->GetConnectivite();
282               TElemNum aIndex = aPolygoneInfo->GetIndex();
283
284               TInt nbPolygons = aPolygoneInfo->GetNbElem();
285
286               for (TInt iPG = 0; iPG < nbPolygons; iPG++) {
287                 // get nodes
288                 TInt aCurrPG_FirstNodeIndex = aIndex[iPG] - 1;
289                 int nbNodes = aPolygoneInfo->GetNbConn(iPG);
290                 std::vector<int> nodes_ids (nbNodes);
291                 //for (TInt inode = 0; inode < nbNodes; inode++) {
292                 //  nodes_ids[inode] = aConn[aCurrPG_FirstNodeIndex + inode];
293                 //}
294 #ifdef _EDF_NODE_IDS_
295                 if (anIsNodeNum) {
296                   for (TInt inode = 0; inode < nbNodes; inode++) {
297                     nodes_ids[inode] = aNodeInfo->GetElemNum(aConn[aCurrPG_FirstNodeIndex + inode] - 1);
298                   }
299                 } else {
300                   for (TInt inode = 0; inode < nbNodes; inode++) {
301                     nodes_ids[inode] = aConn[aCurrPG_FirstNodeIndex + inode];
302                   }
303                 }
304 #else
305                 for (TInt inode = 0; inode < nbNodes; inode++) {
306                   nodes_ids[inode] = aConn[aCurrPG_FirstNodeIndex + inode];
307                 }
308 #endif
309
310                 bool isRenum = false;
311                 SMDS_MeshElement* anElement = NULL;
312                 TInt aFamNum = aPolygoneInfo->GetFamNum(iPG);
313
314                 try {
315                   if (anIsElemNum) {
316                     anElement = myMesh->AddPolygonalFaceWithID
317                       (nodes_ids, aPolygoneInfo->GetElemNum(iPG));
318                   }
319                   if (!anElement) {
320                     std::vector<const SMDS_MeshNode*> nodes (nbNodes);
321                     for (int inode = 0; inode < nbNodes; inode++) {
322                       nodes[inode] = FindNode(myMesh, nodes_ids[inode]);
323                     }
324                     anElement = myMesh->AddPolygonalFace(nodes);
325                     isRenum = anIsElemNum;
326                   }
327                 } catch (const std::exception& exc) {
328                   aResult = DRS_FAIL;
329                 } catch (...) {
330                   aResult = DRS_FAIL;
331                 }
332
333                 if (!anElement) {
334                   aResult = DRS_WARN_SKIP_ELEM;
335                 } else {
336                   if (isRenum) {
337                     anIsElemNum = eFAUX;
338                     takeNumbers = false;
339                     if (aResult < DRS_WARN_RENUMBER)
340                       aResult = DRS_WARN_RENUMBER;
341                   }
342                   if ( checkFamilyID ( aFamily, aFamNum ))
343                   {
344                     // Save reference to this element from its family
345                     aFamily->AddElement(anElement);
346                     aFamily->SetType(anElement->GetType());
347                   }
348                 }
349               } // for (TInt iPG = 0; iPG < nbPolygons; iPG++)
350               continue;
351
352             } else if (aGeom == ePOLYEDRE) {
353               PPolyedreInfo aPolyedreInfo = aMed->GetPPolyedreInfo(aMeshInfo,anEntity,aGeom);
354               EBooleen anIsElemNum = takeNumbers ? aPolyedreInfo->IsElemNum() : eFAUX;
355
356               TElemNum aConn       = aPolyedreInfo->GetConnectivite();
357               TElemNum aFacesIndex = aPolyedreInfo->GetFacesIndex();
358               TElemNum aIndex      = aPolyedreInfo->GetIndex();
359
360               TInt nbPolyedres = aPolyedreInfo->GetNbElem();
361
362               for (int iPE = 0; iPE < nbPolyedres; iPE++) {
363                 // get faces
364                 int aCurrPE_FirstFaceIndex = aIndex[iPE] - 1;
365                 int aNextPE_FirstFaceIndex = aIndex[iPE + 1] - 1;
366                 int nbFaces = aNextPE_FirstFaceIndex - aCurrPE_FirstFaceIndex;
367                 std::vector<int> quantities (nbFaces);
368                 for (int iFa = 0; iFa < nbFaces; iFa++) {
369                   int aCurrFace_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex + iFa] - 1;
370                   int aNextFace_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex + iFa + 1] - 1;
371
372                   int nbNodes = aNextFace_FirstNodeIndex - aCurrFace_FirstNodeIndex;
373                   quantities[iFa] = nbNodes;
374                 }
375
376                 // get nodes
377                 int aCurrPE_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex] - 1;
378                 int nbPENodes = aPolyedreInfo->GetNbConn(iPE);
379                 std::vector<int> nodes_ids (nbPENodes);
380                 //for (int inode = 0; inode < nbPENodes; inode++) {
381                 //  nodes_ids[inode] = aConn[aCurrPE_FirstNodeIndex + inode];
382                 //}
383 #ifdef _EDF_NODE_IDS_
384                 if (anIsNodeNum) {
385                   for (int inode = 0; inode < nbPENodes; inode++) {
386                     nodes_ids[inode] = aNodeInfo->GetElemNum(aConn[aCurrPE_FirstNodeIndex + inode] - 1);
387                   }
388                 } else {
389                   for (int inode = 0; inode < nbPENodes; inode++) {
390                     nodes_ids[inode] = aConn[aCurrPE_FirstNodeIndex + inode];
391                   }
392                 }
393 #else
394                 for (int inode = 0; inode < nbPENodes; inode++) {
395                   nodes_ids[inode] = aConn[aCurrPE_FirstNodeIndex + inode];
396                 }
397 #endif
398
399                 bool isRenum = false;
400                 SMDS_MeshElement* anElement = NULL;
401                 TInt aFamNum = aPolyedreInfo->GetFamNum(iPE);
402
403                 try {
404                   if (anIsElemNum) {
405                     anElement = myMesh->AddPolyhedralVolumeWithID
406                       (nodes_ids, quantities, aPolyedreInfo->GetElemNum(iPE));
407                   }
408                   if (!anElement) {
409                     std::vector<const SMDS_MeshNode*> nodes (nbPENodes);
410                     for (int inode = 0; inode < nbPENodes; inode++) {
411                       nodes[inode] = FindNode(myMesh, nodes_ids[inode]);
412                     }
413                     anElement = myMesh->AddPolyhedralVolume(nodes, quantities);
414                     isRenum = anIsElemNum;
415                   }
416                 } catch (const std::exception& exc) {
417                   aResult = DRS_FAIL;
418                 } catch (...) {
419                   aResult = DRS_FAIL;
420                 }
421
422                 if (!anElement) {
423                   aResult = DRS_WARN_SKIP_ELEM;
424                 } else {
425                   if (isRenum) {
426                     anIsElemNum = eFAUX;
427                     takeNumbers = false;
428                     if (aResult < DRS_WARN_RENUMBER)
429                       aResult = DRS_WARN_RENUMBER;
430                   }
431                   if ( checkFamilyID ( aFamily, aFamNum ))
432                   {
433                     // Save reference to this element from its family
434                     aFamily->AddElement(anElement);
435                     aFamily->SetType(anElement->GetType());
436                   }
437                 }
438               } // for (int iPE = 0; iPE < nbPolyedres; iPE++)
439               continue;
440
441             } else {
442             }
443
444             PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
445             EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
446             TInt aNbElems = aCellInfo->GetNbElem();
447             if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
448             if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
449
450             for(int iElem = 0; iElem < aNbElems; iElem++){
451               TInt aNbNodes = -1;
452               switch(aGeom){
453               case eSEG2:
454               case eSEG3:
455                 aNbNodes = 2;
456                 break;
457               case eTRIA3:
458               case eTRIA6:
459                 aNbNodes = 3;
460                 break;
461                 break;
462               case eQUAD4:
463               case eQUAD8:
464                 aNbNodes = 4;
465                 break;
466               case eTETRA4:
467               case eTETRA10:
468                 aNbNodes = 4;
469                 break;
470               case ePYRA5:
471               case ePYRA13:
472                 aNbNodes = 5;
473                 break;
474               case ePENTA6:
475               case ePENTA15:
476                 aNbNodes = 6;
477                 break;
478               case eHEXA8:
479               case eHEXA20:
480                 aNbNodes = 8;
481                 break;
482               }
483               vector<TInt> aNodeIds(aNbNodes);
484               bool anIsValidConnect = false;
485
486               try{
487 #ifdef _EDF_NODE_IDS_
488                 if(anIsNodeNum) {
489                   for(int i = 0; i < aNbNodes; i++){
490                     aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
491                   }
492                 }else{
493                   for(int i = 0; i < aNbNodes; i++){
494                     aNodeIds[i] = aCellInfo->GetConn(iElem,i);
495                   }
496                 }
497 #else
498                 for(int i = 0; i < aNbNodes; i++){
499                   aNodeIds[i] = aCellInfo->GetConn(iElem,i);
500                 }
501 #endif
502                 anIsValidConnect = true;
503               }catch(const std::exception& exc){
504                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
505                 aResult = DRS_FAIL;
506               }catch(...){
507                 //INFOS("Unknown exception was cought !!!");
508                 aResult = DRS_FAIL;
509               }
510               
511               if(!anIsValidConnect)
512                 continue;
513
514               bool isRenum = false;
515               SMDS_MeshElement* anElement = NULL;
516               TInt aFamNum = aCellInfo->GetFamNum(iElem);
517               try{
518                 //MESSAGE("Try to create element # " << iElem << " with id = "
519                 //        << aCellInfo->GetElemNum(iElem));
520                 switch(aGeom){
521                 case eSEG2:
522                 case eSEG3:
523                   if(anIsElemNum)
524                     anElement = myMesh->AddEdgeWithID(aNodeIds[0],
525                                                       aNodeIds[1],
526                                                       aCellInfo->GetElemNum(iElem));
527                   if (!anElement) {
528                     anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
529                                                 FindNode(myMesh,aNodeIds[1]));
530                     isRenum = anIsElemNum;
531                   }
532                   break;
533                 case eTRIA3:
534                 case eTRIA6:
535                   aNbNodes = 3;
536                   if(anIsElemNum)
537                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
538                                                       aNodeIds[1],
539                                                       aNodeIds[2],
540                                                       aCellInfo->GetElemNum(iElem));
541                   if (!anElement) {
542                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
543                                                 FindNode(myMesh,aNodeIds[1]),
544                                                 FindNode(myMesh,aNodeIds[2]));
545                     isRenum = anIsElemNum;
546                   }
547                   break;
548                 case eQUAD4:
549                 case eQUAD8:
550                   aNbNodes = 4;
551                   // There is some differnce between SMDS and MED
552                   if(anIsElemNum)
553                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
554                                                       aNodeIds[1],
555                                                       aNodeIds[2],
556                                                       aNodeIds[3],
557                                                       aCellInfo->GetElemNum(iElem));
558                   if (!anElement) {
559                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
560                                                 FindNode(myMesh,aNodeIds[1]),
561                                                 FindNode(myMesh,aNodeIds[2]),
562                                                 FindNode(myMesh,aNodeIds[3]));
563                     isRenum = anIsElemNum;
564                   }
565                   break;
566                 case eTETRA4:
567                 case eTETRA10:
568                   aNbNodes = 4;
569                   if(anIsElemNum)
570                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
571                                                         aNodeIds[1],
572                                                         aNodeIds[2],
573                                                         aNodeIds[3],
574                                                         aCellInfo->GetElemNum(iElem));
575                   if (!anElement) {
576                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
577                                                   FindNode(myMesh,aNodeIds[1]),
578                                                   FindNode(myMesh,aNodeIds[2]),
579                                                   FindNode(myMesh,aNodeIds[3]));
580                     isRenum = anIsElemNum;
581                   }
582                   break;
583                 case ePYRA5:
584                 case ePYRA13:
585                   aNbNodes = 5;
586                   // There is some differnce between SMDS and MED
587                   if(anIsElemNum)
588                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
589                                                         aNodeIds[1],
590                                                         aNodeIds[2],
591                                                         aNodeIds[3],
592                                                         aNodeIds[4],
593                                                         aCellInfo->GetElemNum(iElem));
594                   if (!anElement) {
595                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
596                                                   FindNode(myMesh,aNodeIds[1]),
597                                                   FindNode(myMesh,aNodeIds[2]),
598                                                   FindNode(myMesh,aNodeIds[3]),
599                                                   FindNode(myMesh,aNodeIds[4]));
600                     isRenum = anIsElemNum;
601                   }
602                   break;
603                 case ePENTA6:
604                 case ePENTA15:
605                   aNbNodes = 6;
606                   if(anIsElemNum)
607                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
608                                                         aNodeIds[1],
609                                                         aNodeIds[2],
610                                                         aNodeIds[3],
611                                                         aNodeIds[4],
612                                                         aNodeIds[5],
613                                                         aCellInfo->GetElemNum(iElem));
614                   if (!anElement) {
615                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
616                                                   FindNode(myMesh,aNodeIds[1]),
617                                                   FindNode(myMesh,aNodeIds[2]),
618                                                   FindNode(myMesh,aNodeIds[3]),
619                                                   FindNode(myMesh,aNodeIds[4]),
620                                                   FindNode(myMesh,aNodeIds[5]));
621                     isRenum = anIsElemNum;
622                   }
623                   break;
624                 case eHEXA8:
625                 case eHEXA20:
626                   aNbNodes = 8;
627                   if(anIsElemNum)
628                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
629                                                         aNodeIds[1],
630                                                         aNodeIds[2],
631                                                         aNodeIds[3],
632                                                         aNodeIds[4],
633                                                         aNodeIds[5],
634                                                         aNodeIds[6],
635                                                         aNodeIds[7],
636                                                         aCellInfo->GetElemNum(iElem));
637                   if (!anElement) {
638                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
639                                                   FindNode(myMesh,aNodeIds[1]),
640                                                   FindNode(myMesh,aNodeIds[2]),
641                                                   FindNode(myMesh,aNodeIds[3]),
642                                                   FindNode(myMesh,aNodeIds[4]),
643                                                   FindNode(myMesh,aNodeIds[5]),
644                                                   FindNode(myMesh,aNodeIds[6]),
645                                                   FindNode(myMesh,aNodeIds[7]));
646                     isRenum = anIsElemNum;
647                   }
648                   break;
649                 }
650               }catch(const std::exception& exc){
651                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
652                 aResult = DRS_FAIL;
653               }catch(...){
654                 //INFOS("Unknown exception was cought !!!");
655                 aResult = DRS_FAIL;
656               }
657                 
658               if (!anElement) {
659                 aResult = DRS_WARN_SKIP_ELEM;
660               }
661               else {
662                 if (isRenum) {
663                   anIsElemNum = eFAUX;
664                   takeNumbers = false;
665                   if (aResult < DRS_WARN_RENUMBER)
666                     aResult = DRS_WARN_RENUMBER;
667                 }
668                 if ( checkFamilyID ( aFamily, aFamNum ))
669                 {
670                   // Save reference to this element from its family
671                   aFamily->AddElement(anElement);
672                   aFamily->SetType(anElement->GetType());
673                 }
674               }
675             }
676           }
677         }
678         break;
679       }
680     }
681   }catch(const std::exception& exc){
682     INFOS("Follow exception was cought:\n\t"<<exc.what());
683     aResult = DRS_FAIL;
684   }catch(...){
685     INFOS("Unknown exception was cought !!!");
686     aResult = DRS_FAIL;
687   }
688   if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
689   return aResult;
690 }
691
692 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
693 {
694   list<string> aMeshNames;
695
696   try {
697     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
698     theStatus = DRS_OK;
699     PWrapper aMed = CrWrapper(myFile);
700
701     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
702       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
703         // Reading the MED mesh
704         //---------------------
705         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
706         aMeshNames.push_back(aMeshInfo->GetName());
707       }
708     }
709   }catch(const std::exception& exc){
710     INFOS("Follow exception was cought:\n\t"<<exc.what());
711     theStatus = DRS_FAIL;
712   }catch(...){
713     INFOS("Unknown exception was cought !!!");
714     theStatus = DRS_FAIL;
715   }
716
717   return aMeshNames;
718 }
719
720 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
721 {
722   list<TNameAndType> aResult;
723   set<TNameAndType> aResGroupNames;
724
725   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
726   for (; aFamsIter != myFamilies.end(); aFamsIter++)
727   {
728     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
729     const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
730     set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
731     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
732     {
733       TNameAndType aNameAndType = make_pair( *aGrNamesIter, aFamily->GetType() );
734       // Check, if this is a Group or SubMesh name
735 //if (aName.substr(0, 5) == string("Group")) {
736         if ( aResGroupNames.insert( aNameAndType ).second ) {
737           aResult.push_back( aNameAndType );
738         }
739 //    }
740     }
741   }
742
743   return aResult;
744 }
745
746 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
747 {
748   string aGroupName (theGroup->GetStoreName());
749   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
750
751   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
752   for (; aFamsIter != myFamilies.end(); aFamsIter++)
753   {
754     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
755     if (aFamily->GetType() == theGroup->GetType() && aFamily->MemberOf(aGroupName))
756     {
757       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
758       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
759       const SMDS_MeshElement * element = 0;
760       for (; anElemsIter != anElements.end(); anElemsIter++)
761       {
762         element = *anElemsIter;
763         theGroup->SMDSGroup().Add(element);
764       }
765       if ( element )
766         theGroup->SetType( element->GetType() );
767     }
768   }
769 }
770
771 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
772                                            const int theId)
773 {
774   char submeshGrpName[ 30 ];
775   sprintf( submeshGrpName, "SubMesh %d", theId );
776   string aName (submeshGrpName);
777   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
778   for (; aFamsIter != myFamilies.end(); aFamsIter++)
779   {
780     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
781     if (aFamily->MemberOf(aName))
782     {
783       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
784       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
785       if (aFamily->GetType() == SMDSAbs_Node)
786       {
787         for (; anElemsIter != anElements.end(); anElemsIter++)
788         {
789           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
790           theSubMesh->AddNode(node);
791         }
792       }
793       else
794       {
795         for (; anElemsIter != anElements.end(); anElemsIter++)
796         {
797           theSubMesh->AddElement(*anElemsIter);
798         }
799       }
800     }
801   }
802 }
803
804 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
805 {
806   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
807   for (; aFamsIter != myFamilies.end(); aFamsIter++)
808   {
809     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
810     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
811     set<string>::iterator aGrNamesIter = aGroupNames.begin();
812     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
813     {
814       string aName = *aGrNamesIter;
815       // Check, if this is a Group or SubMesh name
816       if (aName.substr(0, 7) == string("SubMesh"))
817       {
818         int Id = atoi(string(aName).substr(7).c_str());
819         set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
820         set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
821         if (aFamily->GetType() == SMDSAbs_Node)
822         {
823           for (; anElemsIter != anElements.end(); anElemsIter++)
824           {
825             SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
826               ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
827             // find out a shape type
828             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
829             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
830             switch ( aShapeType ) {
831             case TopAbs_FACE:
832               myMesh->SetNodeOnFace(node, Id); break;
833             case TopAbs_EDGE:
834               myMesh->SetNodeOnEdge(node, Id); break;
835             case TopAbs_VERTEX:
836               myMesh->SetNodeOnVertex(node, Id); break;
837             default:
838               myMesh->SetNodeInVolume(node, Id);
839             }
840           }
841         }
842         else
843         {
844           for (; anElemsIter != anElements.end(); anElemsIter++)
845           {
846             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
847           }
848         }
849       }
850     }
851   }
852 }
853 /*!
854  * \brief Ensure aFamily to have required ID
855  * \param aFamily - a family to check and update
856  * \param anID - an ID aFamily should have
857  * \retval bool  - true if successful
858  */
859 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
860 {
861   if ( !aFamily || aFamily->GetId() != anID ) {
862     map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
863     if ( i_fam == myFamilies.end() )
864       return false;
865     aFamily = i_fam->second;
866   }
867   return ( aFamily->GetId() == anID );
868 }
869