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