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