Salome HOME
Result CompSolid should inherits ResultBody. All model realization concerned Naming...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.cpp
index e71b379baaa0dacddc7be125e259d92a9c410cb4..7f414c613d81e747b8651f91ce16ee372d16cb9b 100644 (file)
 #include <ModelAPI_Document.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_ResultBody.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
+
 #include <GeomAlgoAPI_Boolean.h>
-using namespace std;
+#include <GeomAlgoAPI_MakeShapeList.h>
+#include <GeomAlgoAPI_ShapeProps.h>
 
 #define FACE 4
 #define _MODIFY_TAG 1
 #define _DELETED_TAG 2
+#define _SUBSOLIDS_TAG 3 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
+
+//=================================================================================================
 FeaturesPlugin_Boolean::FeaturesPlugin_Boolean()
 {
 }
 
+//=================================================================================================
 void FeaturesPlugin_Boolean::initAttributes()
 {
-  data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::type());
-  data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type());
-  data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type());
+  data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
+
+  AttributeSelectionListPtr aSelection = 
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
+    FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
+  // extrusion works with faces always
+  aSelection->setSelectionType("SOLID");
+
+  aSelection = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
+    FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
+  // extrusion works with faces always
+  aSelection->setSelectionType("SOLID");
+
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
 }
 
+//=================================================================================================
 std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::string& theAttrName)
 {
   std::shared_ptr<ModelAPI_AttributeReference> aObjRef = std::dynamic_pointer_cast<
@@ -41,74 +64,159 @@ std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Boolean::getShape(const std::strin
   return std::shared_ptr<GeomAPI_Shape>();
 }
 
-
+//=================================================================================================
 void FeaturesPlugin_Boolean::execute()
 {
+  // Getting operation type.
   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
       ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID()));
   if (!aTypeAttr)
     return;
-  int aType = aTypeAttr->value();
-
-  std::shared_ptr<GeomAPI_Shape> anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID());
-  if (!anObject)
-    return;
-
-  std::shared_ptr<GeomAPI_Shape> aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID());
-  if (!aTool)
-    return;
-
-  std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
-
-  GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType);
-  if(aFeature && !aFeature->isDone()) {
-    static const std::string aFeatureError = "Boolean feature: algorithm failed";  
-    setError(aFeatureError);
-    return;
+  GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value();
+
+  ListOfShape anObjects, aTools;
+
+  // Getting objects.
+  AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::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);
   }
-   // Check if shape is valid
-  if (aFeature->shape()->isNull()) {
-    static const std::string aShapeError = "Boolean feature: resulting shape is Null";     
-    setError(aShapeError);
-    return;
-  }
-  if(!aFeature->isValid()) {
-    static const std::string aFeatureError = "Boolean feature: resulting shape is not valid";  
-    setError(aFeatureError);
-    return;
-  }  
-  // if result of Boolean operation is same as was before it means that Boolean operation has no sence
-  // and naming provides no result, so, generate an error in this case
-  if (anObject->isEqual(aFeature->shape())) {
-    static const std::string aFeatureError = "Boolean feature: operation was not performed";  
-    setError(aFeatureError);
-    return;
+
+  // Getting tools.
+  AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
+  for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aToolAttr = aToolsSelList->value(aToolsIndex);
+    std::shared_ptr<GeomAPI_Shape> aTool = aToolAttr->value();
+    if(!aTool.get()) {
+      return;
+    }
+    aTools.push_back(aTool);
   }
-  //LoadNamingDS
-  LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType);
 
