]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_RemoveSubShapes.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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
32 #include <GeomAlgoAPI_Copy.h>
33 #include <GeomAlgoAPI_ShapeBuilder.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35
36 //==================================================================================================
37 FeaturesPlugin_RemoveSubShapes::FeaturesPlugin_RemoveSubShapes()
38 {
39 }
40
41 //==================================================================================================
42 void FeaturesPlugin_RemoveSubShapes::initAttributes()
43 {
44   data()->addAttribute(BASE_SHAPE_ID(), ModelAPI_AttributeSelection::typeId());
45
46   data()->addAttribute(SUBSHAPES_ID(), ModelAPI_AttributeSelectionList::typeId());
47 }
48
49 void FeaturesPlugin_RemoveSubShapes::attributeChanged(const std::string& theID)
50 {
51   ModelAPI_Feature::attributeChanged(theID);
52
53   if(theID == BASE_SHAPE_ID()) {
54     AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
55     AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
56     if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
57       return;
58     }
59
60     aSubShapesAttrList->clear();
61
62     ResultPtr aContext = aShapeAttrSelection->context();
63     ResultCompSolidPtr aResultCompSolid =
64       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aContext);
65     if(!aResultCompSolid.get()) {
66       return;
67     }
68
69     GeomShapePtr aBaseShape = aShapeAttrSelection->value();
70     if(!aBaseShape.get()) {
71       aBaseShape = aContext->shape();
72     }
73     if(!aBaseShape.get()) {
74       return;
75     }
76     GeomAPI_Shape::ShapeType aShapeType = aBaseShape->shapeType();
77     if(aShapeType != GeomAPI_Shape::WIRE
78         && aShapeType != GeomAPI_Shape::SHELL
79         && aShapeType != GeomAPI_Shape::COMPSOLID
80         && aShapeType != GeomAPI_Shape::COMPOUND) {
81       return;
82     }
83     for(GeomAPI_ShapeIterator anIt(aBaseShape); anIt.more(); anIt.next()) {
84       GeomShapePtr aSubShape = anIt.current();
85       const int aNumOfSubs = aResultCompSolid->numberOfSubs();
86       if(aNumOfSubs == 0) {
87         aSubShapesAttrList->append(aContext, aSubShape);
88       } else {
89         for(int anIndex = 0; anIndex < aResultCompSolid->numberOfSubs(); ++anIndex) {
90           ResultBodyPtr aSubResult = aResultCompSolid->subResult(anIndex);
91           if(aSubResult->shape()->isEqual(aSubShape)) {
92             aSubShapesAttrList->append(aSubResult, aSubShape);
93             break;
94           }
95         }
96       }
97     }
98   }
99 }
100
101 //==================================================================================================
102 void FeaturesPlugin_RemoveSubShapes::execute()
103 {
104   // Get base shape and sub-shapes list.
105   AttributeSelectionPtr aShapeAttrSelection = selection(BASE_SHAPE_ID());
106   AttributeSelectionListPtr aSubShapesAttrList = selectionList(SUBSHAPES_ID());
107   if(!aShapeAttrSelection.get() || !aSubShapesAttrList.get()) {
108     return;
109   }
110
111   // Get base shape.
112   GeomShapePtr aBaseShape = aShapeAttrSelection->value();
113
114   GeomShapePtr aResultShape;
115   int aSubsNb = aSubShapesAttrList->size();
116   if(aSubsNb > 1) {
117     if(!aBaseShape.get()) {
118       return;
119     }
120     aResultShape = aBaseShape->emptyCopied();
121
122     // Copy sub-shapes from list to new shape.
123     for(int anIndex = 0; anIndex < aSubsNb; ++anIndex) {
124       AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(anIndex);
125       GeomShapePtr aShapeToAdd = anAttrSelectionInList->value();
126       GeomAlgoAPI_ShapeBuilder::add(aResultShape, aShapeToAdd);
127     }
128   } else if(aSubsNb == 1) {
129     AttributeSelectionPtr anAttrSelectionInList = aSubShapesAttrList->value(0);
130     aResultShape = anAttrSelectionInList->value();
131   }
132
133   GeomAlgoAPI_Copy aCopy(aResultShape);
134   aResultShape = aCopy.shape();
135
136   // Store result.
137   ResultBodyPtr aResultBody = document()->createBody(data());
138   aResultBody->storeModified(aBaseShape, aResultShape, 1);
139   aResultBody->loadAndOrientModifiedShapes(&aCopy,
140                                            aBaseShape,
141                                            GeomAPI_Shape::FACE,
142                                            10000,
143                                            "Modified_Face",
144                                            *aCopy.mapOfSubShapes().get(),
145                                            true);
146   setResult(aResultBody);
147 }