Salome HOME
Allow saving groups with non-ascii names.
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
index ba851616c2851908b1dc7c075d991668fad55a39..362874425a1428fc38efa13e0fd4d08130a15e86 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -42,7 +42,7 @@
 //#include <stdlib.h>
 
 #ifdef _DEBUG_
-static int MYDEBUG = 1;
+static int MYDEBUG = 0;
 //#define _DEXCEPT_
 #else
 static int MYDEBUG = 0;
@@ -110,7 +110,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
 #endif
     myFamilies.clear();
     if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
-    PWrapper aMed = CrWrapper(myFile,true);
+    PWrapper aMed = CrWrapperR(myFile);
 
     aResult = DRS_EMPTY;
     TInt aNbMeshes = aMed->GetNbMeshes();
@@ -155,7 +155,8 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
               aFamily->SetGroupAttributVal(anAttrVal);
             }
             if(MYDEBUG) MESSAGE(aGroupName);
-            aFamily->AddGroupName(aGroupName);
+            if ( strncmp( aGroupName.c_str(), NIG_GROUP_PREFIX, strlen(NIG_GROUP_PREFIX) ) != 0 )
+              aFamily->AddGroupName( fixUTF8( aGroupName ));
           }
           aFamily->SetId( aFamId );
           myFamilies[aFamId] = aFamily;
@@ -172,7 +173,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
       //------------------------------------------------------
       PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
       if (!aNodeInfo) {
-        aResult = DRS_FAIL;
+        aResult = addMessage("No nodes", /*isFatal=*/true );
         continue;
       }
       aMeshInfo->myDim=aMeshInfo->mySpaceDim;// ignore meshdim in MEDFile because it can be false
@@ -272,7 +273,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
             if ( anIsElemNum && !aBallInfo->myElemNum->empty() )
               maxID = *std::max_element( aBallInfo->myElemNum->begin(),
                                          aBallInfo->myElemNum->end() );
-            myMesh->getGrid()->AllocateDiameters( maxID ); // performance optimization
+            myMesh->GetGrid()->AllocateDiameters( maxID ); // performance optimization
 
             // create balls
             SMDS_MeshElement* anElement;
@@ -365,9 +366,9 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
                 }
 #ifndef _DEXCEPT_
               } catch(const std::exception& exc) {
-                aResult = DRS_FAIL;
+                aResult = addMessage( exc.what(), /*isFatal=*/true );
               } catch (...) {
-                aResult = DRS_FAIL;
+                aResult = addMessage( "Unknown exception", /*isFatal=*/true );
               }
 #endif
               if ( !anElement ) {
@@ -492,6 +493,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
             case ePYRA13:  aNbNodes = 13; break;
             case ePENTA6:  aNbNodes = 6;  break;
             case ePENTA15: aNbNodes = 15; break;
+            case ePENTA18: aNbNodes = 18; break;
             case eHEXA8:   aNbNodes = 8;  break;
             case eHEXA20:  aNbNodes = 20; break;
             case eHEXA27:  aNbNodes = 27; break;
@@ -500,7 +502,8 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
             default:;
             }
             vector<TInt> aNodeIds(aNbNodes);
-            for(int iElem = 0; iElem < aNbElems; iElem++){
+            for ( int iElem = 0; iElem < aNbElems; iElem++ )
+            {
               bool anIsValidConnect = false;
               TCConnSlice aConnSlice = aCellInfo->GetConnSlice(iElem);
 #ifndef _DEXCEPT_
@@ -521,10 +524,10 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
 #ifndef _DEXCEPT_
               }catch(const std::exception& exc){
                 INFOS("Following exception was caught:\n\t"<<exc.what());
-                aResult = DRS_FAIL;
+                aResult = addMessage( exc.what(), /*isFatal=*/true );
               }catch(...){
                 INFOS("Unknown exception was caught !!!");
-                aResult = DRS_FAIL;
+                aResult = addMessage( "Unknown exception", /*isFatal=*/true );
               }
 #endif          
               if(!anIsValidConnect)
@@ -809,6 +812,41 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
                     isRenum = anIsElemNum;
                   }
                   break;
