Salome HOME
Issue #3122: Move Measurement item to Inspection menu
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.cpp
index c180f725de75577719c1a32c5668da6c997f76c5..1afe60e8bf1c214270da3ba81fc7f26905463ca3 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_BooleanCommon.h"
 
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_Validator.h>
 
 #include <GeomAlgoAPI_Boolean.h>
 #include <GeomAlgoAPI_MakeShapeCustom.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomAPI_Face.h>
 #include <GeomAPI_ShapeIterator.h>
-
+#include <GeomAPI_ShapeExplorer.h>
+#include <GeomAlgoAPI_PaveFiller.h>
+#include <GeomAlgoAPI_CompoundBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 //==================================================================================================
 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
@@ -46,13 +51,15 @@ void FeaturesPlugin_BooleanCommon::initAttributes()
 
   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
+
+  initVersion(THE_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
 }
 
 //==================================================================================================
 void FeaturesPlugin_BooleanCommon::execute()
 {
-  ListOfShape anObjects, aTools, aPlanes;
-  std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
+  ListOfShape aPlanes;
+  ObjectHierarchy anObjects, aTools;
 
   bool isSimpleMode = false;
 
@@ -63,92 +70,55 @@ void FeaturesPlugin_BooleanCommon::execute()
   }
 
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
-  for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
-    AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
-    std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
-    if (!anObject.get()) {
-      return;
-    }
-    ResultPtr aContext = anObjectAttr->context();
-    ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
-    if (!isSimpleMode
-        && aResCompSolidPtr.get()
-        && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
-      std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
-      std::map<std::shared_ptr<GeomAPI_Shape>, 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 {
-      anObjects.push_back(anObject);
-    }
-  }
+  if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
+    return;
+  // Planes are not supported as objects of COMMON operation
+  aPlanes.clear();
 
   // Getting tools.
-  if (!isSimpleMode) {
-    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.get()) {
-        // It could be a construction plane.
-        ResultPtr aContext = aToolAttr->context();
-        aPlanes.push_back(aToolAttr->context()->shape());
-      } else {
-        aTools.push_back(aTool);
-      }
-    }
-  }
+  if (!isSimpleMode &&
+      !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
+    return;
 
-  if ((anObjects.empty() && aCompSolidsObjects.empty())
-      || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
+  if (anObjects.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
     setError(aFeatureError);
     return;
   }
 
+  // version of COMMON feature
+  int aCommonVersion = version();
+
   int aResultIndex = 0;
-  GeomAlgoAPI_MakeShapeList aMakeShapeList;
-  GeomAPI_DataMapOfShapeShape aMapOfShapes;
+  GeomShapePtr aResultCompound;
 
-  if (isSimpleMode)
-  {
-    ListOfShape::iterator anObjectsIt = anObjects.begin();
+  std::string anError;
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
+  std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
+  ListOfShape aResultShapesList;
+
+  if (isSimpleMode) {
+    ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
     GeomShapePtr aShape = *anObjectsIt;
-    for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
+    for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
         new GeomAlgoAPI_Boolean(aShape,
                                 *anObjectsIt,
-                                GeomAlgoAPI_Boolean::BOOL_COMMON));
+                                GeomAlgoAPI_Tools::BOOL_COMMON));
 
-      if (!aCommonAlgo->isDone()) {
-        std::string aFeatureError = "Error: An algorithm failed.";
-        setError(aFeatureError);
-        return;
-      }
-      if (aCommonAlgo->shape()->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        return;
-      }
-      if (!aCommonAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
+      if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
+        setError(anError);
         return;
       }
 
       aShape = aCommonAlgo->shape();
-      aMakeShapeList.appendAlgo(aCommonAlgo);
-      aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
+      aMakeShapeList->appendAlgo(aCommonAlgo);
+    }
+
+    if (aCommonVersion == THE_VERSION_1) {
+      // merge hierarchies of compounds containing objects and tools
+      // and append the result of the FUSE operation
+      aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
     }
 
     GeomAPI_ShapeIterator aShapeIt(aShape);
@@ -156,139 +126,78 @@ void FeaturesPlugin_BooleanCommon::execute()
       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
         document()->createBody(data(), aResultIndex);
 
-      loadNamingDS(aResultBody, anObjects.front(), anObjects, aShape, aMakeShapeList, aMapOfShapes);
+      ListOfShape anObjectList = anObjects.Objects();
+      ListOfShape aToolsList;
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               anObjectList,
+                                               aToolsList,
+                                               aMakeShapeList,
+                                               aShape);
+      GeomShapePtr aBaseShape = anObjectList.front();
+      anObjectList.pop_front();
       setResult(aResultBody, aResultIndex);
       aResultIndex++;
