Salome HOME
Issue #2660: Update data model before save.
[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_ResultBody.h>
29
30
31 #include <GeomAPI_Pnt.h>
32 //--------------------------------------------------------------------------------------
33
34 //--------------------------------------------------------------------------------------
35 ModelHighAPI_Selection::ModelHighAPI_Selection()
36 : myVariantType(VT_Empty)
37 {
38 }
39
40 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
41                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
42 : myVariantType(VT_ResultSubShapePair)
43 , myResultSubShapePair(theContext, theSubShape)
44 {
45 }
46
47 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
48                                                const std::string& theSubShapeName)
49 : myVariantType(VT_TypeSubShapeNamePair)
50 , myTypeSubShapeNamePair(theType, theSubShapeName)
51 {
52 }
53
54 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
55                                                const GeomPointPtr& theSubShapeInnerPoint)
56 : myVariantType(VT_TypeInnerPointPair)
57 , myTypeInnerPointPair(theType, theSubShapeInnerPoint)
58 {
59 }
60
61 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
62   const std::string& theContextName, const int theIndex)
63   : myVariantType(VT_WeakNamingPair)
64   , myWeakNamingPair(theType, std::pair<std::string, int>(theContextName, theIndex))
65 {
66 }
67
68
69 ModelHighAPI_Selection::~ModelHighAPI_Selection()
70 {
71 }
72
73 //--------------------------------------------------------------------------------------
74 void ModelHighAPI_Selection::fillAttribute(
75     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
76 {
77   switch(myVariantType) {
78     case VT_Empty: return;
79     case VT_ResultSubShapePair:
80       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
81       return;
82     case VT_TypeSubShapeNamePair:
83       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
84       break;
85     case VT_TypeInnerPointPair:
86       theAttribute->selectSubShape(myTypeInnerPointPair.first, myTypeInnerPointPair.second);
87       return;
88     case VT_WeakNamingPair:
89       theAttribute->selectSubShape(
90         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
91       break;
92   }
93
94   if (theAttribute->isInvalid()) {
95     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
96     aFeature->setError(
97       std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
98   }
99 }
100
101 //--------------------------------------------------------------------------------------
102 void ModelHighAPI_Selection::appendToList(
103     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
104 {
105   switch(myVariantType) {
106     case VT_Empty: return;
107     case VT_ResultSubShapePair:
108       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
109       return;
110     case VT_TypeSubShapeNamePair:
111       // Note: the reverse order (first - type, second - sub-shape name)
112       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
113       return;
114     case VT_TypeInnerPointPair:
115       // Note: the reverse order (first - type, second - selected point)
116       theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
117       return;
118     case VT_WeakNamingPair:
119       // Note: the reverse order (first - type, second - selected point)
120       theAttribute->append(
121         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
122       return;
123   }
124 }
125
126 //==================================================================================================
127 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
128 {
129   return myVariantType;
130 }
131
132 //==================================================================================================
133 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
134 {
135   return myResultSubShapePair;
136 }
137
138 //==================================================================================================
139 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
140 {
141   return myTypeSubShapeNamePair;
142 }
143
144 //==================================================================================================
145 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
146 {
147   return myTypeInnerPointPair;
148 }
149
150 //==================================================================================================
151 std::string ModelHighAPI_Selection::shapeType() const
152 {
153   switch(myVariantType) {
154   case VT_ResultSubShapePair:
155     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
156                                                myResultSubShapePair.first->shape()->shapeTypeStr();
157   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
158   case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
159   }
160
161   return "SHAPE";
162 }
163
164 //==================================================================================================
165 void ModelHighAPI_Selection::setName(const std::string& theName)
166 {
167   if (myVariantType == VT_ResultSubShapePair) {
168     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
169     if(!aResult.get()) {
170       return;
171     }
172     aResult->data()->setName(theName);
173   }
174 }
175
176 std::string ModelHighAPI_Selection::name() const
177 {
178   if (myVariantType == VT_ResultSubShapePair) {
179     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
180     if (aResult.get())
181       return aResult->data()->name();
182   }
183   return std::string();
184 }
185
186 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
187 {
188   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
189     return;
190
191   AttributeIntArrayPtr aColor =
192       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
193   aColor->setSize(3);
194   aColor->setValue(0, theRed);
195   aColor->setValue(1, theGreen);
196   aColor->setValue(2, theBlue);
197 }
198
199 void ModelHighAPI_Selection::setDeflection(double theValue)
200 {
201   if (myVariantType != VT_ResultSubShapePair)
202     return;
203
204   AttributeDoublePtr aDeflectionAttr =
205     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
206
207   aDeflectionAttr->setValue(theValue);
208 }
209
210 void ModelHighAPI_Selection::setTransparency(double theValue)
211 {
212   if (myVariantType != VT_ResultSubShapePair)
213     return;
214
215   AttributeDoublePtr aTransparencyAttr =
216     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
217
218   aTransparencyAttr->setValue(theValue);
219 }
220
221 int ModelHighAPI_Selection::numberOfSubs() const
222 {
223   if (myVariantType != VT_ResultSubShapePair)
224     return 0;
225
226   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
227   if (!aBody.get())
228     return 0;
229
230   return aBody->numberOfSubs();
231 }
232
233 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
234 {
235   if (myVariantType != VT_ResultSubShapePair)
236     return ModelHighAPI_Selection();
237
238   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
239   if (!aBody)
240     return ModelHighAPI_Selection();
241   if (theIndex >= aBody->numberOfSubs())
242     return ModelHighAPI_Selection();
243
244   ResultBodyPtr aResult = aBody->subResult(theIndex);
245   return ModelHighAPI_Selection(aResult, aResult->shape());
246 }