Salome HOME
Task 3.2. Concealment into multi-level Compounds
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanSmash.cpp
index f0aa745dd3251110493eb09a0aa2b0fe9e202427..a46ed7409b6ca664fb177e54f286f4cd2f55565c 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_BooleanSmash.h"
 
+#include "FeaturesPlugin_Tools.h"
+
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_Tools.h>
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PaveFiller.h>
 #include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAlgoAPI_Tools.h>
+
 #include <GeomAPI_ShapeExplorer.h>
 #include <GeomAPI_ShapeIterator.h>
 
+static const int THE_SMASH_VERSION_1 = 20190506;
+
 //==================================================================================================
 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
@@ -43,134 +48,86 @@ void FeaturesPlugin_BooleanSmash::initAttributes()
 {
   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
+
+  initVersion(THE_SMASH_VERSION_1);
 }
 
 //==================================================================================================
 void FeaturesPlugin_BooleanSmash::execute()
 {
-  ListOfShape anObjects, aTools;
-  std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
-
-  // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList = selectionList(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 (aResCompSolidPtr.get())
-    {
-      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);
-    }
-  }
+  std::string anError;
+  ObjectHierarchy anObjectsHistory, aToolsHistory;
+  ListOfShape aPlanes;
 
-  // Getting tools.
-  AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
-  for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
-    AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
-    GeomShapePtr aTool = aToolAttr->value();
-    if(!aTool.get()) {
-      return;
-    }
-    aTools.push_back(aTool);
-  }
+  // Getting objects and tools.
+  if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
+      !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
+    return;
 
   int aResultIndex = 0;
 
-  if((anObjects.empty() && aCompSolidsObjects.empty())
-     || aTools.empty()) {
+  if (anObjectsHistory.IsEmpty() || aToolsHistory.IsEmpty()) {
     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
     setError(aFeatureError);
     return;
   }
 
+  // Collecting all shapes which will be smashed.
+  ListOfShape aShapesToSmash = anObjectsHistory.Objects();
+
   // List of original shapes for naming.
   ListOfShape anOriginalShapes;
-  anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
+  anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
+  ListOfShape aTools = aToolsHistory.Objects();
   anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
 
-  // Collecting all shapes which will be smashed.
-  ListOfShape aShapesToSmash;
-  aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
-
   // Collecting solids from compsolids which will not be modified in
   // boolean operation and will be added to result.
   ListOfShape aShapesToAdd;
-  for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
-       anIt = aCompSolidsObjects.begin();
-       anIt != aCompSolidsObjects.end();
+  for (ObjectHierarchy::Iterator anIt = anObjectsHistory.Begin();
+       anIt != anObjectsHistory.End();
        ++anIt)
   {
-    std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
-    ListOfShape& aUsedInOperationSolids = anIt->second;
-    anOriginalShapes.push_back(aCompSolid);
-    aShapesToSmash.insert(aShapesToSmash.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())
-    {
-      std::shared_ptr<GeomAPI_Shape> 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);
-      }
+    GeomShapePtr aParent = anObjectsHistory.Parent(*anIt, false);
+    if (aParent) {
+      anOriginalShapes.push_back(aParent);
+
+      ListOfShape aUsed, aNotUsed;
+      anObjectsHistory.SplitCompound(aParent, aUsed, aNotUsed);
+      aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
+
+      // add unused shapes of compounds/compsolids to the history,
+      // to avoid treating them as unused later when constructing a compound containing
+      // the result of Smash and all unused sub-shapes of multi-level compounds
+      for (ListOfShape::iterator anIt = aNotUsed.begin(); anIt != aNotUsed.end(); ++anIt)
+        anObjectsHistory.AddObject(*anIt);
     }
   }
 
-  GeomAlgoAPI_MakeShapeList aMakeShapeList;
-  GeomAPI_DataMapOfShapeShape aMapOfShapes;
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
   if (!aShapesToAdd.empty()) {
     // Cut objects with not used solids.
     std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
       new GeomAlgoAPI_Boolean(aShapesToSmash,
                               aShapesToAdd,
-                              GeomAlgoAPI_Boolean::BOOL_CUT));
+                              GeomAlgoAPI_Tools::BOOL_CUT));
 
     if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
       aShapesToSmash.clear();
       aShapesToSmash.push_back(anObjectsCutAlgo->shape());
