Salome HOME
CEA : Lot2 - Bounding box
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanSmash.cpp
index 9c8bb1e578bffda2db38ab2ccc6990304361f277..0c7f3bab1f292731aa907c874482204f10440651 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  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
@@ -46,105 +46,59 @@ void FeaturesPlugin_BooleanSmash::initAttributes()
 {
   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
+
+  initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
 }
 
 //==================================================================================================
 void FeaturesPlugin_BooleanSmash::execute()
 {
   std::string anError;
-  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);
-    }
-  }
+  GeomAPI_ShapeHierarchy 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.empty() || aToolsHistory.empty()) {
     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 (GeomAPI_ShapeHierarchy::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 aNUIt = aNotUsed.begin(); aNUIt != aNotUsed.end(); ++aNUIt)
+        anObjectsHistory.addObject(*aNUIt);
     }
   }
 
@@ -154,7 +108,7 @@ void FeaturesPlugin_BooleanSmash::execute()
     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();
@@ -166,7 +120,7 @@ void FeaturesPlugin_BooleanSmash::execute()
     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();
@@ -179,7 +133,7 @@ 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 (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
@@ -212,6 +166,13 @@ void FeaturesPlugin_BooleanSmash::execute()
     aMakeShapeList->appendAlgo(aFillerAlgo);
   }
 
+  // take into account a version of SMASH feature
+  if (data()->version() == BOP_VERSION_9_4()) {
+    // 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<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
 
   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,