1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_RemoveSubShapes.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultCompSolid.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAlgoAPI_Copy.h>
33 #include <GeomAlgoAPI_ShapeBuilder.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
36 //==================================================================================================
37 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
41 //==================================================================================================
42 void FeaturesPlugin_RemoveSubShapes::initAttributes()
44 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
46 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
49 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
51 ModelAPI_Feature::attributeChanged(theID);
53 if(theID == BASE_SHAPE_ID()) {
54 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
55 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
56 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
60 aSubShapesAttrList->clear();
62 ResultPtr aContext = aShapeAttrSelection->context();
63 ResultCompSolidPtr aResultCompSolid =
64 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
65 if(!aResultCompSolid.get()) {
69 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
70 if(!aBaseShape.get()) {
71 aBaseShape = aContext->shape();
73 if(!aBaseShape.get()) {
76 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
77 if(aShapeType != GeomAPI_Shape::WIRE
78 && aShapeType != GeomAPI_Shape::SHELL
79 && aShapeType != GeomAPI_Shape::COMPSOLID
80 && aShapeType != GeomAPI_Shape::COMPOUND) {
83 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
84 GeomShapePtr aSubShape = anIt.current();
85 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
87 aSubShapesAttrList->append(aContext, aSubShape);
89 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
90 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
91 if(aSubResult->shape()->isEqual(aSubShape)) {
92 aSubShapesAttrList->append(aSubResult, aSubShape);
101 //==================================================================================================
102 void FeaturesPlugin_RemoveSubShapes::execute()
104 // Get base shape and sub-shapes list.
105 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
106 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
107 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
112 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
114 GeomShapePtr aResultShape;
115 int aSubsNb = aSubShapesAttrList->size();
117 if(!aBaseShape.get()) {
120 aResultShape = aBaseShape->emptyCopied();
122 // Copy sub-shapes from list to new shape.
123 for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
124 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
125 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
126 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
128 } else if(aSubsNb == 1) {
129 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
130 aResultShape = anAttrSelectionInList->value();
133 GeomAlgoAPI_Copy aCopy(aResultShape);
134 aResultShape = aCopy.shape();
137 ResultBodyPtr aResultBody = document()->createBody(data());
138 aResultBody->storeModified(aBaseShape, aResultShape, 1);
139 aResultBody->loadAndOrientModifiedShapes(&aCopy,
144 *aCopy.mapOfSubShapes().get(),
146 setResult(aResultBody);