Salome HOME
Update copyrights
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCut.cpp
index eebff3615f35824001011694ce5f3dac715756c8..784ef8f2b711287d51f949e6b1e19420df58a1b7 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_BooleanCut.h"
 
-#include <ModelAPI_ResultCompSolid.h>
+#include "FeaturesPlugin_Tools.h"
+
+#include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_Tools.h>
 
@@ -29,6 +30,8 @@
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PaveFiller.h>
 #include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAlgoAPI_Tools.h>
+
 #include <GeomAPI_ShapeExplorer.h>
 #include <GeomAPI_ShapeIterator.h>
 
@@ -54,7 +57,7 @@ void FeaturesPlugin_BooleanCut::execute()
       return;
     }
     ResultPtr aContext = anObjectAttr->context();
-    ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
+    ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
     if (aResCompSolidPtr.get())
     {
       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
@@ -99,12 +102,16 @@ void FeaturesPlugin_BooleanCut::execute()
     return;
   }
 
+  std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
+  ListOfShape aResultShapesList;
+  std::string anError;
+
   // For solids cut each object with all tools.
   for(ListOfShape::iterator anObjectsIt = anObjects.begin();
       anObjectsIt != anObjects.end();
       ++anObjectsIt) {
     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
-    GeomAlgoAPI_MakeShapeList aMakeShapeList;
+    std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
       new GeomAlgoAPI_Boolean(anObject,
                               aTools,
@@ -112,23 +119,12 @@ void FeaturesPlugin_BooleanCut::execute()
     GeomShapePtr aResShape = aCutAlgo->shape();
 
     // Checking that the algorithm worked properly.
-    if (!aCutAlgo->isDone()) {
-      static const std::string aFeatureError = "Error: Boolean algorithm failed.";
-      setError(aFeatureError);
-      return;
-    }
-    if(aResShape->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      return;
-    }
-    if (!aCutAlgo->isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
+      setError(anError);
       return;
     }
 
-    aMakeShapeList.appendAlgo(aCutAlgo);
+    aMakeShapeList->appendAlgo(aCutAlgo);
 
     GeomAPI_ShapeIterator aShapeIt(aResShape);
     if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
@@ -136,11 +132,20 @@ void FeaturesPlugin_BooleanCut::execute()
       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
         document()->createBody(data(), aResultIndex);
 
-      loadNamingDS(aResultBody, anObject, aTools, aResShape,
-                   aMakeShapeList, *(aCutAlgo->mapOfSubShapes()),
-                   false);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               anObject,
+                                               aTools,
+                                               aMakeShapeList,
+                                               aResShape);
       setResult(aResultBody, aResultIndex);
       aResultIndex++;
+
+      FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
+      aRBA.resultBody = aResultBody;
+      aRBA.baseShape = anObject;
+      aRBA.makeShape = aMakeShapeList;
+      aResultBaseAlgoList.push_back(aRBA);
+      aResultShapesList.push_back(aResShape);
     }
   }
 
@@ -171,32 +176,19 @@ void FeaturesPlugin_BooleanCut::execute()
       }
     }
 
-    GeomAlgoAPI_MakeShapeList aMakeShapeList;
+    std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
       new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
                               aTools,
                               GeomAlgoAPI_Boolean::BOOL_CUT));
 
     // Checking that the algorithm worked properly.
-    if (!aCutAlgo->isDone()) {
-      static const std::string aFeatureError = "Error: Boolean algorithm failed.";
-      setError(aFeatureError);
-      return;
-    }
-    if (aCutAlgo->shape()->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      return;
-    }
-    if (!aCutAlgo->isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
+      setError(anError);
       return;
     }
 
-    aMakeShapeList.appendAlgo(aCutAlgo);
-    GeomAPI_DataMapOfShapeShape aMapOfShapes;
-    aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
+    aMakeShapeList->appendAlgo(aCutAlgo);
     GeomShapePtr aResultShape = aCutAlgo->shape();
 
     // Add result to not used solids from compsolid.
@@ -211,8 +203,7 @@ void FeaturesPlugin_BooleanCut::execute()
         return;
       }
 
-      aMakeShapeList.appendAlgo(aFillerAlgo);
-      aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
+      aMakeShapeList->appendAlgo(aFillerAlgo);
       aResultShape = aFillerAlgo->shape();
     }
 
