-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File: FeaturesPlugin_Partition.cpp
-// Created: 31 Jul 2015
-// Author: Natalia ERMOLAEVA
+// Copyright (C) 2014-2022 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
+//
#include "FeaturesPlugin_Partition.h"
-#include <ModelAPI_Data.h>
-#include <ModelAPI_Document.h>
#include <ModelAPI_AttributeBoolean.h>
-#include <ModelAPI_AttributeReference.h>
#include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_AttributeSelectionList.h>
#include <ModelAPI_BodyBuilder.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_Document.h>
#include <ModelAPI_ResultBody.h>
-#include <ModelAPI_AttributeSelectionList.h>
#include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
#include <ModelAPI_Validator.h>
+#include <GeomAlgoAPI_Boolean.h>
#include <GeomAlgoAPI_CompoundBuilder.h>
-#include <GeomAlgoAPI_Partition.h>
#include <GeomAlgoAPI_MakeShapeCustom.h>
#include <GeomAlgoAPI_MakeShapeList.h>
+#include <GeomAlgoAPI_Partition.h>
+#include <GeomAlgoAPI_ShapeBuilder.h>
#include <GeomAlgoAPI_ShapeTools.h>
+#include <GeomAlgoAPI_Tools.h>
+
+#include <GeomAPI_Face.h>
+#include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_ShapeIterator.h>
+#include <iostream>
+#include <list>
#include <sstream>
+
//=================================================================================================
FeaturesPlugin_Partition::FeaturesPlugin_Partition()
{
//=================================================================================================
void FeaturesPlugin_Partition::initAttributes()
{
- AttributeSelectionListPtr aSelection =
- std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
- FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
- aSelection->setSelectionType("SOLID");
-
- aSelection =
- std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
- FeaturesPlugin_Partition::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
- aSelection->setSelectionType("SOLID");
-
- ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
-
- data()->addAttribute(COMBINE_ID(), ModelAPI_AttributeBoolean::typeId());
+ data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
+ initVersion(BOP_VERSION_9_4(), selectionList(BASE_OBJECTS_ID()));
}
//=================================================================================================
void FeaturesPlugin_Partition::execute()
{
- ListOfShape anObjects, aTools, aToolsForNaming;
+ GeomAPI_ShapeHierarchy anObjects;
+ ListOfShape aPlanes;
// Getting objects.
- AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Partition::OBJECT_LIST_ID());
- for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
- std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
- std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
- if (!anObject.get()) {
- return;
- }
- anObjects.push_back(anObject);
+ processAttribute(BASE_OBJECTS_ID(), anObjects, aPlanes);
+ if(anObjects.empty()) {
+ static const std::string aFeatureError = "Error: No objects for partition.";
+ setError(aFeatureError);
+ return;
}
- 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()) {
- // it could be a construction plane
- ResultPtr aContext = aToolAttr->context();
- 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.appendAlgo(aMkShCustom);
- aTools.push_back(aTool);
- aToolsForNaming.push_back(aContext->shape());
- }
- } else {
- aTools.push_back(aTool);
- aToolsForNaming.push_back(aTool);
- }
+ ListOfShape aBaseObjects = anObjects.objects();
+ aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end());
+
+ // resize planes to the bounding box of operated shapes
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
+ resizePlanes(anObjects.objects(), aPlanes, aMakeShapeList);
+
+ // cut unused solids of composolids from the objects of partition
+ ListOfShape aTargetObjects, anUnusedSubs;
+ std::string aError;
+ if (!cutSubs(anObjects, aTargetObjects, anUnusedSubs, aMakeShapeList, aError)) {
+ setError(aError);
+ return;
}
+ aBaseObjects.insert(aBaseObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
- // Getting combine flag.
- bool isCombine = boolean(COMBINE_ID())->value();
+ // perform partition first time to split target solids by planes
+ std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(
+ new GeomAlgoAPI_Partition(aTargetObjects, aPlanes));
- if(anObjects.empty()/* || aTools.empty()*/) {
- std::string aFeatureError = "Error: Not enough objects for partition operation.";
- setError(aFeatureError);
+ // Checking that the algorithm worked properly.
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
+ setError(aError);
return;
}
- int aResultIndex = 0;
+ aMakeShapeList->appendAlgo(aPartitionAlgo);
+ GeomShapePtr aResultShape = aPartitionAlgo->shape();
+
+ if (!anUnusedSubs.empty()) {
+ // second pass of a partition to split shared faces of compsolids
+ aTargetObjects.clear();
+ aTargetObjects.push_back(aResultShape);
+ aTargetObjects.insert(aTargetObjects.end(), anUnusedSubs.begin(), anUnusedSubs.end());
- if(isCombine) {
- // Create single result.
- //if(!aTools.empty()) {
- // // This is a workaround for naming. Passing compound of objects as argument instead each object separately.
- // std::shared_ptr<GeomAPI_Shape> aCompoud = GeomAlgoAPI_CompoundBuilder::compound(anObjects);
- // anObjects.clear();
- // anObjects.push_back(aCompoud);
- //}
- std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools));
+ aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape()));
// Checking that the algorithm worked properly.
- if (!aPartitionAlgo->isDone()) {
- static const std::string aFeatureError = "Error: Partition algorithm failed.";
- setError(aFeatureError);
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) {
+ setError(aError);
return;
}
- if (aPartitionAlgo->shape()->isNull()) {
- static const std::string aShapeError = "Error: Resulting shape is Null.";
- setError(aShapeError);
- return;
+
+ aMakeShapeList->appendAlgo(aPartitionAlgo);
+ aResultShape = aPartitionAlgo->shape();
+ }
+
+ int aResultIndex = 0;
+
+ if (data()->version().empty()) {
+ // default behaviors of Partition
+ if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
+ for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) {
+ storeResult(aBaseObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex);
+ ++aResultIndex;
+ }
+ } else {
+ storeResult(aBaseObjects, aPlanes, aResultShape, aMakeShapeList, aResultIndex);
+ ++aResultIndex;
}
- if (!aPartitionAlgo->isValid()) {
- std::string aFeatureError = "Error: Resulting shape is not valid.";
- setError(aFeatureError);
- return;
+ }
+ else {
+ // merge hierarchies of compounds containing objects and tools
+ GeomShapePtr aFirstShape = aResultShape;
+ GeomAPI_ShapeIterator anIt;
+ if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
+ anIt = GeomAPI_ShapeIterator(aResultShape);
+ aFirstShape = anIt.current();
+ anIt.next();
}
- if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo->shape()) > 1.e-7) {
- std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
- aMakeShapeList.appendAlgo(aPartitionAlgo);
- GeomAPI_DataMapOfShapeShape& aMapOfShapes = *aPartitionAlgo->mapOfSubShapes().get();
- std::shared_ptr<GeomAPI_Shape> aBaseShape = anObjects.front();
- anObjects.pop_front();
- aToolsForNaming.insert(aToolsForNaming.end(), anObjects.begin(), anObjects.end());
- loadNamingDS(aResultBody, aBaseShape, 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);
- std::shared_ptr<GeomAlgoAPI_Partition> aPartitionAlgo(new GeomAlgoAPI_Partition(aListWithObject, aTools));
-
- // Checking that the algorithm worked properly.
- if (!aPartitionAlgo->isDone()) {
- static const std::string aFeatureError = "Error: Partition algorithm failed.";
- setError(aFeatureError);
- return;
- }
- if (aPartitionAlgo->shape()->isNull()) {
- static const std::string aShapeError = "Error: Resulting shape is Null.";
- setError(aShapeError);
- return;
- }
- if (!aPartitionAlgo->isValid()) {
- std::string aFeatureError = "Error: Resulting shape is not valid.";
- setError(aFeatureError);
- return;
- }
+ GeomShapePtr aResultCompound =
+ keepUnusedSubsOfCompound(aFirstShape, anObjects, GeomAPI_ShapeHierarchy(), aMakeShapeList);
- if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo->shape()) > 1.e-7) {
- std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
- GeomAlgoAPI_MakeShapeList aMakeShapeListCopy = aMakeShapeList;
- aMakeShapeListCopy.appendAlgo(aPartitionAlgo);
- GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo->mapOfSubShapes().get();
- loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo->shape(), aMakeShapeListCopy, aMapOfShapes);
- setResult(aResultBody, aResultIndex);
- aResultIndex++;
+ if (anIt.more()) {
+ if (aResultCompound->shapeType() != GeomAPI_Shape::COMPOUND) {
+ // put the shape into compound
+ ListOfShape aShapes;
+ aShapes.push_back(aResultCompound);
+ aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
}
+ std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
+ for (; anIt.more(); anIt.next())
+ aBuilder->add(aResultCompound, anIt.current());
+ aMakeShapeList->appendAlgo(aBuilder);
}
+
+ storeResult(aBaseObjects, aPlanes, aResultCompound, aMakeShapeList, aResultIndex);
+ ++aResultIndex;
}
- // remove the rest results if there were produced in the previous pass
+ // 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)
+void FeaturesPlugin_Partition::storeResult(
+ ListOfShape& theObjects, ListOfShape& thePlanes,
+ const GeomShapePtr theResultShape,
+ const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
+ const int theIndex)
+{
+ // Create result body.
+ ResultBodyPtr aResultBody = document()->createBody(data(), theIndex);
+
+ // if result is same as one of the base object, no modification was performed
+ for(ListOfShape::const_iterator anObj = theObjects.cbegin(); anObj != theObjects.cend(); ++anObj)
+ {
+ if (anObj->get() && (*anObj)->isSame(theResultShape)) {
+ aResultBody->store(theResultShape, false);
+ setResult(aResultBody, theIndex);
+ return;
+ }
+ }
+
+ aResultBody->storeModified(theObjects, theResultShape, theMakeShape);
+
+ std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfSubShapes = theMakeShape->mapOfSubShapes();
+ theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end());
+ for (ListOfShape::const_iterator anIt = theObjects.cbegin();
+ anIt != theObjects.cend();
+ ++anIt)
+ {
+ GeomShapePtr aShape = *anIt;
+ aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::EDGE);
+ aResultBody->loadModifiedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
+ aResultBody->loadDeletedShapes(theMakeShape, aShape, GeomAPI_Shape::FACE);
+ }
+
+ setResult(aResultBody, theIndex);
+}
+
+
+//=================================================================================================
+
+static bool cutSubs(ListOfShape& theSubsToCut,
+ const ListOfShape& theTools,
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
+ std::string& theError)
+{
+ if (theSubsToCut.empty() || theTools.empty())
+ return true;
+
+ // cut from current list of solids
+ std::shared_ptr<GeomAlgoAPI_MakeShape> aCutAlgo(
+ new GeomAlgoAPI_Boolean(theSubsToCut, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
+ if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, "", theError))
+ return false;
+ theMakeShapeList->appendAlgo(aCutAlgo);
+
+ // update list of un-selected objects of the partition
+ GeomAPI_Shape::ShapeType aType = theSubsToCut.front()->shapeType();
+ theSubsToCut.clear();
+ for (GeomAPI_ShapeExplorer anExp(aCutAlgo->shape(), aType); anExp.more(); anExp.next())
+ theSubsToCut.push_back(anExp.current());
+ return true;
+}
+
+bool FeaturesPlugin_Partition::cutSubs(
+ GeomAPI_ShapeHierarchy& theHierarchy,
+ ListOfShape& theUsed,
+ ListOfShape& theNotUsed,
+ std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
+ std::string& theError)
{
- //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;
+ theUsed.clear();
+ theNotUsed.clear();
+
+ GeomAPI_ShapeHierarchy::iterator anIt = theHierarchy.begin();
+
+ // compose a set of tools for the CUT operation:
+ // find the list of unused subs of the first argument or use itself
+ ListOfShape aToolsForUsed, aToolsForUnused;
+ GeomShapePtr aFirstArgument = theHierarchy.parent(*anIt, false);
+ if (aFirstArgument && aFirstArgument->shapeType() == GeomAPI_Shape::COMPSOLID) {
+ theHierarchy.splitCompound(aFirstArgument, theUsed, aToolsForUsed);
+ theNotUsed = aToolsForUsed;
+ }
+ else {
+ aFirstArgument = *anIt;
+ theUsed.push_back(aFirstArgument);
+ }
+ aToolsForUnused.push_back(aFirstArgument);
+
+ // cut subs
+ bool isOk = true;
+ for (++anIt; anIt != theHierarchy.end() && isOk; ++anIt) {
+ ListOfShape aUsed, aNotUsed;
+
+ GeomShapePtr aParent = theHierarchy.parent(*anIt, false);
+ if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
+ aParent = theHierarchy.parent(*anIt); // get parent once again to mark its subs as processed
+ theHierarchy.splitCompound(aParent, aUsed, aNotUsed);
+ }
+ else
+ aUsed.push_back(*anIt);
+
+ isOk = ::cutSubs(aUsed, aToolsForUsed, theMakeShapeList, theError)
+ && ::cutSubs(aNotUsed, aToolsForUnused, theMakeShapeList, theError);
+ if (isOk) {
+ theUsed.insert(theUsed.end(), aUsed.begin(), aUsed.end());
+ theNotUsed.insert(theNotUsed.end(), aNotUsed.begin(), aNotUsed.end());
}
}
+
+ return isOk;
}