Salome HOME
19e66b19c75cbd89d6c8e7243f4868c866c2aa74
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_RemoveSubShapes.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_RemoveSubShapes.h"
22
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>
30
31 #include <GeomAPI_ShapeIterator.h>
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <GeomAlgoAPI_Copy.h>
35 #include <GeomAlgoAPI_ShapeBuilder.h>
36 #include <GeomAlgoAPI_ShapeTools.h>
37 #include <GeomAlgoAPI_MakeShapeCustom.h>
38
39
40 //==================================================================================================
41 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
42 : myChangedInCode(false)
43 {
44 }
45
46 //==================================================================================================
47 void FeaturesPlugin_RemoveSubShapes::initAttributes()
48 {
49   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
50
51   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
52
53   data()->addAttribute(SUBSHAPES_TO_KEEP_ID(), ModelAPI_AttributeSelectionList::typeId());
54
55   data()->addAttribute(SUBSHAPES_TO_REMOVE_ID(), ModelAPI_AttributeSelectionList::typeId());
56 }
57
58 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
59 {
60   ModelAPI_Feature::attributeChanged(theID);
61
62   if (myChangedInCode) return;
63
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())
70   {
71     return;
72   }
73
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();
80     return;
81   }
82   const bool isHasSubs = ModelAPI_Tools::hasSubResults(aResultBody);
83
84   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
85   if(!aBaseShape.get()) {
86     aBaseShape = aContext->shape();
87   }
88
89   myChangedInCode = true;
90
91   if(theID == BASE_SHAPE_ID()) {
92     aSubShapesToKeepAttrList->clear();
93     aSubShapesToRemoveAttrList->clear();
94
95     if (!aBaseShape.get()) {
96       return;
97     }
98
99     std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
100     for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
101       GeomShapePtr aSubShape = *anIt;
102       if(!isHasSubs) {
103         aSubShapesToKeepAttrList->append(aContext, aSubShape);
104       } else {
105         std::list<ResultPtr> anAllSubs;
106         ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
107         std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
108         for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
109           ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
110           if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
111             aSubShapesToKeepAttrList->append(aSub, aSubShape);
112             break;
113           }
114         }
115       }
116     }
117   }
118   else if (theID == SUBSHAPES_TO_KEEP_ID())
119   {
120     aSubShapesToRemoveAttrList->clear();
121
122     if (!aBaseShape.get()) {
123       return;
124     }
125
126     int anIndex;
127     const int aSubsToKeepNb = aSubShapesToKeepAttrList->size();
128     std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
129     for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
130       GeomShapePtr aSubShape = *anIt;
131       for(anIndex = 0; anIndex < aSubsToKeepNb; ++anIndex) {
132         AttributeSelectionPtr anAttrSelectionInList = aSubShapesToKeepAttrList->value(anIndex);
133         GeomShapePtr aSubShapeToKeep = anAttrSelectionInList->value();
134         if (aSubShapeToKeep.get() && aSubShapeToKeep->isEqual(aSubShape)) {
135           break;
136         }
137       }
138
139       if (anIndex == aSubsToKeepNb) {
140         if(!isHasSubs) {
141           aSubShapesToRemoveAttrList->append(aContext, aSubShape);
142         } else {
143           std::list<ResultPtr> anAllSubs;
144           ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
145           std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
146           for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
147             ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
148             if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
149               aSubShapesToRemoveAttrList->append(aSub, aSubShape);
150               break;
151             }
152           }
153         }
154       }
155     }
156   }
157   else if (theID == SUBSHAPES_TO_REMOVE_ID())
158   {
159     aSubShapesToKeepAttrList->clear();
160
161     if (!aBaseShape.get()) {
162       return;
163     }
164
165     int anIndex;
166     const int aSubsToRemoveNb = aSubShapesToRemoveAttrList->size();
167     std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
168     for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
169       GeomShapePtr aSubShape = *anIt;
170       for(anIndex = 0; anIndex < aSubsToRemoveNb; ++anIndex) {
171         AttributeSelectionPtr anAttrSelectionInList = aSubShapesToRemoveAttrList->value(anIndex);
172         GeomShapePtr aSubShapeToRemove = anAttrSelectionInList->value();
173         if (aSubShapeToRemove.get() && aSubShapeToRemove->isEqual(aSubShape)) {
174           break;
175         }
176       }
177
178       if (anIndex == aSubsToRemoveNb) {
179         if(!isHasSubs) {
180           aSubShapesToKeepAttrList->append(aContext, aSubShape);
181         } else {
182           std::list<ResultPtr> anAllSubs;
183           ModelAPI_Tools::allSubs(aResultBody, anAllSubs);
184           std::list<ResultPtr>::iterator aSubsIt = anAllSubs.begin();
185           for(; aSubsIt != anAllSubs.end(); aSubsIt++) {
186             ResultBodyPtr aSub = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aSubsIt);
187             if (aSub && aSub->shape().get() && aSub->shape()->isSubShape(aSubShape)) {
188               aSubShapesToKeepAttrList->append(aSub, aSubShape);
189               break;
190             }
191           }
192         }
193       }
194     }
195   }
196
197   myChangedInCode = false;
198 }
199
200 //==================================================================================================
201 void FeaturesPlugin_RemoveSubShapes::execute()
202 {
203   // Get base shape and sub-shapes list.
204   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
205   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_TO_KEEP_ID());
206   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
207     return;
208   }
209
210   // Get base shape.
211   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
212
213   GeomShapePtr aResultShape;
214   int aSubsNb = aSubShapesAttrList->size();
215   if(aSubsNb > 1) {
216     if(!aBaseShape.get()) {
217       return;
218     }
219     aResultShape = aBaseShape->emptyCopied();
220
221     // Copy sub-shapes from list to new shape.
222     for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
223       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
224       GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
225       GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
226     }
227   } else if(aSubsNb == 1) {
228     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
229     aResultShape = anAttrSelectionInList->value();
230   }
231   // deleted and copied must be jointed to one list which keeps all the history
232   GeomAlgoAPI_MakeShapeList aMakeShapeList;
233
234   // find all removed shapes
235   std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aDeletedSubs(new GeomAlgoAPI_MakeShapeCustom);
236   std::set<GeomAPI_Shape::ShapeType> aTypes; // types that where removed
237   aTypes.insert(GeomAPI_Shape::FACE);
238
239   std::list<GeomShapePtr> aSubShapes = GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(aBaseShape);
240   for (ListOfShape::const_iterator anIt = aSubShapes.cbegin(); anIt != aSubShapes.cend(); ++anIt) {
241     GeomShapePtr aSubShape = *anIt;
242     if (!aSubShape.get() || aSubShape->isNull())
243       continue;
244
245     int anIndex;
246     for(anIndex = 0; anIndex < aSubsNb; ++anIndex) {
247       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
248       GeomShapePtr aLeftShape = anAttrSelectionInList->value();
249       if (aSubShape->isEqual(aLeftShape)) {
250         break; // found in a left-list
251       }
252     }
253     if (anIndex == aSubsNb) { // not found in left
254       aDeletedSubs->addDeleted(aSubShape);
255       aTypes.insert(aSubShape->shapeType());
256       if (aSubShape->shapeType() != GeomAPI_Shape::FACE) {
257         GeomAPI_ShapeExplorer aFaces(aSubShape, GeomAPI_Shape::FACE);
258         for(; aFaces.more(); aFaces.next())
259           aDeletedSubs->addDeleted(aFaces.current());
260       }
261     }
262   }
263   aMakeShapeList.appendAlgo(aDeletedSubs);
264
265   std::shared_ptr<GeomAlgoAPI_Copy> aCopy(new GeomAlgoAPI_Copy(aResultShape));
266   aResultShape = aCopy->shape();
267   aMakeShapeList.appendAlgo(aCopy);
268
269   // Store result.
270   ResultBodyPtr aResultBody = document()->createBody(data());
271   aResultBody->storeModified(aBaseShape, aResultShape, 1);
272   std::set<GeomAPI_Shape::ShapeType>::iterator aTypeIter = aTypes.begin();
273   for(; aTypeIter != aTypes.end(); aTypeIter++)
274     aResultBody->loadDeletedShapes(&aMakeShapeList, aBaseShape, *aTypeIter, 1);
275   aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::FACE,
276           2, "Modified_Face", *aMakeShapeList.mapOfSubShapes().get(), true, false, true);
277   aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::EDGE,
278           3, "Modified_Edge", *aMakeShapeList.mapOfSubShapes().get(), false, false, true);
279   aResultBody->loadAndOrientModifiedShapes(&aMakeShapeList, aBaseShape, GeomAPI_Shape::VERTEX,
280           4, "Modified_Vertex", *aMakeShapeList.mapOfSubShapes().get());
281   setResult(aResultBody);
282 }