@@ -222,15 +213,20 @@ void FeaturesPlugin_BooleanCut::execute()
       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
         document()->createBody(data(), aResultIndex);
 
-      loadNamingDS(aResultBody,
-                   aCompSolid,
-                   aTools,
-                   aResultShape,
-                   aMakeShapeList,
-                   aMapOfShapes,
-                   false);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aCompSolid,
+                                               aTools,
+                                               aMakeShapeList,
+                                               aResultShape);
       setResult(aResultBody, aResultIndex);
       aResultIndex++;
+
+      FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
+      aRBA.resultBody = aResultBody;
+      aRBA.baseShape = aCompSolid;
+      aRBA.makeShape = aMakeShapeList;
+      aResultBaseAlgoList.push_back(aRBA);
+      aResultShapesList.push_back(aResultShape);
     }
   }
 
@@ -261,32 +257,19 @@ void FeaturesPlugin_BooleanCut::execute()
       }
     }
 
-    GeomAlgoAPI_MakeShapeList aMakeShapeList;
+    std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
       new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
                               aTools,
                               GeomAlgoAPI_Boolean::BOOL_CUT));
 
     // Checking that the algorithm worked properly.
-    if (!aCutAlgo->isDone()) {
-      static const std::string aFeatureError = "Error: Boolean algorithm failed.";
-      setError(aFeatureError);
-      return;
-    }
-    if (aCutAlgo->shape()->isNull()) {
-      static const std::string aShapeError = "Error: Resulting shape is Null.";
-      setError(aShapeError);
-      return;
-    }
-    if (!aCutAlgo->isValid()) {
-      std::string aFeatureError = "Error: Resulting shape is not valid.";
-      setError(aFeatureError);
+    if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
+      setError(anError);
       return;
     }
 
-    aMakeShapeList.appendAlgo(aCutAlgo);
-    GeomAPI_DataMapOfShapeShape aMapOfShapes;
-    aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
+    aMakeShapeList->appendAlgo(aCutAlgo);
     GeomShapePtr aResultShape = aCutAlgo->shape();
 
     // Add result to not used shape from compound.
@@ -312,82 +295,28 @@ void FeaturesPlugin_BooleanCut::execute()
       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
         document()->createBody(data(), aResultIndex);
 
-      loadNamingDS(aResultBody,
-                   aCompound,
-                   aTools,
-                   aResultShape,
-                   aMakeShapeList,
-                   aMapOfShapes,
-                   false);
+      FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+                                               aCompound,
+                                               aTools,
+                                               aMakeShapeList,
+                                               aResultShape);
       setResult(aResultBody, aResultIndex);
       aResultIndex++;
+
+      FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
+      aRBA.resultBody = aResultBody;
+      aRBA.baseShape = aCompound;
+      aRBA.makeShape = aMakeShapeList;
+      aResultBaseAlgoList.push_back(aRBA);
+      aResultShapesList.push_back(aResultShape);
     }
   }
 
+  // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
+  // result shape has been deleted, but in another it was modified or stayed.
+  GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
+  FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
+
   // remove the rest results if there were produced in the previous pass
   removeResults(aResultIndex);
 }
-
-//==================================================================================================
-void FeaturesPlugin_BooleanCut::loadNamingDS(ResultBodyPtr theResultBody,
-                                             const GeomShapePtr theBaseShape,
-                                             const ListOfShape& theTools,
-                                             const GeomShapePtr theResultShape,
-                                             GeomAlgoAPI_MakeShape& theMakeShape,
-                                             GeomAPI_DataMapOfShapeShape& theMapOfShapes,
-                                             const bool theIsStoreAsGenerated)
-{
-  //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,
-                                               theIsStoreAsGenerated, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
-                                               aModifyETag, aModEName, theMapOfShapes, false,
-                                               theIsStoreAsGenerated, true);
-    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
-                                               aModifyFTag, aModFName, theMapOfShapes, false,
-                                               theIsStoreAsGenerated, 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,
-                                                 theIsStoreAsGenerated, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
-                                                 aModifyETag, aModEName, theMapOfShapes, false,
-                                                 theIsStoreAsGenerated, true);
-
-      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
-                                                 aModifyFTag, aModFName, theMapOfShapes, false,
-                                                 theIsStoreAsGenerated, 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);
-    }
-  }
-}