Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanFuse.cpp
index f6e4599f730e695f2288301057c3d34a5c149211..ceaab696cda12416e929b0b3477ba9b8aeaf9d75 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  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
 //
 // 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_BooleanFuse.h"
@@ -24,6 +23,7 @@
 
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Session.h>
 #include <GeomAlgoAPI_Boolean.h>
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PaveFiller.h>
+#include <GeomAlgoAPI_ShapeBuilder.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomAlgoAPI_Tools.h>
 #include <GeomAlgoAPI_UnifySameDomain.h>
+
 #include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
 
 //==================================================================================================
 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
@@ -56,14 +59,16 @@ void FeaturesPlugin_BooleanFuse::initAttributes()
 
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
+
+  initVersion(THE_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
 }
 
 //==================================================================================================
 void FeaturesPlugin_BooleanFuse::execute()
 {
   std::string anError;
-  ListOfShape anObjects, aTools, anEdgesAndFaces;
-  std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
+  ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
+  ListOfShape aPlanes;
 
   bool isSimpleCreation = false;
 
@@ -75,65 +80,30 @@ void FeaturesPlugin_BooleanFuse::execute()
   }
 
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
-  for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
-    AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
-    GeomShapePtr anObject = anObjectAttr->value();
-    if (!anObject.get()) {
-      return;
-    }
-    ResultPtr aContext = anObjectAttr->context();
-    ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
-    if (!isSimpleCreation
-        && aResCompSolidPtr.get()
-        && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID)
-    {
-      GeomShapePtr aContextShape = aResCompSolidPtr->shape();
-      std::map<GeomShapePtr, ListOfShape>::iterator
-        anIt = aCompSolidsObjects.begin();
-      for (; anIt != aCompSolidsObjects.end(); anIt++) {
-        if (anIt->first->isEqual(aContextShape)) {
-          aCompSolidsObjects[anIt->first].push_back(anObject);
-          break;
-        }
-      }
-      if (anIt == aCompSolidsObjects.end()) {
-        aCompSolidsObjects[aContextShape].push_back(anObject);
-      }
-    } else {
-      if (anObject->shapeType() == GeomAPI_Shape::EDGE
-          || anObject->shapeType() == GeomAPI_Shape::FACE) {
-        anEdgesAndFaces.push_back(anObject);
-      } else {
-        anObjects.push_back(anObject);
-      }
-    }
-  }
+  if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
+    return;
 
   // Getting tools.
-  if (!isSimpleCreation) {
-    AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
-    for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
-      AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
-      GeomShapePtr aTool = aToolAttr->value();
-      if (aTool->shapeType() == GeomAPI_Shape::EDGE
-          || aTool->shapeType() == GeomAPI_Shape::FACE)
-      {
-        anEdgesAndFaces.push_back(aTool);
-      } else {
-        aTools.push_back(aTool);
-      }
-    }
-  }
+  if (!isSimpleCreation &&
+      !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
+    return;
 
-  if ((anObjects.size() + aTools.size() +
-    aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
+  ListOfShape anObjects, aTools, anEdgesAndFaces;
+  // all objects except edges and faces
+  anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
+                                   GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
+  aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
+                                GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
+
+  if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
     setError(aFeatureError);
     return;
   }
 
+  // version of FUSE feature
+  int aFuseVersion = version();
+
   // Collecting all solids which will be fused.
   ListOfShape aSolidsToFuse;
   aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
@@ -142,28 +112,19 @@ void FeaturesPlugin_BooleanFuse::execute()
   // Collecting solids from compsolids which will not be modified
   // in boolean operation and will be added to result.
   ListOfShape aShapesToAdd;
-  for (std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
-       anIt != aCompSolidsObjects.end();
-       ++anIt)
-  {
-    GeomShapePtr aCompSolid = anIt->first;
-    ListOfShape& aUsedInOperationSolids = anIt->second;
-    aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(),
-                         aUsedInOperationSolids.end());
-
-    // Collect solids from compsolid which will not be modified in boolean operation.
-    for (GeomAPI_ShapeExplorer
-         anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
-      GeomShapePtr aSolidInCompSolid = anExp.current();
-      ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
-      for (; anIt != aUsedInOperationSolids.end(); anIt++) {
-        if (aSolidInCompSolid->isEqual(*anIt)) {
-          break;
-        }
-      }
-      if (anIt == aUsedInOperationSolids.end()) {
-        aShapesToAdd.push_back(aSolidInCompSolid);
-      }
+  for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
+       !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
+       ++anObjectsIt) {
+    GeomShapePtr anObject = *anObjectsIt;
+    GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
+
+    if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
+      // mark all subs of this parent as precessed to avoid handling twice
+      aParent = anObjectsHierarchy.Parent(anObject);
+
+      ListOfShape aUsed, aNotUsed;
+      anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
+      aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
     }
   }
 
@@ -175,7 +136,7 @@ void FeaturesPlugin_BooleanFuse::execute()
   GeomShapePtr aCuttedEdgesAndFaces;
   if (!anEdgesAndFaces.empty()) {
     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
-      anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
+      anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
     if (aCutAlgo->isDone()) {
       aCuttedEdgesAndFaces = aCutAlgo->shape();
       aMakeShapeList->appendAlgo(aCutAlgo);
@@ -192,7 +153,7 @@ void FeaturesPlugin_BooleanFuse::execute()
       ListOfShape aOneObjectList;
       aOneObjectList.push_back(*anIt);
       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
-        new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
+        new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
 
       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
         aSolidsToFuse.push_back(aCutAlgo->shape());
@@ -217,7 +178,7 @@ void FeaturesPlugin_BooleanFuse::execute()
   } else if ((anObjects.size() + aTools.size()) > 1) {
     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
       aTools,
-      GeomAlgoAPI_Boolean::BOOL_FUSE));
+      GeomAlgoAPI_Tools::BOOL_FUSE));
 
     // Checking that the algorithm worked properly.
     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
@@ -269,22 +230,27 @@ void FeaturesPlugin_BooleanFuse::execute()
     aMakeShapeList->appendAlgo(aUnifyAlgo);
   }
 
+  if (aFuseVersion == THE_VERSION_1) {
+    // merge hierarchies of compounds containing objects and tools
+    // and append the result of the FUSE operation
+    aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
+  }
+
   int aResultIndex = 0;
 
-  GeomShapePtr aBackShape = anOriginalShapes.back();
-  anOriginalShapes.pop_back();
   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
 
+  ListOfShape anEmptyTools;
   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
-                                           aBackShape,
                                            anOriginalShapes,
+                                           anEmptyTools,
                                            aMakeShapeList,
                                            aShape);
   setResult(aResultBody, aResultIndex);
   aResultIndex++;
 
   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
-                                          aBackShape,
+                                          GeomShapePtr(),
                                           anOriginalShapes,
                                           aMakeShapeList,
                                           aShape);