Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_RemoveSubShapes.cpp
index 75b1d9c09dceec344b4969a45518951d7d32f82e..e1820d2afa961673a185874eccac57760313b208 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "FeaturesPlugin_RemoveSubShapes.h"
@@ -25,6 +24,7 @@
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
 #include <ModelAPI_Validator.h>
 
 #include <GeomAPI_ShapeIterator.h>
@@ -78,7 +78,7 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
     aSubShapesToRemoveAttrList->clear();
     return;
   }
-  const int aNumOfSubs = aResultBody->numberOfSubs();
+  const bool isHasSubs = ModelAPI_Tools::hasSubResults(aResultBody);
 
   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
   if(!aBaseShape.get()) {
@@ -86,93 +86,116 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
   }
 
   myChangedInCode = true;
+  if (theID == BASE_SHAPE_ID() || theID == SUBSHAPES_TO_KEEP_ID() ||
+    theID == SUBSHAPES_TO_REMOVE_ID()) {
+    std::list<ResultPtr> anAllSubs;
+    ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
 
-  if(theID == BASE_SHAPE_ID()) {
-    aSubShapesToKeepAttrList->clear();
-    aSubShapesToRemoveAttrList->clear();
+    if(theID == BASE_SHAPE_ID()) {
+      aSubShapesToKeepAttrList->clear();
+      aSubShapesToRemoveAttrList->clear();
 
-    if (!aBaseShape.get()) {
-      return;
-    }
+      if (!aBaseShape.get()) {
+        return;
+      }
 
-    for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
-      GeomShapePtr aSubShape = anIt.current();
-      if(aNumOfSubs == 0) {
-        aSubShapesToKeepAttrList->append(aContext, aSubShape);
-      } else {
-        for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) {
-          ResultBodyPtr aSubResult = aResultBody->subResult(anIndex);
-          if(aSubResult->shape()->isEqual(aSubShape)) {
-            aSubShapesToKeepAttrList->append(aSubResult, aSubShape);
-            break;
+      std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
+      ListOfShape::const_iterator anIt = aSubShapes.cbegin();
+      for (; anIt != aSubShapes.cend(); ++anIt)
+      {
+        GeomShapePtr aSubShape = *anIt;
+        if(!isHasSubs) {
+          aSubShapesToKeepAttrList->append(aContext, aSubShape);
+        } else {
+          std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
+          for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
+            ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
+            if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
+              aSubShapesToKeepAttrList->append(aSub, aSubShape);
+              break;
+            }
           }
         }
       }
     }
-  }
-  else if (theID == SUBSHAPES_TO_KEEP_ID())
-  {
-    aSubShapesToRemoveAttrList->clear();
+    else if (theID == SUBSHAPES_TO_KEEP_ID())
+    {
+      aSubShapesToRemoveAttrList->clear();
 
-    if (!aBaseShape.get()) {
-      return;
-    }
+      if (!aBaseShape.get()) {
+        return;
+      }
 
-    int anIndex;
-    const int aSubsToKeepNb = aSubShapesToKeepAttrList->size();
-    for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
-      GeomShapePtr aSubShape = anIt.current();
+      int anIndex;
+      // optimization: collect selection attribute values into a map
+      const int aSubsToKeepNb = aSubShapesToKeepAttrList->size();
+      GeomAPI_DataMapOfShapeShape aSubShapesToKeep;
       for(anIndex = 0; anIndex < aSubsToKeepNb; ++anIndex) {
         AttributeSelectionPtr anAttrSelectionInList = aSubShapesToKeepAttrList->value(anIndex);
         GeomShapePtr aSubShapeToKeep = anAttrSelectionInList->value();
-        if (aSubShapeToKeep.get() && aSubShapeToKeep->isEqual(aSubShape)) {
-          break;
-        }
+        if (aSubShapeToKeep.get())
+          aSubShapesToKeep.bind(aSubShapeToKeep, aSubShapeToKeep);
       }
 
-      if (anIndex == aSubsToKeepNb) {
-        if(aNumOfSubs == 0) {
+      std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
+      ListOfShape::const_iterator anIt = aSubShapes.cbegin();
+      for (; anIt != aSubShapes.cend(); ++anIt)
+      {
+        GeomShapePtr aSubShape = *anIt;
+        if (aSubShapesToKeep.isBound(aSubShape))
+          continue;
+
+        if(!isHasSubs) {
           aSubShapesToRemoveAttrList->append(aContext, aSubShape);
-        } else {
-          for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) {
-            ResultBodyPtr aSubResult = aResultBody->subResult(anIndex);
-            if(aSubResult->shape()->isEqual(aSubShape)) {
-              aSubShapesToRemoveAttrList->append(aSubResult, aSubShape);
+        }
+        else {
+          std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
+          for (; aSubsIt != anAllSubs.end(); aSubsIt++) {
+            ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
+            if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
+              aSubShapesToRemoveAttrList->append(aSub, aSubShape);
               break;
             }
           }
         }
       }
     }
-  }
-  else if (theID == SUBSHAPES_TO_REMOVE_ID())
-  {
-    aSubShapesToKeepAttrList->clear();
+    else if (theID == SUBSHAPES_TO_REMOVE_ID())
+    {
+      aSubShapesToKeepAttrList->clear();
 
-    if (!aBaseShape.get()) {
-      return;
-    }
+      if (!aBaseShape.get()) {
+        return;
+      }
 
-    int anIndex;
-    const int aSubsToRemoveNb = aSubShapesToRemoveAttrList->size();
-    for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
-      GeomShapePtr aSubShape = anIt.current();
+      int anIndex;
+      const int aSubsToRemoveNb = aSubShapesToRemoveAttrList->size();
+      GeomAPI_DataMapOfShapeShape aSubShapesToRemove;
       for(anIndex = 0; anIndex < aSubsToRemoveNb; ++anIndex) {
         AttributeSelectionPtr anAttrSelectionInList = aSubShapesToRemoveAttrList->value(anIndex);
         GeomShapePtr aSubShapeToRemove = anAttrSelectionInList->value();
-        if (aSubShapeToRemove.get() && aSubShapeToRemove->isEqual(aSubShape)) {
-          break;
-        }
+        if (aSubShapeToRemove.get())
+          aSubShapesToRemove.bind(aSubShapeToRemove, aSubShapeToRemove);
       }
 
-      if (anIndex == aSubsToRemoveNb) {
-        if(aNumOfSubs == 0) {
+
+      std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
+      ListOfShape::const_iterator anIt = aSubShapes.cbegin();
+      for (; anIt != aSubShapes.cend(); ++anIt)
+      {
+        GeomShapePtr aSubShape = *anIt;
+        if (aSubShapesToRemove.isBound(aSubShape))
+          continue;
+
+        if (!isHasSubs) {
           aSubShapesToKeepAttrList->append(aContext, aSubShape);
-        } else {
-          for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) {
-            ResultBodyPtr aSubResult = aResultBody->subResult(anIndex);
-            if(aSubResult->shape()->isEqual(aSubShape)) {
-              aSubShapesToKeepAttrList->append(aSubResult, aSubShape);
+        }
+        else {
+          std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
+          for (; aSubsIt != anAllSubs.end(); aSubsIt++) {
+            ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
+            if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
+              aSubShapesToKeepAttrList->append(aSub, aSubShape);
               break;
             }
           }
@@ -216,50 +239,75 @@ void FeaturesPlugin_RemoveSubShapes::execute()
     aResultShape = anAttrSelectionInList->value();
   }
   // deleted and copied must be jointed to one list which keeps all the history
-  GeomAlgoAPI_MakeShapeList aMakeShapeList;
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
 
   // find all removed shapes
   std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aDeletedSubs(new GeomAlgoAPI_MakeShapeCustom);
   std::set<GeomAPI_Shape::ShapeType> aTypes; // types that where removed
   aTypes.insert(GeomAPI_Shape::FACE);
-  for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
-    if (!anIt.current().get() || anIt.current()->isNull())
+
+  std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
+  for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
+    GeomShapePtr aSubShape = *anIt;
+    if (!aSubShape.get() || aSubShape->isNull())
       continue;
+
     int anIndex;
     for(anIndex = 0; anIndex < aSubsNb; ++anIndex) {
       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
       GeomShapePtr aLeftShape = anAttrSelectionInList->value();
-      if (anIt.current()->isEqual(aLeftShape)) {
+      if (aSubShape->isEqual(aLeftShape)) {
         break; // found in a left-list
       }
     }
     if (anIndex == aSubsNb) { // not found in left
-      aDeletedSubs->addDeleted(anIt.current());
-      aTypes.insert(anIt.current()->shapeType());
-      if (anIt.current()->shapeType() != GeomAPI_Shape::FACE) {
-        GeomAPI_ShapeExplorer aFaces(anIt.current(), GeomAPI_Shape::FACE);
+      aDeletedSubs->addDeleted(aSubShape);
+      aTypes.insert(aSubShape->shapeType());
+      if (aSubShape->shapeType() != GeomAPI_Shape::FACE) {
+        GeomAPI_ShapeExplorer aFaces(aSubShape, GeomAPI_Shape::FACE);
         for(; aFaces.more(); aFaces.next())
           aDeletedSubs->addDeleted(aFaces.current());
       }
     }
   }
-  aMakeShapeList.appendAlgo(aDeletedSubs);
+  aMakeShapeList->appendAlgo(aDeletedSubs);
 
   std::shared_ptr<GeomAlgoAPI_Copy> aCopy(new GeomAlgoAPI_Copy(aResultShape));
   aResultShape = aCopy->shape();
-  aMakeShapeList.appendAlgo(aCopy);
+  aMakeShapeList->appendAlgo(aCopy);
+
+  if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
+    aResultShape = GeomAlgoAPI_ShapeTools::groupSharedTopology(aResultShape);
+    if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
+      // if the result has only one sub-shape, discard the compound
+      GeomAPI_ShapeIterator aSubIt(aResultShape);
+      GeomShapePtr aSub = aSubIt.current();
+      aSubIt.next();
+      if (!aSubIt.more())
+        aResultShape = aSub;
+    }
+  }
 
   // Store result.
   ResultBodyPtr aResultBody = document()->createBody(data());
-  aResultBody->storeModified(aBaseShape, aResultShape, 1);
-  std::set<GeomAPI_Shape::ShapeType>::iterator aTypeIter = aTypes.begin();
-  for(; aTypeIter != aTypes.end(); aTypeIter++)
-    aResultBody->loadDeletedShapes(&aMakeShapeList, aBaseShape, *aTypeIter, 1);
-  aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::FACE,
-         2, "Modified_Face", *aMakeShapeList.mapOfSubShapes().get(), true, false, true);
-  aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::EDGE,
-         3, "Modified_Edge", *aMakeShapeList.mapOfSubShapes().get(), false, false, true);
-  aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::VERTEX,
-         4, "Modified_Vertex", *aMakeShapeList.mapOfSubShapes().get());
+  std::list<GeomShapePtr> anOldShapes;
+  anOldShapes.push_back(aBaseShape);
+  aResultBody->storeModified(anOldShapes, aResultShape, aMakeShapeList);
+  for (std::set<GeomAPI_Shape::ShapeType>::iterator aTypeIter = aTypes.begin();
+       aTypeIter != aTypes.end();
+       ++aTypeIter)
+  {
+    aResultBody->loadDeletedShapes(aMakeShapeList, aBaseShape, *aTypeIter);
+  }
+
+  aResultBody->loadModifiedShapes(aMakeShapeList,
+                                  aBaseShape,
+                                  GeomAPI_Shape::FACE);
+  aResultBody->loadModifiedShapes(aMakeShapeList,
+                                  aBaseShape,
+                                  GeomAPI_Shape::EDGE);
+  aResultBody->loadModifiedShapes(aMakeShapeList,
+                                  aBaseShape,
+                                  GeomAPI_Shape::VERTEX);
   setResult(aResultBody);
 }