X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Boolean.cpp;h=7fb46f2c998cac3abd103ee68124d04c81f45a1b;hb=d9e58981abfe2d72c00c285ba40b63135629aa86;hp=e71b379baaa0dacddc7be125e259d92a9c410cb4;hpb=3874b57fe5aba25ff5aee2a07654fc23c1ee8eb0;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp b/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp index e71b379ba..7fb46f2c9 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Boolean.cpp @@ -11,23 +11,45 @@ #include #include #include +#include +#include +#include + #include -using namespace std; +#include +#include #define FACE 4 #define _MODIFY_TAG 1 #define _DELETED_TAG 2 +#define _SUBSOLIDS_TAG 3 /// sub solids will be placed at labels 3, 4, etc. if result is compound of solids + +//================================================================================================= FeaturesPlugin_Boolean::FeaturesPlugin_Boolean() { } +//================================================================================================= void FeaturesPlugin_Boolean::initAttributes() { - data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::type()); - data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::type()); - data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::type()); + data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId()); + + AttributeSelectionListPtr aSelection = + std::dynamic_pointer_cast(data()->addAttribute( + FeaturesPlugin_Boolean::OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); + // extrusion works with faces always + aSelection->setSelectionType("SOLID"); + + aSelection = std::dynamic_pointer_cast(data()->addAttribute( + FeaturesPlugin_Boolean::TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); + // extrusion works with faces always + 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_Boolean::getShape(const std::string& theAttrName) { std::shared_ptr aObjRef = std::dynamic_pointer_cast< @@ -41,74 +63,158 @@ std::shared_ptr FeaturesPlugin_Boolean::getShape(const std::strin return std::shared_ptr(); } - +//================================================================================================= void FeaturesPlugin_Boolean::execute() { + // Getting operation type. std::shared_ptr aTypeAttr = std::dynamic_pointer_cast< ModelAPI_AttributeInteger>(data()->attribute(FeaturesPlugin_Boolean::TYPE_ID())); if (!aTypeAttr) return; - int aType = aTypeAttr->value(); - - std::shared_ptr anObject = this->getShape(FeaturesPlugin_Boolean::OBJECT_ID()); - if (!anObject) - return; - - std::shared_ptr aTool = this->getShape(FeaturesPlugin_Boolean::TOOL_ID()); - if (!aTool) - return; - - std::shared_ptr aResultBody = document()->createBody(data()); - - GeomAlgoAPI_Boolean* aFeature = new GeomAlgoAPI_Boolean(anObject, aTool, aType); - if(aFeature && !aFeature->isDone()) { - static const std::string aFeatureError = "Boolean feature: algorithm failed"; - setError(aFeatureError); - return; + GeomAlgoAPI_Boolean::OperationType aType = (GeomAlgoAPI_Boolean::OperationType)aTypeAttr->value(); + + ListOfShape anObjects, aTools; + + // Getting objects. + AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Boolean::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); } - // Check if shape is valid - if (aFeature->shape()->isNull()) { - static const std::string aShapeError = "Boolean feature: resulting shape is Null"; - setError(aShapeError); - return; - } - if(!aFeature->isValid()) { - static const std::string aFeatureError = "Boolean feature: resulting shape is not valid"; - setError(aFeatureError); - return; - } - // if result of Boolean operation is same as was before it means that Boolean operation has no sence - // and naming provides no result, so, generate an error in this case - if (anObject->isEqual(aFeature->shape())) { - static const std::string aFeatureError = "Boolean feature: operation was not performed"; - setError(aFeatureError); - return; + + // Getting tools. + AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::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()) { + return; + } + aTools.push_back(aTool); } - //LoadNamingDS - LoadNamingDS(aFeature, aResultBody, anObject, aTool, aType); - setResult(aResultBody); + int aResultIndex = 0; + + switch(aType) { + case GeomAlgoAPI_Boolean::BOOL_CUT: + case GeomAlgoAPI_Boolean::BOOL_COMMON:{ + if(anObjects.empty() || aTools.empty()) { + std::string aFeatureError = "Not enough objects for boolean operation"; + setError(aFeatureError); + return; + } + + // 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_Boolean aBoolAlgo(aListWithObject, aTools, aType); + + // Checking that the algorithm worked properly. + if(!aBoolAlgo.isDone()) { + static const std::string aFeatureError = "Boolean algorithm failed"; + setError(aFeatureError); + return; + } + if(aBoolAlgo.shape()->isNull()) { + static const std::string aShapeError = "Resulting shape is Null"; + setError(aShapeError); + return; + } + if(!aBoolAlgo.isValid()) { + std::string aFeatureError = "Warning: resulting shape is not valid"; + setError(aFeatureError); + return; + } + + if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) { + std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); + LoadNamingDS(aResultBody, anObject, aTools, aBoolAlgo); + setResult(aResultBody, aResultIndex); + aResultIndex++; + } + } + break; + } + case GeomAlgoAPI_Boolean::BOOL_FUSE: { + if(anObjects.empty() && aTools.size() > 1) { + anObjects.push_back(aTools.back()); + aTools.pop_back(); + }else if(aTools.empty() && anObjects.size() > 1) { + aTools.push_back(anObjects.back()); + anObjects.pop_back(); + } + + if(anObjects.empty() || aTools.empty()) { + std::string aFeatureError = "Not enough objects for boolean operation"; + setError(aFeatureError); + return; + } + + // Fuse all objects and all tools. + GeomAlgoAPI_Boolean aBoolAlgo(anObjects, aTools, aType); + + // Checking that the algorithm worked properly. + if(!aBoolAlgo.isDone()) { + static const std::string aFeatureError = "Boolean algorithm failed"; + setError(aFeatureError); + return; + } + if(aBoolAlgo.shape()->isNull()) { + static const std::string aShapeError = "Resulting shape is Null"; + setError(aShapeError); + return; + } + if(!aBoolAlgo.isValid()) { + std::string aFeatureError = "Warning: resulting shape is not valid"; + setError(aFeatureError); + return; + } + + std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); + LoadNamingDS(aResultBody, anObjects.front(), aTools, aBoolAlgo); + setResult(aResultBody, aResultIndex); + aResultIndex++; + break; + } + default: { + std::string anOperationError = "Error: wrong type of operation"; + setError(anOperationError); + return; + } + } + // remove the rest results if there were produced in the previous pass + removeResults(aResultIndex); } -//============================================================================ -void FeaturesPlugin_Boolean::LoadNamingDS(GeomAlgoAPI_Boolean* theFeature, - std::shared_ptr theResultBody, - std::shared_ptr theObject, - std::shared_ptr theTool, - int theType) -{ - +//================================================================================================= +void FeaturesPlugin_Boolean::LoadNamingDS(std::shared_ptr theResultBody, + const std::shared_ptr& theBaseShape, + const ListOfShape& theTools, + const GeomAlgoAPI_Boolean& theAlgo) +{ //load result - theResultBody->storeModified(theObject, theFeature->shape()); - - GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape(); - theFeature->mapOfShapes(*aSubShapes); - - // Put in DF modified faces - theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theObject, FACE, _MODIFY_TAG, *aSubShapes); - theResultBody->loadAndOrientModifiedShapes(theFeature->makeShape(), theTool, FACE, _MODIFY_TAG, *aSubShapes); - - //Put in DF deleted faces - theResultBody->loadDeletedShapes(theFeature->makeShape(), theObject, FACE, _DELETED_TAG); - theResultBody->loadDeletedShapes(theFeature->makeShape(), theTool, FACE, _DELETED_TAG); + if(theBaseShape->isEqual(theAlgo.shape())) { + theResultBody->store(theAlgo.shape()); + } else { + theResultBody->storeModified(theBaseShape, theAlgo.shape(), _SUBSOLIDS_TAG); + + GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape(); + + std::string aModName = "Modified"; + theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, + _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get()); + theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, FACE, _DELETED_TAG); + + for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) { + theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, FACE, + _MODIFY_TAG, aModName, *theAlgo.mapOfShapes().get()); + theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, FACE, _DELETED_TAG); + } + } }