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