Salome HOME
Polygons and polyhedral volumes support
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMESHDS_Mesh.cxx
index 25e517cf44d0da1b53a5d92e550b3ec1bb8805ae..89a1a7bd41d5272bd0aad4826a3b4117ef70093e 100644 (file)
 
 #include <stdlib.h>
 
+#ifdef _DEBUG_
+static int MYDEBUG = 0;
+#else
+static int MYDEBUG = 0;
+#endif
+
 #define _EDF_NODE_IDS_
 
 using namespace MED;
@@ -136,7 +142,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
   Status aResult = DRS_FAIL;
   try{
     myFamilies.clear();
-    MESSAGE("Perform - myFile : "<<myFile);
+    if(MYDEBUG) MESSAGE("Perform - myFile : "<<myFile);
     PWrapper aMed = CrWrapper(myFile);
 
     aResult = DRS_EMPTY;
@@ -153,30 +159,33 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
         } else {
           aMeshName = myMeshName;
         }
-       MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
+       if(MYDEBUG) MESSAGE("Perform - aMeshName : "<<aMeshName<<"; "<<aMeshInfo->GetName());
        if(aMeshName != aMeshInfo->GetName()) continue;
         aResult = DRS_OK;
        //TInt aMeshDim = aMeshInfo->GetDim();
        
         // Reading MED families to the temporary structure
        //------------------------------------------------
-        TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
-        MESSAGE("Read " << aNbFams << " families");
+       TErr anErr;
+       TInt aNbFams = aMed->GetNbFamilies(aMeshInfo);
+        if(MYDEBUG) MESSAGE("Read " << aNbFams << " families");
         for (TInt iFam = 0; iFam < aNbFams; iFam++) {
-          PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo, iFam+1);
-          TInt aFamId = aFamilyInfo->GetId();
-          MESSAGE("Family " << aFamId << " :");
-
+         PFamilyInfo aFamilyInfo = aMed->GetPFamilyInfo(aMeshInfo,iFam+1,&anErr);
+         if(anErr >= 0){
+           TInt aFamId = aFamilyInfo->GetId();
+           if(MYDEBUG) MESSAGE("Family " << aFamId << " :");
+           
             DriverMED_FamilyPtr aFamily (new DriverMED_Family);
-
+           
             TInt aNbGrp = aFamilyInfo->GetNbGroup();
-            MESSAGE("belong to " << aNbGrp << " groups");
+            if(MYDEBUG) MESSAGE("belong to " << aNbGrp << " groups");
             for (TInt iGr = 0; iGr < aNbGrp; iGr++) {
               string aGroupName = aFamilyInfo->GetGroupName(iGr);
-              MESSAGE(aGroupName);
+              if(MYDEBUG) MESSAGE(aGroupName);
               aFamily->AddGroupName(aGroupName);
             }
             myFamilies[aFamId] = aFamily;
+         }
         }
 
         // Reading MED nodes to the corresponding SMDS structure
@@ -221,7 +230,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
 
        EBooleen anIsNodeNum = aNodeInfo->IsElemNum();
        TInt aNbElems = aNodeInfo->GetNbElem();
