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