From f2f34e30b41047d12277ad87e1adf6c0e410e4ff Mon Sep 17 00:00:00 2001 From: mzn Date: Fri, 31 Aug 2018 14:01:54 +0300 Subject: [PATCH] Issue #2578: EDF 2018-2 Removal of faces. --- src/FeaturesPlugin/CMakeLists.txt | 1 + .../FeaturesPlugin_RemoveSubShapes.cpp | 75 +++++++++------- .../FeaturesPlugin_Validators.cpp | 7 +- .../Test/TestRemoveSubShapes3.py | 86 +++++++++++++++++++ src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp | 23 +++++ src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.h | 5 ++ src/ModelAPI/Test/Test2228.py | 15 +++- 7 files changed, 176 insertions(+), 36 deletions(-) create mode 100644 src/FeaturesPlugin/Test/TestRemoveSubShapes3.py diff --git a/src/FeaturesPlugin/CMakeLists.txt b/src/FeaturesPlugin/CMakeLists.txt index c173fe22a..ccc359c3c 100644 --- a/src/FeaturesPlugin/CMakeLists.txt +++ b/src/FeaturesPlugin/CMakeLists.txt @@ -190,6 +190,7 @@ ADD_UNIT_TESTS(TestExtrusion.py TestUnionFaces.py TestRemoveSubShapes.py TestRemoveSubShapes2.py + TestRemoveSubShapes3.py TestPipe.py TestRecover.py TestRecover1798.py diff --git a/src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp b/src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp index 75b1d9c09..19e66b19c 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -78,7 +79,7 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) aSubShapesToRemoveAttrList->clear(); return; } - const int aNumOfSubs = aResultBody->numberOfSubs(); + const bool isHasSubs = ModelAPI_Tools::hasSubResults(aResultBody); GeomShapePtr aBaseShape = aShapeAttrSelection->value(); if(!aBaseShape.get()) { @@ -95,15 +96,19 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) return; } - for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) { - GeomShapePtr aSubShape = anIt.current(); - if(aNumOfSubs == 0) { + std::list aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape); + for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) { + GeomShapePtr aSubShape = *anIt; + if(!isHasSubs) { aSubShapesToKeepAttrList->append(aContext, aSubShape); } else { - for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) { - ResultBodyPtr aSubResult = aResultBody->subResult(anIndex); - if(aSubResult->shape()->isEqual(aSubShape)) { - aSubShapesToKeepAttrList->append(aSubResult, aSubShape); + std::list anAllSubs; + ModelAPI_Tools::allSubs(aResultBody, anAllSubs); + std::list::iterator aSubsIt = anAllSubs.begin(); + for(; aSubsIt != anAllSubs.end(); aSubsIt++) { + ResultBodyPtr aSub = std::dynamic_pointer_cast(*aSubsIt); + if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) { + aSubShapesToKeepAttrList->append(aSub, aSubShape); break; } } @@ -120,8 +125,9 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) int anIndex; const int aSubsToKeepNb = aSubShapesToKeepAttrList->size(); - for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) { - GeomShapePtr aSubShape = anIt.current(); + std::list aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape); + for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) { + GeomShapePtr aSubShape = *anIt; for(anIndex = 0; anIndex < aSubsToKeepNb; ++anIndex) { AttributeSelectionPtr anAttrSelectionInList = aSubShapesToKeepAttrList->value(anIndex); GeomShapePtr aSubShapeToKeep = anAttrSelectionInList->value(); @@ -131,13 +137,16 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) } if (anIndex == aSubsToKeepNb) { - if(aNumOfSubs == 0) { + if(!isHasSubs) { aSubShapesToRemoveAttrList->append(aContext, aSubShape); } else { - for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) { - ResultBodyPtr aSubResult = aResultBody->subResult(anIndex); - if(aSubResult->shape()->isEqual(aSubShape)) { - aSubShapesToRemoveAttrList->append(aSubResult, aSubShape); + std::list anAllSubs; + ModelAPI_Tools::allSubs(aResultBody, anAllSubs); + std::list::iterator aSubsIt = anAllSubs.begin(); + for(; aSubsIt != anAllSubs.end(); aSubsIt++) { + ResultBodyPtr aSub = std::dynamic_pointer_cast(*aSubsIt); + if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) { + aSubShapesToRemoveAttrList->append(aSub, aSubShape); break; } } @@ -155,8 +164,9 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) int anIndex; const int aSubsToRemoveNb = aSubShapesToRemoveAttrList->size(); - for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) { - GeomShapePtr aSubShape = anIt.current(); + std::list aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape); + for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) { + GeomShapePtr aSubShape = *anIt; for(anIndex = 0; anIndex < aSubsToRemoveNb; ++anIndex) { AttributeSelectionPtr anAttrSelectionInList = aSubShapesToRemoveAttrList->value(anIndex); GeomShapePtr aSubShapeToRemove = anAttrSelectionInList->value(); @@ -166,13 +176,16 @@ void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID) } if (anIndex == aSubsToRemoveNb) { - if(aNumOfSubs == 0) { + if(!isHasSubs) { aSubShapesToKeepAttrList->append(aContext, aSubShape); } else { - for (int anIndex = 0; anIndex < aResultBody->numberOfSubs(); ++anIndex) { - ResultBodyPtr aSubResult = aResultBody->subResult(anIndex); - if(aSubResult->shape()->isEqual(aSubShape)) { - aSubShapesToKeepAttrList->append(aSubResult, aSubShape); + std::list anAllSubs; + ModelAPI_Tools::allSubs(aResultBody, anAllSubs); + std::list::iterator aSubsIt = anAllSubs.begin(); + for(; aSubsIt != anAllSubs.end(); aSubsIt++) { + ResultBodyPtr aSub = std::dynamic_pointer_cast(*aSubsIt); + if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) { + aSubShapesToKeepAttrList->append(aSub, aSubShape); break; } } @@ -222,22 +235,26 @@ void FeaturesPlugin_RemoveSubShapes::execute() std::shared_ptr aDeletedSubs(new GeomAlgoAPI_MakeShapeCustom); std::set aTypes; // types that where removed aTypes.insert(GeomAPI_Shape::FACE); - for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) { - if (!anIt.current().get() || anIt.current()->isNull()) + + std::list aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape); + for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) { + GeomShapePtr aSubShape = *anIt; + if (!aSubShape.get() || aSubShape->isNull()) continue; + int anIndex; for(anIndex = 0; anIndex < aSubsNb; ++anIndex) { AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex); GeomShapePtr aLeftShape = anAttrSelectionInList->value(); - if (anIt.current()->isEqual(aLeftShape)) { + if (aSubShape->isEqual(aLeftShape)) { break; // found in a left-list } } if (anIndex == aSubsNb) { // not found in left - aDeletedSubs->addDeleted(anIt.current()); - aTypes.insert(anIt.current()->shapeType()); - if (anIt.current()->shapeType() != GeomAPI_Shape::FACE) { - GeomAPI_ShapeExplorer aFaces(anIt.current(), GeomAPI_Shape::FACE); + aDeletedSubs->addDeleted(aSubShape); + aTypes.insert(aSubShape->shapeType()); + if (aSubShape->shapeType() != GeomAPI_Shape::FACE) { + GeomAPI_ShapeExplorer aFaces(aSubShape, GeomAPI_Shape::FACE); for(; aFaces.more(); aFaces.next()) aDeletedSubs->addDeleted(aFaces.current()); } diff --git a/src/FeaturesPlugin/FeaturesPlugin_Validators.cpp b/src/FeaturesPlugin/FeaturesPlugin_Validators.cpp index 9dfd94eaa..d44710409 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Validators.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Validators.cpp @@ -805,17 +805,18 @@ bool FeaturesPlugin_ValidatorRemoveSubShapesSelection::isValid(const AttributePt return false; } + std::list aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape); for(int anIndex = 0; anIndex < aSubShapesAttrList->size(); ++anIndex) { bool isSameFound = false; AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex); GeomShapePtr aShapeToAdd = anAttrSelectionInList->value(); - for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) { - if(anIt.current()->isEqual(aShapeToAdd)) { + for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) { + if ((*anIt)->isEqual(aShapeToAdd)) { isSameFound = true; break; } } - if(!isSameFound) { + if (!isSameFound) { theError = "Error: Only sub-shapes of selected shape is allowed for selection."; return false; } diff --git a/src/FeaturesPlugin/Test/TestRemoveSubShapes3.py b/src/FeaturesPlugin/Test/TestRemoveSubShapes3.py new file mode 100644 index 000000000..c7f7d6384 --- /dev/null +++ b/src/FeaturesPlugin/Test/TestRemoveSubShapes3.py @@ -0,0 +1,86 @@ +## Copyright (C) 2014-2017 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 +## + +from salome.shaper import model + +model.begin() +partSet = model.moduleDocument() + +# ============================================================================= +# Test 1. Remove face from shell +# ============================================================================= +Part_1 = model.addPart(partSet) +Part_1_doc = Part_1.document() + +Cylinder_1 = model.addCylinder(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 5, 10) +Face_1 = model.addFace(Part_1_doc, [model.selection("FACE", "Cylinder_1_1/Face_1")]) +Face_2 = model.addFace(Part_1_doc, [model.selection("FACE", "Cylinder_1_1/Face_1")]) +Translation_1 = model.addTranslation(Part_1_doc, [model.selection("FACE", "Face_2_1")], model.selection("EDGE", "PartSet/OZ"), 10) +Shell_1 = model.addShell(Part_1_doc, [model.selection("FACE", "Translation_1_1"), model.selection("FACE", "Face_1_1")]) + +Remove_SubShapes_1 = model.addRemoveSubShapes(Part_1_doc, model.selection("SHELL", "Shell_1_1")) +Remove_SubShapes_1.setSubShapesToRemove([model.selection("FACE", "Shell_1_1/Modified_Face_2")]) +model.do() + +model.checkResult(Remove_SubShapes_1, model, 1, [0], [0], [1], [4], [8]) +model.testHaveNamingSubshapes(Remove_SubShapes_1, model, Part_1_doc) + +# ============================================================================= +# Test 2. Remove faces from compound +# ============================================================================= +Part_2 = model.addPart(partSet) +Part_2_doc = Part_2.document() + +Cylinder_1 = model.addCylinder(Part_2_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 5, 10) +Face_1 = model.addFace(Part_2_doc, [model.selection("FACE", "Cylinder_1_1/Face_1")]) +LinearCopy_1 = model.addMultiTranslation(Part_2_doc, [model.selection("FACE", "Face_1_1")], model.selection("EDGE", "PartSet/OZ"), 10, 2) +LinearCopy_2 = model.addMultiTranslation(Part_2_doc, [model.selection("COMPOUND", "LinearCopy_1_1")], model.selection("EDGE", "PartSet/OZ"), 20, 2) + +Remove_SubShapes_2 = model.addRemoveSubShapes(Part_2_doc, model.selection("COMPOUND", "LinearCopy_2_1")) +Remove_SubShapes_2.setSubShapesToRemove([model.selection("FACE", "LinearCopy_2_1_2_2"), model.selection("FACE", "LinearCopy_2_1_2_1")]) +model.do() + +model.checkResult(Remove_SubShapes_2, model, 1, [2], [0], [2], [8], [16]) +model.testHaveNamingSubshapes(Remove_SubShapes_2, model, Part_2_doc) + +# ============================================================================= +# Test 3. Remove solid from compsolid +# ============================================================================= +Part_3 = model.addPart(partSet) +Part_3_doc = Part_3.document() + +Box_1 = model.addBox(Part_3_doc, 10, 10, 10) +LinearCopy_1 = model.addMultiTranslation(Part_3_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OY"), 10, 6) +CompSolid_1_objects = [model.selection("SOLID", "LinearCopy_1_1_1"), model.selection("SOLID", "LinearCopy_1_1_2"), model.selection("SOLID", "LinearCopy_1_1_3"), model.selection("SOLID", "LinearCopy_1_1_4"), model.selection("SOLID", "LinearCopy_1_1_5"), model.selection("SOLID", "LinearCopy_1_1_6")] +CompSolid_1 = model.addCompSolid(Part_3_doc, CompSolid_1_objects) + +Remove_SubShapes_3_objects = [model.selection("SOLID", "CompSolid_1_1_3"), model.selection("SOLID", "CompSolid_1_1_4")] +Remove_SubShapes_3 = model.addRemoveSubShapes(Part_3_doc, model.selection("COMPSOLID", "CompSolid_1_1")) +Remove_SubShapes_3.setSubShapesToRemove(Remove_SubShapes_3_objects) +model.do() + +model.checkResult(Remove_SubShapes_3, model, 1, [4], [4], [24], [96], [192]) +model.testHaveNamingSubshapes(Remove_SubShapes_3, model, Part_3_doc) + +# ============================================================================= +# Test 4. Check Python dump +# ============================================================================= +model.end() +assert(model.checkPythonDump()) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp index d39fd6030..a47c77c7f 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -1064,3 +1065,25 @@ std::shared_ptr GeomAlgoAPI_ShapeTools::wireToEdge( } return anEdge; } + +ListOfShape GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(const GeomShapePtr& theShape) +{ + ListOfShape aSubShapes; + + if (!theShape->isCompound() && !theShape->isCompSolid() && + !theShape->isShell() && !theShape->isWire()) { + return aSubShapes; + } + + for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) { + GeomShapePtr aSubShape = anIt.current(); + if (aSubShape->isVertex() || aSubShape->isEdge() || + aSubShape->isFace() || aSubShape->isSolid()) { + aSubShapes.push_back(aSubShape); + } else { + aSubShapes.splice(aSubShapes.end(), getLowLevelSubShapes(aSubShape)); + } + } + + return aSubShapes; +} \ No newline at end of file diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.h b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.h index a9211e397..af2a81f5c 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.h @@ -187,6 +187,11 @@ public: /// \brief Reapproximate a wire to build a single edge GEOMALGOAPI_EXPORT static std::shared_ptr wireToEdge( const std::shared_ptr& theWire); + + /// \brief Get non-composite sub-shapes of the given shape. + /// \param[in] theShape shape that should be exploded + /// \return list of sub-shapes (vertices, edges, faces, solids) + GEOMALGOAPI_EXPORT static ListOfShape getLowLevelSubShapes(const GeomShapePtr& theShape); }; #endif diff --git a/src/ModelAPI/Test/Test2228.py b/src/ModelAPI/Test/Test2228.py index 46c17b094..1da5212a8 100644 --- a/src/ModelAPI/Test/Test2228.py +++ b/src/ModelAPI/Test/Test2228.py @@ -58,14 +58,21 @@ Face_1 = model.addFace(Part_1_doc, [model.selection("EDGE", "Sketch_2/Edge-Sketc Extrusion_4 = model.addExtrusion(Part_1_doc, [model.selection("WIRE", "Sketch_1/Wire-SketchCircle_2_2f")], model.selection(), model.selection("FACE", "Extrusion_2_1/From_Face_1"), 0, model.selection(), 0) Boolean_2 = model.addCut(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], [model.selection("SOLID", "Extrusion_4_1")]) Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Cut_1_1"), model.selection("SOLID", "Cut_2_1"), model.selection("FACE", "Face_1_1")]) +Remove_SubShapes_1_objects = [model.selection("SOLID", "Partition_1_1_1_1"), model.selection("SOLID", "Partition_1_1_1_2"), model.selection("SOLID", "Partition_1_1_1_3"), model.selection("SOLID", "Partition_1_1_1_4")] Remove_SubShapes_1 = model.addRemoveSubShapes(Part_1_doc, model.selection("COMPOUND", "Partition_1_1")) -Remove_SubShapes_1.setSubShapesToKeep([model.selection("COMPSOLID", "Partition_1_1_1")]) -model.end() +Remove_SubShapes_1.setSubShapesToKeep(Remove_SubShapes_1_objects) + +model.end() # check that remove sub-shapes contains correct selection from ModelAPI import * aFactory = ModelAPI_Session.get().validators() assert(aFactory.validate(Remove_SubShapes_1.feature())) -assert(Remove_SubShapes_1.subshapesToKeep().size() == 1) -assert(Remove_SubShapes_1.subshapesToKeep().value(0).namingName() == "Partition_1_1_1") +assert(Remove_SubShapes_1.subshapesToKeep().size() == 4) +assert(Remove_SubShapes_1.subshapesToKeep().value(0).namingName() == "Partition_1_1_1_1") +assert(Remove_SubShapes_1.subshapesToKeep().value(1).namingName() == "Partition_1_1_1_2") +assert(Remove_SubShapes_1.subshapesToKeep().value(2).namingName() == "Partition_1_1_1_3") +assert(Remove_SubShapes_1.subshapesToKeep().value(3).namingName() == "Partition_1_1_1_4") +assert(Remove_SubShapes_1.subshapesToRemove().size() == 1) +assert(Remove_SubShapes_1.subshapesToRemove().value(0).namingName() == "Partition_1_1_2") -- 2.39.2