Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
index 2b3188ddcbd4d5f4fcc439063fa034cc734974a5..466e12856c66c1fe3f3b66305a28df7debe26709 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  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_Union.h"
 
 #include <GeomAlgoAPI_Boolean.h>
+#include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PaveFiller.h>
+#include <GeomAlgoAPI_ShapeBuilder.h>
+#include <GeomAlgoAPI_Tools.h>
 
 #include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
 
+#include <ModelAPI_AttributeBoolean.h>>
+#include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
-#include <ModelAPI_ResultCompSolid.h>
+#include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
 
+
+static const double DEFAULT_FUZZY = 1.e-5;
+
+
 //=================================================================================================
 FeaturesPlugin_Union::FeaturesPlugin_Union()
 {
@@ -39,141 +48,108 @@ FeaturesPlugin_Union::FeaturesPlugin_Union()
 void FeaturesPlugin_Union::initAttributes()
 {
   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
+
+  data()->addAttribute(USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
+  data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
+  boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
+  real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
+
+  initVersion(BOP_VERSION_9_4(), selectionList(BASE_OBJECTS_ID()));
 }
 
 //=================================================================================================
 void FeaturesPlugin_Union::execute()
 {
-  ListOfShape anObjects;
-  std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
+  GeomAPI_ShapeHierarchy anObjects;
+  ListOfShape anEmptyList;
 
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList =
-    selectionList(FeaturesPlugin_Union::BASE_OBJECTS_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();
-    ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(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);
-    }
-  }
-
-  // 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(); anIt++) {
-    std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
-    ListOfShape& aUsedInOperationSolids = anIt->second;
-    anObjects.insert(anObjects.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);
-      }
-    }
-  }
+  if (!processAttribute(BASE_OBJECTS_ID(), anObjects, anEmptyList))
+    return;
 
-  if(anObjects.size() < 2) {
+  if(anObjects.objects().size() < 2) {
     setError("Error: Not enough objects for operation. Should be at least 2.");
     return;
   }
 
+  // Getting fuzzy parameter.
+  // Used as additional tolerance to eliminate tiny results.
+  // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
+  bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
+  double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
+
+  std::string anError;
+  int aResultIndex = 0;
+  std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
+  ListOfShape aResultShapesList;
+
+  GeomShapePtr aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(ListOfShape());
+
   // Fuse objects.
-  ListOfShape aTools;
-  aTools.splice(aTools.begin(), anObjects, anObjects.begin());
-  std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
-                                                            aTools,
-                                                            GeomAlgoAPI_Boolean::BOOL_FUSE));
-
-  // Checking that the algorithm worked properly.
-  GeomAlgoAPI_MakeShapeList aMakeShapeList;
-  GeomAPI_DataMapOfShapeShape aMapOfShapes;
-  if(!aFuseAlgo->isDone()) {
-    setError("Error: Boolean algorithm failed.");
-    return;
-  }
-  if(aFuseAlgo->shape()->isNull()) {
-    setError("Error: Resulting shape is Null.");
-    return;
+  bool isOk = true;
+  for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
+       anObjectsIt != anObjects.end() && isOk;
+       ++anObjectsIt) {
+    GeomShapePtr anObject = *anObjectsIt;
+    GeomShapePtr aParent = anObjects.parent(anObject, false);
+
+    if (aParent && aParent->shapeType() <= GeomAPI_Shape::COMPSOLID) {
+      // get parent once again to mark it and the subs as processed
+      aParent = anObjects.parent(anObject);
+      // compsolid handling
+      isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_FUSE,
+                              anObjects, aParent, anEmptyList, anEmptyList,
+                              aFuzzy,
+                              aResultIndex, aResultBaseAlgoList, aResultShapesList,
+                              aResultCompound);
+    } else {
+      // process object as is
+      isOk = processObject(GeomAlgoAPI_Tools::BOOL_FUSE,
+                           anObject, anEmptyList, anEmptyList,
+                           aFuzzy,
+                           aResultIndex, aResultBaseAlgoList, aResultShapesList,
+                           aResultCompound);
+    }
   }
-  if(!aFuseAlgo->isValid()) {
-    setError("Error: Resulting shape is not valid.");
-    return;
+
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
+  for (std::vector<ModelAPI_Tools::ResultBaseAlgo>::iterator
+       aRBAIt = aResultBaseAlgoList.begin();
+       aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
+    aMakeShapeList->appendAlgo(aRBAIt->makeShape);
   }
 
-  GeomShapePtr aShape = aFuseAlgo->shape();
-  aMakeShapeList.appendAlgo(aFuseAlgo);
-  aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
-
-  // Store original shapes for naming.
-  anObjects.splice(anObjects.begin(), aTools);
-  anObjects.insert(anObjects.end(), aShapesToAdd.begin(), aShapesToAdd.end());
-
-  // Combine result with not used solids from compsolid.
-  if(aShapesToAdd.size() > 0) {
-    aShapesToAdd.push_back(aShape);
-    std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
-      new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
-    if(!aFillerAlgo->isDone()) {
-      setError("Error: PaveFiller algorithm failed.");
-      return;
-    }
-    if(aFillerAlgo->shape()->isNull()) {
-      setError("Error: Resulting shape is Null.");
-      return;
-    }
-    if(!aFillerAlgo->isValid()) {
-      setError("Error: Resulting shape is not valid.");
-      return;
+  GeomShapePtr aShape;
+  GeomAPI_ShapeIterator aCIt(aResultCompound);
+  if (data()->version().empty()) {
+    // if the compound consists of a single sub-shape, take it,
+    // otherwise, take the full compound
+    aShape = aCIt.current();
+    aCIt.next();
+    if (aCIt.more())
+      aShape = aResultCompound;
+  }
+  else {
+    // merge hierarchies of compounds containing objects and tools
+    aShape = keepUnusedSubsOfCompound(aCIt.current(), anObjects, GeomAPI_ShapeHierarchy(),
+                                      aMakeShapeList);
+    for (aCIt.next(); aCIt.more(); aCIt.next()) {
+      std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
+      aBuilder->add(aShape, aCIt.current());
+      aMakeShapeList->appendAlgo(aBuilder);
     }
-
-    aShape = aFillerAlgo->shape();
-    aMakeShapeList.appendAlgo(aFillerAlgo);
-    aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
   }
 
   // Store result and naming.
-  const int aModifyTag = 1;
-  const int aDeletedTag = 2;
-  /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
-  const int aSubsolidsTag = 3;
-  const std::string aModName = "Modified";
-
   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
-  aResultBody->storeModified(anObjects.front(), aShape, aSubsolidsTag);
-
-  for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
-    aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE,
-                                             aModifyTag, aModName, aMapOfShapes);
-    aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
+  ListOfShape anObjectsList = anObjects.objects();
+  aResultBody->storeModified(anObjectsList.front(), aShape);
+
+  for(ListOfShape::const_iterator anIter = anObjectsList.begin();
+      anIter != anObjectsList.end(); ++anIter) {
+    aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::EDGE);
+    aResultBody->loadModifiedShapes(aMakeShapeList, *anIter, GeomAPI_Shape::FACE);
+    //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
   }
 
   setResult(aResultBody);