-// 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 "FeaturesPlugin_Tools.h"
+
#include <ModelAPI_ResultBody.h>
#include <ModelAPI_AttributeSelectionList.h>
#include <ModelAPI_Tools.h>
#include <GeomAlgoAPI_MakeShapeList.h>
#include <GeomAlgoAPI_PaveFiller.h>
#include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAlgoAPI_Tools.h>
+
#include <GeomAPI_ShapeExplorer.h>
#include <GeomAPI_ShapeIterator.h>
-//==================================================================================================
-const int ModifyVTag = 1;
-const int ModifyETag = 2;
-const int ModifyFTag = 3;
-const int DeletedTag = 4;
-/// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
-const int SubsolidsTag = 5;
-
-
//==================================================================================================
FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
: FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
}
//==================================================================================================
-void FeaturesPlugin_BooleanCut::execute()
+void FeaturesPlugin_BooleanCut::initAttributes()
{
- ListOfShape anObjects, aTools;
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
-
- // Getting objects.
- AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
- for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
- AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
- std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
- if(!anObject.get()) {
- return;
- }
- ResultPtr aContext = anObjectAttr->context();
- ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
- if (aResCompSolidPtr.get())
- {
- std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
- GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
- aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
-
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
- anIt = aMap.begin();
- for (; anIt != aMap.end(); anIt++) {
- if (anIt->first->isEqual(aContextShape)) {
- aMap[anIt->first].push_back(anObject);
- break;
- }
- }
- if (anIt == aMap.end()) {
- aMap[aContextShape].push_back(anObject);
- }
+ FeaturesPlugin_Boolean::initAttributes();
+ initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
+}
- } else {
- anObjects.push_back(anObject);
- }
- }
+//==================================================================================================
+void FeaturesPlugin_BooleanCut::execute()
+{
+ GeomAPI_ShapeHierarchy anObjects, aTools;
+ ListOfShape aPlanes;
- // Getting tools.
- AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
- for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
- AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
- GeomShapePtr aTool = aToolAttr->value();
- if(!aTool.get()) {
- return;
- }
- aTools.push_back(aTool);
- }
+ // Getting objects and tools
+ if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes) ||
+ !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
+ return;
int aResultIndex = 0;
- if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
- || aTools.empty()) {
+ if(anObjects.empty() || aTools.empty()) {
std::string aFeatureError = "Error: Not enough objects for boolean operation.";
setError(aFeatureError);
return;
}
- std::vector<ResultBaseAlgo> aResultBaseAlgoList;
+ 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;
- std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
- std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
- new GeomAlgoAPI_Boolean(anObject,
- aTools,
- GeomAlgoAPI_Boolean::BOOL_CUT));
- 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);
- return;
- }
-
- aMakeShapeList->appendAlgo(aCutAlgo);
-
- GeomAPI_ShapeIterator aShapeIt(aResShape);
- if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
- {
- std::shared_ptr<ModelAPI_ResultBody> aResultBody =
- document()->createBody(data(), aResultIndex);
-
- loadNamingDS(aResultBody, anObject, aTools, aResShape,
- *aMakeShapeList, *(aCutAlgo->mapOfSubShapes()),
- false);
- setResult(aResultBody, aResultIndex);
- aResultIndex++;
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
- ResultBaseAlgo aRBA;
- aRBA.resultBody = aResultBody;
- aRBA.baseShape = anObject;
- aRBA.makeShape = aMakeShapeList;
- aResultBaseAlgoList.push_back(aRBA);
- aResultShapesList.push_back(aResShape);
- }
+ GeomShapePtr aResultCompound;
+ if (data()->version() == BOP_VERSION_9_4()) {
+ // merge hierarchies of compounds containing objects and tools
+ aResultCompound =
+ keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
}
- // Compsolids handling
- 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;
- ListOfShape& aUsedInOperationSolids = anIt->second;
-
- // Collecting solids from compsolids which will not be modified in boolean operation.
- ListOfShape aNotUsedSolids;
- for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
- anExp.more();
- anExp.next())
- {
- std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
- ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
- for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
- if (aSolidInCompSolid->isEqual(*aUsedIt)) {
- break;
- }
+ // For solids cut each object with all tools.
+ bool isOk = true;
+ for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
+ anObjectsIt != anObjects.end() && isOk;
+ ++anObjectsIt) {
+ GeomShapePtr anObject = *anObjectsIt;
+ GeomShapePtr aParent = anObjects.parent(anObject);
+
+ if (aParent) {
+ GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
+ if (aShapeType == GeomAPI_Shape::COMPOUND) {
+ // Compound handling
+ isOk = processCompound(GeomAlgoAPI_Tools::BOOL_CUT,
+ anObjects, aParent, aTools.objects(),
+ aResultIndex, aResultBaseAlgoList, aResultShapesList,
+ aResultCompound);
}
- if (aUsedIt == aUsedInOperationSolids.end()) {
- aNotUsedSolids.push_back(aSolidInCompSolid);
+ else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
+ // Compsolid handling
+ isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_CUT,
+ anObjects, aParent, aTools.objects(), ListOfShape(),
+ aResultIndex, aResultBaseAlgoList, aResultShapesList,
+ aResultCompound);
}
- }
-
- 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);
- return;
- }
-
- aMakeShapeList->appendAlgo(aCutAlgo);
- GeomAPI_DataMapOfShapeShape aMapOfShapes;
- aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
- GeomShapePtr aResultShape = aCutAlgo->shape();
-
- // Add result to not used solids from compsolid.
- if(!aNotUsedSolids.empty()) {
- ListOfShape aShapesToAdd = aNotUsedSolids;
- aShapesToAdd.push_back(aCutAlgo->shape());
- std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
- new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
- if(!aFillerAlgo->isDone()) {
- std::string aFeatureError = "Error: PaveFiller algorithm failed.";
- setError(aFeatureError);
- return;
- }
-
- aMakeShapeList->appendAlgo(aFillerAlgo);
- aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
- aResultShape = aFillerAlgo->shape();
- }
-
- GeomAPI_ShapeIterator aShapeIt(aResultShape);
- if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
- {
- std::shared_ptr<ModelAPI_ResultBody> aResultBody =
- document()->createBody(data(), aResultIndex);
-
- loadNamingDS(aResultBody,
- aCompSolid,
- aTools,
- aResultShape,
- *aMakeShapeList,
- aMapOfShapes,
- false);
- setResult(aResultBody, aResultIndex);
- aResultIndex++;
-
- ResultBaseAlgo aRBA;
- aRBA.resultBody = aResultBody;
- aRBA.baseShape = aCompSolid;
- aRBA.makeShape = aMakeShapeList;
- aResultBaseAlgoList.push_back(aRBA);
- aResultShapesList.push_back(aResultShape);
+ } else {
+ // process object as is
+ isOk = processObject(GeomAlgoAPI_Tools::BOOL_CUT,
+ anObject, aTools.objects(), aPlanes,
+ aResultIndex, aResultBaseAlgoList, aResultShapesList,
+ aResultCompound);
}
}
- // Compounds handling
- for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
- anIt = aCompoundObjects.begin();
- anIt != aCompoundObjects.end();
- ++anIt)
- {
- std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
- ListOfShape& aUsedInOperationShapes = anIt->second;
-
- // Collecting shapes from compound which will not be modified in boolean operation.
- ListOfShape aNotUsedShapes;
- for (GeomAPI_ShapeIterator aCompIt(aCompound);
- aCompIt.more();
- aCompIt.next())
- {
- std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
- ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
- for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
- if (aShapeInCompound->isEqual(*aUsedIt)) {
- break;
- }
- }
- if (aUsedIt == aUsedInOperationShapes.end()) {
- aNotUsedShapes.push_back(aShapeInCompound);
- }
- }
-
- 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);
- return;
- }
-
- aMakeShapeList->appendAlgo(aCutAlgo);
- GeomAPI_DataMapOfShapeShape aMapOfShapes;
- aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
- GeomShapePtr aResultShape = aCutAlgo->shape();
-
- // Add result to not used shape from compound.
- if (!aNotUsedShapes.empty()) {
- ListOfShape aShapesForResult = aNotUsedShapes;
- if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
- for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
- aShapesForResult.push_back(aResultIt.current());
- }
- } else {
- aShapesForResult.push_back(aResultShape);
- }
-
- if (aShapesForResult.size() == 1) {
- aResultShape = aShapesForResult.front();
- } else {
- aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
- }
- }
-
- GeomAPI_ShapeIterator aShapeIt(aResultShape);
- if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
- std::shared_ptr<ModelAPI_ResultBody> aResultBody =
- document()->createBody(data(), aResultIndex);
-
- loadNamingDS(aResultBody,
- aCompound,
- aTools,
- aResultShape,
- *aMakeShapeList,
- aMapOfShapes,
- false);
- setResult(aResultBody, aResultIndex);
- aResultIndex++;
-
- ResultBaseAlgo aRBA;
- aRBA.resultBody = aResultBody;
- aRBA.baseShape = aCompound;
- aRBA.makeShape = aMakeShapeList;
- aResultBaseAlgoList.push_back(aRBA);
- aResultShapesList.push_back(aResultShape);
- }
- }
+ storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
+ aMakeShapeList, aResultBaseAlgoList);
// 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);
- storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
+ if (!aResultCompound)
+ aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
+ FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
+ aTools.objects(),
+ aResultCompound);
// 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 {
- theResultBody->storeModified(theBaseShape, theResultShape, SubsolidsTag);
-
- 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,
- ModifyVTag, aModVName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
- ModifyETag, aModEName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
- ModifyFTag, aModFName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
-
- for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
- {
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
- ModifyVTag, aModVName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
-
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
- ModifyETag, aModEName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
-
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
- ModifyFTag, aModFName, theMapOfShapes, false,
- theIsStoreAsGenerated, true);
- }
- }
-}
-
-//==================================================================================================
-void FeaturesPlugin_BooleanCut::storeDeletedShapes(
- std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
- const ListOfShape& theTools,
- const GeomShapePtr theResultShapesCompound)
-{
- for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
- anIt != theResultBaseAlgoList.end();
- ++anIt)
- {
- ResultBaseAlgo& aRCA = *anIt;
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- aRCA.baseShape,
- GeomAPI_Shape::VERTEX,
- DeletedTag,
- theResultShapesCompound);
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- aRCA.baseShape,
- GeomAPI_Shape::EDGE,
- DeletedTag,
- theResultShapesCompound);
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- aRCA.baseShape,
- GeomAPI_Shape::FACE,
- DeletedTag,
- theResultShapesCompound);
-
- for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
- {
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- *anIter,
- GeomAPI_Shape::VERTEX,
- DeletedTag,
- theResultShapesCompound);
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- *anIter,
- GeomAPI_Shape::EDGE,
- DeletedTag,
- theResultShapesCompound);
- aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
- *anIter,
- GeomAPI_Shape::FACE,
- DeletedTag,
- theResultShapesCompound);
- }
- }
-}