X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Partition.cpp;h=46a16c54c80c358dcdbd13136d19a03b457f5418;hb=88ee9b2b81cf93a6324336b57e30cc8a3a487499;hp=94b7f4bcebaedc02a626d62dc782a610b804e61e;hpb=2a232230cb243c76c34315623d6e6689f57e83f4;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 94b7f4bce..46a16c54c --- a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp @@ -1,30 +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() { @@ -33,195 +57,228 @@ FeaturesPlugin_Partition::FeaturesPlugin_Partition() //================================================================================================= void FeaturesPlugin_Partition::initAttributes() { - AttributeSelectionListPtr aSelection = - std::dynamic_pointer_cast(data()->addAttribute( - FeaturesPlugin_Partition::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); - aSelection->setSelectionType("SOLID"); - - aSelection = - std::dynamic_pointer_cast(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()); -} - -//================================================================================================= -std::shared_ptr FeaturesPlugin_Partition::getShape(const std::string& theAttrName) -{ - std::shared_ptr aObjRef = - std::dynamic_pointer_cast(data()->attribute(theAttrName)); - if (aObjRef) { - std::shared_ptr aConstr = - std::dynamic_pointer_cast(aObjRef->value()); - if (aConstr) - return aConstr->shape(); - } - return std::shared_ptr(); + 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 anObjectAttr = anObjectsSelList->value(anObjectsIndex); - std::shared_ptr 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 > 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 aToolAttr = aToolsSelList->value(aToolsIndex); - std::shared_ptr 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 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 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 aPartitionAlgo( + new GeomAlgoAPI_Partition(aTargetObjects, aPlanes)); - if(anObjects.empty()/* || aTools.empty()*/) { - std::string aFeatureError = "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(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 aCompoud = GeomAlgoAPI_CompoundBuilder::compound(anObjects); - anObjects.clear(); - anObjects.push_back(aCompoud); - } - GeomAlgoAPI_Partition aPartitionAlgo(anObjects, aTools); + 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 (!aPartitionAlgo.isDone()) { - static const std::string aFeatureError = "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 = "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 = "Warning: 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 aResultBody = document()->createBody(data(), aResultIndex); - aMakeShapeList.appendAlgo(aPartitionAlgo.makeShape()); - GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get(); - loadNamingDS(aResultBody, anObjects.front(), 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 anObject = *anObjectsIt; - ListOfShape aListWithObject; aListWithObject.push_back(anObject); - GeomAlgoAPI_Partition aPartitionAlgo(aListWithObject, aTools); - - // Checking that the algorithm worked properly. - if (!aPartitionAlgo.isDone()) { - static const std::string aFeatureError = "Partition algorithm failed"; - setError(aFeatureError); - return; - } - if (aPartitionAlgo.shape()->isNull()) { - static const std::string aShapeError = "Resulting shape is Null"; - setError(aShapeError); - return; - } - if (!aPartitionAlgo.isValid()) { - std::string aFeatureError = "Warning: 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 aResultBody = document()->createBody(data(), aResultIndex); - aMakeShapeList.appendAlgo(aPartitionAlgo.makeShape()); - GeomAPI_DataMapOfShapeShape aMapOfShapes = *aPartitionAlgo.mapOfShapes().get(); - loadNamingDS(aResultBody, anObject, aToolsForNaming, aPartitionAlgo.shape(), aMakeShapeList, 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 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 theResultBody, - const std::shared_ptr theBaseShape, - const ListOfShape& theTools, - const std::shared_ptr theResultShape, - GeomAlgoAPI_MakeShape& theMakeShape, - GeomAPI_DataMapOfShapeShape& theMapOfShapes) +void FeaturesPlugin_Partition::storeResult( + ListOfShape& theObjects, ListOfShape& thePlanes, + const GeomShapePtr theResultShape, + const std::shared_ptr theMakeShape, + const int theIndex) { - //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; + // 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 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& 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; }