+                case ePENTA18:
+                  aNbNodes = 18;
+                  if(anIsElemNum)
+                    anElement = myMesh->AddVolumeWithID(aNodeIds[0], aNodeIds[1],
+                                                        aNodeIds[2], aNodeIds[3],
+                                                        aNodeIds[4], aNodeIds[5],
+                                                        aNodeIds[6], aNodeIds[7],
+                                                        aNodeIds[8], aNodeIds[9],
+                                                        aNodeIds[10], aNodeIds[11],
+                                                        aNodeIds[12], aNodeIds[13],
+                                                        aNodeIds[14], aNodeIds[15],
+                                                        aNodeIds[16], aNodeIds[17],
+                                                        aCellInfo->GetElemNum(iElem));
+                  if (!anElement) {
+                    anElement = myMesh->AddVolume(FindNode(myMesh,aNodeIds[0]),
+                                                  FindNode(myMesh,aNodeIds[1]),
+                                                  FindNode(myMesh,aNodeIds[2]),
+                                                  FindNode(myMesh,aNodeIds[3]),
+                                                  FindNode(myMesh,aNodeIds[4]),
+                                                  FindNode(myMesh,aNodeIds[5]),
+                                                  FindNode(myMesh,aNodeIds[6]),
+                                                  FindNode(myMesh,aNodeIds[7]),
+                                                  FindNode(myMesh,aNodeIds[8]),
+                                                  FindNode(myMesh,aNodeIds[9]),
+                                                  FindNode(myMesh,aNodeIds[10]),
+                                                  FindNode(myMesh,aNodeIds[11]),
+                                                  FindNode(myMesh,aNodeIds[12]),
+                                                  FindNode(myMesh,aNodeIds[13]),
+                                                  FindNode(myMesh,aNodeIds[14]),
+                                                  FindNode(myMesh,aNodeIds[15]),
+                                                  FindNode(myMesh,aNodeIds[16]),
+                                                  FindNode(myMesh,aNodeIds[17]));
+                    isRenum = anIsElemNum;
+                  }
+                  break;
                 case eHEXA8:
                   aNbNodes = 8;
                   if(anIsElemNum)
@@ -950,17 +988,19 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
                   }
                   break;
 
+                default:;
+
                 } // switch(aGeom)
 
 #ifndef _DEXCEPT_
-              }catch(const std::exception& exc){
+              } catch(const std::exception& exc) {
                 INFOS("The following exception was caught:\n\t"<<exc.what());
-                aResult = DRS_FAIL;
-              }catch(...){
+                aResult = addMessage( exc.what(), /*isFatal=*/true );
+              } catch(...) {
                 INFOS("Unknown exception was caught !!!");
-                aResult = DRS_FAIL;
+                aResult = addMessage( "Unknown exception", /*isFatal=*/true );
               }
-#endif          
+#endif
               if (!anElement) {
                 aResult = DRS_WARN_SKIP_ELEM;
               }
@@ -973,31 +1013,31 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
                 }
                 if ( DriverMED::checkFamilyID ( aFamily, aFamNum, myFamilies )) {
                   // Save reference to this element from its family
-                  myFamilies[aFamNum]->AddElement(anElement);
-                  myFamilies[aFamNum]->SetType(anElement->GetType());
+                  aFamily->AddElement(anElement);
+                  aFamily->SetType(anElement->GetType());
                 }
               }
-            }
-          }}
-        }
-      }
+            } // loop on aNbElems
+          }} // switch(aGeom)
+        } // loop on aGeom2Size
+      } // loop on aEntityInfo
+
       if (aDescendingEntitiesMap.Extent()) isDescConn = true; // Mantis issue 0020483
+
     } // for(int iMesh = 0; iMesh < aNbMeshes; iMesh++)
 #ifndef _DEXCEPT_
   }
   catch(const std::exception& exc)
   {
     INFOS("The following exception was caught:\n\t"<<exc.what());
-    aResult = DRS_FAIL;
+    aResult = addMessage( exc.what(), /*isFatal=*/true );
   }
   catch(...)
   {
     INFOS("Unknown exception was caught !!!");
-    aResult = DRS_FAIL;
+    aResult = addMessage( "Unknown exception", /*isFatal=*/true );
   }
 #endif
