-// 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_BooleanFuse.h"
-#include <ModelAPI_ResultCompSolid.h>
+#include "FeaturesPlugin_Tools.h"
+
+#include <ModelAPI_ResultBody.h>
#include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeInteger.h>
#include <ModelAPI_AttributeSelectionList.h>
#include <ModelAPI_AttributeString.h>
#include <ModelAPI_Session.h>
#include <GeomAlgoAPI_Boolean.h>
#include <GeomAlgoAPI_MakeShapeList.h>
#include <GeomAlgoAPI_PaveFiller.h>
+#include <GeomAlgoAPI_ShapeBuilder.h>
#include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAlgoAPI_Tools.h>
#include <GeomAlgoAPI_UnifySameDomain.h>
+
#include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
+
+static const int THE_FUSE_VERSION_1 = 20190506;
//==================================================================================================
FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
+
+ AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
+ aVerAttr->setIsArgument(false);
+ ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
+ if (!integer(VERSION_ID())->isInitialized() &&
+ !selectionList(OBJECT_LIST_ID())->isInitialized() &&
+ !selectionList(TOOL_LIST_ID())->isInitialized()) {
+ // this is a newly created feature (not read from file),
+ // so, initialize the latest version
+ integer(VERSION_ID())->setValue(THE_FUSE_VERSION_1);
+ }
}
//==================================================================================================
void FeaturesPlugin_BooleanFuse::execute()
{
- ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
+ std::string anError;
+ ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
+ ListOfShape aPlanes;
bool isSimpleCreation = false;
AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
if (aCreationMethodAttr.get()
- && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
+ && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
+ {
isSimpleCreation = true;
}
// Getting objects.
- AttributeSelectionListPtr anObjectsSelList =
- selectionList(FeaturesPlugin_Boolean::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();
- ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
- if (!isSimpleCreation
- && aResCompSolidPtr.get()
- && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
- std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
- std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
- anIt = aCompSolidsObjects.begin();
- for (; anIt != aCompSolidsObjects.end(); anIt++) {
- if (anIt->first->isEqual(aContextShape)) {
- aCompSolidsObjects[anIt->first].push_back(anObject);
- break;
- }
- }
- if (anIt == aCompSolidsObjects.end()) {
- aCompSolidsObjects[aContextShape].push_back(anObject);
- }
- } else {
- if (anObject->shapeType() == GeomAPI_Shape::EDGE
- || anObject->shapeType() == GeomAPI_Shape::FACE) {
- anEdgesAndFaces.push_back(anObject);
- } else {
- anObjects.push_back(anObject);
- }
- }
- }
+ if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
+ return;
// Getting tools.
- if (!isSimpleCreation) {
- AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
- for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
- AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
- GeomShapePtr aTool = aToolAttr->value();
- if (!aTool.get()) {
- // It could be a construction plane.
- ResultPtr aContext = aToolAttr->context();
- aPlanes.push_back(aToolAttr->context()->shape());
- } else if (aTool->shapeType() == GeomAPI_Shape::EDGE
- || aTool->shapeType() == GeomAPI_Shape::FACE) {
- anEdgesAndFaces.push_back(aTool);
- } else {
- aTools.push_back(aTool);
- }
- }
- }
+ if (!isSimpleCreation &&
+ !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
+ return;
+
+ ListOfShape anObjects, aTools, anEdgesAndFaces;
+ // all objects except edges and faces
+ anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
+ GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
+ aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
+ GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
- if ((anObjects.size() + aTools.size() +
- aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
+ if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
std::string aFeatureError = "Error: Not enough objects for boolean operation.";
setError(aFeatureError);
return;
}
+ // version of FUSE feature
+ AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
+ int aFuseVersion = 0;
+ if (aVersionAttr && aVersionAttr->isInitialized())
+ aFuseVersion = aVersionAttr->value();
+
+//// isSimpleCreation = isSimpleCreation && aFuseVersion < THE_FUSE_VERSION_1;
+
// Collecting all solids which will be fused.
ListOfShape aSolidsToFuse;
aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
// 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
- anIt = aCompSolidsObjects.begin();
- anIt != aCompSolidsObjects.end(); anIt++) {
- std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
- ListOfShape& aUsedInOperationSolids = anIt->second;
- aSolidsToFuse.insert(aSolidsToFuse.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()) {
- std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
- ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
- for (; anIt != aUsedInOperationSolids.end(); anIt++) {
- if (aSolidInCompSolid->isEqual(*anIt)) {
- break;
- }
- }
- if (anIt == aUsedInOperationSolids.end()) {
- aShapesToAdd.push_back(aSolidInCompSolid);
- }
+ for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
+ !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
+ ++anObjectsIt) {
+ GeomShapePtr anObject = *anObjectsIt;
+ GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
+
+ if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
+ // mark all subs of this parent as precessed to avoid handling twice
+ aParent = anObjectsHierarchy.Parent(anObject);
+
+ ListOfShape aUsed, aNotUsed;
+ anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
+ aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
}
}
anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
// Cut edges and faces(if we have any) with solids.
- GeomAlgoAPI_MakeShapeList aMakeShapeList;
- GeomAPI_DataMapOfShapeShape aMapOfShapes;
- std::shared_ptr<GeomAPI_Shape> aCuttedEdgesAndFaces;
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
+ GeomShapePtr aCuttedEdgesAndFaces;
if (!anEdgesAndFaces.empty()) {
std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
- anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
+ anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
if (aCutAlgo->isDone()) {
aCuttedEdgesAndFaces = aCutAlgo->shape();
- aMakeShapeList.appendAlgo(aCutAlgo);
- aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
+ aMakeShapeList->appendAlgo(aCutAlgo);
}
}
anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
ListOfShape aOneObjectList;
aOneObjectList.push_back(*anIt);
std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
- new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
+ new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
aSolidsToFuse.push_back(aCutAlgo->shape());
- aMakeShapeList.appendAlgo(aCutAlgo);
- aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
+ aMakeShapeList->appendAlgo(aCutAlgo);
}
}
}
}
// Fuse all objects and all tools.
- std::shared_ptr<GeomAPI_Shape> aShape;
+ GeomShapePtr aShape;
if (anObjects.size() == 1 && aTools.empty()) {
aShape = anObjects.front();
} else if (anObjects.empty() && aTools.size() == 1) {
} else if ((anObjects.size() + aTools.size()) > 1) {
std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
aTools,
- GeomAlgoAPI_Boolean::BOOL_FUSE));
+ GeomAlgoAPI_Tools::BOOL_FUSE));
// Checking that the algorithm worked properly.
- if (!aFuseAlgo->isDone()) {
- static const std::string aFeatureError = "Error: Boolean algorithm failed.";
- setError(aFeatureError);
- return;
- }
- if (aFuseAlgo->shape()->isNull()) {
- static const std::string aShapeError = "Error: Resulting shape is Null.";
- setError(aShapeError);
- return;
- }
- if (!aFuseAlgo->isValid()) {
- std::string aFeatureError = "Error: Resulting shape is not valid.";
- setError(aFeatureError);
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
+ setError(anError);
return;
}
aShape = aFuseAlgo->shape();
- aMakeShapeList.appendAlgo(aFuseAlgo);
- aMapOfShapes.merge(aFuseAlgo->mapOfSubShapes());
+ aMakeShapeList->appendAlgo(aFuseAlgo);
}
// Combine result with not used solids from compsolid and edges and faces (if we have any).
}
std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
- if (!aFillerAlgo->isDone()) {
- std::string aFeatureError = "Error: PaveFiller algorithm failed.";
- setError(aFeatureError);
- return;
- }
- if (aFillerAlgo->shape()->isNull()) {
- static const std::string aShapeError = "Error: Resulting shape is Null.";
- setError(aShapeError);
- return;
- }
- if (!aFillerAlgo->isValid()) {
- std::string aFeatureError = "Error: Resulting shape is not valid.";
- setError(aFeatureError);
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
+ setError(anError);
return;
}
aShape = aFillerAlgo->shape();
- aMakeShapeList.appendAlgo(aFillerAlgo);
- aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
+ aMakeShapeList->appendAlgo(aFillerAlgo);
}
bool isRemoveEdges = false;
std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
new GeomAlgoAPI_UnifySameDomain(aShape));
- if (!aUnifyAlgo->isDone()) {
- std::string aFeatureError = "Error: PaveFiller algorithm failed.";
- setError(aFeatureError);
- return;
- }
- if (aUnifyAlgo->shape()->isNull()) {
- static const std::string aShapeError = "Error: Resulting shape is Null.";
- setError(aShapeError);
- return;
- }
- if (!aUnifyAlgo->isValid()) {
- std::string aFeatureError = "Error: Resulting shape is not valid.";
- setError(aFeatureError);
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
+ setError(anError);
return;
}
aShape = aUnifyAlgo->shape();
- aMakeShapeList.appendAlgo(aUnifyAlgo);
- aMapOfShapes.merge(aUnifyAlgo->mapOfSubShapes());
+ aMakeShapeList->appendAlgo(aUnifyAlgo);
+ }
+
+ if (aFuseVersion == THE_FUSE_VERSION_1) {
+ // merge hierarchies of compounds containing objects and tools
+ // and append the result of the FUSE operation
+ aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
}
int aResultIndex = 0;
- std::shared_ptr<GeomAPI_Shape> aBackShape = anOriginalShapes.back();
- anOriginalShapes.pop_back();
- std::shared_ptr<ModelAPI_ResultBody> aResultBody =
- document()->createBody(data(), aResultIndex);
- loadNamingDS(aResultBody, aBackShape, anOriginalShapes,
- aShape, aMakeShapeList, aMapOfShapes);
+ ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
+
+ ListOfShape anEmptyTools;
+ FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
+ anOriginalShapes,
+ anEmptyTools,
+ aMakeShapeList,
+ aShape);
setResult(aResultBody, aResultIndex);
aResultIndex++;
+ FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
+ GeomShapePtr(),
+ anOriginalShapes,
+ aMakeShapeList,
+ aShape);
+
// remove the rest results if there were produced in the previous pass
removeResults(aResultIndex);
}
//==================================================================================================
-void FeaturesPlugin_BooleanFuse::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)
+GeomShapePtr FeaturesPlugin_BooleanFuse::keepUnusedSubsOfCompound(
+ const GeomShapePtr& theFuseResult,
+ const ObjectHierarchy& theObjectsHierarchy,
+ const ObjectHierarchy& theToolsHierarchy,
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
{
- //load result
- if (theBaseShape->isEqual(theResultShape)) {
- theResultBody->store(theResultShape, false);
- } else {
- const int aModifyTag = 1;
- const int aModifyEdgeTag = 2;
- const int aModifyFaceTag = 3;
- const int aDeletedTag = 4;
- /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
- const int aSubsolidsTag = 5;
-
- theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
-
- const std::string aModName = "Modified";
- const std::string aModEName = "Modified_Edge";
- const std::string aModFName = "Modified_Face";
-
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
- aModifyEdgeTag, aModEName, theMapOfShapes, false,
- false, true);
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
- aModifyFaceTag, aModFName, theMapOfShapes, false,
- false, true);
- theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
- GeomAPI_Shape::FACE, aDeletedTag);
-
- int aTag;
- std::string aName;
- for (ListOfShape::const_iterator
- anIter = theTools.begin(); anIter != theTools.end(); anIter++) {
- if ((*anIter)->shapeType() <= GeomAPI_Shape::FACE) {
- aTag = aModifyFaceTag;
- aName = aModFName;
- } else {
- aTag = aModifyEdgeTag;
- aName = aModEName;
- }
- theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter,
- aName == aModEName ? GeomAPI_Shape::EDGE
- : GeomAPI_Shape::FACE,
- aTag, aName, theMapOfShapes, false, false, true);
- theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
+ ListOfShape aCompounds;
+ theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
+ theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
+
+ GeomShapePtr aResultShape = theFuseResult;
+ if (!aCompounds.empty()) {
+ aResultShape = aCompounds.front();
+ aCompounds.pop_front();
+
+ std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
+ for (ListOfShape::iterator anIt = aCompounds.begin(); anIt != aCompounds.end(); ++anIt) {
+ for (GeomAPI_ShapeIterator aSub(*anIt); aSub.more(); aSub.next())
+ aBuilder->add(aResultShape, aSub.current());
}
+
+ aBuilder->add(aResultShape, theFuseResult);
+
+ theMakeShapeList->appendAlgo(aBuilder);
}
+ return aResultShape;
}