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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "FeaturesPlugin_RemoveSubShapes.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultCompSolid.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Validator.h>
29 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAlgoAPI_Copy.h>
32 #include <GeomAlgoAPI_ShapeBuilder.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
35 //==================================================================================================
36 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
40 //==================================================================================================
41 void FeaturesPlugin_RemoveSubShapes::initAttributes()
43 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
45 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
48 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
50 ModelAPI_Feature::attributeChanged(theID);
52 if(theID == BASE_SHAPE_ID()) {
53 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
54 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
55 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
59 aSubShapesAttrList->clear();
61 ResultPtr aContext = aShapeAttrSelection->context();
62 ResultCompSolidPtr aResultCompSolid =
63 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
64 if(!aResultCompSolid.get()) {
68 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
69 if(!aBaseShape.get()) {
70 aBaseShape = aContext->shape();
72 if(!aBaseShape.get()) {
75 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
76 if(aShapeType != GeomAPI_Shape::WIRE
77 && aShapeType != GeomAPI_Shape::SHELL
78 && aShapeType != GeomAPI_Shape::COMPSOLID
79 && aShapeType != GeomAPI_Shape::COMPOUND) {
82 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
83 GeomShapePtr aSubShape = anIt.current();
84 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
86 aSubShapesAttrList->append(aContext, aSubShape);
88 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
89 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
90 if(aSubResult->shape()->isEqual(aSubShape)) {
91 aSubShapesAttrList->append(aSubResult, aSubShape);
100 //==================================================================================================
101 void FeaturesPlugin_RemoveSubShapes::execute()
103 // Get base shape and sub-shapes list.
104 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
105 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
106 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
111 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
113 GeomShapePtr aResultShape;
114 int aSubsNb = aSubShapesAttrList->size();
116 if(!aBaseShape.get()) {
119 aResultShape = aBaseShape->emptyCopied();
121 // Copy sub-shapes from list to new shape.
122 for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
123 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
124 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
125 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
127 } else if(aSubsNb == 1) {
128 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
129 aResultShape = anAttrSelectionInList->value();
132 GeomAlgoAPI_Copy aCopy(aResultShape);
133 aResultShape = aCopy.shape();
136 ResultBodyPtr aResultBody = document()->createBody(data());
137 aResultBody->storeModified(aBaseShape, aResultShape, 1);
138 aResultBody->loadAndOrientModifiedShapes(&aCopy,
143 *aCopy.mapOfSubShapes().get(),
145 setResult(aResultBody);