Salome HOME
updated copyright message
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Intersection.cpp
index 93a02c79fe6399e022263c595acfd5ce5a4f37be..d7cc5aeb9e7dda0082538fef75a17afad5a88518 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // 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_Intersection.h"
 #include <ModelAPI_Document.h>
 #include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAlgoAPI_Intersection.h>
+#include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_Tools.h>
 #include <GeomAPI_ShapeExplorer.h>
 
 #include <sstream>
 
+
+static const std::string INTERSECTION_VERSION_1("v9.5");
+static const double DEFAULT_FUZZY = 1.e-5;
+
+
 //=================================================================================================
 FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
 {
@@ -40,36 +48,39 @@ FeaturesPlugin_Intersection::FeaturesPlugin_Intersection()
 //=================================================================================================
 void FeaturesPlugin_Intersection::initAttributes()
 {
-  data()->addAttribute(OBJECT_LIST_ID(),
+  AttributePtr anObjectsAttr = data()->addAttribute(OBJECT_LIST_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(INTERSECTION_VERSION_1, anObjectsAttr, AttributePtr());
 }
 
 //=================================================================================================
 void FeaturesPlugin_Intersection::execute()
 {
-  ListOfShape anObjects;
-
+  GeomAPI_ShapeHierarchy anObjectsHierarchy;
+  ListOfShape aPlanes;
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
-  for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
-    std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
-      anObjectsSelList->value(anObjectsIndex);
-    std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
-    if (!anObject.get()) {
-      return;
-    }
-    anObjects.push_back(anObject);
-  }
-
-  if(anObjects.empty()) {
+  if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes)) {
     setError("Error: Objects or tools are empty.");
     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);
+
   int aResultIndex = 0;
 
   // Create result.
-  GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects));
+  const ListOfShape& anObjects = anObjectsHierarchy.objects();
+  GeomMakeShapePtr anIntersectionAlgo(new GeomAlgoAPI_Intersection(anObjects, aFuzzy));
 
   // Checking that the algorithm worked properly.
   std::string anError;
@@ -78,39 +89,32 @@ void FeaturesPlugin_Intersection::execute()
     return;
   }
 
+  std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
+  aMakeShapeList->appendAlgo(anIntersectionAlgo);
+
+  GeomShapePtr aResShape = anIntersectionAlgo->shape();
+  if (data()->version() == INTERSECTION_VERSION_1) {
+    // merge hierarchies of compounds containing objects and tools
+    // and append the result of the FUSE operation
+    aResShape = keepUnusedSubsOfCompound(aResShape, anObjectsHierarchy,
+                                         GeomAPI_ShapeHierarchy(), aMakeShapeList);
+  }
+
   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
-  loadNamingDS(aResultBody, anObjects, anIntersectionAlgo);
+  ModelAPI_Tools::loadModifiedShapes(aResultBody,
+                                     anObjects,
+                                     ListOfShape(),
+                                     aMakeShapeList,
+                                     aResShape);
   setResult(aResultBody, aResultIndex);
   aResultIndex++;
 
+  ModelAPI_Tools::loadDeletedShapes(aResultBody,
+                                    GeomShapePtr(),
+                                    anObjects,
+                                    aMakeShapeList,
+                                    aResShape);
 
   // remove the rest results if there were produced in the previous pass
   removeResults(aResultIndex);
 }
-
-//=================================================================================================
-void FeaturesPlugin_Intersection::loadNamingDS(ResultBodyPtr theResultBody,
-                                               const ListOfShape& theObjects,
-                                               const GeomMakeShapePtr& theMakeShape)
-{
-  std::shared_ptr<GeomAPI_Shape> aResultShape = theMakeShape->shape();
-
-  if(theObjects.front()->isEqual(aResultShape)) {
-    theResultBody->store(aResultShape, false);
-    return;
-  }
-
-  theResultBody->storeModified(theObjects.front(), aResultShape);
-
-  const int aShapeTypesNb = 3;
-  const GeomAPI_Shape::ShapeType aShapeTypes[aShapeTypesNb] = {GeomAPI_Shape::VERTEX,
-                                                               GeomAPI_Shape::EDGE,
-                                                               GeomAPI_Shape::FACE };
-  for (ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
-    const GeomShapePtr aShape = *anIt;
-    for(int anIndex = 0; anIndex < aShapeTypesNb; ++anIndex) {
-      theResultBody->loadModifiedShapes(theMakeShape, aShape, aShapeTypes[anIndex]);
-      theResultBody->loadGeneratedShapes(theMakeShape, aShape, aShapeTypes[anIndex]);
-    }
-  }
-}