X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Partition.cpp;h=46a16c54c80c358dcdbd13136d19a03b457f5418;hb=9628c44ddd75b2adc08bb7c4ddc690b2e09db3c5;hp=ec16dd41e37acba5405c9690906c7aaa7a8fd210;hpb=1d864d8beda4d31de109734a9f7f006cb361e91c;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp old mode 100755 new mode 100644 index ec16dd41e..46a16c54c --- a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp @@ -1,32 +1,54 @@ -// 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 -#include #include -#include #include +#include +#include #include +#include +#include #include -#include #include +#include #include +#include #include -#include #include #include +#include +#include #include +#include +#include +#include #include +#include +#include #include + //================================================================================================= FeaturesPlugin_Partition::FeaturesPlugin_Partition() { @@ -36,78 +58,111 @@ FeaturesPlugin_Partition::FeaturesPlugin_Partition() void FeaturesPlugin_Partition::initAttributes() { data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId()); + initVersion(BOP_VERSION_9_4(), selectionList(BASE_OBJECTS_ID())); } //================================================================================================= void FeaturesPlugin_Partition::execute() { - ListOfShape anObjects, aPlanes; + GeomAPI_ShapeHierarchy anObjects; + ListOfShape aPlanes; // Getting objects. - AttributeSelectionListPtr anObjectsSelList = selectionList(BASE_OBJECTS_ID()); - for(int anIndex = 0; anIndex < anObjectsSelList->size(); ++anIndex) { - AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anIndex); - GeomShapePtr anObject = anObjectAttr->value(); - if(!anObject.get()) { - // It could be a construction plane. - ResultPtr aContext = anObjectAttr->context(); - aPlanes.push_back(anObjectAttr->context()->shape()); - } else { - 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; } - std::list > aBoundingPoints = GeomAlgoAPI_ShapeTools::getBoundingBox(anObjects, 1.0); + ListOfShape aBaseObjects = anObjects.objects(); + aBaseObjects.insert(aBaseObjects.end(), aPlanes.begin(), aPlanes.end()); - // Resize planes. - ListOfShape aTools; + // resize planes to the bounding box of operated shapes std::shared_ptr aMakeShapeList(new GeomAlgoAPI_MakeShapeList()); - for(ListOfShape::const_iterator anIt = aPlanes.cbegin(); anIt != aPlanes.cend(); ++anIt) { - GeomShapePtr aPlane = *anIt; - GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints); - std::shared_ptr aMkShCustom(new GeomAlgoAPI_MakeShapeCustom); - aMkShCustom->addModified(aPlane, aTool); - aMakeShapeList->appendAlgo(aMkShCustom); - aTools.push_back(aTool); + 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()); - // Create single result. - std::shared_ptr aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, aTools)); + // perform partition first time to split target solids by planes + std::shared_ptr aPartitionAlgo( + new GeomAlgoAPI_Partition(aTargetObjects, aPlanes)); // 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); + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) { + setError(aError); return; } + 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()); + + aPartitionAlgo.reset(new GeomAlgoAPI_Partition(aTargetObjects, ListOfShape())); + + // Checking that the algorithm worked properly. + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPartitionAlgo, getKind(), aError)) { + setError(aError); + return; + } + + aMakeShapeList->appendAlgo(aPartitionAlgo); + 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); + + 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; } - } else { - storeResult(anObjects, aResultShape, aMakeShapeList, aResultIndex); + } + 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(); + } + + GeomShapePtr aResultCompound = + keepUnusedSubsOfCompound(aFirstShape, anObjects, GeomAPI_ShapeHierarchy(), aMakeShapeList); + + 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 aBuilder(new GeomAlgoAPI_ShapeBuilder); + for (; anIt.more(); anIt.next()) + aBuilder->add(aResultCompound, anIt.current()); + aMakeShapeList->appendAlgo(aBuilder); + } + + storeResult(aBaseObjects, aPlanes, aResultCompound, aMakeShapeList, aResultIndex); ++aResultIndex; } @@ -116,58 +171,114 @@ void FeaturesPlugin_Partition::execute() } //================================================================================================= -void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects, - const GeomShapePtr theResultShape, - const std::shared_ptr theMakeShape, - const int theIndex) +void FeaturesPlugin_Partition::storeResult( + ListOfShape& theObjects, ListOfShape& thePlanes, + const GeomShapePtr theResultShape, + const std::shared_ptr theMakeShape, + const int theIndex) { - // Find base. - 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; - } - } - if(aBaseShape.get()) { - break; - } - } - // Create result body. ResultBodyPtr aResultBody = document()->createBody(data(), theIndex); - // Store modified shape. - if(aBaseShape->isEqual(theResultShape)) { - aResultBody->store(theResultShape); - return; + // 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; + } } - const int aDelTag = 1; - const int aSubTag = 2; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids - int aModTag = aSubTag + 10000; - const std::string aModName = "Modified"; - - aResultBody->storeModified(aBaseShape, theResultShape, aSubTag); + aResultBody->storeModified(theObjects, theResultShape, theMakeShape); std::shared_ptr aMapOfSubShapes = theMakeShape->mapOfSubShapes(); - 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); - aModTag += 10000; + 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& theMakeShapeList, + std::string& theError) +{ + if (theSubsToCut.empty() || theTools.empty()) + return true; + + // cut from current list of solids + std::shared_ptr 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& theMakeShapeList, + std::string& theError) +{ + 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; +}