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_AttributeString.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAlgoAPI_Copy.h>
35 #include <GeomAlgoAPI_ShapeBuilder.h>
36 #include <GeomAlgoAPI_ShapeTools.h>
37 #include <GeomAlgoAPI_MakeShapeCustom.h>
40 //==================================================================================================
41 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
42 : myChangedInCode(false)
46 //==================================================================================================
47 void FeaturesPlugin_RemoveSubShapes::initAttributes()
49 data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
53 data()->addAttribute(SUBSHAPES_TO_KEEP_ID(), ModelAPI_AttributeSelectionList::typeId());
55 data()->addAttribute(SUBSHAPES_TO_REMOVE_ID(), ModelAPI_AttributeSelectionList::typeId());
58 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
60 ModelAPI_Feature::attributeChanged(theID);
62 if (myChangedInCode) return;
64 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
65 AttributeSelectionListPtr aSubShapesToKeepAttrList = selectionList(SUBSHAPES_TO_KEEP_ID());
66 AttributeSelectionListPtr aSubShapesToRemoveAttrList = selectionList(SUBSHAPES_TO_REMOVE_ID());
67 if (!aShapeAttrSelection.get()
68 || !aSubShapesToKeepAttrList.get()
69 || !aSubShapesToRemoveAttrList.get())
74 ResultPtr aContext = aShapeAttrSelection->context();
75 ResultBodyPtr aResultBody =
76 std::dynamic_pointer_cast<ModelAPI_ResultBody>(aContext);
77 if (!aResultBody.get()) {
78 aSubShapesToKeepAttrList->clear();
79 aSubShapesToRemoveAttrList->clear();
82 const bool isHasSubs = ModelAPI_Tools::hasSubResults(aResultBody);
84 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
85 if(!aBaseShape.get()) {
86 aBaseShape = aContext->shape();
89 myChangedInCode = true;
91 if(theID == BASE_SHAPE_ID()) {
92 aSubShapesToKeepAttrList->clear();
93 aSubShapesToRemoveAttrList->clear();
95 if (!aBaseShape.get()) {
99 std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
100 for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt)
102 GeomShapePtr aSubShape = *anIt;
104 aSubShapesToKeepAttrList->append(aContext, aSubShape);
106 std::list<ResultPtr> anAllSubs;
107 ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
108 std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
109 for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
110 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
111 if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
112 aSubShapesToKeepAttrList->append(aSub, aSubShape);
119 else if (theID == SUBSHAPES_TO_KEEP_ID())
121 aSubShapesToRemoveAttrList->clear();
123 if (!aBaseShape.get()) {
128 const int aSubsToKeepNb = aSubShapesToKeepAttrList->size();
129 std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
130 for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt)
132 GeomShapePtr aSubShape = *anIt;
133 for(anIndex = 0; anIndex < aSubsToKeepNb; ++anIndex) {
134 AttributeSelectionPtr anAttrSelectionInList = aSubShapesToKeepAttrList->value(anIndex);
135 GeomShapePtr aSubShapeToKeep = anAttrSelectionInList->value();
136 if (aSubShapeToKeep.get() && aSubShapeToKeep->isEqual(aSubShape)) {
141 if (anIndex == aSubsToKeepNb) {
143 aSubShapesToRemoveAttrList->append(aContext, aSubShape);
145 std::list<ResultPtr> anAllSubs;
146 ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
147 std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
148 for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
149 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
150 if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
151 aSubShapesToRemoveAttrList->append(aSub, aSubShape);
159 else if (theID == SUBSHAPES_TO_REMOVE_ID())
161 aSubShapesToKeepAttrList->clear();
163 if (!aBaseShape.get()) {
168 const int aSubsToRemoveNb = aSubShapesToRemoveAttrList->size();
169 std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
170 for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt)
172 GeomShapePtr aSubShape = *anIt;
173 for(anIndex = 0; anIndex < aSubsToRemoveNb; ++anIndex) {
174 AttributeSelectionPtr anAttrSelectionInList = aSubShapesToRemoveAttrList->value(anIndex);
175 GeomShapePtr aSubShapeToRemove = anAttrSelectionInList->value();
176 if (aSubShapeToRemove.get() && aSubShapeToRemove->isEqual(aSubShape)) {
181 if (anIndex == aSubsToRemoveNb) {
183 aSubShapesToKeepAttrList->append(aContext, aSubShape);
185 std::list<ResultPtr> anAllSubs;
186 ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
187 std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
188 for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
189 ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
190 if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
191 aSubShapesToKeepAttrList->append(aSub, aSubShape);
200 myChangedInCode = false;
203 //==================================================================================================
204 void FeaturesPlugin_RemoveSubShapes::execute()
206 // Get base shape and sub-shapes list.
207 AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
208 AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_TO_KEEP_ID());
209 if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
214 GeomShapePtr aBaseShape = aShapeAttrSelection->value();
216 GeomShapePtr aResultShape;
217 int aSubsNb = aSubShapesAttrList->size();
219 if(!aBaseShape.get()) {
222 aResultShape = aBaseShape->emptyCopied();
224 // Copy sub-shapes from list to new shape.
225 for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
226 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
227 GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
228 GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
230 } else if(aSubsNb == 1) {
231 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
232 aResultShape = anAttrSelectionInList->value();
234 // deleted and copied must be jointed to one list which keeps all the history
235 GeomAlgoAPI_MakeShapeList aMakeShapeList;
237 // find all removed shapes
238 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aDeletedSubs(new GeomAlgoAPI_MakeShapeCustom);
239 std::set<GeomAPI_Shape::ShapeType> aTypes; // types that where removed
240 aTypes.insert(GeomAPI_Shape::FACE);
242 std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
243 for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
244 GeomShapePtr aSubShape = *anIt;
245 if (!aSubShape.get() || aSubShape->isNull())
249 for(anIndex = 0; anIndex < aSubsNb; ++anIndex) {
250 AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
251 GeomShapePtr aLeftShape = anAttrSelectionInList->value();
252 if (aSubShape->isEqual(aLeftShape)) {
253 break; // found in a left-list
256 if (anIndex == aSubsNb) { // not found in left
257 aDeletedSubs->addDeleted(aSubShape);
258 aTypes.insert(aSubShape->shapeType());
259 if (aSubShape->shapeType() != GeomAPI_Shape::FACE) {
260 GeomAPI_ShapeExplorer aFaces(aSubShape, GeomAPI_Shape::FACE);
261 for(; aFaces.more(); aFaces.next())
262 aDeletedSubs->addDeleted(aFaces.current());
266 aMakeShapeList.appendAlgo(aDeletedSubs);
268 std::shared_ptr<GeomAlgoAPI_Copy> aCopy(new GeomAlgoAPI_Copy(aResultShape));
269 aResultShape = aCopy->shape();
270 aMakeShapeList.appendAlgo(aCopy);
273 ResultBodyPtr aResultBody = document()->createBody(data());
274 aResultBody->storeModified(aBaseShape, aResultShape, 1);
275 std::set<GeomAPI_Shape::ShapeType>::iterator aTypeIter = aTypes.begin();
276 for(; aTypeIter != aTypes.end(); aTypeIter++)
277 aResultBody->loadDeletedShapes(&aMakeShapeList, aBaseShape, *aTypeIter, 1);
278 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::FACE,
279 2, "Modified_Face", *aMakeShapeList.mapOfSubShapes().get(), true, false, true);
280 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::EDGE,
281 3, "Modified_Edge", *aMakeShapeList.mapOfSubShapes().get(), false, false, true);
282 aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::VERTEX,
283 4, "Modified_Vertex", *aMakeShapeList.mapOfSubShapes().get());
284 setResult(aResultBody);