]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp
Salome HOME
dbf4fc1ed059b17d9d6b768ebb3cfb0040376267
[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_ResultBody.h>
25 #include <ModelAPI_ResultCompSolid.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAPI_ShapeExplorer.h>
32
33 #include <GeomAlgoAPI_Copy.h>
34 #include <GeomAlgoAPI_ShapeBuilder.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_MakeShapeCustom.h>
37
38
39 //==================================================================================================
40 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
41 {
42 }
43
44 //==================================================================================================
45 void FeaturesPlugin_RemoveSubShapes::initAttributes()
46 {
47   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
48
49   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
50 }
51
52 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
53 {
54   ModelAPI_Feature::attributeChanged(theID);
55
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()) {
60       return;
61     }
62
63     aSubShapesAttrList->clear();
64
65     ResultPtr aContext = aShapeAttrSelection->context();
66     ResultCompSolidPtr aResultCompSolid =
67       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
68     if(!aResultCompSolid.get()) {
69       return;
70     }
71
72     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
73     if(!aBaseShape.get()) {
74       aBaseShape = aContext->shape();
75     }
76     if(!aBaseShape.get()) {
77       return;
78     }
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) {
84       return;
85     }
86     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
87       GeomShapePtr aSubShape = anIt.current();
88       const int aNumOfSubs = aResultCompSolid->numberOfSubs();
89       if(aNumOfSubs == 0) {
90         aSubShapesAttrList->append(aContext, aSubShape);
91       } else {
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);
96             break;
97           }
98         }
99       }
100     }
101   }
102 }
103
104 //==================================================================================================
105 void FeaturesPlugin_RemoveSubShapes::execute()
106 {
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()) {
111     return;
112   }
113
114   // Get base shape.
115   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
116
117   GeomShapePtr aResultShape;
118   int aSubsNb = aSubShapesAttrList->size();
119   if(aSubsNb > 1) {
120     if(!aBaseShape.get()) {
121       return;
122     }
123     aResultShape = aBaseShape->emptyCopied();
124
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);
130     }
131   } else if(aSubsNb == 1) {
132     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
133     aResultShape = anAttrSelectionInList->value();
134   }
135
136   // find all removed shapes
137   GeomAlgoAPI_MakeShapeCustom aDeletedSubs;
138   for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
139     if (!anIt.current().get() || anIt.current()->isNull())
140       continue;
141     int anIndex;
142     for(anIndex = 0; anIndex < aSubsNb; ++anIndex) {
143       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
144       GeomShapePtr aLeftShape = anAttrSelectionInList->value();
145       if (anIt.current()->isEqual(aLeftShape)) {
146         break; // found in a left-list
147       }
148     }
149     if (anIndex == aSubsNb) { // not found in left
150       GeomAPI_ShapeExplorer aFaces(anIt.current(), GeomAPI_Shape::FACE);
151       for(; aFaces.more(); aFaces.next())
152         aDeletedSubs.addDeleted(aFaces.current());
153     }
154   }
155
156
157   GeomAlgoAPI_Copy aCopy(aResultShape);
158   aResultShape = aCopy.shape();
159
160   // Store result.
161   ResultBodyPtr aResultBody = document()->createBody(data());
162   aResultBody->storeModified(aBaseShape, aResultShape, 1);
163   aResultBody->loadDeletedShapes(&aDeletedSubs, aBaseShape, GeomAPI_Shape::FACE, 1);
164   aResultBody->loadAndOrientModifiedShapes(&aCopy,
165                                            aBaseShape,
166                                            GeomAPI_Shape::FACE,
167                                            2,
168                                            "Modified_Face",
169                                            *aCopy.mapOfSubShapes().get(),
170                                            true);
171   setResult(aResultBody);
172 }