+
+      aToolsList = anObjectList;
+      FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
+      aRBA.resultBody = aResultBody;
+      aRBA.baseShape = aBaseShape;
+      aRBA.makeShape = aMakeShapeList;
+      aResultBaseAlgoList.push_back(aRBA);
+      aResultShapesList.push_back(aShape);
     }
   } else {
-    for (ListOfShape::iterator anObjectsIt = anObjects.begin();
-         anObjectsIt != anObjects.end();
+    if (aCommonVersion == THE_VERSION_1) {
+      // merge hierarchies of compounds containing objects and tools
+      aResultCompound =
+          keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
+    }
+
+    bool isOk = true;
+    for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
+         anObjectsIt != anObjects.End() && isOk;
          ++anObjectsIt)
     {
-      std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
-      ListOfShape aListWithObject;
-      aListWithObject.push_back(anObject);
-      GeomAlgoAPI_MakeShapeList aMakeShapeList;
-      std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
-      GeomShapePtr aResShape;
-
-      std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
-        GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
-
-      // Resize planes.
-      ListOfShape aToolsWithPlanes = aTools;
-      for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
-           anIt != aPlanes.cend();
-           ++anIt) {
-        GeomShapePtr aPlane = *anIt;
-        GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
-        std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
-          new GeomAlgoAPI_MakeShapeCustom);
-        aMkShCustom->addModified(aPlane, aTool);
-        aMakeShapeList.appendAlgo(aMkShCustom);
-        aToolsWithPlanes.push_back(aTool);
-      }
-
-      aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
-        aToolsWithPlanes,
-        GeomAlgoAPI_Boolean::BOOL_COMMON));
-      aResShape = aBoolAlgo->shape();
-
-      // Checking that the algorithm worked properly.
-      if (!aBoolAlgo->isDone()) {
-        static const std::string aFeatureError = "Error: Boolean algorithm failed.";
-        setError(aFeatureError);
-        return;
-      }
-      if (aResShape->isNull()) {
-        static const std::string aShapeError = "Error: Resulting shape is Null.";
-        setError(aShapeError);
-        return;
-      }
-      if (!aBoolAlgo->isValid()) {
-        std::string aFeatureError = "Error: Resulting shape is not valid.";
-        setError(aFeatureError);
-        return;
-      }
-
-      aMakeShapeList.appendAlgo(aBoolAlgo);
-
-      GeomAPI_ShapeIterator aShapeIt(aResShape);
-      if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
-        std::shared_ptr<ModelAPI_ResultBody> aResultBody =
-          document()->createBody(data(), aResultIndex);
-
-        loadNamingDS(aResultBody, anObject, aTools, aResShape,
-                     aMakeShapeList, *(aBoolAlgo->mapOfSubShapes()));
-        setResult(aResultBody, aResultIndex);
-        aResultIndex++;
+      GeomShapePtr anObject = *anObjectsIt;
+      GeomShapePtr aParent = anObjects.Parent(anObject);
+
+      if (aParent) {
+        GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
+        if (aShapeType == GeomAPI_Shape::COMPOUND) {
+          // Compound handling
+          isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
+                                 anObjects, aParent, aTools.Objects(),
+                                 aResultIndex, aResultBaseAlgoList, aResultShapesList,
+                                 aResultCompound);
+        }
+        else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
+          // Compsolid handling
+          isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
+                                  anObjects, aParent, aTools.Objects(), ListOfShape(),
+                                  aResultIndex, aResultBaseAlgoList, aResultShapesList,
+                                  aResultCompound);
+        }
+      } else {
+        // process object as is
+        isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
+                             anObject, aTools.Objects(), aPlanes,
+                             aResultIndex, aResultBaseAlgoList, aResultShapesList,
+                             aResultCompound);
       }
     }
+
+    storeResult(anObjects.Objects(), aTools.Objects(), aResultCompound, aResultIndex,
+                aMakeShapeList, aResultBaseAlgoList);
   }
 
+  // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
+  // result shape has been deleted, but in another it was modified or stayed.
+  if (!aResultCompound)
+    aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
+  FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
+                                          aTools.Objects(),
+                                          aResultCompound);
+
   // remove the rest results if there were produced in the previous pass
   removeResults(aResultIndex);
 }
-
-//==================================================================================================
-void FeaturesPlugin_BooleanCommon::loadNamingDS(ResultBodyPtr theResultBody,
-                                                const GeomShapePtr theBaseShape,
-                                                const ListOfShape& theTools,
-                                                const GeomShapePtr theResultShape,
-                                                GeomAlgoAPI_MakeShape& theMakeShape,
-                                                GeomAPI_DataMapOfShapeShape& theMapOfShapes)
-{
-  //load result
-  if (theBaseShape->isEqual(theResultShape)) {
-    theResultBody->store(theResultShape, false);
-  } else {
-    const int aModifyVTag = 1;
-    const int aModifyETag = 2;
-    const int aModifyFTag = 3;
-    const int aDeletedTag = 4;
-    /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
-    const int aSubsolidsTag = 5;
-
-    theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
-
-    const std::string aModVName = "Modified_Vertex";
-    const std::string aModEName = "Modified_Edge";
-    const std::string aModFName = "Modified_Face";
-
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
-                                               aModifyVTag, aModVName, theMapOfShapes, false,
-                                               false, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
-                                               aModifyETag, aModEName, theMapOfShapes, false,
-                                               false, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
-                                               aModifyFTag, aModFName, theMapOfShapes, false,
-                                               false, true);
-
-    theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
-                                     GeomAPI_Shape::VERTEX, aDeletedTag);
-    theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
-                                     GeomAPI_Shape::EDGE, aDeletedTag);
-    theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
-                                     GeomAPI_Shape::FACE, aDeletedTag);
-
-    for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
-    {
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
-                                                 aModifyVTag, aModVName, theMapOfShapes, false,
-                                                 false, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
-                                                 aModifyETag, aModEName, theMapOfShapes, false,
-                                                 false, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
-                                                 aModifyFTag, aModFName, theMapOfShapes, false,
-                                                 false, true);
-
-      theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
-      theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
-      theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
-    }
-  }
-}