Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Partition.cpp
index ec16dd41e37acba5405c9690906c7aaa7a8fd210..9268f95d7f1ebe055df9948b85165ac4af1468de 100755 (executable)
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 
+#include <GeomAPI_Face.h>
+#include <GeomAPI_ShapeExplorer.h>
 #include <GeomAPI_ShapeIterator.h>
 
+#include <iostream>
 #include <sstream>
 
+static GeomShapePtr findBase(const GeomShapePtr theObjectShape,
+                             const GeomShapePtr theResultShape,
+                             const GeomAPI_Shape::ShapeType theShapeType,
+                             const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape);
+
 //=================================================================================================
 FeaturesPlugin_Partition::FeaturesPlugin_Partition()
 {
@@ -63,7 +71,8 @@ void FeaturesPlugin_Partition::execute()
     return;
   }
 
-  std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
+  std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
+    GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0);
 
   // Resize planes.
   ListOfShape aTools;
@@ -78,7 +87,8 @@ void FeaturesPlugin_Partition::execute()
   }
 
   // Create single result.
-  std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools));
+  std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
+    new GeomAlgoAPI_Partition(anObjects, aTools));
 
   // Checking that the algorithm worked properly.
   if (!aPartitionAlgo->isDone()) {
@@ -100,14 +110,13 @@ void FeaturesPlugin_Partition::execute()
   GeomShapePtr aResultShape = aPartitionAlgo->shape();
 
   int aResultIndex = 0;
-  anObjects.insert(anObjects.end(), aPlanes.begin(), aPlanes.end());
   if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
     for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
-      storeResult(anObjects, anIt.current(), aMakeShapeList, aResultIndex);
+      storeResult(anObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
       ++aResultIndex;
     }
   } else {
-    storeResult(anObjects, aResultShape, aMakeShapeList, aResultIndex);
+    storeResult(anObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
     ++aResultIndex;
   }
 
@@ -116,26 +125,29 @@ void FeaturesPlugin_Partition::execute()
 }
 
 //=================================================================================================
-void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects,
-                                           const GeomShapePtr theResultShape,
-                                           const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
-                                           const int theIndex)
+void FeaturesPlugin_Partition::storeResult(
+  ListOfShape& theObjects, ListOfShape& thePlanes,
+  const GeomShapePtr theResultShape,
+  const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
+  const int theIndex)
 {
-  // Find base.
+  // Find base. The most complicated is the real modified object (#1799 if box is partitioned by 
+  // two planes the box is the base, not planes, independently on the order in the list).
   GeomShapePtr aBaseShape;
   for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
     GeomShapePtr anObjectShape = *anIt;
-    ListOfShape aModifiedShapes;
-    theMakeShape->modified(anObjectShape, aModifiedShapes);
-    for(ListOfShape::const_iterator aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
-      GeomShapePtr aModShape = *aModIt;
-      if(theResultShape->isSubShape(aModShape)) {
-        aBaseShape = anObjectShape;
-        break;
-      }
+    GeomShapePtr aCandidate =
+      findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape);
+    if(!aCandidate.get()) {
+      aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape);
     }
-    if(aBaseShape.get()) {
-      break;
+    if (!aCandidate.get())
+      aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape);
+
+    if(aCandidate.get()) {
+      if (!aBaseShape.get() || aBaseShape->shapeType() > aCandidate->shapeType()) {
+        aBaseShape = aCandidate;
+      }
     }
   }
 
@@ -143,31 +155,70 @@ void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects,
   ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
 
   // Store modified shape.
-  if(aBaseShape->isEqual(theResultShape)) {
+  if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) {
     aResultBody->store(theResultShape);
+    setResult(aResultBody, theIndex);
     return;
   }
 
   const int aDelTag = 1;
-  const int aSubTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
+  /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
+  const int aSubTag = 2; 
   int aModTag = aSubTag + 10000;
   const std::string aModName = "Modified";
 
   aResultBody->storeModified(aBaseShape, theResultShape, aSubTag);
 
   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
+  theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
   int anIndex = 1;
   for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) {
-    std::ostringstream aStream;
-    aStream << aModName << "_" << anIndex++;
-    aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::EDGE,
-                                             aModTag, aStream.str(), *aMapOfSubShapes.get(), true);
-    aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE,
-                                             aModTag, aStream.str(), *aMapOfSubShapes.get(), true);
-    aResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::EDGE, aDelTag);
-    aResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);
+    GeomShapePtr aShape = *anIt;
+    std::string aModEdgeName = aModName + "_Edge_" + std::to_string((long long)anIndex);
+    std::string aModFaceName = aModName + "_Face_" + std::to_string((long long)anIndex++);
+    aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE,
+                                             aModTag, aModEdgeName, *aMapOfSubShapes.get(), true);
     aModTag += 10000;
+    aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE,
+                                             aModTag, aModFaceName, *aMapOfSubShapes.get(), true);
+    aModTag += 10000;
+    aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE, aDelTag);
+    aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, aDelTag);
   }
 
   setResult(aResultBody, theIndex);
 }
+
+
+//=================================================================================================
+GeomShapePtr findBase(const GeomShapePtr theObjectShape,
+                      const GeomShapePtr theResultShape,
+                      const GeomAPI_Shape::ShapeType theShapeType,
+                      const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
+{
+  GeomShapePtr aBaseShape;
+  std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
+  for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType);
+      anObjectSubShapesExp.more();
+      anObjectSubShapesExp.next()) {
+    GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current();
+    ListOfShape aModifiedShapes;
+    theMakeShape->modified(anObjectSubShape, aModifiedShapes);
+    for(ListOfShape::const_iterator 
+        aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) {
+      GeomShapePtr aModShape = *aModIt;
+      if(aMapOfSubShapes->isBound(aModShape)) {
+        aModShape = aMapOfSubShapes->find(aModShape);
+      }
+      if(theResultShape->isSubShape(aModShape)) {
+        aBaseShape = theObjectShape;
+        break;
+      }
+    }
+    if(aBaseShape.get()) {
+      break;
+    }
+  }
+
+  return aBaseShape;
+}