Salome HOME
Copyright update 2021
[plugins/hybridplugin.git] / src / HYBRIDPlugin / HYBRIDPlugin_HYBRID.cxx
index 079995a7710f541b612dea8901a696897fc09338..7a432e97bae24a02984f6c37a74b0d62ada7c8d5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -70,6 +70,7 @@
 #include <Basics_Utils.hxx>
 #include <utilities.h>
 
+#include <fstream>
 #include <algorithm>
 
 #define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
@@ -167,11 +168,11 @@ bool HYBRIDPlugin_HYBRID::CheckHypothesis ( SMESH_Mesh&         aMesh,
 
 TopoDS_Shape HYBRIDPlugin_HYBRID::entryToShape(std::string entry)
 {
-  if ( SMESH_Gen_i::getStudyServant()->_is_nil() )
+  if ( SMESH_Gen_i::GetSMESHGen()->getStudyServant()->_is_nil() )
     throw SALOME_Exception("MG-HYBRID plugin can't work w/o publishing in the study");
   GEOM::GEOM_Object_var aGeomObj;
   TopoDS_Shape S = TopoDS_Shape();
-  SALOMEDS::SObject_var aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( entry.c_str() );
+  SALOMEDS::SObject_var aSObj = SMESH_Gen_i::GetSMESHGen()->getStudyServant()->FindObjectID( entry.c_str() );
   if (!aSObj->_is_nil() ) {
     CORBA::Object_var obj = aSObj->GetObject();
     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
@@ -190,7 +191,7 @@ TopoDS_Shape HYBRIDPlugin_HYBRID::entryToShape(std::string entry)
 static void addElemInMeshGroup(SMESH_Mesh*             theMesh,
                                const SMDS_MeshElement* anElem,
                                std::string&            groupName,
-                               std::set<std::string>&  groupsToRemove)
+                               std::set<std::string>&  /*groupsToRemove*/)
 {
   if ( !anElem ) return; // issue 0021776
 
@@ -211,8 +212,7 @@ static void addElemInMeshGroup(SMESH_Mesh*             theMesh,
   
   if (!groupDone)
   {
-    int groupId;
-    SMESH_Group* aGroup = theMesh->AddGroup(anElem->GetType(), groupName.c_str(), groupId);
+    SMESH_Group* aGroup = theMesh->AddGroup(anElem->GetType(), groupName.c_str());
     aGroup->SetName( groupName.c_str() );
     SMESHDS_Group* aGroupDS = static_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
     aGroupDS->SMDSGroup().Add(anElem);
@@ -303,13 +303,12 @@ static void makeDomainGroups( std::vector< std::vector< const SMDS_MeshElement*
     }
     // create and fill the groups
     size_t iElem = 0;
-    int groupID;
     do
     {
       SMESH_Group* group = groupOfType[ elems[ iElem ]->GetType() ];
       if ( !group )
         group = theHelper->GetMesh()->AddGroup( elems[ iElem ]->GetType(),
-                                                domainName.c_str(), groupID );
+                                                domainName.c_str() );
       SMDS_MeshGroup& groupDS =
         static_cast< SMESHDS_Group* >( group->GetGroupDS() )->SMDSGroup();
 
@@ -331,13 +330,13 @@ static bool readGMFFile(MG_HYBRID_API*                          MGOutput,
                         SMESH_MesherHelper*                     theHelper,
                         std::vector <const SMDS_MeshNode*> &    theNodeByHybridId,
                         std::vector <const SMDS_MeshElement*> & theFaceByHybridId,
-                        std::map<const SMDS_MeshNode*,int> &    theNodeToHybridIdMap,
+                        std::map<const SMDS_MeshNode*,int> &    /*theNodeToHybridIdMap*/,
                         std::vector<std::string> &              aNodeGroupByHybridId,
                         std::vector<std::string> &              anEdgeGroupByHybridId,
                         std::vector<std::string> &              aFaceGroupByHybridId,
                         std::set<std::string> &                 groupsToRemove,
                         bool                                    toMakeGroupsOfDomains=false,
-                        bool                                    toMeshHoles=true)
+                        bool                                    /*toMeshHoles*/=true)
 {
   std::string tmpStr;
   SMESHDS_Mesh* theMeshDS = theHelper->GetMeshDS();
@@ -346,38 +345,54 @@ static bool readGMFFile(MG_HYBRID_API*                          MGOutput,
   // if imprinting, the original mesh faces are modified
   // => we clear all the faces to retrieve them from Hybrid output mesh.
   std::vector<int> facesWithImprinting;
-  if (theAlgo->getHyp()) 
+  if (theAlgo->getHyp())
     facesWithImprinting = theAlgo->getHyp()->GetFacesWithImprinting();
 
   if ( ! facesWithImprinting.empty() ) {
 #ifdef _DEBUG_
-      std::cout << "Imprinting => Clear original mesh" << std::endl;
+    std::cout << "Imprinting => Clear original mesh" << std::endl;
 #endif
-      SMDS_ElemIteratorPtr eIt = theMeshDS->elementsIterator();
-      while( eIt->more() )
-        theMeshDS->RemoveFreeElement( eIt->next(), /*sm=*/0 );
-      SMDS_NodeIteratorPtr nIt = theMeshDS->nodesIterator();
-      while ( nIt->more() )
-        theMeshDS->RemoveFreeNode( nIt->next(), /*sm=*/0 );
-
-      theNodeByHybridId.clear();
-      theFaceByHybridId.clear();
+    SMESH_subMesh* smOfSolid =
+      theHelper->GetMesh()->GetSubMesh( theHelper->GetSubShape() );
+    SMESH_subMeshIteratorPtr smIt =
+      smOfSolid->getDependsOnIterator(/*includeSelf=*/false, /*complexShapeFirst=*/true);
+    while ( smIt->more() )
+    {
+      SMESH_subMesh* sm = smIt->next();
+      if ( SMESHDS_SubMesh * smDS = sm->GetSubMeshDS() )
+      {
+        SMDS_ElemIteratorPtr eIt = smDS->GetElements();
+        while( eIt->more() )
+        {
+          theMeshDS->RemoveFreeElement( eIt->next(), smDS );
+        }
+        SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
+        while ( nIt->more() )
+        {
+          const SMDS_MeshNode* n = nIt->next();
+          if ( n->NbInverseElements() == 0 )
+            theMeshDS->RemoveFreeNode( n, smDS );
+        }
+      }
+    }
+    theNodeByHybridId.clear();
+    theFaceByHybridId.clear();
   }
 
-  int nbMeshNodes = theMeshDS->NbNodes();
   int nbInitialNodes = theNodeByHybridId.size();
 
-  const bool isQuadMesh = 
+#ifdef _DEBUG_
+  int nbMeshNodes = theMeshDS->NbNodes();
+  const bool isQuadMesh =
     theHelper->GetMesh()->NbEdges( ORDER_QUADRATIC ) ||
     theHelper->GetMesh()->NbFaces( ORDER_QUADRATIC ) ||
     theHelper->GetMesh()->NbVolumes( ORDER_QUADRATIC );
-    
-#ifdef _DEBUG_
+
   std::cout << "theNodeByHybridId.size(): " << nbInitialNodes << std::endl;
   std::cout << "theHelper->GetMesh()->NbNodes(): " << nbMeshNodes << std::endl;
   std::cout << "isQuadMesh: " << isQuadMesh << std::endl;
 #endif
-  
+
   // ---------------------------------
   // Read generated elements and nodes
   // ---------------------------------
@@ -603,24 +618,30 @@ static bool readGMFFile(MG_HYBRID_API*                          MGOutput,
           if (fullyCreatedElement) {
             aCreatedElem = theHelper->AddFace( node[0], node[1], node[2], noID, force3d );
             // add iElem < aFaceGroupByHybridId.size() to avoid crash if imprinting with hexa core with MeshGems <= 2.4-5
-            if ( !aFaceGroupByHybridId.empty() && iElem < aFaceGroupByHybridId.size() && !aFaceGroupByHybridId[iElem].empty() ) {
+            if ( iElem < (int)aFaceGroupByHybridId.size() && !aFaceGroupByHybridId[iElem].empty() ) {
               addElemInMeshGroup(theHelper->GetMesh(), aCreatedElem, aFaceGroupByHybridId[iElem], groupsToRemove);
             }
             // add element in shape for groups on geom to work
-            theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
-            for ( int iN = 0; iN < 3; ++iN )
-              if ( node[iN]->getshapeId() < 1 )
-                theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            if ( domainID[iElem] > 0 )
+            {
+              theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
+              for ( int iN = 0; iN < 3; ++iN )
+                if ( node[iN]->getshapeId() < 1 )
+                  theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            }
           }
           break;
         case GmfQuadrilaterals:
           if (fullyCreatedElement) {
             aCreatedElem = theHelper->AddFace( node[0], node[1], node[2], node[3], noID, force3d );
             // add element in shape for groups on geom to work
-            theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
-            for ( int iN = 0; iN < 3; ++iN )
-              if ( node[iN]->getshapeId() < 1 )
-                theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            if ( domainID[iElem] > 0 )
+            {
+              theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
+              for ( int iN = 0; iN < 3; ++iN )
+                if ( node[iN]->getshapeId() < 1 )
+                  theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            }
           }
           break;
         case GmfTetrahedra:
@@ -795,7 +816,7 @@ static bool readGMFFile(MG_HYBRID_API*                          MGOutput,
   std::map<int, std::set<int> >::const_iterator subdomainIt = subdomainId2tetraId.begin();
   std::string aSubdomainFileName = theFile;
   aSubdomainFileName = aSubdomainFileName + ".subdomain";
-  ofstream aSubdomainFile  ( aSubdomainFileName  , ios::out);
+  std::ofstream aSubdomainFile  ( aSubdomainFileName  , ios::out);
 
   aSubdomainFile << "Nb subdomains " << subdomainId2tetraId.size() << std::endl;
   for(;subdomainIt != subdomainId2tetraId.end() ; ++subdomainIt) {
@@ -1469,7 +1490,7 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   // Write aSmdsToHybridIdMap to temp file
   std::string aSmdsToHybridIdMapFileName;
   aSmdsToHybridIdMapFileName = aGenericName + ".ids";  // ids relation
-  ofstream aIdsFile  ( aSmdsToHybridIdMapFileName  , ios::out);
+  std::ofstream aIdsFile  ( aSmdsToHybridIdMapFileName  , ios::out);
   Ok = aIdsFile.rdbuf()->is_open();
   if (!Ok) {
     INFOS( "Can't write into " << aSmdsToHybridIdMapFileName);
@@ -1499,7 +1520,7 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   // run hybrid mesher
   // -----------------
 
-  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp, theMesh );
+  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp );
 
   if ( mgHybrid.IsExecutable() )
   {
@@ -1701,7 +1722,7 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   // run hybrid mesher
   // -----------------
 
-  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp, theMesh );
+  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp );
 
   if ( mgHybrid.IsExecutable() )
   {
@@ -2278,9 +2299,9 @@ namespace
      */
     void ProcessEvent(const int                       event,
                       const int                       eventType,
-                      SMESH_subMesh*                  subMesh,
+                      SMESH_subMesh*                  /*subMesh*/,
                       SMESH_subMeshEventListenerData* data,
-                      const SMESH_Hypothesis*         hyp)
+                      const SMESH_Hypothesis*         /*hyp*/)
     {
       if ( SMESH_subMesh::SUBMESH_LOADED == event &&
            SMESH_subMesh::COMPUTE_EVENT  == eventType &&
@@ -2319,11 +2340,11 @@ namespace
     /*!
      * \brief Treat events of the subMesh
      */
-    void ProcessEvent(const int                       event,
+    void ProcessEvent(const int                       /*event*/,
                       const int                       eventType,
                       SMESH_subMesh*                  subMesh,
-                      SMESH_subMeshEventListenerData* data,
-                      const SMESH_Hypothesis*         hyp)
+                      SMESH_subMeshEventListenerData* /*data*/,
+                      const SMESH_Hypothesis*         /*hyp*/)
     {
       if (SMESH_subMesh::ALGO_EVENT == eventType &&
           !subMesh->GetAlgo() )