X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Partition.cpp;h=5272cd8188a8afb92283c3f1ece5ae6bde57f968;hb=0fc732191ab734b184641afda92d024f80da812d;hp=6000c9d8431e8cc102bd828e01f2ddbafd2c7910;hpb=61174325c0d2309e15854d81f138e9a8832083f9;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp index 6000c9d84..5272cd818 100755 --- a/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Partition.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -16,10 +17,24 @@ #include #include +#include #include +#include #include #include +#include +#include +#include + +#include +#include + +static GeomShapePtr findBase(const GeomShapePtr theObjectShape, + const GeomShapePtr theResultShape, + const GeomAPI_Shape::ShapeType theShapeType, + const std::shared_ptr theMakeShape); + //================================================================================================= FeaturesPlugin_Partition::FeaturesPlugin_Partition() { @@ -28,140 +43,182 @@ 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(), OBJECT_LIST_ID()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID()); -} - -//================================================================================================= -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()); } //================================================================================================= void FeaturesPlugin_Partition::execute() { - ListOfShape anObjects, aTools; + ListOfShape anObjects, 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; + 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); } - anObjects.push_back(anObject); } - // 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::faceToInfinitePlane(aContext->shape()); - } - } - if (!aTool.get()) { - return; - } + 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); } - int aResultIndex = 0; + // Create single result. + std::shared_ptr aPartitionAlgo( + new GeomAlgoAPI_Partition(anObjects, aTools)); - if (anObjects.empty() || aTools.empty()) { - std::string aFeatureError = "Not enough objects for partition operation"; + // 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(); - // Cut each object with all tools - 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; - } - - if (GeomAlgoAPI_ShapeTools::volume(aPartitionAlgo.shape()) > 1.e-7) { - std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); - aResultBody->store(aPartitionAlgo.shape()); - loadNamingDS(aResultBody, anObject, aTools, aPartitionAlgo); - setResult(aResultBody, aResultIndex); - aResultIndex++; + int aResultIndex = 0; + if(aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) { + for(GeomAPI_ShapeIterator anIt(aResultShape); anIt.more(); anIt.next()) { + storeResult(anObjects, aPlanes, anIt.current(), aMakeShapeList, aResultIndex); + ++aResultIndex; } + } else { + storeResult(anObjects, aPlanes, aResultShape, 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 GeomAlgoAPI_Partition& thePartitionAlgo) +void FeaturesPlugin_Partition::storeResult( + ListOfShape& theObjects, ListOfShape& thePlanes, + const GeomShapePtr theResultShape, + const std::shared_ptr theMakeShape, + const int theIndex) { - //load result - if(theBaseShape->isEqual(thePartitionAlgo.shape())) { - theResultBody->store(thePartitionAlgo.shape()); - } else { - const int aModifyTag = 1; - const int aDeletedTag = 2; - const int aSubsolidsTag = 3; /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids + // Find base. The most complicated is the real modified object (#1799 if box is partitioned by + // two planes the box is the base, not planes, independently on the order in the list). + GeomShapePtr aBaseShape; + for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) { + GeomShapePtr anObjectShape = *anIt; + GeomShapePtr aCandidate = + findBase(anObjectShape, theResultShape, GeomAPI_Shape::VERTEX, theMakeShape); + if(!aCandidate.get()) { + aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::EDGE, theMakeShape); + } + if (!aCandidate.get()) + aCandidate = findBase(anObjectShape, theResultShape, GeomAPI_Shape::FACE, theMakeShape); + + if(aCandidate.get()) { + if (!aBaseShape.get() || aBaseShape->shapeType() > aCandidate->shapeType()) { + aBaseShape = aCandidate; + } + } + } + + // Create result body. + ResultBodyPtr aResultBody = document()->createBody(data(), theIndex); - theResultBody->storeModified(theBaseShape, thePartitionAlgo.shape(), aSubsolidsTag); + // Store modified shape. + if(!aBaseShape.get() || aBaseShape->isEqual(theResultShape)) { + aResultBody->store(theResultShape); + setResult(aResultBody, theIndex); + return; + } + + const int aDelTag = 1; + /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids + const int aSubTag = 2; + int aModTag = aSubTag + 10000; + const std::string aModName = "Modified"; + + aResultBody->storeModified(aBaseShape, theResultShape, aSubTag); + + std::shared_ptr aMapOfSubShapes = theMakeShape->mapOfSubShapes(); + theObjects.insert(theObjects.end(), thePlanes.begin(), thePlanes.end()); + int anIndex = 1; + for(ListOfShape::const_iterator anIt = theObjects.cbegin(); anIt != theObjects.cend(); ++anIt) { + GeomShapePtr aShape = *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(), aShape, GeomAPI_Shape::EDGE, + aModTag, aModEdgeName, *aMapOfSubShapes.get(), true); + aModTag += 10000; + aResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, + aModTag, aModFaceName, *aMapOfSubShapes.get(), true); + aModTag += 10000; + aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::EDGE, aDelTag); + aResultBody->loadDeletedShapes(theMakeShape.get(), aShape, GeomAPI_Shape::FACE, aDelTag); + } - std::shared_ptr aMkShape = thePartitionAlgo.makeShape(); - std::shared_ptr aMapOfShapes = thePartitionAlgo.mapOfShapes(); + setResult(aResultBody, theIndex); +} - std::string aModName = "Modified"; - theResultBody->loadAndOrientModifiedShapes(aMkShape.get(), theBaseShape, GeomAPI_Shape::FACE, - aModifyTag, aModName, *aMapOfShapes.get()); - theResultBody->loadDeletedShapes(aMkShape.get(), theBaseShape, GeomAPI_Shape::FACE, aDeletedTag); - for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) { - theResultBody->loadAndOrientModifiedShapes(aMkShape.get(), *anIter, GeomAPI_Shape::FACE, - aModifyTag, aModName, *aMapOfShapes.get()); - theResultBody->loadDeletedShapes(aMkShape.get(), *anIter, GeomAPI_Shape::FACE, aDeletedTag); +//================================================================================================= +GeomShapePtr findBase(const GeomShapePtr theObjectShape, + const GeomShapePtr theResultShape, + const GeomAPI_Shape::ShapeType theShapeType, + const std::shared_ptr theMakeShape) +{ + GeomShapePtr aBaseShape; + std::shared_ptr aMapOfSubShapes = theMakeShape->mapOfSubShapes(); + for(GeomAPI_ShapeExplorer anObjectSubShapesExp(theObjectShape, theShapeType); + anObjectSubShapesExp.more(); + anObjectSubShapesExp.next()) { + GeomShapePtr anObjectSubShape = anObjectSubShapesExp.current(); + ListOfShape aModifiedShapes; + theMakeShape->modified(anObjectSubShape, aModifiedShapes); + for(ListOfShape::const_iterator + aModIt = aModifiedShapes.cbegin(); aModIt != aModifiedShapes.cend(); ++aModIt) { + GeomShapePtr aModShape = *aModIt; + if(aMapOfSubShapes->isBound(aModShape)) { + aModShape = aMapOfSubShapes->find(aModShape); + } + if(theResultShape->isSubShape(aModShape)) { + aBaseShape = theObjectShape; + break; + } + } + if(aBaseShape.get()) { + break; } } + + return aBaseShape; }