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