-      aMakeShapeList.appendAlgo(anObjectsCutAlgo);
-      aMapOfShapes.merge(anObjectsCutAlgo->mapOfSubShapes());
+      aMakeShapeList->appendAlgo(anObjectsCutAlgo);
     }
 
     // Cut tools with not used solids.
     std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
       new GeomAlgoAPI_Boolean(aTools,
                               aShapesToAdd,
-                              GeomAlgoAPI_Boolean::BOOL_CUT));
+                              GeomAlgoAPI_Tools::BOOL_CUT));
 
     if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
       aTools.clear();
       aTools.push_back(aToolsCutAlgo->shape());
-      aMakeShapeList.appendAlgo(aToolsCutAlgo);
-      aMapOfShapes.merge(aToolsCutAlgo->mapOfSubShapes());
+      aMakeShapeList->appendAlgo(aToolsCutAlgo);
     }
   }
 
@@ -178,129 +135,64 @@ void FeaturesPlugin_BooleanSmash::execute()
   std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
     new GeomAlgoAPI_Boolean(aShapesToSmash,
                             aTools,
-                            GeomAlgoAPI_Boolean::BOOL_CUT));
+                            GeomAlgoAPI_Tools::BOOL_CUT));
 
   // Checking that the algorithm worked properly.
-  if (!aBoolAlgo->isDone()) {
-    static const std::string aFeatureError = "Error: Boolean algorithm failed.";
-    setError(aFeatureError);
-    return;
-  }
-  if (aBoolAlgo->shape()->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);
+  if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
+    setError(anError);
     return;
   }
-  aMakeShapeList.appendAlgo(aBoolAlgo);
-  aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
+
+  aMakeShapeList->appendAlgo(aBoolAlgo);
 
   // Put all (cut result, tools and not used solids) to PaveFiller.
-  aShapesToAdd.push_back(aBoolAlgo->shape());
+  GeomShapePtr aShape = aBoolAlgo->shape();
+  GeomAPI_ShapeIterator anIt(aShape);
+  if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
+    aShapesToAdd.push_back(aShape);
+  }
   aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
 
-  std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
-    new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
-  if (!aFillerAlgo->isDone()) {
-    std::string aFeatureError = "Error: PaveFiller algorithm failed.";
-    setError(aFeatureError);
-    return;
+  if (aShapesToAdd.size() == 1) {
+    aShape = aShapesToAdd.front();
   }
-  if (aFillerAlgo->shape()->isNull()) {
-    static const std::string aShapeError = "Error: Resulting shape is Null.";
-    setError(aShapeError);
-    return;
-  }
-  if (!aFillerAlgo->isValid()) {
-    std::string aFeatureError = "Error: Resulting shape is not valid.";
-    setError(aFeatureError);
-    return;
+  else {
+    std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
+      new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
+      setError(anError);
+      return;
+    }
+
+    aShape = aFillerAlgo->shape();
+    aMakeShapeList->appendAlgo(aFillerAlgo);
   }
 
-  std::shared_ptr<GeomAPI_Shape> aShape = aFillerAlgo->shape();
-  aMakeShapeList.appendAlgo(aFillerAlgo);
-  aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
+  // take into account a version of SMASH feature
+  int aSmashVersion = version();
+  if (aSmashVersion == THE_SMASH_VERSION_1) {
+    // merge hierarchies of compounds containing objects and tools
+    // and append the result of the FUSE operation
+    aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
+  }
 
-  std::shared_ptr<GeomAPI_Shape> aFrontShape = anOriginalShapes.front();
-  anOriginalShapes.pop_front();
   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
-  loadNamingDS(aResultBody,
-               aFrontShape,
-               anOriginalShapes,
-               aShape,
-               aMakeShapeList,
-               aMapOfShapes);
+
+  FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                           anOriginalShapes,
+                                           anOriginalShapes,
+                                           aMakeShapeList,
+                                           aShape);
 
   setResult(aResultBody, aResultIndex);
   aResultIndex++;
 
+  FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
+                                          GeomShapePtr(),
+                                          anOriginalShapes,
+                                          aMakeShapeList,
+                                          aShape);
+
   // remove the rest results if there were produced in the previous pass
   removeResults(aResultIndex);
 }
-
-//==================================================================================================
-void FeaturesPlugin_BooleanSmash::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,
-                                               true, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
-                                               aModifyETag, aModEName, theMapOfShapes, false,
-                                               true, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
-                                               aModifyFTag, aModFName, theMapOfShapes, false,
-                                               true, 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,
-                                                 true, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
-                                                 aModifyETag, aModEName, theMapOfShapes, false,
-                                                 true, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
-                                                 aModifyFTag, aModFName, theMapOfShapes, false,
-                                                 true, 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);
-    }
-  }
-}