-       MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
+       if(MYDEBUG) MESSAGE("Perform - aNodeInfo->GetNbElem() = "<<aNbElems<<"; anIsNodeNum = "<<anIsNodeNum);
         for(TInt iElem = 0; iElem < aNbElems; iElem++){
           double aCoords[3] = {0.0, 0.0, 0.0};
           for(TInt iDim = 0; iDim < 3; iDim++)
@@ -259,12 +268,150 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
          MED::TGeom::const_iterator anTGeomIter = aTGeom.begin();
          for(; anTGeomIter != aTGeom.end(); anTGeomIter++){
            const EGeometrieElement& aGeom = anTGeomIter->first;
-           if(aGeom == ePOINT1) continue;
+
+           if (aGeom == ePOINT1) {
+              continue;
+
+            } else if (aGeom == ePOLYGONE) {
+              PPolygoneInfo aPolygoneInfo = aMed->GetPPolygoneInfo(aMeshInfo,anEntity,aGeom);
+              EBooleen anIsElemNum = takeNumbers ? aPolygoneInfo->IsElemNum() : eFAUX;
+
+              TElemNum aConn  = aPolygoneInfo->GetConnectivite();
+              TElemNum aIndex = aPolygoneInfo->GetIndex();
+
+              TInt nbPolygons = aPolygoneInfo->GetNbElem();
+
+              for (TInt iPG = 0; iPG < nbPolygons; iPG++) {
+                // get nodes
+                TInt aCurrPG_FirstNodeIndex = aIndex[iPG] - 1;
+                int nbNodes = aPolygoneInfo->GetNbConn(iPG);
+                std::vector<int> nodes_ids (nbNodes);
+                for (TInt inode = 0; inode < nbNodes; inode++) {
+                  nodes_ids[inode] = aConn[aCurrPG_FirstNodeIndex + inode];
+                }
+
+                bool isRenum = false;
+                SMDS_MeshElement* anElement = NULL;
+                TInt aFamNum = aPolygoneInfo->GetFamNum(iPG);
+
+                try {
+                  if (anIsElemNum) {
+                    anElement = myMesh->AddPolygonalFaceWithID
+                      (nodes_ids, aPolygoneInfo->GetElemNum(iPG));
+                  }
+                  if (!anElement) {
+                    std::vector<const SMDS_MeshNode*> nodes (nbNodes);
+                    for (int inode = 0; inode < nbNodes; inode++) {
+                      nodes[inode] = FindNode(myMesh, nodes_ids[inode]);
+                    }
+                    anElement = myMesh->AddPolygonalFace(nodes);
+                    isRenum = anIsElemNum;
+                  }
+                } catch (const std::exception& exc) {
+                  aResult = DRS_FAIL;
+                } catch (...) {
+                  aResult = DRS_FAIL;
+                }
+
+                if (!anElement) {
+                  aResult = DRS_WARN_SKIP_ELEM;
+                } else {
+                  if (isRenum) {
+                    anIsElemNum = eFAUX;
+                    takeNumbers = false;
+                    if (aResult < DRS_WARN_RENUMBER)
+                      aResult = DRS_WARN_RENUMBER;
+                  }
+                  if (myFamilies.find(aFamNum) != myFamilies.end()) {
+                    // Save reference to this element from its family
+                    myFamilies[aFamNum]->AddElement(anElement);
+                    myFamilies[aFamNum]->SetType(anElement->GetType());
+                  }
+                }
+              } // for (TInt iPG = 0; iPG < nbPolygons; iPG++)
+              continue;
+
+            } else if (aGeom == ePOLYEDRE) {
+              PPolyedreInfo aPolyedreInfo = aMed->GetPPolyedreInfo(aMeshInfo,anEntity,aGeom);
+              EBooleen anIsElemNum = takeNumbers ? aPolyedreInfo->IsElemNum() : eFAUX;
+
+              TElemNum aConn       = aPolyedreInfo->GetConnectivite();
+              TElemNum aFacesIndex = aPolyedreInfo->GetFacesIndex();
+              TElemNum aIndex      = aPolyedreInfo->GetIndex();
+
+              TInt nbPolyedres = aPolyedreInfo->GetNbElem();
+
+              for (int iPE = 0; iPE < nbPolyedres; iPE++) {
+                // get faces
+                int aCurrPE_FirstFaceIndex = aIndex[iPE] - 1;
+                int aNextPE_FirstFaceIndex = aIndex[iPE + 1] - 1;
+                int nbFaces = aNextPE_FirstFaceIndex - aCurrPE_FirstFaceIndex;
+                std::vector<int> quantities (nbFaces);
+                for (int iFa = 0; iFa < nbFaces; iFa++) {
+                  int aCurrFace_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex + iFa] - 1;
+                  int aNextFace_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex + iFa + 1] - 1;
+
+                  int nbNodes = aNextFace_FirstNodeIndex - aCurrFace_FirstNodeIndex;
+                  quantities[iFa] = nbNodes;
+                }
+
+                // get nodes
+                int aCurrPE_FirstNodeIndex = aFacesIndex[aCurrPE_FirstFaceIndex] - 1;
+                int nbPENodes = aPolyedreInfo->GetNbConn(iPE);
+                std::vector<int> nodes_ids (nbPENodes);
+                for (int inode = 0; inode < nbPENodes; inode++) {
+                  nodes_ids[inode] = aConn[aCurrPE_FirstNodeIndex + inode];
+                }
+
+                bool isRenum = false;
+                SMDS_MeshElement* anElement = NULL;
+                TInt aFamNum = aPolyedreInfo->GetFamNum(iPE);
+
+                try {
+                  if (anIsElemNum) {
+                    anElement = myMesh->AddPolyhedralVolumeWithID
+                      (nodes_ids, quantities, aPolyedreInfo->GetElemNum(iPE));
+                  }
+                  if (!anElement) {
+                    std::vector<const SMDS_MeshNode*> nodes (nbPENodes);
+                    for (int inode = 0; inode < nbPENodes; inode++) {
+                      nodes[inode] = FindNode(myMesh, nodes_ids[inode]);
+                    }
+                    anElement = myMesh->AddPolyhedralVolume(nodes, quantities);
+                    isRenum = anIsElemNum;
+                  }
+                } catch (const std::exception& exc) {
+                  aResult = DRS_FAIL;
+                } catch (...) {
+                  aResult = DRS_FAIL;
+                }
+
+                if (!anElement) {
+                  aResult = DRS_WARN_SKIP_ELEM;
+                } else {
+                  if (isRenum) {
+                    anIsElemNum = eFAUX;
+                    takeNumbers = false;
+                    if (aResult < DRS_WARN_RENUMBER)
+                      aResult = DRS_WARN_RENUMBER;
+                  }
+                  if (myFamilies.find(aFamNum) != myFamilies.end()) {
+                    // Save reference to this element from its family
+                    myFamilies[aFamNum]->AddElement(anElement);
+                    myFamilies[aFamNum]->SetType(anElement->GetType());
+                  }
+                }
+              } // for (int iPE = 0; iPE < nbPolyedres; iPE++)
+              continue;
+
+            } else {
+            }
+
            PCellInfo aCellInfo = aMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
            EBooleen anIsElemNum = takeNumbers ? aCellInfo->IsElemNum() : eFAUX;
            TInt aNbElems = aCellInfo->GetNbElem();
