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