-  if (myMesh)
-    myMesh->compactMesh();
 
   // Mantis issue 0020483
   if (aResult == DRS_OK && isDescConn) {
@@ -1016,7 +1056,7 @@ list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
   try {
     if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
     theStatus = DRS_OK;
-    PWrapper aMed = CrWrapper(myFile);
+    PWrapper aMed = CrWrapperR(myFile);
 
     if (TInt aNbMeshes = aMed->GetNbMeshes()) {
       for (int iMesh = 0; iMesh < aNbMeshes; iMesh++) {
@@ -1050,8 +1090,8 @@ list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
     set<string>::const_iterator aGrNamesIter = aGroupNames.begin();
     for (; aGrNamesIter != aGroupNames.end(); aGrNamesIter++)
     {
-      const set< SMDSAbs_ElementType >& types = aFamily->GetTypes();
-      set< SMDSAbs_ElementType >::const_iterator type = types.begin();
+      const ElemTypeSet& types = aFamily->GetTypes();
+      ElemTypeSet::const_iterator type = types.begin();
       for ( ; type != types.end(); ++type )
       {
         TNameAndType aNameAndType = make_pair( *aGrNamesIter, *type );
@@ -1067,28 +1107,59 @@ list<TNameAndType> DriverMED_R_SMESHDS_Mesh::GetGroupNamesAndTypes()
 
 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
 {
-  string aGroupName (theGroup->GetStoreName());
+  TFamilyVec * famVecPtr;
+
+  if ( myGroups2FamiliesMap.IsEmpty() ) // PAL23514
+  {
+    TFamilyVec famVector( 1 );
+    map<int, DriverMED_FamilyPtr>::iterator famIter = myFamilies.begin();
+    for ( ; famIter != myFamilies.end(); famIter++ )
+    {
+      DriverMED_FamilyPtr    family = famIter->second;
+      const MED::TStringSet& groups = family->GetGroupNames();
+      famVector[ 0 ] = family;
+      MED::TStringSet::const_iterator grpIter = groups.begin();
+      for ( ; grpIter != groups.end(); ++grpIter )
+      {
+        TCollection_AsciiString groupName = grpIter->c_str();
+        if (( famVecPtr = myGroups2FamiliesMap.ChangeSeek( groupName )))
+          famVecPtr->push_back( family );
+        else
+          myGroups2FamiliesMap.Bind( groupName, famVector );
+      }
+    }
+  }
+
+  const char* aGroupName = theGroup->GetStoreName();
   if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
 
-  map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
-  for (; aFamsIter != myFamilies.end(); aFamsIter++)
+  if (( famVecPtr = myGroups2FamiliesMap.ChangeSeek( aGroupName )))
   {
-    DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
-    if (aFamily->GetTypes().count( theGroup->GetType() ) && aFamily->MemberOf(aGroupName))
+    size_t groupSize = 0;
+    for ( size_t i = 0; i < famVecPtr->size(); ++i )
+    {
+      DriverMED_FamilyPtr aFamily = (*famVecPtr)[i];
+      groupSize += aFamily->NbElements( theGroup->GetType() );
+    }
+    theGroup->SMDSGroup().Reserve( groupSize );
+
+    for ( size_t i = 0; i < famVecPtr->size(); ++i )
     {
-      const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
-      set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
-      for (; anElemsIter != anElements.end(); anElemsIter++)
+      DriverMED_FamilyPtr aFamily = (*famVecPtr)[i];
+      if ( aFamily->GetTypes().count( theGroup->GetType() ))
       {
-        const SMDS_MeshElement * element = *anElemsIter;
-        if ( element->GetType() == theGroup->GetType() ) // Issue 0020576
-          theGroup->SMDSGroup().Add(element);
+        const ElementsSet&           anElements = aFamily->GetElements();
+        ElementsSet::const_iterator anElemsIter = anElements.begin();
+        for (; anElemsIter != anElements.end(); anElemsIter++)
+        {
+          const SMDS_MeshElement * element = *anElemsIter;
+          if ( element->GetType() == theGroup->GetType() ) // Issue 0020576
+            theGroup->SMDSGroup().Add(element);
+        }
+        int aGroupAttrVal = aFamily->GetGroupAttributVal();
+        if( aGroupAttrVal != 0 )
+          theGroup->SetColorGroup(aGroupAttrVal);
       }
-      int aGroupAttrVal = aFamily->GetGroupAttributVal();
-      if( aGroupAttrVal != 0)
-        theGroup->SetColorGroup(aGroupAttrVal);
-//       if ( element ) -- Issue 0020576
-//         theGroup->SetType( theGroup->SMDSGroup().GetType() );
     }
   }
 }
@@ -1105,8 +1176,8 @@ void DriverMED_R_SMESHDS_Mesh::GetSubMesh (SMESHDS_SubMesh* theSubMesh,
     DriverMED_FamilyPtr aFamily = (*aFamsIter).second;
     if (aFamily->MemberOf(aName))
     {
-      const set<const SMDS_MeshElement *>& anElements = aFamily->GetElements();
-      set<const SMDS_MeshElement *>::const_iterator anElemsIter = anElements.begin();
+      const ElementsSet&           anElements = aFamily->GetElements();
+      ElementsSet::const_iterator anElemsIter = anElements.begin();
       if (aFamily->GetType() == SMDSAbs_Node)
       {
         for (; anElemsIter != anElements.end(); anElemsIter++)
@@ -1141,14 +1212,13 @@ void DriverMED_R_SMESHDS_Mesh::CreateAllSubMeshes ()
       if (aName.substr(0, 7) == string("SubMesh"))
       {
         int Id = atoi(string(aName).substr(7).c_str());
-        set<const SMDS_MeshElement *> anElements = aFamily->GetElements();
-        set<const SMDS_MeshElement *>::iterator anElemsIter = anElements.begin();
+        const ElementsSet&           anElements = aFamily->GetElements();
+        ElementsSet::const_iterator anElemsIter = anElements.begin();
         if (aFamily->GetType() == SMDSAbs_Node)
         {
           for (; anElemsIter != anElements.end(); anElemsIter++)
           {
-            SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>
-              ( static_cast<const SMDS_MeshNode*>( *anElemsIter ));
+            const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( *anElemsIter );
             // find out a shape type
             TopoDS_Shape aShape = myMesh->IndexToShape( Id );
             int aShapeType = ( aShape.IsNull() ? -1 : aShape.ShapeType() );