-           MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
-           MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
+           if(MYDEBUG) MESSAGE("Perform - anEntity = "<<anEntity<<"; anIsElemNum = "<<anIsElemNum);
+           if(MYDEBUG) MESSAGE("Perform - aGeom = "<<aGeom<<"; aNbElems = "<<aNbElems);
 
            for(int iElem = 0; iElem < aNbElems; iElem++){
              TInt aNbNodes = -1;
@@ -300,26 +447,42 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
                break;
              }
              vector<TInt> aNodeIds(aNbNodes);
+             bool anIsValidConnect = false;
+
+             try{
 #ifdef _EDF_NODE_IDS_
-             if(anIsNodeNum) {
-               for(int i = 0; i < aNbNodes; i++){
-                 aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
+               if(anIsNodeNum) {
+                 for(int i = 0; i < aNbNodes; i++){
+                   aNodeIds[i] = aNodeInfo->GetElemNum(aCellInfo->GetConn(iElem,i)-1);
+                 }
+               }else{
+                 for(int i = 0; i < aNbNodes; i++){
+                   aNodeIds[i] = aCellInfo->GetConn(iElem,i);
+                 }
                }
-             }else{
+#else
                for(int i = 0; i < aNbNodes; i++){
                  aNodeIds[i] = aCellInfo->GetConn(iElem,i);
                }
-             }
-#else
-             for(int i = 0; i < aNbNodes; i++){
-               aNodeIds[i] = aCellInfo->GetConn(iElem,i);
-             }
 #endif
+               anIsValidConnect = true;
+             }catch(const std::exception& exc){
+               //INFOS("Follow exception was cought:\n\t"<<exc.what());
+               aResult = DRS_FAIL;
+             }catch(...){
+               //INFOS("Unknown exception was cought !!!");
+               aResult = DRS_FAIL;
+             }
+             
+             if(!anIsValidConnect)
+               continue;
 
              bool isRenum = false;
              SMDS_MeshElement* anElement = NULL;
              TInt aFamNum = aCellInfo->GetFamNum(iElem);
              try{
+                //MESSAGE("Try to create element # " << iElem << " with id = "
+                //        << aCellInfo->GetElemNum(iElem));
                switch(aGeom){
                case eSEG2:
                case eSEG3:
@@ -487,7 +650,7 @@ Driver_Mesh::Status DriverMED_R_SMESHDS_Mesh::Perform()
     INFOS("Unknown exception was cought !!!");
     aResult = DRS_FAIL;
   }
-  MESSAGE("Perform - aResult status = "<<aResult);
+  if(MYDEBUG) MESSAGE("Perform - aResult status = "<<aResult);
   return aResult;
 }
 
@@ -496,7 +659,7 @@ list<string> DriverMED_R_SMESHDS_Mesh::GetMeshNames(Status& theStatus)
   list<string> aMeshNames;
 
   try {
-    MESSAGE("GetMeshNames - myFile : " << myFile);
+    if(MYDEBUG) MESSAGE("GetMeshNames - myFile : " << myFile);
     theStatus = DRS_OK;
     PWrapper aMed = CrWrapper(myFile);
 
@@ -549,7 +712,7 @@ list<string> DriverMED_R_SMESHDS_Mesh::GetGroupNames()
 void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
 {
   string aGroupName (theGroup->GetStoreName());
-  MESSAGE("Get Group " << aGroupName);
+  if(MYDEBUG) MESSAGE("Get Group " << aGroupName);
 
   map<int, DriverMED_FamilyPtr>::iterator aFamsIter = myFamilies.begin();
   for (; aFamsIter != myFamilies.end(); aFamsIter++)
@@ -563,7 +726,7 @@ void DriverMED_R_SMESHDS_Mesh::GetGroup(SMESHDS_Group* theGroup)
       for (; anElemsIter != anElements.end(); anElemsIter++)
       {
         element = *anElemsIter;
-        theGroup->SMDSGroup().Add(element);
+       theGroup->SMDSGroup().Add(element);
       }
       if ( element )
         theGroup->SetType( element->GetType() );