Salome HOME
[PythonAPI] Fix error in parameter wrapper
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.cpp
index 362081c0bc879260d67041940c2fffc6ca28bc0b..2a584910c218521ee7350e879b2a02d8122ec9ca 100755 (executable)
@@ -8,6 +8,7 @@
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
+#include <ModelAPI_AttributeBoolean.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_BodyBuilder.h>
 #include <ModelAPI_Validator.h>
 
 #include <GeomAlgoAPI_Partition.h>
+#include <GeomAlgoAPI_MakeShapeCustom.h>
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 
+#include <sstream>
+
 //=================================================================================================
 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
 {
@@ -38,8 +42,9 @@ void FeaturesPlugin_Partition::initAttributes()
     FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
   aSelection->setSelectionType("SOLID");
 
-  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
+
+  data()->addAttribute(COMBINE_ID(), ModelAPI_AttributeBoolean::typeId());
 }
 
 //=================================================================================================
@@ -59,7 +64,7 @@ std::shared_ptr<GeomAPI_Shape> FeaturesPlugin_Partition::getShape(const std::str
 //=================================================================================================
 void FeaturesPlugin_Partition::execute()
 {
-  ListOfShape anObjects, aTools;
+  ListOfShape anObjects, aTools, aToolsForNaming;
 
   // Getting objects.
   AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
@@ -72,37 +77,45 @@ void FeaturesPlugin_Partition::execute()
     anObjects.push_back(anObject);
   }
 
+  GeomAlgoAPI_MakeShapeList aMakeShapeList;
+  std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
+
   // Getting tools.
   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Partition::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()) {
+    if(!aTool.get()) {
       // it could be a construction plane
       ResultPtr aContext = aToolAttr->context();
-      if (aContext.get()) {
-        aTool = GeomAlgoAPI_ShapeTools::faceToInfinitePlane(aContext->shape());
+      if(aContext.get()) {
+        aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aContext->shape(), aBoundingPoints);
+        std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(new GeomAlgoAPI_MakeShapeCustom);
+        aMkShCustom->addModified(aContext->shape(), aTool);
+        aMakeShapeList.append(aMkShCustom);
+        aTools.push_back(aTool);
+        aToolsForNaming.push_back(aContext->shape());
       }
+    } else {
+      aTools.push_back(aTool);
+      aToolsForNaming.push_back(aTool);
     }
-    if (!aTool.get()) {
-      return;
-    }
-    aTools.push_back(aTool);
   }
 
-  int aResultIndex = 0;
+  // Getting combine flag.
+  bool isCombine = boolean(COMBINE_ID())->value();
 
-  if (anObjects.empty() || aTools.empty()) {
+  if(anObjects.empty()/* || aTools.empty()*/) {
     std::string aFeatureError = "Not enough objects for partition 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_Partition aPartitionAlgo(aListWithObject, aTools);
+  int aResultIndex = 0;
+
+  if(isCombine) {
+    // Create single result.
+    GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools);
 
     // Checking that the algorithm worked properly.
     if (!aPartitionAlgo.isDone()) {
@@ -123,16 +136,85 @@ void FeaturesPlugin_Partition::execute()
 
     if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
       std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
-
-      aResultBody->store(aPartitionAlgo.shape());
-
-//      LoadNamingDS(aResultBody, anObject, aTools, aPartitionAlgo);
-
+      aMakeShapeList.append(aPartitionAlgo.makeShape());
+      GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
+      loadNamingDS(aResultBody, anObjects.front(), aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
       setResult(aResultBody, aResultIndex);
       aResultIndex++;
     }
+  } else {
+    // Create result for each object.
+    for (ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
+      std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
+      ListOfShape aListWithObject; aListWithObject.push_back(anObject);
+      GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools);
+
+      // Checking that the algorithm worked properly.
+      if (!aPartitionAlgo.isDone()) {
+        static const std::string aFeatureError = "Partition algorithm failed";
+        setError(aFeatureError);
+        return;
+      }
+      if (aPartitionAlgo.shape()->isNull()) {
+        static const std::string aShapeError = "Resulting shape is Null";
+        setError(aShapeError);
+        return;
+      }
+      if (!aPartitionAlgo.isValid()) {
+        std::string aFeatureError = "Warning: resulting shape is not valid";
+        setError(aFeatureError);
+        return;
+      }
+
+      if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) {
+        std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
+        aMakeShapeList.append(aPartitionAlgo.makeShape());
+        GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get();
+        loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, aMapOfShapes);
+        setResult(aResultBody, aResultIndex);
+        aResultIndex++;
+      }
+    }
   }
 
   // remove the rest results if there were produced in the previous pass
   removeResults(aResultIndex);
 }
+
+//=================================================================================================
+void FeaturesPlugin_Partition::loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
+                                            const std::shared_ptr<GeomAPI_Shape> theBaseShape,
+                                            const ListOfShape& theTools,
+                                            const std::shared_ptr<GeomAPI_Shape> theResultShape,
+                                            GeomAlgoAPI_MakeShape& theMakeShape,
+                                            GeomAPI_DataMapOfShapeShape& theMapOfShapes)
+{
+  //load result
+  if(theBaseShape->isEqual(theResultShape)) {
+    theResultBody->store(theResultShape);
+  } else {
+    const int aDeletedTag = 1;
+    const int aSubsolidsTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
+    const int aModifyTag = 100000;
+    int aModifyToolsTag = 200000;
+    std::ostringstream aStream;
+
+    theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
+
+    std::string aModName = "Modified";
+    theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
+                                               aModifyTag, aModName, theMapOfShapes, true);
+    theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDeletedTag);
+
+    int anIndex = 1;
+    for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
+      aStream.str(std::string());
+      aStream.clear();
+      aStream << aModName << "_" << anIndex++;
+      theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
+                                                 aModifyToolsTag, aStream.str(), theMapOfShapes, true);
+      theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
+      aModifyToolsTag += 10000;
+    }
+  }
+}