Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
1 //  Copyright (C) 2007-2010  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
23 //  SMESH DriverMED : driver to read and write 'med' files
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                     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                     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               TInt aNbNodes = -1;
342               switch(aGeom){
343               case eSEG2:    aNbNodes = 2;  break;
344               case eSEG3:    aNbNodes = 3;  break;
345               case eTRIA3:   aNbNodes = 3;  break;
346               case eTRIA6:   aNbNodes = 6;  break;
347               case eQUAD4:   aNbNodes = 4;  break;
348               case eQUAD8:   aNbNodes = 8;  break;
349               case eTETRA4:  aNbNodes = 4;  break;
350               case eTETRA10: aNbNodes = 10; break;
351               case ePYRA5:   aNbNodes = 5;  break;
352               case ePYRA13:  aNbNodes = 13; break;
353               case ePENTA6:  aNbNodes = 6;  break;
354               case ePENTA15: aNbNodes = 15; break;
355               case eHEXA8:   aNbNodes = 8;  break;
356               case eHEXA20:  aNbNodes = 20; break;
357               case ePOINT1:  aNbNodes = 1;  break;
358               default:;
359               }
360               vector<TInt> aNodeIds(aNbNodes);
361               for(int iElem = 0; iElem < aNbElems; iElem++){
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 ePOINT1:
401                     //anElement = FindNode(myMesh,aNodeIds[0]);
402                     if(anIsElemNum)
403                       anElement = myMesh->Add0DElementWithID
404                         (aNodeIds[0], aCellInfo->GetElemNum(iElem));
405                     if (!anElement) {
406                       anElement = myMesh->Add0DElement(FindNode(myMesh,aNodeIds[0]));
407                       isRenum = anIsElemNum;
408                     }
409                     break;
410                   case eSEG2:
411                     if(anIsElemNum)
412                       anElement = myMesh->AddEdgeWithID(aNodeIds[0],
413                                                         aNodeIds[1],
414                                                         aCellInfo->GetElemNum(iElem));
415                     if (!anElement) {
416                       anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
417                                                   FindNode(myMesh,aNodeIds[1]));
418                       isRenum = anIsElemNum;
419                     }
420                     break;
421                   case eSEG3:
422                     if(anIsElemNum)
423                       anElement = myMesh->AddEdgeWithID(aNodeIds[0],
424                                                         aNodeIds[1],
425                                                         aNodeIds[2],
426                                                         aCellInfo->GetElemNum(iElem));
427                     if (!anElement) {
428                       anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
429                                                   FindNode(myMesh,aNodeIds[1]),
430                                                   FindNode(myMesh,aNodeIds[2]));
431                       isRenum = anIsElemNum;
432                     }
433                     break;
434                   case eTRIA3:
435                     aNbNodes = 3;
436                     if(anIsElemNum)
437                       anElement = myMesh->AddFaceWithID(aNodeIds[0],
438                                                         aNodeIds[1],
439                                                         aNodeIds[2],
440                                                         aCellInfo->GetElemNum(iElem));
441                     if (!anElement) {
442                       anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
443                                                   FindNode(myMesh,aNodeIds[1]),
444                                                   FindNode(myMesh,aNodeIds[2]));
445                       isRenum = anIsElemNum;
446                     }
447                     break;
448                   case eTRIA6:
449                     aNbNodes = 6;
450                     if(anIsElemNum)
451                       anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
452                                                         aNodeIds[2], aNodeIds[3],
453                                                         aNodeIds[4], aNodeIds[5],
454                                                         aCellInfo->GetElemNum(iElem));
455                     if (!anElement) {
456                       anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
457                                                   FindNode(myMesh,aNodeIds[1]),
458                                                   FindNode(myMesh,aNodeIds[2]),
459                                                   FindNode(myMesh,aNodeIds[3]),
460                                                   FindNode(myMesh,aNodeIds[4]),
461                                                   FindNode(myMesh,aNodeIds[5]));
462                       isRenum = anIsElemNum;
463                     }
464                     break;
465                   case eQUAD4:
466                     aNbNodes = 4;
467                     if(anIsElemNum)
468                       anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
469                                                         aNodeIds[2], aNodeIds[3],
470                                                         aCellInfo->GetElemNum(iElem));
471                     if (!anElement) {
472                       anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
473                                                   FindNode(myMesh,aNodeIds[1]),
474                                                   FindNode(myMesh,aNodeIds[2]),
475                                                   FindNode(myMesh,aNodeIds[3]));
476                       isRenum = anIsElemNum;
477                     }
478                     break;
479                   case eQUAD8:
480                     aNbNodes = 8;
481                     if(anIsElemNum)
482                       anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
483                                                         aNodeIds[2], aNodeIds[3],
484                                                         aNodeIds[4], aNodeIds[5],
485                                                         aNodeIds[6], aNodeIds[7],
486                                                         aCellInfo->GetElemNum(iElem));
487                     if (!anElement) {
488                       anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
489                                                   FindNode(myMesh,aNodeIds[1]),
490                                                   FindNode(myMesh,aNodeIds[2]),
491                                                   FindNode(myMesh,aNodeIds[3]),
492                                                   FindNode(myMesh,aNodeIds[4]),
493                                                   FindNode(myMesh,aNodeIds[5]),
494                                                   FindNode(myMesh,aNodeIds[6]),
495                                                   FindNode(myMesh,aNodeIds[7]));
496                       isRenum = anIsElemNum;
497                     }
498                     break;
499                   case eTETRA4:
500                     aNbNodes = 4;
501                     if(anIsElemNum)
502                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
503                                                           aNodeIds[2], aNodeIds[3],
504                                                           aCellInfo->GetElemNum(iElem));
505                     if (!anElement) {
506                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
507                                                     FindNode(myMesh,aNodeIds[1]),
508                                                     FindNode(myMesh,aNodeIds[2]),
509                                                     FindNode(myMesh,aNodeIds[3]));
510                       isRenum = anIsElemNum;
511                     }
512                     break;
513                   case eTETRA10:
514                     aNbNodes = 10;
515                     if(anIsElemNum)
516                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
517                                                           aNodeIds[2], aNodeIds[3],
518                                                           aNodeIds[4], aNodeIds[5],
519                                                           aNodeIds[6], aNodeIds[7],
520                                                           aNodeIds[8], aNodeIds[9],
521                                                           aCellInfo->GetElemNum(iElem));
522                     if (!anElement) {
523                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
524                                                     FindNode(myMesh,aNodeIds[1]),
525                                                     FindNode(myMesh,aNodeIds[2]),
526                                                     FindNode(myMesh,aNodeIds[3]),
527                                                     FindNode(myMesh,aNodeIds[4]),
528                                                     FindNode(myMesh,aNodeIds[5]),
529                                                     FindNode(myMesh,aNodeIds[6]),
530                                                     FindNode(myMesh,aNodeIds[7]),
531                                                     FindNode(myMesh,aNodeIds[8]),
532                                                     FindNode(myMesh,aNodeIds[9]));
533                       isRenum = anIsElemNum;
534                     }
535                     break;
536                   case ePYRA5:
537                     aNbNodes = 5;
538                     // There is some differnce between SMDS and MED
539                     if(anIsElemNum)
540                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
541                                                           aNodeIds[2], aNodeIds[3],
542                                                           aNodeIds[4],
543                                                           aCellInfo->GetElemNum(iElem));
544                     if (!anElement) {
545                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
546                                                     FindNode(myMesh,aNodeIds[1]),
547                                                     FindNode(myMesh,aNodeIds[2]),
548                                                     FindNode(myMesh,aNodeIds[3]),
549                                                     FindNode(myMesh,aNodeIds[4]));
550                       isRenum = anIsElemNum;
551                     }
552                     break;
553                   case ePYRA13:
554                     aNbNodes = 13;
555                     // There is some differnce between SMDS and MED
556                     if(anIsElemNum)
557                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
558                                                           aNodeIds[2], aNodeIds[3],
559                                                           aNodeIds[4], aNodeIds[5],
560                                                           aNodeIds[6], aNodeIds[7],
561                                                           aNodeIds[8], aNodeIds[9],
562                                                           aNodeIds[10], aNodeIds[11],
563                                                           aNodeIds[12],
564                                                           aCellInfo->GetElemNum(iElem));
565                     if (!anElement) {
566                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
567                                                     FindNode(myMesh,aNodeIds[1]),
568                                                     FindNode(myMesh,aNodeIds[2]),
569                                                     FindNode(myMesh,aNodeIds[3]),
570                                                     FindNode(myMesh,aNodeIds[4]),
571                                                     FindNode(myMesh,aNodeIds[5]),
572                                                     FindNode(myMesh,aNodeIds[6]),
573                                                     FindNode(myMesh,aNodeIds[7]),
574                                                     FindNode(myMesh,aNodeIds[8]),
575                                                     FindNode(myMesh,aNodeIds[9]),
576                                                     FindNode(myMesh,aNodeIds[10]),
577                                                     FindNode(myMesh,aNodeIds[11]),
578                                                     FindNode(myMesh,aNodeIds[12]));
579                       isRenum = anIsElemNum;
580                     }
581                     break;
582                   case ePENTA6:
583                     aNbNodes = 6;
584                     if(anIsElemNum)
585                       anElement = myMesh->AddVolumeWithID(aNodeIds[0],
586                                                           aNodeIds[1],
587                                                           aNodeIds[2],
588                                                           aNodeIds[3],
589                                                           aNodeIds[4],
590                                                           aNodeIds[5],
591                                                           aCellInfo->GetElemNum(iElem));
592                     if (!anElement) {
593                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
594                                                     FindNode(myMesh,aNodeIds[1]),
595                                                     FindNode(myMesh,aNodeIds[2]),
596                                                     FindNode(myMesh,aNodeIds[3]),
597                                                     FindNode(myMesh,aNodeIds[4]),
598                                                     FindNode(myMesh,aNodeIds[5]));
599                       isRenum = anIsElemNum;
600                     }
601                     break;
602                   case ePENTA15:
603                     aNbNodes = 15;
604                     if(anIsElemNum)
605                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
606                                                           aNodeIds[2], aNodeIds[3],
607                                                           aNodeIds[4], aNodeIds[5],
608                                                           aNodeIds[6], aNodeIds[7],
609                                                           aNodeIds[8], aNodeIds[9],
610                                                           aNodeIds[10], aNodeIds[11],
611                                                           aNodeIds[12], aNodeIds[13],
612                                                           aNodeIds[14],
613                                                           aCellInfo->GetElemNum(iElem));
614                     if (!anElement) {
615                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
616                                                     FindNode(myMesh,aNodeIds[1]),
617                                                     FindNode(myMesh,aNodeIds[2]),
618                                                     FindNode(myMesh,aNodeIds[3]),
619                                                   FindNode(myMesh,aNodeIds[4]),
620                                                     FindNode(myMesh,aNodeIds[5]),
621                                                     FindNode(myMesh,aNodeIds[6]),
622                                                     FindNode(myMesh,aNodeIds[7]),
623                                                     FindNode(myMesh,aNodeIds[8]),
624                                                     FindNode(myMesh,aNodeIds[9]),
625                                                     FindNode(myMesh,aNodeIds[10]),
626                                                     FindNode(myMesh,aNodeIds[11]),
627                                                     FindNode(myMesh,aNodeIds[12]),
628                                                     FindNode(myMesh,aNodeIds[13]),
629                                                     FindNode(myMesh,aNodeIds[14]));
630                       isRenum = anIsElemNum;
631                     }
632                     break;
633                   case eHEXA8:
634                     aNbNodes = 8;
635                     if(anIsElemNum)
636                       anElement = myMesh->AddVolumeWithID(aNodeIds[0],
637                                                           aNodeIds[1],
638                                                           aNodeIds[2],
639                                                           aNodeIds[3],
640                                                           aNodeIds[4],
641                                                           aNodeIds[5],
642                                                           aNodeIds[6],
643                                                           aNodeIds[7],
644                                                           aCellInfo->GetElemNum(iElem));
645                     if (!anElement) {
646                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
647                                                     FindNode(myMesh,aNodeIds[1]),
648                                                     FindNode(myMesh,aNodeIds[2]),
649                                                     FindNode(myMesh,aNodeIds[3]),
650                                                     FindNode(myMesh,aNodeIds[4]),
651                                                     FindNode(myMesh,aNodeIds[5]),
652                                                     FindNode(myMesh,aNodeIds[6]),
653                                                     FindNode(myMesh,aNodeIds[7]));
654                       isRenum = anIsElemNum;
655                     }
656                     break;
657                   case eHEXA20:
658                     aNbNodes = 20;
659                     if(anIsElemNum)
660                       anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
661                                                           aNodeIds[2], aNodeIds[3],
662                                                           aNodeIds[4], aNodeIds[5],
663                                                           aNodeIds[6], aNodeIds[7],
664                                                           aNodeIds[8], aNodeIds[9],
665                                                           aNodeIds[10], aNodeIds[11],
666                                                           aNodeIds[12], aNodeIds[13],
667                                                           aNodeIds[14], aNodeIds[15],
668                                                           aNodeIds[16], aNodeIds[17],
669                                                           aNodeIds[18], aNodeIds[19],
670                                                           aCellInfo->GetElemNum(iElem));
671                     if (!anElement) {
672                       anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
673                                                     FindNode(myMesh,aNodeIds[1]),
674                                                     FindNode(myMesh,aNodeIds[2]),
675                                                     FindNode(myMesh,aNodeIds[3]),
676                                                     FindNode(myMesh,aNodeIds[4]),
677                                                     FindNode(myMesh,aNodeIds[5]),
678                                                     FindNode(myMesh,aNodeIds[6]),
679                                                     FindNode(myMesh,aNodeIds[7]),
680                                                     FindNode(myMesh,aNodeIds[8]),
681                                                     FindNode(myMesh,aNodeIds[9]),
682                                                     FindNode(myMesh,aNodeIds[10]),
683                                                     FindNode(myMesh,aNodeIds[11]),
684                                                     FindNode(myMesh,aNodeIds[12]),
685                                                     FindNode(myMesh,aNodeIds[13]),
686                                                     FindNode(myMesh,aNodeIds[14]),
687                                                     FindNode(myMesh,aNodeIds[15]),
688                                                     FindNode(myMesh,aNodeIds[16]),
689                                                     FindNode(myMesh,aNodeIds[17]),
690                                                     FindNode(myMesh,aNodeIds[18]),
691                                                     FindNode(myMesh,aNodeIds[19]));
692                       isRenum = anIsElemNum;
693                     }
694                     break;
695                   }
696
697 #ifndef _DEXCEPT_
698                 }catch(const std::exception& exc){
699                   //INFOS("Follow exception was cought:\n\t"<<exc.what());
700                   aResult = DRS_FAIL;
701                 }catch(...){
702                   //INFOS("Unknown exception was cought !!!");
703                   aResult = DRS_FAIL;
704                 }
705 #endif          
706                 if (!anElement) {
707                   aResult = DRS_WARN_SKIP_ELEM;
708                 }
709                 else {
710                   if (isRenum) {
711                     anIsElemNum = eFAUX;
712                     takeNumbers = false;
713                     if (aResult < DRS_WARN_RENUMBER)
714                       aResult = DRS_WARN_RENUMBER;
715                   }
716                   if ( checkFamilyID ( aFamily, aFamNum )) {
717                     // Save reference to this element from its family
718                     myFamilies[aFamNum]->AddElement(anElement);
719                     myFamilies[aFamNum]->SetType(anElement->GetType());
720                   }
721                 }
722               }
723             }}
724           }
725         }
726       }
727     }
728 #ifndef _DEXCEPT_
729   }catch(const std::exception& exc){
730     INFOS("Follow exception was cought:\n\t"<<exc.what());
731     aResult = DRS_FAIL;
732   }catch(...){
733     INFOS("Unknown exception was cought !!!");
734     aResult = DRS_FAIL;
735   }
736 #endif
737   if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
738   return aResult;
739 }
740
741 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
742 {
743   list<string> aMeshNames;
744
745   try {
746     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
747     theStatus = DRS_OK;
748     PWrapper aMed = CrWrapper(myFile);
749
750     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
751       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
752         // Reading the MED mesh
753         //---------------------
754         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
755         aMeshNames.push_back(aMeshInfo->GetName());
756       }
757     }
758   }catch(const std::exception& exc){
759     INFOS("Follow exception was cought:\n\t"<<exc.what());
760     theStatus = DRS_FAIL;
761   }catch(...){
762     INFOS("Unknown exception was cought !!!");
763     theStatus = DRS_FAIL;
764   }
765
766   return aMeshNames;
767 }
768
769 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
770 {
771   list<TNameAndType> aResult;
772   set<TNameAndType> aResGroupNames;
773
774   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
775   for (; aFamsIter != myFamilies.end(); aFamsIter++)
776   {
777     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
778     const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
779     set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
780     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
781     {
782       const set< SMDSAbs_ElementType >& types = aFamily->GetTypes();
783       set< SMDSAbs_ElementType >::const_iterator type = types.begin();
784       for ( ; type != types.end(); ++type )
785       {
786         TNameAndType aNameAndType = make_pair( *aGrNamesIter, *type );
787         if ( aResGroupNames.insert( aNameAndType ).second ) {
788           aResult.push_back( aNameAndType );
789         }
790       }
791     }
792   }
793
794   return aResult;
795 }
796
797 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
798 {
799   string aGroupName (theGroup->GetStoreName());
800   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
801
802   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
803   for (; aFamsIter != myFamilies.end(); aFamsIter++)
804   {
805     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
806     if (aFamily->GetTypes().count( theGroup->GetType() ) && aFamily->MemberOf(aGroupName))
807     {
808       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
809       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
810       for (; anElemsIter != anElements.end(); anElemsIter++)
811       {
812         const SMDS_MeshElement * element = *anElemsIter;
813         if ( element->GetType() == theGroup->GetType() ) // Issue 0020576
814           theGroup->SMDSGroup().Add(element);
815       }
816       int aGroupAttrVal = aFamily->GetGroupAttributVal();
817       if( aGroupAttrVal != 0)
818         theGroup->SetColorGroup(aGroupAttrVal);
819 //       if ( element ) -- Issue 0020576
820 //         theGroup->SetType( theGroup->SMDSGroup().GetType() );
821     }
822   }
823 }
824
825 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
826                                            const int theId)
827 {
828   char submeshGrpName[ 30 ];
829   sprintf( submeshGrpName, "SubMesh %d", theId );
830   string aName (submeshGrpName);
831   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
832   for (; aFamsIter != myFamilies.end(); aFamsIter++)
833   {
834     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
835     if (aFamily->MemberOf(aName))
836     {
837       const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
838       set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
839       if (aFamily->GetType() == SMDSAbs_Node)
840       {
841         for (; anElemsIter != anElements.end(); anElemsIter++)
842         {
843           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
844           theSubMesh->AddNode(node);
845         }
846       }
847       else
848       {
849         for (; anElemsIter != anElements.end(); anElemsIter++)
850         {
851           theSubMesh->AddElement(*anElemsIter);
852         }
853       }
854     }
855   }
856 }
857
858 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
859 {
860   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
861   for (; aFamsIter != myFamilies.end(); aFamsIter++)
862   {
863     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
864     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
865     set<string>::iterator aGrNamesIter = aGroupNames.begin();
866     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
867     {
868       string aName = *aGrNamesIter;
869       // Check, if this is a Group or SubMesh name
870       if (aName.substr(0, 7) == string("SubMesh"))
871       {
872         int Id = atoi(string(aName).substr(7).c_str());
873         set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
874         set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
875         if (aFamily->GetType() == SMDSAbs_Node)
876         {
877           for (; anElemsIter != anElements.end(); anElemsIter++)
878           {
879             SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
880               ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
881             // find out a shape type
882             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
883             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
884             switch ( aShapeType ) {
885             case TopAbs_FACE:
886               myMesh->SetNodeOnFace(node, Id); break;
887             case TopAbs_EDGE:
888               myMesh->SetNodeOnEdge(node, Id); break;
889             case TopAbs_VERTEX:
890               myMesh->SetNodeOnVertex(node, Id); break;
891             default:
892               myMesh->SetNodeInVolume(node, Id);
893             }
894           }
895         }
896         else
897         {
898           for (; anElemsIter != anElements.end(); anElemsIter++)
899           {
900             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
901           }
902         }
903       }
904     }
905   }
906 }
907 /*!
908  * \brief Ensure aFamily to have required ID
909  * \param aFamily - a family to check and update
910  * \param anID - an ID aFamily should have
911  * \retval bool  - true if successful
912  */
913 bool DriverMED_R_SMESHDS_Mesh::checkFamilyID(DriverMED_FamilyPtr & aFamily, int anID) const
914 {
915   if ( !aFamily || aFamily->GetId() != anID ) {
916     map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
917     if ( i_fam == myFamilies.end() )
918       return false;
919     aFamily = i_fam->second;
920   }
921   return ( aFamily->GetId() == anID );
922 }
923
924
925 /*! \brief Reading the structured mesh and convert to non structured (by filling of smesh structure for non structured mesh)
926  * \param theWrapper  - PWrapper const pointer
927  * \param theMeshInfo - PMeshInfo const pointer
928  * \return TRUE, if successfully. Else FALSE
929  */
930 bool DriverMED_R_SMESHDS_Mesh::buildMeshGrille(const MED::PWrapper& theWrapper,
931                                                const MED::PMeshInfo& theMeshInfo)
932 {
933   bool res = true;
934
935   MED::PGrilleInfo aGrilleInfo = theWrapper->GetPGrilleInfo(theMeshInfo);
936   MED::TInt aNbNodes = aGrilleInfo->GetNbNodes();
937   MED::TInt aNbCells = aGrilleInfo->GetNbCells();
938   MED::TInt aMeshDim = theMeshInfo->GetDim();
939   DriverMED_FamilyPtr aFamily;
940   for(MED::TInt iNode=0;iNode < aNbNodes; iNode++){
941     double aCoords[3] = {0.0, 0.0, 0.0};
942     const SMDS_MeshNode* aNode;
943     MED::TNodeCoord aMEDNodeCoord = aGrilleInfo->GetCoord(iNode);
944     for(MED::TInt iDim=0;iDim<aMeshDim;iDim++)
945       aCoords[(int)iDim] = aMEDNodeCoord[(int)iDim];
946     aNode = myMesh->AddNodeWithID(aCoords[0],aCoords[1],aCoords[2],(int)iNode);
947
948     if((aGrilleInfo->myFamNumNode).size() > 0){
949       TInt aFamNum = aGrilleInfo->GetFamNumNode(iNode);
950       if ( checkFamilyID ( aFamily, aFamNum ))
951         {
952           aFamily->AddElement(aNode);
953           aFamily->SetType(SMDSAbs_Node);
954         }
955     }
956     
957   }
958
959   SMDS_MeshElement* anElement = NULL;
960   MED::TIntVector aNodeIds;
961   for(MED::TInt iCell=0;iCell < aNbCells; iCell++){
962     aNodeIds = aGrilleInfo->GetConn(iCell);
963     switch(aGrilleInfo->GetGeom()){
964     case MED::eSEG2:
965       if(aNodeIds.size() != 2){
966         res = false;
967         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 2!="<<aNodeIds.size());
968       }
969       anElement = myMesh->AddEdgeWithID(aNodeIds[0],
970                                         aNodeIds[1],
971                                         iCell);
972       break;
973     case MED::eQUAD4:
974       if(aNodeIds.size() != 4){
975         res = false;
976         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 4!="<<aNodeIds.size());
977       }
978       anElement = myMesh->AddFaceWithID(aNodeIds[0],
979                                         aNodeIds[2],
980                                         aNodeIds[3],
981                                         aNodeIds[1],
982                                         iCell);
983       break;
984     case MED::eHEXA8:
985       if(aNodeIds.size() != 8){
986         res = false;
987         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 8!="<<aNodeIds.size());
988       }
989       anElement = myMesh->AddVolumeWithID(aNodeIds[0],
990                                           aNodeIds[2],
991                                           aNodeIds[3],
992                                           aNodeIds[1],
993                                           aNodeIds[4],
994                                           aNodeIds[6],
995                                           aNodeIds[7],
996                                           aNodeIds[5],
997                                           iCell);
998       break;
999     default:
1000       break;
1001     }
1002     
1003     if((aGrilleInfo->myFamNum).size() > 0){
1004       TInt aFamNum = aGrilleInfo->GetFamNum(iCell);
1005       if ( checkFamilyID ( aFamily, aFamNum )){
1006         aFamily->AddElement(anElement);
1007         aFamily->SetType(anElement->GetType());
1008       }
1009     }
1010   }
1011
1012   return res;
1013 }