Salome HOME
Initial implementation of support of any level of hierarchy in Result Bodies.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Union.cpp
index a7c62749a0812102675e613f49aa07482a0ccfee..33bd7450b473240a9628247f6af01064b41909b8 100644 (file)
@@ -1,19 +1,35 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        FeaturesPlugin_Union.cpp
-// Created:     17 June 2016
-// Author:      Dmitry Bobylev
+// Copyright (C) 2014-2017  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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "FeaturesPlugin_Union.h"
 
 #include <GeomAlgoAPI_Boolean.h>
 #include <GeomAlgoAPI_MakeShapeList.h>
 #include <GeomAlgoAPI_PaveFiller.h>
+#include <GeomAlgoAPI_UnifySameDomain.h>
 
 #include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
 
 #include <ModelAPI_AttributeSelectionList.h>
-#include <ModelAPI_ResultCompSolid.h>
+#include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
 
 //=================================================================================================
@@ -34,7 +50,7 @@ void FeaturesPlugin_Union::execute()
   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
 
   // Getting objects.
-  AttributeSelectionListPtr anObjectsSelList = 
+  AttributeSelectionListPtr anObjectsSelList =
     selectionList(FeaturesPlugin_Union::BASE_OBJECTS_ID());
   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
@@ -43,10 +59,10 @@ void FeaturesPlugin_Union::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();
-      std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator 
+      std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
         anIt = aCompSolidsObjects.begin();
       for(; anIt != aCompSolidsObjects.end(); anIt++) {
         if(anIt->first->isEqual(aContextShape)) {
@@ -65,7 +81,7 @@ void FeaturesPlugin_Union::execute()
   // Collecting solids from compsolids which will not be modified in
   // boolean operation and will be added to result.
   ListOfShape aShapesToAdd;
-  for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator 
+  for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
     anIt = aCompSolidsObjects.begin();
     anIt != aCompSolidsObjects.end(); anIt++) {
     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
@@ -73,7 +89,7 @@ void FeaturesPlugin_Union::execute()
     anObjects.insert(anObjects.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end());
 
     // Collect solids from compsolid which will not be modified in boolean operation.
-    for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
+    for(GeomAPI_ShapeIterator anExp(aCompSolid); anExp.more(); anExp.next()) {
       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
       for(; anIt != aUsedInOperationSolids.end(); anIt++) {
@@ -93,31 +109,36 @@ void FeaturesPlugin_Union::execute()
   }
 
   // Fuse objects.
+  std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo;
   ListOfShape aTools;
-  aTools.splice(aTools.begin(), anObjects, anObjects.begin());
-  std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
-                                                            aTools,
-                                                            GeomAlgoAPI_Boolean::BOOL_FUSE));
+  if (anObjects.front()->shapeType() == GeomAPI_Shape::SOLID) {
+    aTools.splice(aTools.begin(), anObjects, anObjects.begin());
+    anAlgo.reset(new GeomAlgoAPI_Boolean(anObjects,
+                 aTools,
+                 GeomAlgoAPI_Boolean::BOOL_FUSE));
+  } else {
+    anAlgo.reset(new GeomAlgoAPI_UnifySameDomain(anObjects));
+  }
 
   // Checking that the algorithm worked properly.
   GeomAlgoAPI_MakeShapeList aMakeShapeList;
   GeomAPI_DataMapOfShapeShape aMapOfShapes;
-  if(!aFuseAlgo->isDone()) {
+  if(!anAlgo->isDone()) {
     setError("Error: Boolean algorithm failed.");
     return;
   }
-  if(aFuseAlgo->shape()->isNull()) {
+  if(anAlgo->shape()->isNull()) {
     setError("Error: Resulting shape is Null.");
     return;
   }
-  if(!aFuseAlgo->isValid()) {
+  if(!anAlgo->isValid()) {
     setError("Error: Resulting shape is not valid.");
     return;
   }
 
-  GeomShapePtr aShape = aFuseAlgo->shape();
-  aMakeShapeList.appendAlgo(aFuseAlgo);
-  aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
+  GeomShapePtr aShape = anAlgo->shape();
+  aMakeShapeList.appendAlgo(anAlgo);
+  aMapOfShapes.merge(anAlgo->mapOfSubShapes());
 
   // Store original shapes for naming.
   anObjects.splice(anObjects.begin(), aTools);
@@ -145,21 +166,28 @@ void FeaturesPlugin_Union::execute()
     aMakeShapeList.appendAlgo(aFillerAlgo);
     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
   }
+  // workaround: make copy to name edges correctly
 
   // Store result and naming.
-  const int aModifyTag = 1;
-  const int aDeletedTag = 2;
-  /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids
-  const int aSubsolidsTag = 3; 
-  const std::string aModName = "Modified";
+  const int aModifyEdgeTag = 1;
+  const int aModifyFaceTag = 2;
+  const int aDeletedTag = 3;
+  /// sub solids will be placed at labels 4, 5 etc. if result is compound of solids
+  const int aSubsolidsTag = 4;
+  const std::string aModEName = "Modified_Edge";
+  const std::string aModFName = "Modified_Face";
 
   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
   aResultBody->storeModified(anObjects.front(), aShape, aSubsolidsTag);
 
   for(ListOfShape::const_iterator anIter = anObjects.begin(); anIter != anObjects.end(); ++anIter) {
+    aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::EDGE,
+                                             aModifyEdgeTag, aModEName, aMapOfShapes,
+                                             true, false, true);
     aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE,
-                                             aModifyTag, aModName, aMapOfShapes);
-    aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
+                                             aModifyFaceTag, aModFName, aMapOfShapes,
+                                             true, false, true);
+    //aResultBody->loadDeletedShapes(&aMakeShapeList, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
   }
 
   setResult(aResultBody);