-  setResult(aResultBody);
+  int aResultIndex = 0;
+
+  switch(aType) {
+    case GeomAlgoAPI_Boolean::BOOL_CUT:
+    case GeomAlgoAPI_Boolean::BOOL_COMMON:{
+      if(anObjects.empty() || aTools.empty()) {
+        std::string aFeatureError = "Not enough objects for boolean operation";
+        setError(aFeatureError);
+        return;
+      }
+
+      // Cut each object with all tools
+      for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
+        std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
+        ListOfShape aListWithObject;
+        aListWithObject.push_back(anObject);
+        GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, aType);
+
+        // Checking that the algorithm worked properly.
+        if(!aBoolAlgo.isDone()) {
+          static const std::string aFeatureError = "Boolean algorithm failed";
+          setError(aFeatureError);
+          return;
+        }
+        if(aBoolAlgo.shape()->isNull()) {
+          static const std::string aShapeError = "Resulting shape is Null";
+          setError(aShapeError);
+          return;
+        }
+        if(!aBoolAlgo.isValid()) {
+          std::string aFeatureError = "Warning: resulting shape is not valid";
+          setError(aFeatureError);
+          return;
+        }
+
+        if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) {
+          std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
+          LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo);
+          setResult(aResultBody, aResultIndex);
+          aResultIndex++;
+        }
+      }
+      break;
+    }
+    case GeomAlgoAPI_Boolean::BOOL_FUSE: {
+      if(anObjects.empty() && aTools.size() > 1) {
+        anObjects.push_back(aTools.back());
+        aTools.pop_back();
+      }else if(aTools.empty() && anObjects.size() > 1) {
+        aTools.push_back(anObjects.back());
+        anObjects.pop_back();
+      }
+
+      if(anObjects.empty() || aTools.empty()) {
+        std::string aFeatureError = "Not enough objects for boolean operation";
+        setError(aFeatureError);
+        return;
+      }
+
+      // Fuse all objects and all tools.
+      GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType);
+
+      // Checking that the algorithm worked properly.
+      if(!aBoolAlgo.isDone()) {
+        static const std::string aFeatureError = "Boolean algorithm failed";
+        setError(aFeatureError);
+        return;
+      }
+      if(aBoolAlgo.shape()->isNull()) {
+        static const std::string aShapeError = "Resulting shape is Null";
+        setError(aShapeError);
+        return;
+      }
+      if(!aBoolAlgo.isValid()) {
+        std::string aFeatureError = "Warning: resulting shape is not valid";
+        setError(aFeatureError);
+        return;
+      }
+
+      std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
+      LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo);
+      setResult(aResultBody, aResultIndex);
+      aResultIndex++;
+      break;
+    }
+    default: {
+      std::string anOperationError = "Error: wrong type of operation";
+      setError(anOperationError);
+      return;
+    }
+  }
+  // remove the rest results if there were produced in the previous pass
+  removeResults(aResultIndex);
 }
 
-//============================================================================
-void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, 
-                                               std::shared_ptr<ModelAPI_ResultBody> theResultBody, 
-                                               std::shared_ptr<GeomAPI_Shape> theObject,
-                                               std::shared_ptr<GeomAPI_Shape> theTool,
-                                               int theType)
-{  
-
+//=================================================================================================
+void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
+                                          const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
+                                          const ListOfShape& theTools,
+                                          const GeomAlgoAPI_Boolean& theAlgo)
+{
+  ModelAPI_BodyBuilder* aResultBuilder = theResultBody->getBodyBuilder();
   //load result
-  theResultBody->storeModified(theObject, theFeature->shape()); 
-
-  GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
-  theFeature->mapOfShapes(*aSubShapes);
-
-  // Put in DF modified faces
-  theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes);
-  theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool,   FACE, _MODIFY_TAG, *aSubShapes);
-
-  //Put in DF deleted faces
-  theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG);
-  theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool,   FACE, _DELETED_TAG);  
+  if(theBaseShape->isEqual(theAlgo.shape())) {
+    aResultBuilder->store(theAlgo.shape());
+  } else {
+    aResultBuilder->storeModified(theBaseShape, theAlgo.shape(), _SUBSOLIDS_TAG);
+
+    GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape();
+
+    std::string aModName = "Modified";
+    aResultBuilder->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE,
+                                               _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
+    aResultBuilder->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG);
+
+    for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
+      aResultBuilder->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE,
+                                                 _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get());
+      aResultBuilder->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG);
+    }
+  }
 }