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