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