Salome HOME
Merge branch 'mbs/32757' of https://codev-tuleap.cea.fr/plugins/git/salome/shaper...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.cpp
index 46a16c54c80c358dcdbd13136d19a03b457f5418..e8788e76ad16a9a2e1486e8c7527810b395b9d5f 100644 (file)
@@ -20,6 +20,7 @@
 #include "FeaturesPlugin_Partition.h"
 
 #include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeSelectionList.h>
@@ -49,6 +50,9 @@
 #include <sstream>
 
 
+static const double DEFAULT_FUZZY = 1.e-5;
+
+
 //=================================================================================================
 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
 {
@@ -58,6 +62,12 @@ FeaturesPlugin_Partition::FeaturesPlugin_Partition()
 void FeaturesPlugin_Partition::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()));
 }
 
@@ -75,6 +85,12 @@ void FeaturesPlugin_Partition::execute()
     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);
+
   ListOfShape aBaseObjects = anObjects.objects();
   aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
 
@@ -85,7 +101,7 @@ void FeaturesPlugin_Partition::execute()
   // cut unused solids of composolids from the objects of partition
   ListOfShape aTargetObjects, anUnusedSubs;
   std::string aError;
-  if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aMakeShapeList, aError)) {
+  if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aFuzzy, aMakeShapeList, aError)) {
     setError(aError);
     return;
   }
@@ -93,7 +109,7 @@ void FeaturesPlugin_Partition::execute()
 
   // perform partition first time to split target solids by planes
   std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
-      new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
+      new GeomAlgoAPI_Partition(aTargetObjects, aPlanes, aFuzzy));
 
   // Checking that the algorithm worked properly.
   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
@@ -209,9 +225,9 @@ void FeaturesPlugin_Partition::storeResult(
 
 
 //=================================================================================================
-
 static bool cutSubs(ListOfShape& theSubsToCut,
                     const ListOfShape& theTools,
+                    const double theFuzzy,
                     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
                     std::string& theError)
 {
@@ -220,7 +236,7 @@ static bool cutSubs(ListOfShape& theSubsToCut,
 
   // cut from current list of solids
   std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo(
-      new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
+      new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT, theFuzzy));
   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
     return false;
   theMakeShapeList->appendAlgo(aCutAlgo);
@@ -233,10 +249,12 @@ static bool cutSubs(ListOfShape& theSubsToCut,
   return true;
 }
 
+//=================================================================================================
 bool FeaturesPlugin_Partition::cutSubs(
     GeomAPI_ShapeHierarchy& theHierarchy,
     ListOfShape& theUsed,
     ListOfShape& theNotUsed,
+    const double theFuzzy,
     std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
     std::string& theError)
 {
@@ -272,8 +290,8 @@ bool FeaturesPlugin_Partition::cutSubs(
     else
       aUsed.push_back(*anIt);
 
-    isOk = ::cutSubs(aUsed, aToolsForUsed, theMakeShapeList, theError)
-        && ::cutSubs(aNotUsed, aToolsForUnused, theMakeShapeList, theError);
+    isOk = ::cutSubs(aUsed, aToolsForUsed, theFuzzy, theMakeShapeList, theError)
+        && ::cutSubs(aNotUsed, aToolsForUnused, theFuzzy, theMakeShapeList, theError);
     if (isOk) {
       theUsed.insert(theUsed.end(), aUsed.begin(), aUsed.end());
       theNotUsed.insert(theNotUsed.end(), aNotUsed.begin(), aNotUsed.end());