]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Selection.cpp
Salome HOME
Issue #2358: Preserve state of folders on move in/out objects to a folder
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.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 "ModelHighAPI_Selection.h"
22
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeIntArray.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_ResultCompSolid.h>
29 //--------------------------------------------------------------------------------------
30
31 //--------------------------------------------------------------------------------------
32 ModelHighAPI_Selection::ModelHighAPI_Selection()
33 : myVariantType(VT_Empty)
34 {
35 }
36
37 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
38                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
39 : myVariantType(VT_ResultSubShapePair)
40 , myResultSubShapePair(theContext, theSubShape)
41 {
42 }
43
44 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
45                                                const std::string& theSubShapeName)
46 : myVariantType(VT_TypeSubShapeNamePair)
47 , myTypeSubShapeNamePair(theType, theSubShapeName)
48 {
49 }
50
51 ModelHighAPI_Selection::~ModelHighAPI_Selection()
52 {
53 }
54
55 //--------------------------------------------------------------------------------------
56 void ModelHighAPI_Selection::fillAttribute(
57     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
58 {
59   switch(myVariantType) {
60     case VT_Empty: return;
61     case VT_ResultSubShapePair:
62       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
63       return;
64     case VT_TypeSubShapeNamePair:
65       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
66       if(theAttribute->isInvalid()) {
67         FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
68         aFeature->setError(
69           std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
70       }
71       return;
72   }
73 }
74
75 //--------------------------------------------------------------------------------------
76 void ModelHighAPI_Selection::appendToList(
77     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
78 {
79   switch(myVariantType) {
80     case VT_Empty: return;
81     case VT_ResultSubShapePair:
82       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
83       return;
84     case VT_TypeSubShapeNamePair:
85       // Note: the reverse order (first - type, second - sub-shape name)
86       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
87       return;
88   }
89 }
90
91 //==================================================================================================
92 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
93 {
94   return myVariantType;
95 }
96
97 //==================================================================================================
98 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
99 {
100   return myResultSubShapePair;
101 }
102
103 //==================================================================================================
104 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
105 {
106   return myTypeSubShapeNamePair;
107 }
108
109 //==================================================================================================
110 std::string ModelHighAPI_Selection::shapeType() const
111 {
112   switch(myVariantType) {
113   case VT_ResultSubShapePair:
114     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
115                                                myResultSubShapePair.first->shape()->shapeTypeStr();
116   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
117   }
118
119   return "SHAPE";
120 }
121
122 //==================================================================================================
123 void ModelHighAPI_Selection::setName(const std::string& theName)
124 {
125   if (myVariantType == VT_ResultSubShapePair) {
126     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
127     if(!aResult.get()) {
128       return;
129     }
130     aResult->data()->setName(theName);
131   }
132 }
133
134 std::string ModelHighAPI_Selection::name() const
135 {
136   if (myVariantType == VT_ResultSubShapePair) {
137     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
138     if (aResult.get())
139       return aResult->data()->name();
140   }
141   return std::string();
142 }
143
144 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
145 {
146   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
147     return;
148
149   AttributeIntArrayPtr aColor =
150       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
151   aColor->setSize(3);
152   aColor->setValue(0, theRed);
153   aColor->setValue(1, theGreen);
154   aColor->setValue(2, theBlue);
155 }
156
157 void ModelHighAPI_Selection::setDeflection(double theValue)
158 {
159   if (myVariantType != VT_ResultSubShapePair)
160     return;
161
162   AttributeDoublePtr aDeflectionAttr =
163     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
164
165   aDeflectionAttr->setValue(theValue);
166 }
167
168 void ModelHighAPI_Selection::setTransparency(double theValue)
169 {
170   if (myVariantType != VT_ResultSubShapePair)
171     return;
172
173   AttributeDoublePtr aTransparencyAttr =
174     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
175
176   aTransparencyAttr->setValue(theValue);
177 }
178
179 int ModelHighAPI_Selection::numberOfSubs() const
180 {
181   if (myVariantType != VT_ResultSubShapePair)
182     return 0;
183
184   ResultCompSolidPtr aCompSolid =
185       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
186   if (!aCompSolid)
187     return 0;
188
189   return aCompSolid->numberOfSubs();
190 }
191
192 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
193 {
194   if (myVariantType != VT_ResultSubShapePair)
195     return ModelHighAPI_Selection();
196
197   ResultCompSolidPtr aCompSolid =
198       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
199   if (!aCompSolid)
200     return ModelHighAPI_Selection();
201
202   ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
203   return ModelHighAPI_Selection(aResult, aResult->shape());
204 }