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