Salome HOME
Fix for bug 10378: Regression: application hangs up after trying to import MED file...
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
1 //  SMESH DriverMED : driver to read and write 'med' files
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : DriverMED_R_SMESHDS_Mesh.cxx
25 //  Module : SMESH
26
27 #include "DriverMED_R_SMESHDS_Mesh.h"
28 #include "DriverMED_R_SMDS_Mesh.h"
29 #include "SMESHDS_Mesh.hxx"
30 #include "utilities.h"
31
32 #include "DriverMED_Family.h"
33
34 #include "SMESHDS_Group.hxx"
35
36 #include "MED_Factory.hxx"
37 #include "MED_Utilities.hxx"
38
39 #include <stdlib.h>
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 #define _EDF_NODE_IDS_
48
49 using namespace MED;
50
51 void DriverMED_R_SMESHDS_Mesh::SetMeshName(string theMeshName)
52 {
53   myMeshName = theMeshName;
54 }
55
56 static const SMDS_MeshNode* 
57 FindNode(const SMDS_Mesh* theMesh, TInt theId){
58   const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
59   if(aNode) return aNode;
60   EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
61 }
62
63
64 enum ECoordName{eX, eY, eZ, eNone};
65 typedef TFloat (*TGetCoord)(MED::PNodeInfo&, TInt);
66
67 template<ECoordName TheCoordId>
68 TFloat GetCoord(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
69   return thePNodeInfo->GetNodeCoord(theElemId,TheCoordId);
70 }
71
72 template<>
73 TFloat GetCoord<eNone>(MED::PNodeInfo& thePNodeInfo, TInt theElemId){
74   return 0.0;
75 }
76
77
78 static TGetCoord aXYZGetCoord[3] = {
79   &GetCoord<eX>, 
80   &GetCoord<eY>, 
81   &GetCoord<eZ>
82 };
83
84
85 static TGetCoord aXYGetCoord[3] = {
86   &GetCoord<eX>, 
87   &GetCoord<eY>, 
88   &GetCoord<eNone>
89 };
90
91 static TGetCoord aYZGetCoord[3] = {
92   &GetCoord<eNone>,
93   &GetCoord<eX>, 
94   &GetCoord<eY>
95 };
96
97 static TGetCoord aXZGetCoord[3] = {
98   &GetCoord<eX>, 
99   &GetCoord<eNone>,
100   &GetCoord<eY>
101 };
102
103
104 static TGetCoord aXGetCoord[3] = {
105   &GetCoord<eX>, 
106   &GetCoord<eNone>,
107   &GetCoord<eNone>
108 };
109
110 static TGetCoord aYGetCoord[3] = {
111   &GetCoord<eNone>,
112   &GetCoord<eX>, 
113   &GetCoord<eNone>
114 };
115
116 static TGetCoord aZGetCoord[3] = {
117   &GetCoord<eNone>,
118   &GetCoord<eNone>,
119   &GetCoord<eX>
120 };
121
122
123 class TCoordHelper{
124   MED::PNodeInfo myPNodeInfo;
125   TGetCoord* myGetCoord;
126 public:
127   TCoordHelper(const MED::PNodeInfo& thePNodeInfo,
128                TGetCoord* theGetCoord):
129     myPNodeInfo(thePNodeInfo),
130     myGetCoord(theGetCoord)
131   {}
132   virtual ~TCoordHelper(){}
133   TFloat GetCoord(TInt theElemId, TInt theCoodId){
134     return (*myGetCoord[theCoodId])(myPNodeInfo,theElemId);
135   }
136 };
137 typedef boost::shared_ptr<TCoordHelper> TCoordHelperPtr;
138
139
140 Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
141 {
142   Status aResult = DRS_FAIL;
143   try{
144     myFamilies.clear();
145     if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
146     PWrapper aMed = CrWrapper(myFile);
147
148     aResult = DRS_EMPTY;
149     if(TInt aNbMeshes = aMed->GetNbMeshes()){
150       for(int iMesh = 0; iMesh < aNbMeshes; iMesh++){
151         // Reading the MED mesh
152         //---------------------
153         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
154         string aMeshName;
155         if (myMeshId != -1) {
156           ostringstream aMeshNameStr;
157           aMeshNameStr<<myMeshId;
158           aMeshName = aMeshNameStr.str();
159         } else {
160           aMeshName = myMeshName;
161         }
162         if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
163         if(aMeshName != aMeshInfo->GetName()) continue;
164         aResult = DRS_OK;
165         //TInt aMeshDim = aMeshInfo->GetDim();
166         
167         // Reading MED families to the temporary structure
168         //------------------------------------------------
169         TErr anErr;
170         TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
171         if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
172         for (TInt iFam = 0; iFam < aNbFams; iFam++) {
173           PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
174           if(anErr >= 0){
175             TInt aFamId = aFamilyInfo->GetId();
176             if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
177             
178             DriverMED_FamilyPtr aFamily (new DriverMED_Family);
179             
180             TInt aNbGrp = aFamilyInfo->GetNbGroup();
181             if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
182             for (TInt iGr = 0; iGr < aNbGrp; iGr++) {
183               string aGroupName = aFamilyInfo->GetGroupName(iGr);
184               if(MYDEBUG) MESSAGE(aGroupName);
185               aFamily->AddGroupName(aGroupName);
186             }
187             aFamily->SetId( aFamId );
188             myFamilies[aFamId] = aFamily;
189           }
190         }
191
192         // Reading MED nodes to the corresponding SMDS structure
193         //------------------------------------------------------
194         PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
195
196         TCoordHelperPtr aCoordHelperPtr;
197         {
198           TInt aMeshDimension = aMeshInfo->GetDim();
199           bool anIsDimPresent[3] = {false, false, false};
200           for(TInt iDim = 0; iDim < aMeshDimension; iDim++){
201             string aDimName = aNodeInfo->GetCoordName(iDim);
202             if(aDimName == "x" || aDimName == "X")
203               anIsDimPresent[eX] = true;
204             else if(aDimName == "y" || aDimName == "Y")
205               anIsDimPresent[eY] = true;
206             else if(aDimName == "z" || aDimName == "Z")
207               anIsDimPresent[eZ] = true;
208           }
209           switch(aMeshDimension){
210           case 3:
211             aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
212             break;
213           case 2:
214             if(anIsDimPresent[eY] && anIsDimPresent[eZ])
215               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
216             else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
217               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
218             else
219               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
220             break;
221           case 1:
222             if(anIsDimPresent[eY])
223               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
224             else if(anIsDimPresent[eZ])
225               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
226             else
227               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
228             break;
229           }
230         }
231
232         EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
233         TInt aNbElems = aNodeInfo->GetNbElem();
234         if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
235         DriverMED_FamilyPtr aFamily;
236         for(TInt iElem = 0; iElem < aNbElems; iElem++){
237           double aCoords[3] = {0.0, 0.0, 0.0};
238           for(TInt iDim = 0; iDim < 3; iDim++)
239             aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
240           const SMDS_MeshNode* aNode;
241           if(anIsNodeNum) {
242             aNode = myMesh->AddNodeWithID
243               (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
244           } else {
245             aNode = myMesh->AddNode
246               (aCoords[0],aCoords[1],aCoords[2]);
247           }
248           //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
249
250           // Save reference to this node from its family
251           TInt aFamNum = aNodeInfo->GetFamNum(iElem);
252           if ( checkFamilyID ( aFamily, aFamNum ))
253           {
254             aFamily->AddElement(aNode);
255             aFamily->SetType(SMDSAbs_Node);
256           }
257         }
258
259         // Reading pre information about all MED cells
260         //--------------------------------------------
261         bool takeNumbers = true;  // initially we trust the numbers from file
262         MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
263         MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
264         for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
265           const EEntiteMaillage& anEntity = anEntityIter->first;
266           if(anEntity == eNOEUD) continue;
267           // Reading MED cells to the corresponding SMDS structure
268           //------------------------------------------------------
269           const MED::TGeom& aTGeom = anEntityIter->second;
270           MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
271           for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
272             const EGeometrieElement& aGeom = anTGeomIter->first;
273             if(aGeom == ePOINT1) continue;
274             PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
275             EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
276             TInt aNbElems = aCellInfo->GetNbElem();
277             if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
278             if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
279
280             for(int iElem = 0; iElem < aNbElems; iElem++){
281               TInt aNbNodes = -1;
282               switch(aGeom){
283               case eSEG2:
284               case eSEG3:
285                 aNbNodes = 2;
286                 break;
287               case eTRIA3:
288               case eTRIA6:
289                 aNbNodes = 3;
290                 break;
291                 break;
292               case eQUAD4:
293               case eQUAD8:
294                 aNbNodes = 4;
295                 break;
296               case eTETRA4:
297               case eTETRA10:
298                 aNbNodes = 4;
299                 break;
300               case ePYRA5:
301               case ePYRA13:
302                 aNbNodes = 5;
303                 break;
304               case ePENTA6:
305               case ePENTA15:
306                 aNbNodes = 6;
307                 break;
308               case eHEXA8:
309               case eHEXA20:
310                 aNbNodes = 8;
311                 break;
312               }
313               vector<TInt> aNodeIds(aNbNodes);
314               bool anIsValidConnect = false;
315
316               try{
317 #ifdef _EDF_NODE_IDS_
318                 if(anIsNodeNum) {
319                   for(int i = 0; i < aNbNodes; i++){
320                     aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
321                   }
322                 }else{
323                   for(int i = 0; i < aNbNodes; i++){
324                     aNodeIds[i] = aCellInfo->GetConn(iElem,i);
325                   }
326                 }
327 #else
328                 for(int i = 0; i < aNbNodes; i++){
329                   aNodeIds[i] = aCellInfo->GetConn(iElem,i);
330                 }
331 #endif
332                 anIsValidConnect = true;
333               }catch(const std::exception& exc){
334                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
335                 aResult = DRS_FAIL;
336               }catch(...){
337                 //INFOS("Unknown exception was cought !!!");
338                 aResult = DRS_FAIL;
339               }
340               
341               if(!anIsValidConnect)
342                 continue;
343
344               bool isRenum = false;
345               SMDS_MeshElement* anElement = NULL;
346               TInt aFamNum = aCellInfo->GetFamNum(iElem);
347               try{
348                 //MESSAGE("Try to create element # " << iElem << " with id = "
349                 //        << aCellInfo->GetElemNum(iElem));
350                 switch(aGeom){
351                 case eSEG2:
352                 case eSEG3:
353                   if(anIsElemNum)
354                     anElement = myMesh->AddEdgeWithID(aNodeIds[0],
355                                                       aNodeIds[1],
356                                                       aCellInfo->GetElemNum(iElem));
357                   if (!anElement) {
358                     anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
359                                                 FindNode(myMesh,aNodeIds[1]));
360                     isRenum = anIsElemNum;
361                   }
362                   break;
363                 case eTRIA3:
364                 case eTRIA6:
365                   aNbNodes = 3;
366                   if(anIsElemNum)
367                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
368                                                       aNodeIds[1],
369                                                       aNodeIds[2],
370                                                       aCellInfo->GetElemNum(iElem));
371                   if (!anElement) {
372                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
373                                                 FindNode(myMesh,aNodeIds[1]),
374                                                 FindNode(myMesh,aNodeIds[2]));
375                     isRenum = anIsElemNum;
376                   }
377                   break;
378                 case eQUAD4:
379                 case eQUAD8:
380                   aNbNodes = 4;
381                   // There is some differnce between SMDS and MED
382                   if(anIsElemNum)
383                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
384                                                       aNodeIds[1],
385                                                       aNodeIds[2],
386                                                       aNodeIds[3],
387                                                       aCellInfo->GetElemNum(iElem));
388                   if (!anElement) {
389                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
390                                                 FindNode(myMesh,aNodeIds[1]),
391                                                 FindNode(myMesh,aNodeIds[2]),
392                                                 FindNode(myMesh,aNodeIds[3]));
393                     isRenum = anIsElemNum;
394                   }
395                   break;
396                 case eTETRA4:
397                 case eTETRA10:
398                   aNbNodes = 4;
399                   if(anIsElemNum)
400                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
401                                                         aNodeIds[1],
402                                                         aNodeIds[2],
403                                                         aNodeIds[3],
404                                                         aCellInfo->GetElemNum(iElem));
405                   if (!anElement) {
406                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
407                                                   FindNode(myMesh,aNodeIds[1]),
408                                                   FindNode(myMesh,aNodeIds[2]),
409                                                   FindNode(myMesh,aNodeIds[3]));
410                     isRenum = anIsElemNum;
411                   }
412                   break;
413                 case ePYRA5:
414                 case ePYRA13:
415                   aNbNodes = 5;
416                   // There is some differnce between SMDS and MED
417                   if(anIsElemNum)
418                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
419                                                         aNodeIds[1],
420                                                         aNodeIds[2],
421                                                         aNodeIds[3],
422                                                         aNodeIds[4],
423                                                         aCellInfo->GetElemNum(iElem));
424                   if (!anElement) {
425                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
426                                                   FindNode(myMesh,aNodeIds[1]),
427                                                   FindNode(myMesh,aNodeIds[2]),
428                                                   FindNode(myMesh,aNodeIds[3]),
429                                                   FindNode(myMesh,aNodeIds[4]));
430                     isRenum = anIsElemNum;
431                   }
432                   break;
433                 case ePENTA6:
434                 case ePENTA15:
435                   aNbNodes = 6;
436                   if(anIsElemNum)
437                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
438                                                         aNodeIds[1],
439                                                         aNodeIds[2],
440                                                         aNodeIds[3],
441                                                         aNodeIds[4],
442                                                         aNodeIds[5],
443                                                         aCellInfo->GetElemNum(iElem));
444                   if (!anElement) {
445                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
446                                                   FindNode(myMesh,aNodeIds[1]),
447                                                   FindNode(myMesh,aNodeIds[2]),
448                                                   FindNode(myMesh,aNodeIds[3]),
449                                                   FindNode(myMesh,aNodeIds[4]),
450                                                   FindNode(myMesh,aNodeIds[5]));
451                     isRenum = anIsElemNum;
452                   }
453                   break;
454                 case eHEXA8:
455                 case eHEXA20:
456                   aNbNodes = 8;
457                   if(anIsElemNum)
458                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
459                                                         aNodeIds[1],
460                                                         aNodeIds[2],
461                                                         aNodeIds[3],
462                                                         aNodeIds[4],
463                                                         aNodeIds[5],
464                                                         aNodeIds[6],
465                                                         aNodeIds[7],
466                                                         aCellInfo->GetElemNum(iElem));
467                   if (!anElement) {
468                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
469                                                   FindNode(myMesh,aNodeIds[1]),
470                                                   FindNode(myMesh,aNodeIds[2]),
471                                                   FindNode(myMesh,aNodeIds[3]),
472                                                   FindNode(myMesh,aNodeIds[4]),
473                                                   FindNode(myMesh,aNodeIds[5]),
474                                                   FindNode(myMesh,aNodeIds[6]),
475                                                   FindNode(myMesh,aNodeIds[7]));
476                     isRenum = anIsElemNum;
477                   }
478                   break;
479                 }
480               }catch(const std::exception& exc){
481                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
482                 aResult = DRS_FAIL;
483               }catch(...){
484                 //INFOS("Unknown exception was cought !!!");
485                 aResult = DRS_FAIL;
486               }
487                 
488               if (!anElement) {
489                 aResult = DRS_WARN_SKIP_ELEM;
490               }
491               else {
492                 if (isRenum) {
493                   anIsElemNum = eFAUX;
494                   takeNumbers = false;
495                   if (aResult < DRS_WARN_RENUMBER)
496                     aResult = DRS_WARN_RENUMBER;
497                 }
498                 if ( checkFamilyID ( aFamily, aFamNum ))
499                 {
500                   // Save reference to this element from its family
501                   aFamily->AddElement(anElement);
502                   aFamily->SetType(anElement->GetType());
503                 }
504               }
505             }
506           }
507         }
508         break;
509       }
510     }
511   }catch(const std::exception& exc){
512     INFOS("Follow exception was cought:\n\t"<<exc.what());
513     aResult = DRS_FAIL;
514   }catch(...){
515     INFOS("Unknown exception was cought !!!");
516     aResult = DRS_FAIL;
517   }
518   if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
519   return aResult;
520 }
521
522 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
523 {
524   list<string> aMeshNames;
525
526   try {
527     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
528     theStatus = DRS_OK;
529     PWrapper aMed = CrWrapper(myFile);
530
531     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
532       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
533         // Reading the MED mesh
534         //---------------------
535         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
536         aMeshNames.push_back(aMeshInfo->GetName());
537       }
538     }
539   }catch(const std::exception& exc){
540     INFOS("Follow exception was cought:\n\t"<<exc.what());
541     theStatus = DRS_FAIL;
542   }catch(...){
543     INFOS("Unknown exception was cought !!!");
544     theStatus = DRS_FAIL;
545   }
546
547   return aMeshNames;
548 }
549
550 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
551 {
552   list<TNameAndType> aResult;
553   set<TNameAndType> aResGroupNames;
554
555   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
556   for (; aFamsIter != myFamilies.end(); aFamsIter++)
557   {
558     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
559     const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
560     set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
561     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
562     {
563       TNameAndType aNameAndType = make_pair( *aGrNamesIter, aFamily->GetType() );
564       // Check, if this is a Group or SubMesh name
565 //if (aName.substr(0, 5) == string("Group")) {
566         if ( aResGroupNames.insert( aNameAndType ).second ) {
567           aResult.push_back( aNameAndType );
568         }
569 //    }
570     }
571   }
572
573   return aResult;
574 }
575
576 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
577 {
578   string aGroupName (theGroup->GetStoreName());
579   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
580
581   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
582   for (; aFamsIter != myFamilies.end(); aFamsIter++)
583   {
584     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
585     if (aFamily->GetType() == theGroup->GetType() && aFamily->MemberOf(aGroupName))
586     {
587       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
588       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
589       const SMDS_MeshElement * element = 0;
590       for (; anElemsIter != anElements.end(); anElemsIter++)
591       {
592         element = *anElemsIter;
593         theGroup->SMDSGroup().Add(element);
594       }
595       if ( element )
596         theGroup->SetType( element->GetType() );
597     }
598   }
599 }
600
601 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
602                                            const int theId)
603 {
604   char submeshGrpName[ 30 ];
605   sprintf( submeshGrpName, "SubMesh %d", theId );
606   string aName (submeshGrpName);
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->MemberOf(aName))
612     {
613       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
614       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
615       if (aFamily->GetType() == SMDSAbs_Node)
616       {
617         for (; anElemsIter != anElements.end(); anElemsIter++)
618         {
619           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
620           theSubMesh->AddNode(node);
621         }
622       }
623       else
624       {
625         for (; anElemsIter != anElements.end(); anElemsIter++)
626         {
627           theSubMesh->AddElement(*anElemsIter);
628         }
629       }
630     }
631   }
632 }
633
634 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
635 {
636   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
637   for (; aFamsIter != myFamilies.end(); aFamsIter++)
638   {
639     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
640     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
641     set<string>::iterator aGrNamesIter = aGroupNames.begin();
642     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
643     {
644       string aName = *aGrNamesIter;
645       // Check, if this is a Group or SubMesh name
646       if (aName.substr(0, 7) == string("SubMesh"))
647       {
648         int Id = atoi(string(aName).substr(7).c_str());
649         set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
650         set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
651         if (aFamily->GetType() == SMDSAbs_Node)
652         {
653           for (; anElemsIter != anElements.end(); anElemsIter++)
654           {
655             SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
656               ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
657             // find out a shape type
658             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
659             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
660             switch ( aShapeType ) {
661             case TopAbs_FACE:
662               myMesh->SetNodeOnFace(node, Id); break;
663             case TopAbs_EDGE:
664               myMesh->SetNodeOnEdge(node, Id); break;
665             case TopAbs_VERTEX:
666               myMesh->SetNodeOnVertex(node, Id); break;
667             default:
668               myMesh->SetNodeInVolume(node, Id);
669             }
670           }
671         }
672         else
673         {
674           for (; anElemsIter != anElements.end(); anElemsIter++)
675           {
676             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
677           }
678         }
679       }
680     }
681   }
682 }
683 /*!
684  * \brief Ensure aFamily to have required ID
685  * \param aFamily - a family to check and update
686  * \param anID - an ID aFamily should have
687  * \retval bool  - true if successful
688  */
689 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
690 {
691   if ( !aFamily || aFamily->GetId() != anID ) {
692     map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
693     if ( i_fam == myFamilies.end() )
694       return false;
695     aFamily = i_fam->second;
696   }
697   return ( aFamily->GetId() == anID );
698 }
699