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>
31 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAlgoAPI_Copy.h>
34 #include <GeomAlgoAPI_ShapeBuilder.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_MakeShapeCustom.h>
39 //==================================================================================================
40 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
44 //==================================================================================================
45 void FeaturesPlugin_RemoveSubShapes::initAttributes()
47 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
52 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
54 ModelAPI_Feature::attributeChanged(theID);
56 if(theID == BASE_SHAPE_ID()) {
57 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
58 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
59 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
63 aSubShapesAttrList->clear();
65 ResultPtr aContext = aShapeAttrSelection->context();
66 ResultCompSolidPtr aResultCompSolid =
67 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
68 if(!aResultCompSolid.get()) {
72 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
73 if(!aBaseShape.get()) {
74 aBaseShape = aContext->shape();
76 if(!aBaseShape.get()) {
79 GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
80 if(aShapeType != GeomAPI_Shape::WIRE
81 && aShapeType != GeomAPI_Shape::SHELL
82 && aShapeType != GeomAPI_Shape::COMPSOLID
83 && aShapeType != GeomAPI_Shape::COMPOUND) {
86 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
87 GeomShapePtr aSubShape = anIt.current();
88 const int aNumOfSubs = aResultCompSolid->numberOfSubs();
90 aSubShapesAttrList->append(aContext, aSubShape);
92 for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
93 ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
94 if(aSubResult->shape()->isEqual(aSubShape)) {
95 aSubShapesAttrList->append(aSubResult, aSubShape);
104 //==================================================================================================
105 void FeaturesPlugin_RemoveSubShapes::execute()
107 // Get base shape and sub-shapes list.
108 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
109 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
110 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
115 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
117 GeomShapePtr aResultShape;
118 int aSubsNb = aSubShapesAttrList->size();
120 if(!aBaseShape.get()) {
123 aResultShape = aBaseShape->emptyCopied();
125 // Copy sub-shapes from list to new shape.
126 for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
127 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
128 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
129 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
131 } else if(aSubsNb == 1) {
132 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
133 aResultShape = anAttrSelectionInList->value();
136 // find all removed shapes
137 GeomAlgoAPI_MakeShapeCustom aDeletedSubs;
138 std::set<GeomAPI_Shape::ShapeType> aTypes; // types that where removed
139 aTypes.insert(GeomAPI_Shape::FACE);
140 for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
141 if (!anIt.current().get() || anIt.current()->isNull())
144 for(anIndex = 0; anIndex < aSubsNb; ++anIndex) {
145 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
146 GeomShapePtr aLeftShape = anAttrSelectionInList->value();
147 if (anIt.current()->isEqual(aLeftShape)) {
148 break; // found in a left-list
151 if (anIndex == aSubsNb) { // not found in left
152 aDeletedSubs.addDeleted(anIt.current());
153 aTypes.insert(anIt.current()->shapeType());
154 if (anIt.current()->shapeType() != GeomAPI_Shape::FACE) {
155 GeomAPI_ShapeExplorer aFaces(anIt.current(), GeomAPI_Shape::FACE);
156 for(; aFaces.more(); aFaces.next())
157 aDeletedSubs.addDeleted(aFaces.current());
163 GeomAlgoAPI_Copy aCopy(aResultShape);
164 aResultShape = aCopy.shape();
167 ResultBodyPtr aResultBody = document()->createBody(data());
168 aResultBody->storeModified(aBaseShape, aResultShape, 1);
169 std::set<GeomAPI_Shape::ShapeType>::iterator aTypeIter = aTypes.begin();
170 for(; aTypeIter != aTypes.end(); aTypeIter++)
171 aResultBody->loadDeletedShapes(&aDeletedSubs, aBaseShape, *aTypeIter, 1);
172 aResultBody->loadAndOrientModifiedShapes(&aCopy,
177 *aCopy.mapOfSubShapes().get(),
179 setResult(aResultBody);