X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_CompositeBoolean.cpp;h=c51ac16b5ddcee205c620920ca1ea4b3db3fdf03;hb=86c435d69e7b27d36c037ed028d79d8f32f021ac;hp=f8fff4cdde077df292f856a7cf095e60e2b8813f;hpb=d9e58981abfe2d72c00c285ba40b63135629aa86;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp index f8fff4cdd..c51ac16b5 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp @@ -8,10 +8,20 @@ #include #include +#include #include #include +#include +#include +#include -#include +#include +#include +#include +#include +#include +#include +#include //================================================================================================= void FeaturesPlugin_CompositeBoolean::initAttributes() @@ -24,6 +34,9 @@ void FeaturesPlugin_CompositeBoolean::initAttributes() aSelection->setSelectionType("SOLID"); initMakeSolidsAttributes(); + + data()->addAttribute(SKETCH_SELECTION_ID(), ModelAPI_AttributeSelection::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SKETCH_SELECTION_ID()); } //================================================================================================= @@ -33,7 +46,7 @@ std::shared_ptr FeaturesPlugin_CompositeBoolean::addFeature(st if (aNew) { data()->reference(SKETCH_OBJECT_ID())->setValue(aNew); } - // set as current also after it becomes sub to set correctly enabled for other sketch subs + // set as current also after it becomes sub to set correctly enabled for other sketch subs document()->setCurrentFeature(aNew, false); return aNew; } @@ -46,7 +59,7 @@ int FeaturesPlugin_CompositeBoolean::numberOfSubs(bool forTree) const } //================================================================================================= -std::shared_ptr FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) const +std::shared_ptr FeaturesPlugin_CompositeBoolean::subFeature(const int theIndex, bool forTree) { if (theIndex == 0) return std::dynamic_pointer_cast(data()->reference(SKETCH_OBJECT_ID())->value()); @@ -56,9 +69,12 @@ std::shared_ptr FeaturesPlugin_CompositeBoolean::subFeature(co //================================================================================================= int FeaturesPlugin_CompositeBoolean::subFeatureId(const int theIndex) const { - std::shared_ptr aFeature = subFeature(theIndex); - if (aFeature.get()) - return aFeature->data()->featureId(); + if (theIndex == 0) { + FeaturePtr aFeature = + std::dynamic_pointer_cast(data()->reference(SKETCH_OBJECT_ID())->value()); + if (aFeature.get()) + return aFeature->data()->featureId(); + } return -1; } @@ -79,13 +95,27 @@ void FeaturesPlugin_CompositeBoolean::removeFeature(std::shared_ptrisValid()) { // on abort of sketch of this composite it may be invalid + FeaturePtr aSketch = + std::dynamic_pointer_cast(data()->reference(SKETCH_OBJECT_ID())->value()); + if (aSketch.get() && aSketch->data()->isValid()) { + document()->removeFeature(aSketch); + } + } + ModelAPI_CompositeFeature::erase(); +} + + //================================================================================================= void FeaturesPlugin_CompositeBoolean::execute() { // Getting faces to create solids. std::shared_ptr aSketchFeature = std::dynamic_pointer_cast( reference(SKETCH_OBJECT_ID())->value()); - if(!aSketchFeature) { + if(!aSketchFeature || aSketchFeature->results().empty()) { return; } ResultPtr aSketchRes = aSketchFeature->results().front(); @@ -93,35 +123,62 @@ void FeaturesPlugin_CompositeBoolean::execute() if(!aConstruction.get()) { return; } + selection(SKETCH_SELECTION_ID())->setValue(aSketchRes, std::shared_ptr()); int aSketchFacesNum = aConstruction->facesNum(); if(aSketchFacesNum == 0) { - return; //TODO: set error message + return; } - ListOfShape aSketchFacesList; + ListOfShape aFacesList; for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) { std::shared_ptr aFace = std::dynamic_pointer_cast(aConstruction->face(aFaceIndex)); - aSketchFacesList.push_back(aFace); + aFacesList.push_back(aFace); } - // Pass faces to soldis creation function. - ListOfShape aBooleanTools = MakeSolids(aSketchFacesList); - if(aBooleanTools.empty()) { + // Searching faces with common edges. + ListOfShape aShells; + ListOfShape aFreeFaces; + std::shared_ptr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aFacesList); + GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, aShells, aFreeFaces); + aShells.insert(aShells.end(), aFreeFaces.begin(), aFreeFaces.end()); + + // Pass shells/faces to solids creation function. + ListOfShape aTools; + std::list> aSolidsAlgos; + makeSolids(aShells, aTools, aSolidsAlgos); + if(aTools.empty()) { return; } // Getting objects for boolean operation. - ListOfShape aBooleanObjects; + ListOfShape anObjects; + std::map, ListOfShape> aCompSolidsObjects; AttributeSelectionListPtr anObjectsSelList = selectionList(BOOLEAN_OBJECTS_ID()); - if (anObjectsSelList->size() == 0) { + if(anObjectsSelList->size() == 0) { return; } for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { - std::shared_ptr anObjectAttr = anObjectsSelList->value(anObjectsIndex); + AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex); std::shared_ptr anObject = anObjectAttr->value(); if(!anObject.get()) { return; } - aBooleanObjects.push_back(anObject); + ResultPtr aContext = anObjectAttr->context(); + ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext); + if(aResCompSolidPtr.get()) { + std::shared_ptr aContextShape = aResCompSolidPtr->shape(); + std::map, ListOfShape>::iterator anIt = aCompSolidsObjects.begin(); + for(; anIt != aCompSolidsObjects.end(); anIt++) { + if(anIt->first->isEqual(aContextShape)) { + aCompSolidsObjects[anIt->first].push_back(anObject); + break; + } + } + if(anIt == aCompSolidsObjects.end()) { + aCompSolidsObjects[aContextShape].push_back(anObject); + } + } else { + anObjects.push_back(anObject); + } } // Cut from each object solids. @@ -131,11 +188,11 @@ void FeaturesPlugin_CompositeBoolean::execute() case GeomAlgoAPI_Boolean::BOOL_CUT: case GeomAlgoAPI_Boolean::BOOL_COMMON:{ // Cut each object with all tools - for(ListOfShape::iterator anObjectsIt = aBooleanObjects.begin(); anObjectsIt != aBooleanObjects.end(); anObjectsIt++) { + 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, aBooleanTools, myBooleanOperationType); + GeomAlgoAPI_Boolean aBoolAlgo(aListWithObject, aTools, myBooleanOperationType); // Checking that the algorithm worked properly. if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) { @@ -143,9 +200,65 @@ void FeaturesPlugin_CompositeBoolean::execute() return; } - if(GeomAlgoAPI_ShapeProps::volume(aBoolAlgo.shape()) > 1.e-7) { + if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo.shape()) > 1.e-7) { + std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); + loadNamingDS(aResultBody, aShells, aSolidsAlgos, anObject, aTools, aBoolAlgo.shape(), + *aBoolAlgo.makeShape(), *aBoolAlgo.mapOfShapes()); + setResult(aResultBody, aResultIndex); + aResultIndex++; + } + } + + // Compsolids handling + for(std::map, ListOfShape>::iterator anIt = aCompSolidsObjects.begin(); + anIt != aCompSolidsObjects.end(); anIt++) { + std::shared_ptr aCompSolid = anIt->first; + ListOfShape& aUsedInOperationSolids = anIt->second; + + // Collecting solids from compsolids which will not be modified in boolean operation. + ListOfShape aNotUsedSolids; + for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) { + std::shared_ptr aSolidInCompSolid = anExp.current(); + ListOfShape::iterator anIt = aUsedInOperationSolids.begin(); + for(; anIt != aUsedInOperationSolids.end(); anIt++) { + if(aSolidInCompSolid->isEqual(*anIt)) { + break; + } + } + if(anIt == aUsedInOperationSolids.end()) { + aNotUsedSolids.push_back(aSolidInCompSolid); + } + } + + GeomAlgoAPI_Boolean aBoolAlgo(aUsedInOperationSolids, aTools, myBooleanOperationType); + + // Checking that the algorithm worked properly. + if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) { + setError("Boolean algorithm failed"); + return; + } + + GeomAlgoAPI_MakeShapeList aMakeShapeList; + aMakeShapeList.append(aBoolAlgo.makeShape()); + GeomAPI_DataMapOfShapeShape aMapOfShapes; + aMapOfShapes.merge(aBoolAlgo.mapOfShapes()); + + // Add result to not used solids from compsolid. + ListOfShape aShapesToAdd = aNotUsedSolids; + aShapesToAdd.push_back(aBoolAlgo.shape()); + GeomAlgoAPI_PaveFiller aFillerAlgo(aShapesToAdd, true); + if(!aFillerAlgo.isDone()) { + std::string aFeatureError = "PaveFiller algorithm failed"; + setError(aFeatureError); + return; + } + + aMakeShapeList.append(aFillerAlgo.makeShape()); + aMapOfShapes.merge(aFillerAlgo.mapOfShapes()); + + if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo.shape()) > 1.e-7) { std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); - LoadNamingDS(aResultBody, anObject, aBooleanTools, aBoolAlgo); + loadNamingDS(aResultBody, aShells, aSolidsAlgos, aCompSolid, aTools, aFillerAlgo.shape(), aMakeShapeList, aMapOfShapes); setResult(aResultBody, aResultIndex); aResultIndex++; } @@ -153,17 +266,101 @@ void FeaturesPlugin_CompositeBoolean::execute() break; } case GeomAlgoAPI_Boolean::BOOL_FUSE: { + // Collecting all solids which will be fused. + ListOfShape aSolidsToFuse; + aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end()); + aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end()); + + // Collecting solids from compsolids which will not be modified in boolean operation. + ListOfShape aNotUsedSolids; + for(std::map, ListOfShape>::iterator anIt = aCompSolidsObjects.begin(); + anIt != aCompSolidsObjects.end(); anIt++) { + std::shared_ptr aCompSolid = anIt->first; + ListOfShape& aUsedInOperationSolids = anIt->second; + aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(), aUsedInOperationSolids.end()); + + // Collect solids from compsolid which will not be modified in boolean operation. + for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) { + std::shared_ptr aSolidInCompSolid = anExp.current(); + ListOfShape::iterator anIt = aUsedInOperationSolids.begin(); + for(; anIt != aUsedInOperationSolids.end(); anIt++) { + if(aSolidInCompSolid->isEqual(*anIt)) { + break; + } + } + if(anIt == aUsedInOperationSolids.end()) { + aNotUsedSolids.push_back(aSolidInCompSolid); + } + } + } + + ListOfShape anOriginalSolids = aSolidsToFuse; + anOriginalSolids.insert(anOriginalSolids.end(), aNotUsedSolids.begin(), aNotUsedSolids.end()); + GeomAlgoAPI_MakeShapeList aMakeShapeList; + GeomAPI_DataMapOfShapeShape aMapOfShapes; + + // If we have compsolids then cut with not used solids all others. + if(!aNotUsedSolids.empty()) { + aSolidsToFuse.clear(); + for(ListOfShape::iterator anIt = anOriginalSolids.begin(); anIt != anOriginalSolids.end(); anIt++) { + ListOfShape aOneObjectList; + aOneObjectList.push_back(*anIt); + GeomAlgoAPI_Boolean aCutAlgo(aOneObjectList, aNotUsedSolids, GeomAlgoAPI_Boolean::BOOL_CUT); + + if(GeomAlgoAPI_ShapeTools::volume(aCutAlgo.shape()) > 1.e-7) { + aSolidsToFuse.push_back(aCutAlgo.shape()); + aMakeShapeList.append(aCutAlgo.makeShape()); + aMapOfShapes.merge(aCutAlgo.mapOfShapes()); + } + } + } + + anObjects.clear(); + anObjects.push_back(aSolidsToFuse.back()); + aSolidsToFuse.pop_back(); + aTools = aSolidsToFuse; + // Fuse all objects and all tools. - GeomAlgoAPI_Boolean aBoolAlgo(aBooleanObjects, aBooleanTools, myBooleanOperationType); + GeomAlgoAPI_Boolean aFuseAlgo(anObjects, aTools, myBooleanOperationType); // Checking that the algorithm worked properly. - if(!aBoolAlgo.isDone() || aBoolAlgo.shape()->isNull() || !aBoolAlgo.isValid()) { - setError("Boolean algorithm failed"); + if(!aFuseAlgo.isDone() || aFuseAlgo.shape()->isNull() || !aFuseAlgo.isValid()) { + static const std::string aFeatureError = "Boolean algorithm failed"; + setError(aFeatureError); return; } + std::shared_ptr aShape = aFuseAlgo.shape(); + aMakeShapeList.append(aFuseAlgo.makeShape()); + aMapOfShapes.merge(aFuseAlgo.mapOfShapes()); + + // Add result to not used solids from compsolid (if we have any). + if(!aNotUsedSolids.empty()) { + aNotUsedSolids.push_back(aShape); + GeomAlgoAPI_PaveFiller aFillerAlgo(aNotUsedSolids, true); + if(!aFillerAlgo.isDone()) { + std::string aFeatureError = "PaveFiller algorithm failed"; + setError(aFeatureError); + return; + } + if(aFillerAlgo.shape()->isNull()) { + static const std::string aShapeError = "Resulting shape is Null"; + setError(aShapeError); + return; + } + if(!aFillerAlgo.isValid()) { + std::string aFeatureError = "Warning: resulting shape is not valid"; + setError(aFeatureError); + return; + } + + aShape = aFillerAlgo.shape(); + aMakeShapeList.append(aFillerAlgo.makeShape()); + aMapOfShapes.merge(aFillerAlgo.mapOfShapes()); + } + std::shared_ptr aResultBody = document()->createBody(data(), aResultIndex); - LoadNamingDS(aResultBody, aBooleanObjects.front(), aBooleanTools, aBoolAlgo); + loadNamingDS(aResultBody, aShells, aSolidsAlgos, anOriginalSolids.front(), anOriginalSolids, aShape, aMakeShapeList, aMapOfShapes); setResult(aResultBody, aResultIndex); aResultIndex++; break; @@ -179,30 +376,86 @@ void FeaturesPlugin_CompositeBoolean::execute() } //================================================================================================= -void FeaturesPlugin_CompositeBoolean::LoadNamingDS(std::shared_ptr theResultBody, - const std::shared_ptr& theBaseShape, +void FeaturesPlugin_CompositeBoolean::loadNamingDS(std::shared_ptr theResultBody, + const ListOfShape& theShells, + const std::list>& theSolidsAlgos, + const std::shared_ptr theBaseShape, const ListOfShape& theTools, - const GeomAlgoAPI_Boolean& theAlgo) + const std::shared_ptr theResultShape, + GeomAlgoAPI_MakeShape& theMakeShape, + GeomAPI_DataMapOfShapeShape& theMapOfShapes) { //load result - if(theBaseShape->isEqual(theAlgo.shape())) { - theResultBody->store(theAlgo.shape()); + if(theBaseShape->isEqual(theResultShape)) { + theResultBody->store(theResultShape); } else { - theResultBody->storeModified(theBaseShape, theAlgo.shape()); + const int aGenTag = 1; + const int aModTag = 2; + const int aDelTag = 3; + const int aSubsolidsTag=4; /// sub solids will be placed at labels 6, 7, etc. if result is compound of solids + int aToTag = 5; // may be many labels, starting from this index + int aFromTag = 10000; // may be many labels, starting from this index or last aToTag index + const std::string aGenName = "Generated"; + const std::string aModName = "Modified"; + const std::string aLatName = "LateralFace"; + const std::string aFromName = "FromFace"; + const std::string aToName = "ToFace"; - GeomAPI_DataMapOfShapeShape* aSubShapes = new GeomAPI_DataMapOfShapeShape(); + theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag); - const int aModTag = 1; - const int aDeleteTag = 2; - const std::string aModName = "Modified"; - theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, - aModTag, aModName, *theAlgo.mapOfShapes().get()); - theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), theBaseShape, GeomAPI_Shape::FACE, aDeleteTag); + ListOfShape::const_iterator aShellsIter = theShells.begin(); + std::list>::const_iterator aSolidsAlgosIter = theSolidsAlgos.begin(); + for(; aShellsIter != theShells.end() && aSolidsAlgosIter != theSolidsAlgos.end(); aShellsIter++, aSolidsAlgosIter++) { + ListOfShape aFromFaces; + ListOfShape aToFaces; + std::shared_ptr aSubShapes; + + //Insert lateral face : Face from Edge + if(std::dynamic_pointer_cast(*aSolidsAlgosIter)) { + std::shared_ptr aPrismAlgo = std::dynamic_pointer_cast(*aSolidsAlgosIter); + aSubShapes = aPrismAlgo->mapOfShapes(); + theResultBody->loadAndOrientGeneratedShapes(aPrismAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag, + aLatName, *aSubShapes.get()); + + aFromFaces = aPrismAlgo->fromFaces(); + aToFaces = aPrismAlgo->toFaces(); + } else if(std::dynamic_pointer_cast(*aSolidsAlgosIter)) { + std::shared_ptr aRevolAlgo = std::dynamic_pointer_cast(*aSolidsAlgosIter); + aSubShapes = aRevolAlgo->mapOfShapes(); + theResultBody->loadAndOrientGeneratedShapes(aRevolAlgo->makeShape().get(), *aShellsIter, GeomAPI_Shape::EDGE, aGenTag, + aLatName, *aSubShapes.get()); + aFromFaces = aRevolAlgo->fromFaces(); + aToFaces = aRevolAlgo->toFaces(); + } + + //Insert to faces + for(ListOfShape::const_iterator anIt = aToFaces.cbegin(); anIt != aToFaces.cend(); anIt++) { + std::shared_ptr aToFace = *anIt; + if(aSubShapes->isBound(aToFace)) { + aToFace = aSubShapes->find(aToFace); + } + theResultBody->generated(aToFace, aToName, aToTag++); + } + + //Insert from faces + if (aFromTag < aToTag) aFromTag = aToTag; + for(ListOfShape::const_iterator anIt = aFromFaces.cbegin(); anIt != aFromFaces.cend(); anIt++) { + std::shared_ptr aFromFace = *anIt; + if(aSubShapes->isBound(aFromFace)) { + aFromFace = aSubShapes->find(aFromFace); + } + theResultBody->generated(aFromFace, aFromName, aFromTag++); + } + } + + theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, + aModTag, aModName, theMapOfShapes); + theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE, aDelTag); for(ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++) { - theResultBody->loadAndOrientModifiedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, - aModTag, aModName, *theAlgo.mapOfShapes().get()); - theResultBody->loadDeletedShapes(theAlgo.makeShape().get(), *anIter, GeomAPI_Shape::FACE, aDeleteTag); + theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, + aModTag, aModName, theMapOfShapes); + theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDelTag); } } }