Salome HOME
0020576: EDF 1163 SMESH: Random type of group at MED import
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  SMESH DriverMED : driver to read and write 'med' files
23 //  File   : DriverMED_R_SMESHDS_Mesh.cxx
24 //  Module : SMESH
25 //
26 #include "DriverMED_R_SMESHDS_Mesh.h"
27 #include "DriverMED_R_SMDS_Mesh.h"
28 #include "SMESHDS_Mesh.hxx"
29 #include "utilities.h"
30
31 #include "DriverMED_Family.h"
32
33 #include "SMESHDS_Group.hxx"
34
35 #include "MED_Factory.hxx"
36 #include "MED_CoordUtils.hxx"
37 #include "MED_Utilities.hxx"
38
39 #include <stdlib.h>
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 0;
43 //#define _DEXCEPT_
44 #else
45 static int MYDEBUG = 0;
46 #endif
47
48 #define _EDF_NODE_IDS_
49
50 using namespace MED;
51 using namespace std;
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                     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                     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               TInt aNbNodes = -1;
341               switch(aGeom){
342               case eSEG2:    aNbNodes = 2;  break;
343               case eSEG3:    aNbNodes = 3;  break;
344               case eTRIA3:   aNbNodes = 3;  break;
345               case eTRIA6:   aNbNodes = 6;  break;
346               case eQUAD4:   aNbNodes = 4;  break;
347               case eQUAD8:   aNbNodes = 8;  break;
348               case eTETRA4:  aNbNodes = 4;  break;
349               case eTETRA10: aNbNodes = 10; break;
350               case ePYRA5:   aNbNodes = 5;  break;
351               case ePYRA13:  aNbNodes = 13; break;
352               case ePENTA6:  aNbNodes = 6;  break;
353               case ePENTA15: aNbNodes = 15; break;
354               case eHEXA8:   aNbNodes = 8;  break;
355               case eHEXA20:  aNbNodes = 20; break;
356               case ePOINT1:  aNbNodes = 1;  break;
357               default:;
358               }
359               vector<TInt> aNodeIds(aNbNodes);
360               for(int iElem = 0; iElem < aNbElems; iElem++){
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       const set< SMDSAbs_ElementType >& types = aFamily->GetTypes();
775       set< SMDSAbs_ElementType >::const_iterator type = types.begin();
776       for ( ; type != types.end(); ++type )
777       {
778         TNameAndType aNameAndType = make_pair( *aGrNamesIter, *type );
779         if ( aResGroupNames.insert( aNameAndType ).second ) {
780           aResult.push_back( aNameAndType );
781         }
782       }
783     }
784   }
785
786   return aResult;
787 }
788
789 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
790 {
791   string aGroupName (theGroup->GetStoreName());
792   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
793
794   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
795   for (; aFamsIter != myFamilies.end(); aFamsIter++)
796   {
797     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
798     if (aFamily->GetTypes().count( theGroup->GetType() ) && aFamily->MemberOf(aGroupName))
799     {
800       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
801       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
802       for (; anElemsIter != anElements.end(); anElemsIter++)
803       {
804         const SMDS_MeshElement * element = *anElemsIter;
805         if ( element->GetType() == theGroup->GetType() ) // Issue 0020576
806           theGroup->SMDSGroup().Add(element);
807       }
808       int aGroupAttrVal = aFamily->GetGroupAttributVal();
809       if( aGroupAttrVal != 0)
810         theGroup->SetColorGroup(aGroupAttrVal);
811 //       if ( element ) -- Issue 0020576
812 //         theGroup->SetType( theGroup->SMDSGroup().GetType() );
813     }
814   }
815 }
816
817 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
818                                            const int theId)
819 {
820   char submeshGrpName[ 30 ];
821   sprintf( submeshGrpName, "SubMesh %d", theId );
822   string aName (submeshGrpName);
823   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
824   for (; aFamsIter != myFamilies.end(); aFamsIter++)
825   {
826     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
827     if (aFamily->MemberOf(aName))
828     {
829       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
830       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
831       if (aFamily->GetType() == SMDSAbs_Node)
832       {
833         for (; anElemsIter != anElements.end(); anElemsIter++)
834         {
835           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
836           theSubMesh->AddNode(node);
837         }
838       }
839       else
840       {
841         for (; anElemsIter != anElements.end(); anElemsIter++)
842         {
843           theSubMesh->AddElement(*anElemsIter);
844         }
845       }
846     }
847   }
848 }
849
850 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
851 {
852   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
853   for (; aFamsIter != myFamilies.end(); aFamsIter++)
854   {
855     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
856     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
857     set<string>::iterator aGrNamesIter = aGroupNames.begin();
858     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
859     {
860       string aName = *aGrNamesIter;
861       // Check, if this is a Group or SubMesh name
862       if (aName.substr(0, 7) == string("SubMesh"))
863       {
864         int Id = atoi(string(aName).substr(7).c_str());
865         set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
866         set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
867         if (aFamily->GetType() == SMDSAbs_Node)
868         {
869           for (; anElemsIter != anElements.end(); anElemsIter++)
870           {
871             SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
872               ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
873             // find out a shape type
874             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
875             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
876             switch ( aShapeType ) {
877             case TopAbs_FACE:
878               myMesh->SetNodeOnFace(node, Id); break;
879             case TopAbs_EDGE:
880               myMesh->SetNodeOnEdge(node, Id); break;
881             case TopAbs_VERTEX:
882               myMesh->SetNodeOnVertex(node, Id); break;
883             default:
884               myMesh->SetNodeInVolume(node, Id);
885             }
886           }
887         }
888         else
889         {
890           for (; anElemsIter != anElements.end(); anElemsIter++)
891           {
892             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
893           }
894         }
895       }
896     }
897   }
898 }
899 /*!
900  * \brief Ensure aFamily to have required ID
901  * \param aFamily - a family to check and update
902  * \param anID - an ID aFamily should have
903  * \retval bool  - true if successful
904  */
905 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
906 {
907   if ( !aFamily || aFamily->GetId() != anID ) {
908     map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
909     if ( i_fam == myFamilies.end() )
910       return false;
911     aFamily = i_fam->second;
912   }
913   return ( aFamily->GetId() == anID );
914 }
915
916
917 /*! \brief Reading the structured mesh and convert to non structured (by filling of smesh structure for non structured mesh)
918  * \param theWrapper  - PWrapper const pointer
919  * \param theMeshInfo - PMeshInfo const pointer
920  * \return TRUE, if successfully. Else FALSE
921  */
922 bool DriverMED_R_SMESHDS_Mesh::buildMeshGrille(const MED::PWrapper& theWrapper,
923                                                const MED::PMeshInfo& theMeshInfo)
924 {
925   bool res = true;
926
927   MED::PGrilleInfo aGrilleInfo = theWrapper->GetPGrilleInfo(theMeshInfo);
928   MED::TInt aNbNodes = aGrilleInfo->GetNbNodes();
929   MED::TInt aNbCells = aGrilleInfo->GetNbCells();
930   MED::TInt aMeshDim = theMeshInfo->GetDim();
931   DriverMED_FamilyPtr aFamily;
932   for(MED::TInt iNode=0;iNode < aNbNodes; iNode++){
933     double aCoords[3] = {0.0, 0.0, 0.0};
934     const SMDS_MeshNode* aNode;
935     MED::TNodeCoord aMEDNodeCoord = aGrilleInfo->GetCoord(iNode);
936     for(MED::TInt iDim=0;iDim<aMeshDim;iDim++)
937       aCoords[(int)iDim] = aMEDNodeCoord[(int)iDim];
938     aNode = myMesh->AddNodeWithID(aCoords[0],aCoords[1],aCoords[2],(int)iNode);
939
940     if((aGrilleInfo->myFamNumNode).size() > 0){
941       TInt aFamNum = aGrilleInfo->GetFamNumNode(iNode);
942       if ( checkFamilyID ( aFamily, aFamNum ))
943         {
944           aFamily->AddElement(aNode);
945           aFamily->SetType(SMDSAbs_Node);
946         }
947     }
948     
949   }
950
951   SMDS_MeshElement* anElement = NULL;
952   MED::TIntVector aNodeIds;
953   for(MED::TInt iCell=0;iCell < aNbCells; iCell++){
954     aNodeIds = aGrilleInfo->GetConn(iCell);
955     switch(aGrilleInfo->GetGeom()){
956     case MED::eSEG2:
957       if(aNodeIds.size() != 2){
958         res = false;
959         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 2!="<<aNodeIds.size());
960       }
961       anElement = myMesh->AddEdgeWithID(aNodeIds[0],
962                                         aNodeIds[1],
963                                         iCell);
964       break;
965     case MED::eQUAD4:
966       if(aNodeIds.size() != 4){
967         res = false;
968         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 4!="<<aNodeIds.size());
969       }
970       anElement = myMesh->AddFaceWithID(aNodeIds[0],
971                                         aNodeIds[2],
972                                         aNodeIds[3],
973                                         aNodeIds[1],
974                                         iCell);
975       break;
976     case MED::eHEXA8:
977       if(aNodeIds.size() != 8){
978         res = false;
979         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 8!="<<aNodeIds.size());
980       }
981       anElement = myMesh->AddVolumeWithID(aNodeIds[0],
982                                           aNodeIds[2],
983                                           aNodeIds[3],
984                                           aNodeIds[1],
985                                           aNodeIds[4],
986                                           aNodeIds[6],
987                                           aNodeIds[7],
988                                           aNodeIds[5],
989                                           iCell);
990       break;
991     default:
992       break;
993     }
994     
995     if((aGrilleInfo->myFamNum).size() > 0){
996       TInt aFamNum = aGrilleInfo->GetFamNum(iCell);
997       if ( checkFamilyID ( aFamily, aFamNum )){
998         aFamily->AddElement(anElement);
999         aFamily->SetType(anElement->GetType());
1000       }
1001     }
1002   }
1003
1004   return res;
1005 }