X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Partition.cpp;h=4e84bef15a1df03452186b6c29365aaebd3dedde;hb=919584a7e5ee83c384873c2627b9865e8ba02272;hp=284acd11863f87592599489e73932f6ce84873b2;hpb=82d3fff7d0ed2c089da5aa6b106789be076081f3;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp index 284acd118..4e84bef15 100755 --- a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,17 @@ #include #include +#include +#include +#include +#include +#include + +#include +#include + +#include + //================================================================================================= FeaturesPlugin_Partition::FeaturesPlugin_Partition() { @@ -24,34 +36,141 @@ 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(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId()); } //================================================================================================= -std::shared_ptr FeaturesPlugin_Partition::getShape(const std::string& theAttrName) +void FeaturesPlugin_Partition::execute() { - std::shared_ptr aObjRef = std::dynamic_pointer_cast< - ModelAPI_AttributeReference>(data()->attribute(theAttrName)); - if (aObjRef) { - std::shared_ptr aConstr = std::dynamic_pointer_cast< - ModelAPI_ResultBody>(aObjRef->value()); - if (aConstr) - return aConstr->shape(); - } - return std::shared_ptr(); + ListOfShape anObjects, 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); + } + } + + 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); + + // Resize planes. + ListOfShape aTools; + 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); + } + + // Create single result. + std::shared_ptr aPartitionAlgo(new GeomAlgoAPI_Partition(anObjects, 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; + } + aMakeShapeList->appendAlgo(aPartitionAlgo); + GeomShapePtr 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); + ++aResultIndex; + } + } else { + storeResult(anObjects, aResultShape, aMakeShapeList, aResultIndex); + ++aResultIndex; + } + + // Remove the rest results if there were produced in the previous pass. + removeResults(aResultIndex); } //================================================================================================= -void FeaturesPlugin_Partition::execute() +void FeaturesPlugin_Partition::storeResult(const ListOfShape& theObjects, + 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.get() || aBaseShape->isEqual(theResultShape)) { + aResultBody->store(theResultShape); + 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); + + std::shared_ptr aMapOfSubShapes = theMakeShape->mapOfSubShapes(); + int anIndex = 1; + for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) { + std::string aModEdgeName = aModName + "_Edge_" + std::to_string((long long)anIndex); + std::string aModFaceName = aModName + "_Face_" + std::to_string((long long)anIndex++); + aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::EDGE, + aModTag, aModEdgeName, *aMapOfSubShapes.get(), true); + aModTag += 1000; + aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, + aModTag, aModFaceName, *aMapOfSubShapes.get(), true); + aModTag += 1000; + aResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::EDGE, aDelTag); + aResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag); + } + + setResult(aResultBody, theIndex); }