Salome HOME
fix biquadratic quadratic pentahedron(18 nodes prism)
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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
29 #include "DriverMED_Family.h"
30 #include "SMESHDS_Group.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Comment.hxx"
33
34 #include "MED_CoordUtils.hxx"
35 #include "MED_Factory.hxx"
36 #include "MED_Utilities.hxx"
37
38 #include <NCollection_Map.hxx>
39
40 #include "utilities.h"
41
42 //#include <stdlib.h>
43
44 #ifdef _DEBUG_
45 static int MYDEBUG = 0;
46 //#define _DEXCEPT_
47 #else
48 static int MYDEBUG = 0;
49 #endif
50
51 #define _EDF_NODE_IDS_
52
53 using namespace MED;
54 using namespace std;
55
56 typedef std::map<int, DriverMED_FamilyPtr> TID2FamilyMap;
57
58 namespace DriverMED
59 {
60   bool buildMeshGrille(const MED::PWrapper&  theWrapper,
61                        const MED::PMeshInfo& theMeshInfo,
62                        SMESHDS_Mesh*         theMesh,
63                        const TID2FamilyMap&  myFamilies);
64   /*!
65    * \brief Ensure aFamily has a required ID
66    * \param aFamily - a family to check
67    * \param anID - an ID aFamily should have
68    * \param myFamilies - a map of the family ID to the Family
69    * \retval bool  - true if successful
70    */
71   bool checkFamilyID(DriverMED_FamilyPtr & aFamily,
72                      int                   anID,
73                      const TID2FamilyMap&  myFamilies);
74
75
76   const SMDS_MeshNode* FindNode(const SMDS_Mesh* theMesh, TInt theId)
77   {
78     const SMDS_MeshNode* aNode = theMesh->FindNode(theId);
79     if(aNode) return aNode;
80     EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
81   }
82
83 }
84
85 //================================================================================
86 /*!
87  * \brief Stores a mesh name
88  */
89 //================================================================================
90
91 void DriverMED_R_SMESHDS_Mesh::SetMeshName(string theMeshName)
92 {
93   myMeshName = theMeshName;
94 }
95
96 //================================================================================
97 /*!
98  * \brief Reads a med file
99  */
100 //================================================================================
101
102 Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
103 {
104   using namespace DriverMED;
105
106   Status aResult = DRS_FAIL;
107   bool isDescConn = false; // Mantis issue 0020483
108 #ifndef _DEXCEPT_
109   try {
110 #endif
111     myFamilies.clear();
112     if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
113     PWrapper aMed = CrWrapper(myFile,true);
114
115     aResult = DRS_EMPTY;
116     TInt aNbMeshes = aMed->GetNbMeshes();
117     for (int iMesh = 0; iMesh < aNbMeshes; iMesh++)
118     {
119       // Reading the MED mesh
120       //---------------------
121       PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
122
123       string aMeshName;
124       if (myMeshId != -1) aMeshName = SMESH_Comment( myMeshId );
125       else                aMeshName = myMeshName;
126
127       if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
128       if ( aMeshName != aMeshInfo->GetName() ) continue;
129       aResult = DRS_OK;
130
131       // Reading MED families to the temporary structure
132       //------------------------------------------------
133       TErr anErr;
134       TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
135       if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
136       for (TInt iFam = 0; iFam < aNbFams; iFam++)
137       {
138         PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
139         if(anErr >= 0){
140           TInt aFamId = aFamilyInfo->GetId();
141           if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
142
143           DriverMED_FamilyPtr aFamily (new DriverMED_Family);
144
145           TInt aNbGrp = aFamilyInfo->GetNbGroup();
146           if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
147           bool isAttrOk = false;
148           if(aFamilyInfo->GetNbAttr() == aNbGrp)
149             isAttrOk = true;
150           for (TInt iGr = 0; iGr < aNbGrp; iGr++)
151           {
152             string aGroupName = aFamilyInfo->GetGroupName(iGr);
153             if ( isAttrOk ) {
154               TInt anAttrVal = aFamilyInfo->GetAttrVal(iGr);
155               aFamily->SetGroupAttributVal(anAttrVal);
156             }
157             if(MYDEBUG) MESSAGE(aGroupName);
158             if ( strncmp( aGroupName.c_str(), NIG_GROUP_PREFIX, strlen(NIG_GROUP_PREFIX) ) != 0 )
159               aFamily->AddGroupName(aGroupName);
160           }
161           aFamily->SetId( aFamId );
162           myFamilies[aFamId] = aFamily;
163         }
164       }
165
166       if (aMeshInfo->GetType() == MED::eSTRUCTURE)
167       {
168         /*bool aRes = */DriverMED::buildMeshGrille(aMed,aMeshInfo,myMesh,myFamilies);
169         continue;
170       }
171
172       // Reading MED nodes to the corresponding SMDS structure
173       //------------------------------------------------------
174       PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
175       if (!aNodeInfo) {
176         aResult = addMessage("No nodes", /*isFatal=*/true );
177         continue;
178       }
179       aMeshInfo->myDim=aMeshInfo->mySpaceDim;// ignore meshdim in MEDFile because it can be false
180       PCoordHelper aCoordHelper = GetCoordHelper(aNodeInfo);
181
182       EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
183       TInt aNbElems = aNodeInfo->GetNbElem();
184       if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
185       DriverMED_FamilyPtr aFamily;
186       for ( TInt iElem = 0; iElem < aNbElems; iElem++ )
187       {
188         TCCoordSlice aCoordSlice = aNodeInfo->GetCoordSlice(iElem);
189         double aCoords[3] = {0.0, 0.0, 0.0};
190         for(TInt iDim = 0; iDim < 3; iDim++)
191           aCoords[iDim] = aCoordHelper->GetCoord(aCoordSlice,iDim);
192         const SMDS_MeshNode* aNode;
193         if ( anIsNodeNum ) {
194           aNode = myMesh->AddNodeWithID
195             (aCoords[0],aCoords[1],aCoords[2],aNodeInfo->GetElemNum(iElem));
196         }
197         else {
198           aNode = myMesh->AddNodeWithID
199             (aCoords[0],aCoords[1],aCoords[2], iElem+1);
200         }
201
202         // Save reference to this node from its family
203         TInt aFamNum = aNodeInfo->GetFamNum(iElem);
204         if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies ))
205         {
206           aFamily->AddElement(aNode);
207           aFamily->SetType(SMDSAbs_Node);
208         }
209       }
210
211       // Are there any MED cells in descending connectivity
212       // Mantis issue 0020483
213       //---------------------------------------------------
214       NCollection_Map<EEntiteMaillage> aDescendingEntitiesMap;
215       if (!isDescConn) {
216         MED::TEntityInfo aEntityInfoDesc = aMed->GetEntityInfo(aMeshInfo, eDESC);
217         MED::TEntityInfo::iterator anEntityIterDesc = aEntityInfoDesc.begin();
218         //for (; anEntityIterDesc != aEntityInfoDesc.end() && !isDescConn; anEntityIterDesc++) {
219         for (; anEntityIterDesc != aEntityInfoDesc.end(); anEntityIterDesc++) {
220           const EEntiteMaillage& anEntity = anEntityIterDesc->first;
221           aDescendingEntitiesMap.Add(anEntity);
222           //if (anEntity != eNOEUD) isDescConn = true;
223         }
224       }
225
226       // Reading pre information about all MED cells
227       //--------------------------------------------
228       typedef MED::TVector<int> TNodeIds;
229       bool takeNumbers = true;  // initially we trust the numbers from file
230       MED::TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo, eNOD);
231       MED::TEntityInfo::iterator anEntityIter = aEntityInfo.begin();
232
233       for (; anEntityIter != aEntityInfo.end(); anEntityIter++)
234       {
235         const EEntiteMaillage& anEntity = anEntityIter->first;
236         aDescendingEntitiesMap.Remove(anEntity); // Mantis issue 0020483
237         if (anEntity == eNOEUD) continue;
238
239         // Reading MED cells to the corresponding SMDS structure
240         //------------------------------------------------------
241         const MED::TGeom2Size& aGeom2Size = anEntityIter->second;
242         MED::TGeom2Size::const_iterator aGeom2SizeIter = aGeom2Size.begin();
243         for ( ; aGeom2SizeIter != aGeom2Size.end(); aGeom2SizeIter++)
244         {
245           const EGeometrieElement& aGeom = aGeom2SizeIter->first;
246
247           if ( anEntity == eSTRUCT_ELEMENT ) // MED_BALL (issue 0021459)
248           {
249             PBallInfo aBallInfo = aMed->GetPBallInfo(aMeshInfo);
250             TInt      aNbBalls  = aBallInfo->GetNbElem();
251
252             EBooleen anIsElemNum = takeNumbers ? aBallInfo->IsElemNum() : eFAUX;
253             if ( anIsElemNum && aBallInfo->myElemNum->empty() )
254               anIsElemNum = eFAUX;
255
256             // get supporting nodes
257             TNodeIds aNodeIds;
258 #ifdef _EDF_NODE_IDS_
259             if(anIsNodeNum) {
260               aNodeIds.resize( aNbBalls );
261               for(TInt iBall = 0; iBall < aNbBalls && anIsNodeNum; iBall++)
262               {
263                 aNodeIds[iBall] = aNodeInfo->GetElemNum( (*aBallInfo->myConn)[ iBall ]-1 );
264                 anIsNodeNum = myMesh->FindNode( aNodeIds[iBall] ) ? eVRAI : eFAUX;
265               }
266             }
267 #endif
268             if ( !anIsNodeNum )
269               aNodeIds.swap( *(aBallInfo->myConn ));
270
271             // allocate array of diameters
272             vtkIdType maxID = myMesh->MaxElementID() + aNbBalls;
273             if ( anIsElemNum && !aBallInfo->myElemNum->empty() )
274               maxID = *std::max_element( aBallInfo->myElemNum->begin(),
275                                          aBallInfo->myElemNum->end() );
276             myMesh->getGrid()->AllocateDiameters( maxID ); // performance optimization
277
278             // create balls
279             SMDS_MeshElement* anElement;
280             DriverMED_FamilyPtr aFamily;
281             for ( TInt iBall = 0; iBall < aNbBalls; iBall++)
282             {
283               anElement = 0;
284               if ( anIsElemNum ) {
285                 if (!(anElement = myMesh->AddBallWithID( aNodeIds[iBall],
286                                                          aBallInfo->myDiameters[iBall],
287                                                          aBallInfo->GetElemNum(iBall))))
288                   anIsElemNum = eFAUX;
289               }
290               if ( !anElement )
291                 myMesh->AddBall( myMesh->FindNode( aNodeIds[iBall]),
292                                  aBallInfo->myDiameters[iBall] );
293
294               // Save reference to this element from its family
295               TInt aFamNum = aBallInfo->GetFamNum(iBall);
296               if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies ))
297               {
298                 aFamily->AddElement(anElement);
299                 aFamily->SetType( SMDSAbs_Ball );
300               }
301             }
302
303             if ( !anIsElemNum &&
304                  ( takeNumbers && aBallInfo->IsElemNum() && !aBallInfo->myElemNum->empty() ))
305               if ( aResult < DRS_WARN_RENUMBER )
306                 aResult = DRS_WARN_RENUMBER;
307
308             continue;
309           } // MED_BALL
310
311           switch(aGeom) {
312           // case ePOINT1: ## PAL16410
313           //     break;
314           case ePOLYGONE:
315           case ePOLYGON2:
316           {
317             PPolygoneInfo aPolygoneInfo = aMed->GetPPolygoneInfo(aMeshInfo,anEntity,aGeom);
318             EBooleen anIsElemNum = takeNumbers ? aPolygoneInfo->IsElemNum() : eFAUX;
319
320             typedef SMDS_MeshFace* (SMESHDS_Mesh::* FAddPolyWithID)
321               (const std::vector<int> & nodes_ids, const int ID);
322             typedef SMDS_MeshFace* (SMESHDS_Mesh::* FAddPolygon)
323               (const std::vector<const SMDS_MeshNode*> & nodes);
324
325             FAddPolyWithID addPolyWithID = & SMESHDS_Mesh::AddPolygonalFaceWithID;
326             FAddPolygon       addPolygon = & SMESHDS_Mesh::AddPolygonalFace;
327             if ( aGeom == ePOLYGON2 ) {
328               addPolyWithID = & SMESHDS_Mesh::AddQuadPolygonalFaceWithID;
329               addPolygon    = & SMESHDS_Mesh::AddQuadPolygonalFace;
330             }
331             TNodeIds aNodeIds;
332             vector<const SMDS_MeshNode*> aNodes;
333             const TInt aNbElem = aPolygoneInfo->GetNbElem();
334             for ( TInt iElem = 0; iElem < aNbElem; iElem++ )
335             {
336               MED::TCConnSlice aConnSlice = aPolygoneInfo->GetConnSlice(iElem);
337               TInt aNbConn = aPolygoneInfo->GetNbConn(iElem);
338               aNodeIds.resize( aNbConn );
339 #ifdef _EDF_NODE_IDS_
340               if(anIsNodeNum)
341                 for(TInt iConn = 0; iConn < aNbConn; iConn++)
342                   aNodeIds[iConn] = aNodeInfo->GetElemNum(aConnSlice[iConn] - 1);
343               else
344                 for(TInt iConn = 0; iConn < aNbConn; iConn++)
345                   aNodeIds[iConn] = aConnSlice[iConn];
346 #else
347               for(TInt iConn = 0; iConn < aNbConn; iConn++)
348                 aNodeIds[iConn] = aConnSlice[iConn];
349 #endif
350               bool isRenum = false;
351               SMDS_MeshElement* anElement = NULL;
352               TInt aFamNum = aPolygoneInfo->GetFamNum(iElem);
353 #ifndef _DEXCEPT_
354               try {
355 #endif
356                 if ( anIsElemNum ) {
357                   TInt anElemId = aPolygoneInfo->GetElemNum( iElem );
358                   anElement = (myMesh->*addPolyWithID)( aNodeIds, anElemId );
359                 }
360                 if ( !anElement ) {
361                   aNodes.resize( aNbConn );
362                   for ( TInt iConn = 0; iConn < aNbConn; iConn++ )
363                     aNodes[iConn] = FindNode( myMesh, aNodeIds[iConn] );
364                   anElement = (myMesh->*addPolygon)( aNodes );
365                   isRenum = anIsElemNum;
366                 }
367 #ifndef _DEXCEPT_
368               } catch(const std::exception& exc) {
369                 aResult = addMessage( exc.what(), /*isFatal=*/true );
370               } catch (...) {
371                 aResult = addMessage( "Unknown exception", /*isFatal=*/true );
372               }
373 #endif
374               if ( !anElement ) {
375                 aResult = DRS_WARN_SKIP_ELEM;
376               }
377               else {
378                 if ( isRenum ) {
379                   anIsElemNum = eFAUX;
380                   takeNumbers = false;
381                   if(aResult < DRS_WARN_RENUMBER)
382                     aResult = DRS_WARN_RENUMBER;
383                 }
384                 if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies ))
385                 {
386                   // Save reference to this element from its family
387                   aFamily->AddElement(anElement);
388                   aFamily->SetType(anElement->GetType());
389                 }
390               }
391             }
392             break;
393           }
394           case ePOLYEDRE: {
395             PPolyedreInfo aPolyedreInfo = aMed->GetPPolyedreInfo(aMeshInfo,anEntity,aGeom);
396             EBooleen anIsElemNum = takeNumbers ? aPolyedreInfo->IsElemNum() : eFAUX;
397
398             TInt aNbElem = aPolyedreInfo->GetNbElem();
399             for(TInt iElem = 0; iElem < aNbElem; iElem++){
400               MED::TCConnSliceArr aConnSliceArr = aPolyedreInfo->GetConnSliceArr(iElem);
401               TInt aNbFaces = aConnSliceArr.size();
402               typedef MED::TVector<int> TQuantities;
403               TQuantities aQuantities(aNbFaces);
404               TInt aNbNodes = aPolyedreInfo->GetNbNodes(iElem);
405               TNodeIds aNodeIds(aNbNodes);
406               for(TInt iFace = 0, iNode = 0; iFace < aNbFaces; iFace++){
407                 MED::TCConnSlice aConnSlice = aConnSliceArr[iFace];
408                 TInt aNbConn = aConnSlice.size();
409                 aQuantities[iFace] = aNbConn;
410 #ifdef _EDF_NODE_IDS_
411                 if(anIsNodeNum)
412                   for(TInt iConn = 0; iConn < aNbConn; iConn++)
413                   {
414                     aNodeIds[iNode] = aNodeInfo->GetElemNum(aConnSlice[iConn] - 1);
415                     iNode++;
416                   }
417                 else
418                   for(TInt iConn = 0; iConn < aNbConn; iConn++)
419                   {
420                     aNodeIds[iNode++] = aConnSlice[iConn];
421                   }
422 #else
423                 for(TInt iConn = 0; iConn < aNbConn; iConn++)
424                 {
425                   aNodeIds[iNode++] = aConnSlice[iConn];
426                 }
427 #endif          
428               }
429
430               bool isRenum = false;
431               SMDS_MeshElement* anElement = NULL;
432               TInt aFamNum = aPolyedreInfo->GetFamNum(iElem);
433                 
434 #ifndef _DEXCEPT_
435               try{
436 #endif
437                 if(anIsElemNum){
438                   TInt anElemId = aPolyedreInfo->GetElemNum(iElem);
439                   anElement = myMesh->AddPolyhedralVolumeWithID(aNodeIds,aQuantities,anElemId);
440                 }
441                 if(!anElement){
442                   vector<const SMDS_MeshNode*> aNodes(aNbNodes);
443                   for(TInt iConn = 0; iConn < aNbNodes; iConn++)
444                     aNodes[iConn] = FindNode(myMesh,aNodeIds[iConn]);
445                   anElement = myMesh->AddPolyhedralVolume(aNodes,aQuantities);
446                   isRenum = anIsElemNum;
447                 }
448 #ifndef _DEXCEPT_
449               }catch(const std::exception& exc){
450                 aResult = DRS_FAIL;
451               }catch(...){
452                 aResult = DRS_FAIL;
453               }
454 #endif          
455               if(!anElement){
456                 aResult = DRS_WARN_SKIP_ELEM;
457               }else{
458                 if(isRenum){
459                   anIsElemNum = eFAUX;
460                   takeNumbers = false;
461                   if (aResult < DRS_WARN_RENUMBER)
462                     aResult = DRS_WARN_RENUMBER;
463                 }
464                 if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies )) {
465                   // Save reference to this element from its family
466                   aFamily->AddElement(anElement);
467                   aFamily->SetType(anElement->GetType());
468                 }
469               }
470             }
471             break;
472           }
473           default: {
474             PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
475             EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
476             TInt aNbElems = aCellInfo->GetNbElem();
477             if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
478             if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
479
480             TInt aNbNodes = -1;
481             switch(aGeom){
482             case eSEG2:    aNbNodes = 2;  break;
483             case eSEG3:    aNbNodes = 3;  break;
484             case eTRIA3:   aNbNodes = 3;  break;
485             case eTRIA6:   aNbNodes = 6;  break;
486             case eTRIA7:   aNbNodes = 7;  break;
487             case eQUAD4:   aNbNodes = 4;  break;
488             case eQUAD8:   aNbNodes = 8;  break;
489             case eQUAD9:   aNbNodes = 9;  break;
490             case eTETRA4:  aNbNodes = 4;  break;
491             case eTETRA10: aNbNodes = 10; break;
492             case ePYRA5:   aNbNodes = 5;  break;
493             case ePYRA13:  aNbNodes = 13; break;
494             case ePENTA6:  aNbNodes = 6;  break;
495             case ePENTA15: aNbNodes = 15; break;
496             case ePENTA18: aNbNodes = 18; break;
497             case eHEXA8:   aNbNodes = 8;  break;
498             case eHEXA20:  aNbNodes = 20; break;
499             case eHEXA27:  aNbNodes = 27; break;
500             case eOCTA12:  aNbNodes = 12; break;
501             case ePOINT1:  aNbNodes = 1;  break;
502             default:;
503             }
504             vector<TInt> aNodeIds(aNbNodes);
505             for ( int iElem = 0; iElem < aNbElems; iElem++ )
506             {
507               bool anIsValidConnect = false;
508               TCConnSlice aConnSlice = aCellInfo->GetConnSlice(iElem);
509 #ifndef _DEXCEPT_
510               try{
511 #endif
512 #ifdef _EDF_NODE_IDS_
513                 if(anIsNodeNum)
514                   for(int iNode = 0; iNode < aNbNodes; iNode++)
515                     aNodeIds[iNode] = aNodeInfo->GetElemNum(aConnSlice[iNode] - 1);
516                 else
517                   for(int iNode = 0; iNode < aNbNodes; iNode++)
518                     aNodeIds[iNode] = aConnSlice[iNode];
519 #else
520                 for(int iNode = 0; iNode < aNbNodes; iNode++)
521                   aNodeIds[iNode] = aConnSlice[iNode];
522 #endif
523                 anIsValidConnect = true;
524 #ifndef _DEXCEPT_
525               }catch(const std::exception& exc){
526                 INFOS("Following exception was caught:\n\t"<<exc.what());
527                 aResult = addMessage( exc.what(), /*isFatal=*/true );
528               }catch(...){
529                 INFOS("Unknown exception was caught !!!");
530                 aResult = addMessage( "Unknown exception", /*isFatal=*/true );
531               }
532 #endif          
533               if(!anIsValidConnect)
534                 continue;
535
536               bool isRenum = false;
537               const SMDS_MeshElement* anElement = NULL;
538               TInt aFamNum = aCellInfo->GetFamNum(iElem);
539 #ifndef _DEXCEPT_
540               try{
541 #endif
542                 //MESSAGE("Try to create element # " << iElem << " with id = "
543                 //        << aCellInfo->GetElemNum(iElem));
544                 switch(aGeom) {
545                 case ePOINT1:
546                   //anElement = FindNode(myMesh,aNodeIds[0]);
547                   if(anIsElemNum)
548                     anElement = myMesh->Add0DElementWithID
549                       (aNodeIds[0], aCellInfo->GetElemNum(iElem));
550                   if (!anElement) {
551                     anElement = myMesh->Add0DElement(FindNode(myMesh,aNodeIds[0]));
552                     isRenum = anIsElemNum;
553                   }
554                   break;
555                 case eSEG2:
556                   if(anIsElemNum)
557                     anElement = myMesh->AddEdgeWithID(aNodeIds[0],
558                                                       aNodeIds[1],
559                                                       aCellInfo->GetElemNum(iElem));
560                   if (!anElement) {
561                     anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
562                                                 FindNode(myMesh,aNodeIds[1]));
563                     isRenum = anIsElemNum;
564                   }
565                   break;
566                 case eSEG3:
567                   if(anIsElemNum)
568                     anElement = myMesh->AddEdgeWithID(aNodeIds[0],
569                                                       aNodeIds[1],
570                                                       aNodeIds[2],
571                                                       aCellInfo->GetElemNum(iElem));
572                   if (!anElement) {
573                     anElement = myMesh->AddEdge(FindNode(myMesh,aNodeIds[0]),
574                                                 FindNode(myMesh,aNodeIds[1]),
575                                                 FindNode(myMesh,aNodeIds[2]));
576                     isRenum = anIsElemNum;
577                   }
578                   break;
579                 case eTRIA3:
580                   aNbNodes = 3;
581                   if(anIsElemNum)
582                     anElement = myMesh->AddFaceWithID(aNodeIds[0],
583                                                       aNodeIds[1],
584                                                       aNodeIds[2],
585                                                       aCellInfo->GetElemNum(iElem));
586                   if (!anElement) {
587                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
588                                                 FindNode(myMesh,aNodeIds[1]),
589                                                 FindNode(myMesh,aNodeIds[2]));
590                     isRenum = anIsElemNum;
591                   }
592                   break;
593                 case eTRIA6:
594                   aNbNodes = 6;
595                   if(anIsElemNum)
596                     anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
597                                                       aNodeIds[2], aNodeIds[3],
598                                                       aNodeIds[4], aNodeIds[5],
599                                                       aCellInfo->GetElemNum(iElem));
600                   if (!anElement) {
601                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
602                                                 FindNode(myMesh,aNodeIds[1]),
603                                                 FindNode(myMesh,aNodeIds[2]),
604                                                 FindNode(myMesh,aNodeIds[3]),
605                                                 FindNode(myMesh,aNodeIds[4]),
606                                                 FindNode(myMesh,aNodeIds[5]));
607                     isRenum = anIsElemNum;
608                   }
609                   break;
610                 case eTRIA7:
611                   aNbNodes = 7;
612                   if(anIsElemNum)
613                     anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
614                                                       aNodeIds[2], aNodeIds[3],
615                                                       aNodeIds[4], aNodeIds[5], aNodeIds[6],
616                                                       aCellInfo->GetElemNum(iElem));
617                   if (!anElement) {
618                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
619                                                 FindNode(myMesh,aNodeIds[1]),
620                                                 FindNode(myMesh,aNodeIds[2]),
621                                                 FindNode(myMesh,aNodeIds[3]),
622                                                 FindNode(myMesh,aNodeIds[4]),
623                                                 FindNode(myMesh,aNodeIds[5]),
624                                                 FindNode(myMesh,aNodeIds[6]));
625                     isRenum = anIsElemNum;
626                   }
627                   break;
628                 case eQUAD4:
629                   aNbNodes = 4;
630                   if(anIsElemNum)
631                     anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
632                                                       aNodeIds[2], aNodeIds[3],
633                                                       aCellInfo->GetElemNum(iElem));
634                   if (!anElement) {
635                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
636                                                 FindNode(myMesh,aNodeIds[1]),
637                                                 FindNode(myMesh,aNodeIds[2]),
638                                                 FindNode(myMesh,aNodeIds[3]));
639                     isRenum = anIsElemNum;
640                   }
641                   break;
642                 case eQUAD8:
643                   aNbNodes = 8;
644                   if(anIsElemNum)
645                     anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
646                                                       aNodeIds[2], aNodeIds[3],
647                                                       aNodeIds[4], aNodeIds[5],
648                                                       aNodeIds[6], aNodeIds[7],
649                                                       aCellInfo->GetElemNum(iElem));
650                   if (!anElement) {
651                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
652                                                 FindNode(myMesh,aNodeIds[1]),
653                                                 FindNode(myMesh,aNodeIds[2]),
654                                                 FindNode(myMesh,aNodeIds[3]),
655                                                 FindNode(myMesh,aNodeIds[4]),
656                                                 FindNode(myMesh,aNodeIds[5]),
657                                                 FindNode(myMesh,aNodeIds[6]),
658                                                 FindNode(myMesh,aNodeIds[7]));
659                     isRenum = anIsElemNum;
660                   }
661                   break;
662                 case eQUAD9:
663                   aNbNodes = 9;
664                   if(anIsElemNum)
665                     anElement = myMesh->AddFaceWithID(aNodeIds[0], aNodeIds[1],
666                                                       aNodeIds[2], aNodeIds[3],
667                                                       aNodeIds[4], aNodeIds[5],
668                                                       aNodeIds[6], aNodeIds[7], aNodeIds[8],
669                                                       aCellInfo->GetElemNum(iElem));
670                   if (!anElement) {
671                     anElement = myMesh->AddFace(FindNode(myMesh,aNodeIds[0]),
672                                                 FindNode(myMesh,aNodeIds[1]),
673                                                 FindNode(myMesh,aNodeIds[2]),
674                                                 FindNode(myMesh,aNodeIds[3]),
675                                                 FindNode(myMesh,aNodeIds[4]),
676                                                 FindNode(myMesh,aNodeIds[5]),
677                                                 FindNode(myMesh,aNodeIds[6]),
678                                                 FindNode(myMesh,aNodeIds[7]),
679                                                 FindNode(myMesh,aNodeIds[8]));
680                     isRenum = anIsElemNum;
681                   }
682                   break;
683                 case eTETRA4:
684                   aNbNodes = 4;
685                   if(anIsElemNum)
686                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
687                                                         aNodeIds[2], aNodeIds[3],
688                                                         aCellInfo->GetElemNum(iElem));
689                   if (!anElement) {
690                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
691                                                   FindNode(myMesh,aNodeIds[1]),
692                                                   FindNode(myMesh,aNodeIds[2]),
693                                                   FindNode(myMesh,aNodeIds[3]));
694                     isRenum = anIsElemNum;
695                   }
696                   break;
697                 case eTETRA10:
698                   aNbNodes = 10;
699                   if(anIsElemNum)
700                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
701                                                         aNodeIds[2], aNodeIds[3],
702                                                         aNodeIds[4], aNodeIds[5],
703                                                         aNodeIds[6], aNodeIds[7],
704                                                         aNodeIds[8], aNodeIds[9],
705                                                         aCellInfo->GetElemNum(iElem));
706                   if (!anElement) {
707                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
708                                                   FindNode(myMesh,aNodeIds[1]),
709                                                   FindNode(myMesh,aNodeIds[2]),
710                                                   FindNode(myMesh,aNodeIds[3]),
711                                                   FindNode(myMesh,aNodeIds[4]),
712                                                   FindNode(myMesh,aNodeIds[5]),
713                                                   FindNode(myMesh,aNodeIds[6]),
714                                                   FindNode(myMesh,aNodeIds[7]),
715                                                   FindNode(myMesh,aNodeIds[8]),
716                                                   FindNode(myMesh,aNodeIds[9]));
717                     isRenum = anIsElemNum;
718                   }
719                   break;
720                 case ePYRA5:
721                   aNbNodes = 5;
722                   if(anIsElemNum)
723                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
724                                                         aNodeIds[2], aNodeIds[3],
725                                                         aNodeIds[4],
726                                                         aCellInfo->GetElemNum(iElem));
727                   if (!anElement) {
728                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
729                                                   FindNode(myMesh,aNodeIds[1]),
730                                                   FindNode(myMesh,aNodeIds[2]),
731                                                   FindNode(myMesh,aNodeIds[3]),
732                                                   FindNode(myMesh,aNodeIds[4]));
733                     isRenum = anIsElemNum;
734                   }
735                   break;
736                 case ePYRA13:
737                   aNbNodes = 13;
738                   if(anIsElemNum)
739                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
740                                                         aNodeIds[2], aNodeIds[3],
741                                                         aNodeIds[4], aNodeIds[5],
742                                                         aNodeIds[6], aNodeIds[7],
743                                                         aNodeIds[8], aNodeIds[9],
744                                                         aNodeIds[10], aNodeIds[11],
745                                                         aNodeIds[12],
746                                                         aCellInfo->GetElemNum(iElem));
747                   if (!anElement) {
748                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
749                                                   FindNode(myMesh,aNodeIds[1]),
750                                                   FindNode(myMesh,aNodeIds[2]),
751                                                   FindNode(myMesh,aNodeIds[3]),
752                                                   FindNode(myMesh,aNodeIds[4]),
753                                                   FindNode(myMesh,aNodeIds[5]),
754                                                   FindNode(myMesh,aNodeIds[6]),
755                                                   FindNode(myMesh,aNodeIds[7]),
756                                                   FindNode(myMesh,aNodeIds[8]),
757                                                   FindNode(myMesh,aNodeIds[9]),
758                                                   FindNode(myMesh,aNodeIds[10]),
759                                                   FindNode(myMesh,aNodeIds[11]),
760                                                   FindNode(myMesh,aNodeIds[12]));
761                     isRenum = anIsElemNum;
762                   }
763                   break;
764                 case ePENTA6:
765                   aNbNodes = 6;
766                   if(anIsElemNum)
767                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
768                                                         aNodeIds[1],
769                                                         aNodeIds[2],
770                                                         aNodeIds[3],
771                                                         aNodeIds[4],
772                                                         aNodeIds[5],
773                                                         aCellInfo->GetElemNum(iElem));
774                   if (!anElement) {
775                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
776                                                   FindNode(myMesh,aNodeIds[1]),
777                                                   FindNode(myMesh,aNodeIds[2]),
778                                                   FindNode(myMesh,aNodeIds[3]),
779                                                   FindNode(myMesh,aNodeIds[4]),
780                                                   FindNode(myMesh,aNodeIds[5]));
781                     isRenum = anIsElemNum;
782                   }
783                   break;
784                 case ePENTA15:
785                   aNbNodes = 15;
786                   if(anIsElemNum)
787                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
788                                                         aNodeIds[2], aNodeIds[3],
789                                                         aNodeIds[4], aNodeIds[5],
790                                                         aNodeIds[6], aNodeIds[7],
791                                                         aNodeIds[8], aNodeIds[9],
792                                                         aNodeIds[10], aNodeIds[11],
793                                                         aNodeIds[12], aNodeIds[13],
794                                                         aNodeIds[14],
795                                                         aCellInfo->GetElemNum(iElem));
796                   if (!anElement) {
797                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
798                                                   FindNode(myMesh,aNodeIds[1]),
799                                                   FindNode(myMesh,aNodeIds[2]),
800                                                   FindNode(myMesh,aNodeIds[3]),
801                                                   FindNode(myMesh,aNodeIds[4]),
802                                                   FindNode(myMesh,aNodeIds[5]),
803                                                   FindNode(myMesh,aNodeIds[6]),
804                                                   FindNode(myMesh,aNodeIds[7]),
805                                                   FindNode(myMesh,aNodeIds[8]),
806                                                   FindNode(myMesh,aNodeIds[9]),
807                                                   FindNode(myMesh,aNodeIds[10]),
808                                                   FindNode(myMesh,aNodeIds[11]),
809                                                   FindNode(myMesh,aNodeIds[12]),
810                                                   FindNode(myMesh,aNodeIds[13]),
811                                                   FindNode(myMesh,aNodeIds[14]));
812                     isRenum = anIsElemNum;
813                   }
814                   break;
815                 case ePENTA18:
816                   aNbNodes = 18;
817                   if(anIsElemNum)
818                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
819                                                         aNodeIds[2], aNodeIds[3],
820                                                         aNodeIds[4], aNodeIds[5],
821                                                         aNodeIds[6], aNodeIds[7],
822                                                         aNodeIds[8], aNodeIds[9],
823                                                         aNodeIds[10], aNodeIds[11],
824                                                         aNodeIds[12], aNodeIds[13],
825                                                         aNodeIds[14], aNodeIds[15],
826                                                         aNodeIds[16], aNodeIds[17],
827                                                         aCellInfo->GetElemNum(iElem));
828                   if (!anElement) {
829                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
830                                                   FindNode(myMesh,aNodeIds[1]),
831                                                   FindNode(myMesh,aNodeIds[2]),
832                                                   FindNode(myMesh,aNodeIds[3]),
833                                                   FindNode(myMesh,aNodeIds[4]),
834                                                   FindNode(myMesh,aNodeIds[5]),
835                                                   FindNode(myMesh,aNodeIds[6]),
836                                                   FindNode(myMesh,aNodeIds[7]),
837                                                   FindNode(myMesh,aNodeIds[8]),
838                                                   FindNode(myMesh,aNodeIds[9]),
839                                                   FindNode(myMesh,aNodeIds[10]),
840                                                   FindNode(myMesh,aNodeIds[11]),
841                                                   FindNode(myMesh,aNodeIds[12]),
842                                                   FindNode(myMesh,aNodeIds[13]),
843                                                   FindNode(myMesh,aNodeIds[14]),
844                                                   FindNode(myMesh,aNodeIds[15]),
845                                                   FindNode(myMesh,aNodeIds[16]),
846                                                   FindNode(myMesh,aNodeIds[17]));
847                     isRenum = anIsElemNum;
848                   }
849                   break;
850                 case eHEXA8:
851                   aNbNodes = 8;
852                   if(anIsElemNum)
853                     anElement = myMesh->AddVolumeWithID(aNodeIds[0],
854                                                         aNodeIds[1],
855                                                         aNodeIds[2],
856                                                         aNodeIds[3],
857                                                         aNodeIds[4],
858                                                         aNodeIds[5],
859                                                         aNodeIds[6],
860                                                         aNodeIds[7],
861                                                         aCellInfo->GetElemNum(iElem));
862                   if (!anElement) {
863                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
864                                                   FindNode(myMesh,aNodeIds[1]),
865                                                   FindNode(myMesh,aNodeIds[2]),
866                                                   FindNode(myMesh,aNodeIds[3]),
867                                                   FindNode(myMesh,aNodeIds[4]),
868                                                   FindNode(myMesh,aNodeIds[5]),
869                                                   FindNode(myMesh,aNodeIds[6]),
870                                                   FindNode(myMesh,aNodeIds[7]));
871                     isRenum = anIsElemNum;
872                   }
873                   break;
874
875                 case eHEXA20:
876                   aNbNodes = 20;
877                   if(anIsElemNum)
878                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
879                                                         aNodeIds[2], aNodeIds[3],
880                                                         aNodeIds[4], aNodeIds[5],
881                                                         aNodeIds[6], aNodeIds[7],
882                                                         aNodeIds[8], aNodeIds[9],
883                                                         aNodeIds[10], aNodeIds[11],
884                                                         aNodeIds[12], aNodeIds[13],
885                                                         aNodeIds[14], aNodeIds[15],
886                                                         aNodeIds[16], aNodeIds[17],
887                                                         aNodeIds[18], aNodeIds[19],
888                                                         aCellInfo->GetElemNum(iElem));
889                   if (!anElement) {
890                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
891                                                   FindNode(myMesh,aNodeIds[1]),
892                                                   FindNode(myMesh,aNodeIds[2]),
893                                                   FindNode(myMesh,aNodeIds[3]),
894                                                   FindNode(myMesh,aNodeIds[4]),
895                                                   FindNode(myMesh,aNodeIds[5]),
896                                                   FindNode(myMesh,aNodeIds[6]),
897                                                   FindNode(myMesh,aNodeIds[7]),
898                                                   FindNode(myMesh,aNodeIds[8]),
899                                                   FindNode(myMesh,aNodeIds[9]),
900                                                   FindNode(myMesh,aNodeIds[10]),
901                                                   FindNode(myMesh,aNodeIds[11]),
902                                                   FindNode(myMesh,aNodeIds[12]),
903                                                   FindNode(myMesh,aNodeIds[13]),
904                                                   FindNode(myMesh,aNodeIds[14]),
905                                                   FindNode(myMesh,aNodeIds[15]),
906                                                   FindNode(myMesh,aNodeIds[16]),
907                                                   FindNode(myMesh,aNodeIds[17]),
908                                                   FindNode(myMesh,aNodeIds[18]),
909                                                   FindNode(myMesh,aNodeIds[19]));
910                     isRenum = anIsElemNum;
911                   }
912                   break;
913
914                 case eHEXA27:
915                   aNbNodes = 27;
916                   if(anIsElemNum)
917                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
918                                                         aNodeIds[2], aNodeIds[3],
919                                                         aNodeIds[4], aNodeIds[5],
920                                                         aNodeIds[6], aNodeIds[7],
921                                                         aNodeIds[8], aNodeIds[9],
922                                                         aNodeIds[10], aNodeIds[11],
923                                                         aNodeIds[12], aNodeIds[13],
924                                                         aNodeIds[14], aNodeIds[15],
925                                                         aNodeIds[16], aNodeIds[17],
926                                                         aNodeIds[18], aNodeIds[19],
927                                                         aNodeIds[20], aNodeIds[21],
928                                                         aNodeIds[22], aNodeIds[23],
929                                                         aNodeIds[24], aNodeIds[25],
930                                                         aNodeIds[26],
931                                                         aCellInfo->GetElemNum(iElem));
932                   if (!anElement) {
933                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
934                                                   FindNode(myMesh,aNodeIds[1]),
935                                                   FindNode(myMesh,aNodeIds[2]),
936                                                   FindNode(myMesh,aNodeIds[3]),
937                                                   FindNode(myMesh,aNodeIds[4]),
938                                                   FindNode(myMesh,aNodeIds[5]),
939                                                   FindNode(myMesh,aNodeIds[6]),
940                                                   FindNode(myMesh,aNodeIds[7]),
941                                                   FindNode(myMesh,aNodeIds[8]),
942                                                   FindNode(myMesh,aNodeIds[9]),
943                                                   FindNode(myMesh,aNodeIds[10]),
944                                                   FindNode(myMesh,aNodeIds[11]),
945                                                   FindNode(myMesh,aNodeIds[12]),
946                                                   FindNode(myMesh,aNodeIds[13]),
947                                                   FindNode(myMesh,aNodeIds[14]),
948                                                   FindNode(myMesh,aNodeIds[15]),
949                                                   FindNode(myMesh,aNodeIds[16]),
950                                                   FindNode(myMesh,aNodeIds[17]),
951                                                   FindNode(myMesh,aNodeIds[18]),
952                                                   FindNode(myMesh,aNodeIds[19]),
953                                                   FindNode(myMesh,aNodeIds[20]),
954                                                   FindNode(myMesh,aNodeIds[21]),
955                                                   FindNode(myMesh,aNodeIds[22]),
956                                                   FindNode(myMesh,aNodeIds[23]),
957                                                   FindNode(myMesh,aNodeIds[24]),
958                                                   FindNode(myMesh,aNodeIds[25]),
959                                                   FindNode(myMesh,aNodeIds[26]));
960                     isRenum = anIsElemNum;
961                   }
962                   break;
963
964                 case eOCTA12:
965                   aNbNodes = 12;
966                   if(anIsElemNum)
967                     anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
968                                                         aNodeIds[2], aNodeIds[3],
969                                                         aNodeIds[4], aNodeIds[5],
970                                                         aNodeIds[6], aNodeIds[7],
971                                                         aNodeIds[8], aNodeIds[9],
972                                                         aNodeIds[10], aNodeIds[11],
973                                                         aCellInfo->GetElemNum(iElem));
974                   if (!anElement) {
975                     anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
976                                                   FindNode(myMesh,aNodeIds[1]),
977                                                   FindNode(myMesh,aNodeIds[2]),
978                                                   FindNode(myMesh,aNodeIds[3]),
979                                                   FindNode(myMesh,aNodeIds[4]),
980                                                   FindNode(myMesh,aNodeIds[5]),
981                                                   FindNode(myMesh,aNodeIds[6]),
982                                                   FindNode(myMesh,aNodeIds[7]),
983                                                   FindNode(myMesh,aNodeIds[8]),
984                                                   FindNode(myMesh,aNodeIds[9]),
985                                                   FindNode(myMesh,aNodeIds[10]),
986                                                   FindNode(myMesh,aNodeIds[11]));
987                     isRenum = anIsElemNum;
988                   }
989                   break;
990
991                 default:;
992
993                 } // switch(aGeom)
994
995 #ifndef _DEXCEPT_
996               } catch(const std::exception& exc) {
997                 INFOS("The following exception was caught:\n\t"<<exc.what());
998                 aResult = addMessage( exc.what(), /*isFatal=*/true );
999               } catch(...) {
1000                 INFOS("Unknown exception was caught !!!");
1001                 aResult = addMessage( "Unknown exception", /*isFatal=*/true );
1002               }
1003 #endif
1004               if (!anElement) {
1005                 aResult = DRS_WARN_SKIP_ELEM;
1006               }
1007               else {
1008                 if (isRenum) {
1009                   anIsElemNum = eFAUX;
1010                   takeNumbers = false;
1011                   if (aResult < DRS_WARN_RENUMBER)
1012                     aResult = DRS_WARN_RENUMBER;
1013                 }
1014                 if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies )) {
1015                   // Save reference to this element from its family
1016                   aFamily->AddElement(anElement);
1017                   aFamily->SetType(anElement->GetType());
1018                 }
1019               }
1020             } // loop on aNbElems
1021           }} // switch(aGeom)
1022         } // loop on aGeom2Size
1023       } // loop on aEntityInfo
1024
1025       if (aDescendingEntitiesMap.Extent()) isDescConn = true; // Mantis issue 0020483
1026
1027     } // for(int iMesh = 0; iMesh < aNbMeshes; iMesh++)
1028 #ifndef _DEXCEPT_
1029   }
1030   catch(const std::exception& exc)
1031   {
1032     INFOS("The following exception was caught:\n\t"<<exc.what());
1033     aResult = addMessage( exc.what(), /*isFatal=*/true );
1034   }
1035   catch(...)
1036   {
1037     INFOS("Unknown exception was caught !!!");
1038     aResult = addMessage( "Unknown exception", /*isFatal=*/true );
1039   }
1040 #endif
1041   if (myMesh)
1042     myMesh->compactMesh();
1043
1044   // Mantis issue 0020483
1045   if (aResult == DRS_OK && isDescConn) {
1046     INFOS("There are some elements in descending connectivity in med file. They were not read !!!");
1047     aResult = DRS_WARN_DESCENDING;
1048   }
1049
1050   if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
1051   return aResult;
1052 }
1053
1054 list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
1055 {
1056   list<string> aMeshNames;
1057
1058   try {
1059     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
1060     theStatus = DRS_OK;
1061     PWrapper aMed = CrWrapper(myFile);
1062
1063     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
1064       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
1065         // Reading the MED mesh
1066         //---------------------
1067         PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh+1);
1068         aMeshNames.push_back(aMeshInfo->GetName());
1069       }
1070     }
1071   } catch(const std::exception& exc) {
1072     INFOS("Following exception was caught:\n\t"<<exc.what());
1073     theStatus = DRS_FAIL;
1074   } catch(...) {
1075     INFOS("Unknown exception was caught !!!");
1076     theStatus = DRS_FAIL;
1077   }
1078
1079   return aMeshNames;
1080 }
1081
1082 list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
1083 {
1084   list<TNameAndType> aResult;
1085   set<TNameAndType> aResGroupNames;
1086
1087   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
1088   for (; aFamsIter != myFamilies.end(); aFamsIter++)
1089   {
1090     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
1091     const MED::TStringSet& aGroupNames = aFamily->GetGroupNames();
1092     set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
1093     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
1094     {
1095       const set< SMDSAbs_ElementType >& types = aFamily->GetTypes();
1096       set< SMDSAbs_ElementType >::const_iterator type = types.begin();
1097       for ( ; type != types.end(); ++type )
1098       {
1099         TNameAndType aNameAndType = make_pair( *aGrNamesIter, *type );
1100         if ( aResGroupNames.insert( aNameAndType ).second ) {
1101           aResult.push_back( aNameAndType );
1102         }
1103       }
1104     }
1105   }
1106
1107   return aResult;
1108 }
1109
1110 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
1111 {
1112   string aGroupName (theGroup->GetStoreName());
1113   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
1114
1115   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
1116   for (; aFamsIter != myFamilies.end(); aFamsIter++)
1117   {
1118     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
1119     if (aFamily->GetTypes().count( theGroup->GetType() ) && aFamily->MemberOf(aGroupName))
1120     {
1121       const ElementsSet&           anElements = aFamily->GetElements();
1122       ElementsSet::const_iterator anElemsIter = anElements.begin();
1123       for (; anElemsIter != anElements.end(); anElemsIter++)
1124       {
1125         const SMDS_MeshElement * element = *anElemsIter;
1126         if ( element->GetType() == theGroup->GetType() ) // Issue 0020576
1127           theGroup->SMDSGroup().Add(element);
1128       }
1129       int aGroupAttrVal = aFamily->GetGroupAttributVal();
1130       if( aGroupAttrVal != 0)
1131         theGroup->SetColorGroup(aGroupAttrVal);
1132 //       if ( element ) -- Issue 0020576
1133 //         theGroup->SetType( theGroup->SMDSGroup().GetType() );
1134     }
1135   }
1136 }
1137
1138 void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
1139                                            const int theId)
1140 {
1141   char submeshGrpName[ 30 ];
1142   sprintf( submeshGrpName, "SubMesh %d", theId );
1143   string aName (submeshGrpName);
1144   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
1145   for (; aFamsIter != myFamilies.end(); aFamsIter++)
1146   {
1147     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
1148     if (aFamily->MemberOf(aName))
1149     {
1150       const ElementsSet&           anElements = aFamily->GetElements();
1151       ElementsSet::const_iterator anElemsIter = anElements.begin();
1152       if (aFamily->GetType() == SMDSAbs_Node)
1153       {
1154         for (; anElemsIter != anElements.end(); anElemsIter++)
1155         {
1156           const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>(*anElemsIter);
1157           theSubMesh->AddNode(node);
1158         }
1159       }
1160       else
1161       {
1162         for (; anElemsIter != anElements.end(); anElemsIter++)
1163         {
1164           theSubMesh->AddElement(*anElemsIter);
1165         }
1166       }
1167     }
1168   }
1169 }
1170
1171 void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
1172 {
1173   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
1174   for (; aFamsIter != myFamilies.end(); aFamsIter++)
1175   {
1176     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
1177     MED::TStringSet aGroupNames = aFamily->GetGroupNames();
1178     set<string>::iterator aGrNamesIter = aGroupNames.begin();
1179     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
1180     {
1181       string aName = *aGrNamesIter;
1182       // Check, if this is a Group or SubMesh name
1183       if (aName.substr(0, 7) == string("SubMesh"))
1184       {
1185         int Id = atoi(string(aName).substr(7).c_str());
1186         const ElementsSet&           anElements = aFamily->GetElements();
1187         ElementsSet::const_iterator anElemsIter = anElements.begin();
1188         if (aFamily->GetType() == SMDSAbs_Node)
1189         {
1190           for (; anElemsIter != anElements.end(); anElemsIter++)
1191           {
1192             const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( *anElemsIter );
1193             // find out a shape type
1194             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
1195             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );
1196             switch ( aShapeType ) {
1197             case TopAbs_FACE:
1198               myMesh->SetNodeOnFace(node, Id); break;
1199             case TopAbs_EDGE:
1200               myMesh->SetNodeOnEdge(node, Id); break;
1201             case TopAbs_VERTEX:
1202               myMesh->SetNodeOnVertex(node, Id); break;
1203             default:
1204               myMesh->SetNodeInVolume(node, Id);
1205             }
1206           }
1207         }
1208         else
1209         {
1210           for (; anElemsIter != anElements.end(); anElemsIter++)
1211           {
1212             myMesh->SetMeshElementOnShape(*anElemsIter, Id);
1213           }
1214         }
1215       }
1216     }
1217   }
1218 }
1219 /*!
1220  * \brief Ensure aFamily to have required ID
1221  * \param aFamily - a family to check and update
1222  * \param anID - an ID aFamily should have
1223  * \retval bool  - true if successful
1224  */
1225 bool DriverMED::checkFamilyID(DriverMED_FamilyPtr & aFamily,
1226                               int                   anID,
1227                               const TID2FamilyMap&  myFamilies)
1228 {
1229   if ( !aFamily || aFamily->GetId() != anID ) {
1230     map<int, DriverMED_FamilyPtr>::const_iterator i_fam = myFamilies.find(anID);
1231     if ( i_fam == myFamilies.end() )
1232       return false;
1233     aFamily = i_fam->second;
1234   }
1235   return ( aFamily->GetId() == anID );
1236 }
1237
1238 /*!
1239  * \brief Reading the structured mesh and convert to non structured
1240  *        (by filling of smesh structure for non structured mesh)
1241  * \param theWrapper  - PWrapper const pointer
1242  * \param theMeshInfo - PMeshInfo const pointer
1243  * \param myFamilies  - a map of the family ID to the Family
1244  * \return TRUE, if successfully. Else FALSE
1245  */
1246 bool DriverMED::buildMeshGrille(const MED::PWrapper&  theWrapper,
1247                                 const MED::PMeshInfo& theMeshInfo,
1248                                 SMESHDS_Mesh*         myMesh,
1249                                 const TID2FamilyMap&  myFamilies)
1250 {
1251   bool res = true;
1252
1253   MED::PGrilleInfo aGrilleInfo = theWrapper->GetPGrilleInfo(theMeshInfo);
1254   MED::TInt aNbNodes = aGrilleInfo->GetNbNodes();
1255   MED::TInt aNbCells = aGrilleInfo->GetNbCells();
1256   MED::TInt aMeshDim = theMeshInfo->GetDim();
1257   DriverMED_FamilyPtr aFamily;
1258   for(MED::TInt iNode=0;iNode < aNbNodes; iNode++){
1259     double aCoords[3] = {0.0, 0.0, 0.0};
1260     const SMDS_MeshNode* aNode;
1261     MED::TNodeCoord aMEDNodeCoord = aGrilleInfo->GetCoord(iNode);
1262     for(MED::TInt iDim=0;iDim<aMeshDim;iDim++)
1263       aCoords[(int)iDim] = aMEDNodeCoord[(int)iDim];
1264     aNode = myMesh->AddNodeWithID(aCoords[0],aCoords[1],aCoords[2],iNode+1);
1265     if (!aNode) {
1266       EXCEPTION(runtime_error,"buildMeshGrille Error. Node not created! "<<(int)iNode);
1267     }
1268
1269     if((aGrilleInfo->myFamNumNode).size() > 0){
1270       TInt aFamNum = aGrilleInfo->GetFamNumNode(iNode);
1271       if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies ))
1272         {
1273           aFamily->AddElement(aNode);
1274           aFamily->SetType(SMDSAbs_Node);
1275         }
1276     }
1277     
1278   }
1279
1280   SMDS_MeshElement* anElement = NULL;
1281   MED::TIntVector aNodeIds;
1282   for(MED::TInt iCell=0;iCell < aNbCells; iCell++){
1283     aNodeIds = aGrilleInfo->GetConn(iCell);
1284     switch(aGrilleInfo->GetGeom()){
1285     case MED::eSEG2:
1286       if(aNodeIds.size() != 2){
1287         res = false;
1288         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 2!="<<aNodeIds.size());
1289       }
1290       anElement = myMesh->AddEdgeWithID(aNodeIds[0]+1,
1291                                         aNodeIds[1]+1,
1292                                         iCell+1);
1293       break;
1294     case MED::eQUAD4:
1295       if(aNodeIds.size() != 4){
1296         res = false;
1297         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 4!="<<aNodeIds.size());
1298       }
1299       anElement = myMesh->AddFaceWithID(aNodeIds[0]+1,
1300                                         aNodeIds[2]+1,
1301                                         aNodeIds[3]+1,
1302                                         aNodeIds[1]+1,
1303                                         iCell+1);
1304       break;
1305     case MED::eHEXA8:
1306       if(aNodeIds.size() != 8){
1307         res = false;
1308         EXCEPTION(runtime_error,"buildMeshGrille Error. Incorrect size of ids 8!="<<aNodeIds.size());
1309       }
1310       anElement = myMesh->AddVolumeWithID(aNodeIds[0]+1,
1311                                           aNodeIds[2]+1,
1312                                           aNodeIds[3]+1,
1313                                           aNodeIds[1]+1,
1314                                           aNodeIds[4]+1,
1315                                           aNodeIds[6]+1,
1316                                           aNodeIds[7]+1,
1317                                           aNodeIds[5]+1,
1318                                           iCell+1);
1319       break;
1320     default:
1321       break;
1322     }
1323     if (!anElement) {
1324       EXCEPTION(runtime_error,"buildMeshGrille Error. Element not created! "<<iCell);
1325     }
1326     if((aGrilleInfo->myFamNum).size() > 0){
1327       TInt aFamNum = aGrilleInfo->GetFamNum(iCell);
1328       if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies )){
1329         aFamily->AddElement(anElement);
1330         aFamily->SetType(anElement->GetType());
1331       }
1332     }
1333   }
1334
1335   return res;
1336 }