Salome HOME
Fix for the issue #2753 : error when dump/load script
[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::list<double>& theSubShapeInnerPoint)
63 : myVariantType(VT_TypeInnerPointPair)
64 {
65   double aCoordinates[3] = { 0.0, 0.0, 0.0 };
66   double* aCIt = aCoordinates;
67   std::list<double>::const_iterator aPIt = theSubShapeInnerPoint.begin();
68   for (; aPIt != theSubShapeInnerPoint.end(); ++aPIt, ++aCIt)
69     *aCIt = *aPIt;
70
71   GeomPointPtr anInnerPoint(new GeomAPI_Pnt(aCoordinates[0], aCoordinates[1], aCoordinates[2]));
72   myTypeInnerPointPair = std::pair<std::string, GeomPointPtr>(theType, anInnerPoint);
73 }
74
75 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
76   const std::string& theContextName, const int theIndex)
77   : myVariantType(VT_WeakNamingPair)
78   , myWeakNamingPair(theType, std::pair<std::string, int>(theContextName, theIndex))
79 {
80 }
81
82
83 ModelHighAPI_Selection::~ModelHighAPI_Selection()
84 {
85 }
86
87 //--------------------------------------------------------------------------------------
88 void ModelHighAPI_Selection::fillAttribute(
89     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
90 {
91   switch(myVariantType) {
92     case VT_Empty: return;
93     case VT_ResultSubShapePair:
94       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
95       return;
96     case VT_TypeSubShapeNamePair:
97       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
98       break;
99     case VT_TypeInnerPointPair:
100       theAttribute->selectSubShape(myTypeInnerPointPair.first, myTypeInnerPointPair.second);
101       return;
102     case VT_WeakNamingPair:
103       theAttribute->selectSubShape(
104         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
105       break;
106   }
107
108   if (theAttribute->isInvalid()) {
109     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
110     aFeature->setError(
111       std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
112   }
113 }
114
115 //--------------------------------------------------------------------------------------
116 void ModelHighAPI_Selection::appendToList(
117     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
118 {
119   switch(myVariantType) {
120     case VT_Empty: return;
121     case VT_ResultSubShapePair:
122       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
123       return;
124     case VT_TypeSubShapeNamePair:
125       // Note: the reverse order (first - type, second - sub-shape name)
126       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
127       return;
128     case VT_TypeInnerPointPair:
129       // Note: the reverse order (first - type, second - selected point)
130       theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
131       return;
132     case VT_WeakNamingPair:
133       // Note: the reverse order (first - type, second - selected point)
134       theAttribute->append(
135         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
136       return;
137   }
138 }
139
140 //==================================================================================================
141 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
142 {
143   return myVariantType;
144 }
145
146 //==================================================================================================
147 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
148 {
149   return myResultSubShapePair;
150 }
151
152 //==================================================================================================
153 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
154 {
155   return myTypeSubShapeNamePair;
156 }
157
158 //==================================================================================================
159 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
160 {
161   return myTypeInnerPointPair;
162 }
163
164 //==================================================================================================
165 std::string ModelHighAPI_Selection::shapeType() const
166 {
167   switch(myVariantType) {
168   case VT_ResultSubShapePair:
169     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
170                                                myResultSubShapePair.first->shape()->shapeTypeStr();
171   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
172   case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
173   }
174
175   return "SHAPE";
176 }
177
178 //==================================================================================================
179 void ModelHighAPI_Selection::setName(const std::string& theName)
180 {
181   if (myVariantType == VT_ResultSubShapePair) {
182     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
183     if(!aResult.get()) {
184       return;
185     }
186     aResult->data()->setName(theName);
187   }
188 }
189
190 std::string ModelHighAPI_Selection::name() const
191 {
192   if (myVariantType == VT_ResultSubShapePair) {
193     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
194     if (aResult.get())
195       return aResult->data()->name();
196   }
197   return std::string();
198 }
199
200 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
201 {
202   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
203     return;
204
205   AttributeIntArrayPtr aColor =
206       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
207   aColor->setSize(3);
208   aColor->setValue(0, theRed);
209   aColor->setValue(1, theGreen);
210   aColor->setValue(2, theBlue);
211 }
212
213 void ModelHighAPI_Selection::setDeflection(double theValue)
214 {
215   if (myVariantType != VT_ResultSubShapePair)
216     return;
217
218   AttributeDoublePtr aDeflectionAttr =
219     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
220
221   aDeflectionAttr->setValue(theValue);
222 }
223
224 void ModelHighAPI_Selection::setTransparency(double theValue)
225 {
226   if (myVariantType != VT_ResultSubShapePair)
227     return;
228
229   AttributeDoublePtr aTransparencyAttr =
230     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
231
232   aTransparencyAttr->setValue(theValue);
233 }
234
235 int ModelHighAPI_Selection::numberOfSubs() const
236 {
237   if (myVariantType != VT_ResultSubShapePair)
238     return 0;
239
240   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
241   if (!aBody.get())
242     return 0;
243
244   return aBody->numberOfSubs();
245 }
246
247 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
248 {
249   if (myVariantType != VT_ResultSubShapePair)
250     return ModelHighAPI_Selection();
251
252   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
253   if (!aBody)
254     return ModelHighAPI_Selection();
255   if (theIndex >= aBody->numberOfSubs())
256     return ModelHighAPI_Selection();
257
258   ResultBodyPtr aResult = aBody->subResult(theIndex);
259   return ModelHighAPI_Selection(aResult, aResult->shape());
260 }