Salome HOME
Fix PAL8562: rpath (rpath-link) option needs parameter - directory to search shared...
[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             myFamilies[aFamId] = aFamily;
188           }
189         }
190
191         // Reading MED nodes to the corresponding SMDS structure
192         //------------------------------------------------------
193         PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
194
195         TCoordHelperPtr aCoordHelperPtr;
196         {
197           TInt aMeshDimension = aMeshInfo->GetDim();
198           bool anIsDimPresent[3] = {false, false, false};
199           for(TInt iDim = 0; iDim < aMeshDimension; iDim++){
200             string aDimName = aNodeInfo->GetCoordName(iDim);
201             if(aDimName == "x" || aDimName == "X")
202               anIsDimPresent[eX] = true;
203             else if(aDimName == "y" || aDimName == "Y")
204               anIsDimPresent[eY] = true;
205             else if(aDimName == "z" || aDimName == "Z")
206               anIsDimPresent[eZ] = true;
207           }
208           switch(aMeshDimension){
209           case 3:
210             aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYZGetCoord));
211             break;
212           case 2:
213             if(anIsDimPresent[eY] && anIsDimPresent[eZ])
214               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYZGetCoord));
215             else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
216               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXZGetCoord));
217             else
218               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXYGetCoord));
219             break;
220           case 1:
221             if(anIsDimPresent[eY])
222               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aYGetCoord));
223             else if(anIsDimPresent[eZ])
224               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aZGetCoord));
225             else
226               aCoordHelperPtr.reset(new TCoordHelper(aNodeInfo,aXGetCoord));
227             break;
228           }
229         }
230
231         EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
232         TInt aNbElems = aNodeInfo->GetNbElem();
233         if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
234         for(TInt iElem = 0; iElem < aNbElems; iElem++){
235           double aCoords[3] = {0.0, 0.0, 0.0};
236           for(TInt iDim = 0; iDim < 3; iDim++)
237             aCoords[iDim] = aCoordHelperPtr->GetCoord(iElem,iDim);
238           const SMDS_MeshNode* aNode;
239           if(anIsNodeNum) {
240             aNode = myMesh->AddNodeWithID
241               (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
242           } else {
243             aNode = myMesh->AddNode
244               (aCoords[0],aCoords[1],aCoords[2]);
245           }
246           //cout<<aNode->GetID()<<": "<<aNode->X()<<", "<<aNode->Y()<<", "<<aNode->Z()<<endl;
247
248           // Save reference to this node from its family
249           TInt aFamNum = aNodeInfo->GetFamNum(iElem);
250           if (myFamilies.find(aFamNum) != myFamilies.end())
251           {
252             myFamilies[aFamNum]->AddElement(aNode);
253             myFamilies[aFamNum]->SetType(SMDSAbs_Node);
254           }
255         }
256
257         // Reading pre information about all MED cells
258         //--------------------------------------------
259         bool takeNumbers = true;  // initially we trust the numbers from file
260         MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
261         MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
262         for(; anEntityIter != aEntityInfo.end(); anEntityIter++){
263           const EEntiteMaillage& anEntity = anEntityIter->first;
264           if(anEntity == eNOEUD) continue;
265           // Reading MED cells to the corresponding SMDS structure
266           //------------------------------------------------------
267           const MED::TGeom& aTGeom = anEntityIter->second;
268           MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
269           for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
270             const EGeometrieElement& aGeom = anTGeomIter->first;
271             if(aGeom == ePOINT1) continue;
272             PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
273             EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
274             TInt aNbElems = aCellInfo->GetNbElem();
275             if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
276             if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
277
278             for(int iElem = 0; iElem < aNbElems; iElem++){
279               TInt aNbNodes = -1;
280               switch(aGeom){
281               case eSEG2:
282               case eSEG3:
283                 aNbNodes = 2;
284                 break;
285               case eTRIA3:
286               case eTRIA6:
287                 aNbNodes = 3;
288                 break;
289                 break;
290               case eQUAD4:
291               case eQUAD8:
292                 aNbNodes = 4;
293                 break;
294               case eTETRA4:
295               case eTETRA10:
296                 aNbNodes = 4;
297                 break;
298               case ePYRA5:
299               case ePYRA13:
300                 aNbNodes = 5;
301                 break;
302               case ePENTA6:
303               case ePENTA15:
304                 aNbNodes = 6;
305                 break;
306               case eHEXA8:
307               case eHEXA20:
308                 aNbNodes = 8;
309                 break;
310               }
311               vector<TInt> aNodeIds(aNbNodes);
312               bool anIsValidConnect = false;
313
314               try{
315 #ifdef _EDF_NODE_IDS_
316                 if(anIsNodeNum) {
317                   for(int i = 0; i < aNbNodes; i++){
318                     aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
319                   }
320                 }else{
321                   for(int i = 0; i < aNbNodes; i++){
322                     aNodeIds[i] = aCellInfo->GetConn(iElem,i);
323                   }
324                 }
325 #else
326                 for(int i = 0; i < aNbNodes; i++){
327                   aNodeIds[i] = aCellInfo->GetConn(iElem,i);
328                 }
329 #endif
330                 anIsValidConnect = true;
331               }catch(const std::exception& exc){
332                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
333                 aResult = DRS_FAIL;
334               }catch(...){
335                 //INFOS("Unknown exception was cought !!!");
336                 aResult = DRS_FAIL;
337               }
338               
339               if(!anIsValidConnect)
340                 continue;
341
342               bool isRenum = false;
343               SMDS_MeshElement* anElement = NULL;
344               TInt aFamNum = aCellInfo->GetFamNum(iElem);
345               try{
346                 switch(aGeom){
347                 case eSEG2:
348                 case eSEG3:
349                   if(anIsElemNum)
350                     anElement = myMesh->AddEdgeWithID(aNodeIds[0],
351                                                       aNodeIds[1],
352                                                       aCellInfo->GetElemNum(iElem));
353                   if (!anElement) {
354                     anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
355                                                 FindNode(myMesh,aNodeIds[1]));
356                     isRenum = anIsElemNum;
357                   }
358                   break;
359                 case eTRIA3:
360                 case eTRIA6:
361                   aNbNodes = 3;
362                   if(anIsElemNum)
363                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
364                                                       aNodeIds[1],
365                                                       aNodeIds[2],
366                                                       aCellInfo->GetElemNum(iElem));
367                   if (!anElement) {
368                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
369                                                 FindNode(myMesh,aNodeIds[1]),
370                                                 FindNode(myMesh,aNodeIds[2]));
371                     isRenum = anIsElemNum;
372                   }
373                   break;
374                 case eQUAD4:
375                 case eQUAD8:
376                   aNbNodes = 4;
377                   // There is some differnce between SMDS and MED
378                   if(anIsElemNum)
379                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
380                                                       aNodeIds[1],
381                                                       aNodeIds[2],
382                                                       aNodeIds[3],
383                                                       aCellInfo->GetElemNum(iElem));
384                   if (!anElement) {
385                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
386                                                 FindNode(myMesh,aNodeIds[1]),
387                                                 FindNode(myMesh,aNodeIds[2]),
388                                                 FindNode(myMesh,aNodeIds[3]));
389                     isRenum = anIsElemNum;
390                   }
391                   break;
392                 case eTETRA4:
393                 case eTETRA10:
394                   aNbNodes = 4;
395                   if(anIsElemNum)
396                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
397                                                         aNodeIds[1],
398                                                         aNodeIds[2],
399                                                         aNodeIds[3],
400                                                         aCellInfo->GetElemNum(iElem));
401                   if (!anElement) {
402                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
403                                                   FindNode(myMesh,aNodeIds[1]),
404                                                   FindNode(myMesh,aNodeIds[2]),
405                                                   FindNode(myMesh,aNodeIds[3]));
406                     isRenum = anIsElemNum;
407                   }
408                   break;
409                 case ePYRA5:
410                 case ePYRA13:
411                   aNbNodes = 5;
412                   // There is some differnce between SMDS and MED
413                   if(anIsElemNum)
414                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
415                                                         aNodeIds[1],
416                                                         aNodeIds[2],
417                                                         aNodeIds[3],
418                                                         aNodeIds[4],
419                                                         aCellInfo->GetElemNum(iElem));
420                   if (!anElement) {
421                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
422                                                   FindNode(myMesh,aNodeIds[1]),
423                                                   FindNode(myMesh,aNodeIds[2]),
424                                                   FindNode(myMesh,aNodeIds[3]),
425                                                   FindNode(myMesh,aNodeIds[4]));
426                     isRenum = anIsElemNum;
427                   }
428                   break;
429                 case ePENTA6:
430                 case ePENTA15:
431                   aNbNodes = 6;
432                   if(anIsElemNum)
433                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
434                                                         aNodeIds[1],
435                                                         aNodeIds[2],
436                                                         aNodeIds[3],
437                                                         aNodeIds[4],
438                                                         aNodeIds[5],
439                                                         aCellInfo->GetElemNum(iElem));
440                   if (!anElement) {
441                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
442                                                   FindNode(myMesh,aNodeIds[1]),
443                                                   FindNode(myMesh,aNodeIds[2]),
444                                                   FindNode(myMesh,aNodeIds[3]),
445                                                   FindNode(myMesh,aNodeIds[4]),
446                                                   FindNode(myMesh,aNodeIds[5]));
447                     isRenum = anIsElemNum;
448                   }
449                   break;
450                 case eHEXA8:
451                 case eHEXA20:
452                   aNbNodes = 8;
453                   if(anIsElemNum)
454                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
455                                                         aNodeIds[1],
456                                                         aNodeIds[2],
457                                                         aNodeIds[3],
458                                                         aNodeIds[4],
459                                                         aNodeIds[5],
460                                                         aNodeIds[6],
461                                                         aNodeIds[7],
462                                                         aCellInfo->GetElemNum(iElem));
463                   if (!anElement) {
464                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
465                                                   FindNode(myMesh,aNodeIds[1]),
466                                                   FindNode(myMesh,aNodeIds[2]),
467                                                   FindNode(myMesh,aNodeIds[3]),
468                                                   FindNode(myMesh,aNodeIds[4]),
469                                                   FindNode(myMesh,aNodeIds[5]),
470                                                   FindNode(myMesh,aNodeIds[6]),
471                                                   FindNode(myMesh,aNodeIds[7]));
472                     isRenum = anIsElemNum;
473                   }
474                   break;
475                 }
476               }catch(const std::exception& exc){
477                 //INFOS("Follow exception was cought:\n\t"<<exc.what());
478                 aResult = DRS_FAIL;
479               }catch(...){
480                 //INFOS("Unknown exception was cought !!!");
481                 aResult = DRS_FAIL;
482               }
483                 
484               if (!anElement) {
485                 aResult = DRS_WARN_SKIP_ELEM;
486               }
487               else {
488                 if (isRenum) {
489                   anIsElemNum = eFAUX;
490                   takeNumbers = false;
491                   if (aResult < DRS_WARN_RENUMBER)
492                     aResult = DRS_WARN_RENUMBER;
493                 }
494                 if (myFamilies.find(aFamNum) != myFamilies.end()) {
495                   // Save reference to this element from its family
496                   myFamilies[aFamNum]->AddElement(anElement);
497                   myFamilies[aFamNum]->SetType(anElement->GetType());
498                 }
499               }
500             }
501           }
502         }
503         break;
504       }
505     }
506   }catch(const std::exception& exc){
507     INFOS("Follow exception was cought:\n\t"<<exc.what());
508     aResult = DRS_FAIL;
509   }catch(...){
510     INFOS("Unknown exception was cought !!!");
511     aResult = DRS_FAIL;
512   }
513   if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
514   return aResult;
515 }
516
517 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
518 {
519   list<string> aMeshNames;
520
521   try {
522     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
523     theStatus = DRS_OK;
524     PWrapper aMed = CrWrapper(myFile);
525
526     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
527       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
528         // Reading the MED mesh
529         //---------------------
530         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
531         aMeshNames.push_back(aMeshInfo->GetName());
532       }
533     }
534   }catch(const std::exception& exc){
535     INFOS("Follow exception was cought:\n\t"<<exc.what());
536     theStatus = DRS_FAIL;
537   }catch(...){
538     INFOS("Unknown exception was cought !!!");
539     theStatus = DRS_FAIL;
540   }
541
542   return aMeshNames;
543 }
544
545 list<string> DriverMED_R_SMESHDS_Mesh::GetGroupNames()
546 {
547   list<string> aResult;
548   set<string> aResGroupNames;
549
550   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
551   for (; aFamsIter != myFamilies.end(); aFamsIter++)
552   {
553     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
554     const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
555     set<string>::iterator aGrNamesIter = aGroupNames.begin();
556     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
557     {
558       string aName = *aGrNamesIter;
559       // Check, if this is a Group or SubMesh name
560 //if (aName.substr(0, 5) == string("Group")) {
561         if (aResGroupNames.find(aName) == aResGroupNames.end()) {
562           aResGroupNames.insert(aName);
563           aResult.push_back(aName);
564         }
565 //    }
566     }
567   }
568
569   return aResult;
570 }
571
572 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
573 {
574   string aGroupName (theGroup->GetStoreName());
575   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
576
577   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
578   for (; aFamsIter != myFamilies.end(); aFamsIter++)
579   {
580     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
581     if (aFamily->MemberOf(aGroupName))
582     {
583       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
584       set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
585       const SMDS_MeshElement * element = 0;
586       for (; anElemsIter != anElements.end(); anElemsIter++)
587       {
588         element = *anElemsIter;
589         theGroup->SMDSGroup().Add(element);
590       }
591       if ( element )
592         theGroup->SetType( element->GetType() );
593     }
594   }
595 }
596
597 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
598                                            const int theId)
599 {
600   char submeshGrpName[ 30 ];
601   sprintf( submeshGrpName, "SubMesh %d", theId );
602   string aName (submeshGrpName);
603   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
604   for (; aFamsIter != myFamilies.end(); aFamsIter++)
605   {
606     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
607     if (aFamily->MemberOf(aName))
608     {
609       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
610       set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
611       if (aFamily->GetType() == SMDSAbs_Node)
612       {
613         for (; anElemsIter != anElements.end(); anElemsIter++)
614         {
615           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
616           theSubMesh->AddNode(node);
617         }
618       }
619       else
620       {
621         for (; anElemsIter != anElements.end(); anElemsIter++)
622         {
623           theSubMesh->AddElement(*anElemsIter);
624         }
625       }
626     }
627   }
628 }
629
630 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
631 {
632   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
633   for (; aFamsIter != myFamilies.end(); aFamsIter++)
634   {
635     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
636     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
637     set<string>::iterator aGrNamesIter = aGroupNames.begin();
638     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
639     {
640       string aName = *aGrNamesIter;
641       // Check, if this is a Group or SubMesh name
642       if (aName.substr(0, 7) == string("SubMesh"))
643       {
644         int Id = atoi(string(aName).substr(7).c_str());
645         set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
646         set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
647         if (aFamily->GetType() == SMDSAbs_Node)
648         {
649           for (; anElemsIter != anElements.end(); anElemsIter++)
650           {
651             SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
652               ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
653             // find out a shape type
654             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
655             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
656             switch ( aShapeType ) {
657             case TopAbs_FACE:
658               myMesh->SetNodeOnFace(node, Id); break;
659             case TopAbs_EDGE:
660               myMesh->SetNodeOnEdge(node, Id); break;
661             case TopAbs_VERTEX:
662               myMesh->SetNodeOnVertex(node, Id); break;
663             default:
664               myMesh->SetNodeInVolume(node, Id);
665             }
666           }
667         }
668         else
669         {
670           for (; anElemsIter != anElements.end(); anElemsIter++)
671           {
672             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
673           }
674         }
675       }
676     }
677   }
678 }