Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.cpp
1 // Copyright (C) 2014-2020  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
18 //
19
20 #include "ModelHighAPI_Selection.h"
21
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeIntArray.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_ResultBody.h>
28
29
30 #include <GeomAPI_Pnt.h>
31 //--------------------------------------------------------------------------------------
32
33 //--------------------------------------------------------------------------------------
34 ModelHighAPI_Selection::ModelHighAPI_Selection()
35 : myVariantType(VT_Empty)
36 {
37 }
38
39 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
40                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
41 : myVariantType(VT_ResultSubShapePair)
42 , myResultSubShapePair(theContext, theSubShape)
43 {
44 }
45
46 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
47                                                const std::wstring& theSubShapeName)
48 : myVariantType(VT_TypeSubShapeNamePair)
49 , myTypeSubShapeNamePair(theType, theSubShapeName)
50 {
51 }
52
53 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
54                                                const GeomPointPtr& theSubShapeInnerPoint)
55 : myVariantType(VT_TypeInnerPointPair)
56 , myTypeInnerPointPair(theType, theSubShapeInnerPoint)
57 {
58 }
59
60 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
61                                                const std::list<double>& theSubShapeInnerPoint)
62 : myVariantType(VT_TypeInnerPointPair)
63 {
64   double aCoordinates[3] = { 0.0, 0.0, 0.0 };
65   double* aCIt = aCoordinates;
66   std::list<double>::const_iterator aPIt = theSubShapeInnerPoint.begin();
67   for (; aPIt != theSubShapeInnerPoint.end(); ++aPIt, ++aCIt)
68     *aCIt = *aPIt;
69
70   GeomPointPtr anInnerPoint(new GeomAPI_Pnt(aCoordinates[0], aCoordinates[1], aCoordinates[2]));
71   myTypeInnerPointPair = std::pair<std::string, GeomPointPtr>(theType, anInnerPoint);
72 }
73
74 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
75   const std::wstring& theContextName, const int theIndex)
76   : myVariantType(VT_WeakNamingPair),
77   myWeakNamingPair(theType, std::pair<std::wstring, int>(theContextName, theIndex))
78 {
79 }
80
81
82 ModelHighAPI_Selection::~ModelHighAPI_Selection()
83 {
84 }
85
86 //--------------------------------------------------------------------------------------
87 void ModelHighAPI_Selection::fillAttribute(
88     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
89 {
90   switch(myVariantType) {
91     case VT_Empty: return;
92     case VT_ResultSubShapePair:
93       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
94       return;
95     case VT_TypeSubShapeNamePair:
96       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
97       break;
98     case VT_TypeInnerPointPair:
99       theAttribute->selectSubShape(myTypeInnerPointPair.first, myTypeInnerPointPair.second);
100       return;
101     case VT_WeakNamingPair:
102       theAttribute->selectSubShape(
103         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
104       break;
105     case VT_Filtering:
106       break; // do nothing [to avoid compilation warning]
107   }
108
109   if (theAttribute->isInvalid()) {
110     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
111     aFeature->setError(
112       std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
113   }
114 }
115
116 //--------------------------------------------------------------------------------------
117 void ModelHighAPI_Selection::appendToList(
118     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
119 {
120   switch(myVariantType) {
121     case VT_Empty: return;
122     case VT_ResultSubShapePair:
123       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
124       return;
125     case VT_TypeSubShapeNamePair:
126       // Note: the reverse order (first - type, second - sub-shape name)
127       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
128       return;
129     case VT_TypeInnerPointPair:
130       // Note: the reverse order (first - type, second - selected point)
131       theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
132       return;
133     case VT_WeakNamingPair:
134       // Note: the reverse order (first - type, second - selected point)
135       theAttribute->append(
136         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
137       return;
138     case VT_Filtering:
139       theAttribute->setFilters(myFilterFeature);
140       return;
141   }
142 }
143
144 //==================================================================================================
145 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
146 {
147   return myVariantType;
148 }
149
150 //==================================================================================================
151 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
152 {
153   return myResultSubShapePair;
154 }
155
156 //==================================================================================================
157 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
158 {
159   return myTypeSubShapeNamePair;
160 }
161
162 //==================================================================================================
163 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
164 {
165   return myTypeInnerPointPair;
166 }
167
168 //==================================================================================================
169 TypeWeakNamingPair ModelHighAPI_Selection::typeWeakNamingPair() const
170 {
171   return myWeakNamingPair;
172 }
173
174 //==================================================================================================
175 std::string ModelHighAPI_Selection::shapeType() const
176 {
177   switch(myVariantType) {
178   case VT_ResultSubShapePair:
179     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
180                                                myResultSubShapePair.first->shape()->shapeTypeStr();
181   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
182   case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
183   default:
184     break; // do nothing [to avoid compilation warning]
185   }
186
187   return "SHAPE";
188 }
189
190 //==================================================================================================
191 void ModelHighAPI_Selection::setName(const std::wstring& theName)
192 {
193   if (myVariantType == VT_ResultSubShapePair) {
194     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
195     if(!aResult.get()) {
196       return;
197     }
198     aResult->data()->setName(theName);
199   }
200 }
201
202 std::wstring ModelHighAPI_Selection::name() const
203 {
204   if (myVariantType == VT_ResultSubShapePair) {
205     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
206     if (aResult.get())
207       return aResult->data()->name();
208   }
209   return std::wstring();
210 }
211
212 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
213 {
214   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
215     return;
216
217   AttributeIntArrayPtr aColor =
218       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
219   aColor->setSize(3);
220   aColor->setValue(0, theRed);
221   aColor->setValue(1, theGreen);
222   aColor->setValue(2, theBlue);
223 }
224
225 void ModelHighAPI_Selection::setDeflection(double theValue)
226 {
227   if (myVariantType != VT_ResultSubShapePair)
228     return;
229
230   AttributeDoublePtr aDeflectionAttr =
231     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
232
233   aDeflectionAttr->setValue(theValue);
234 }
235
236 // LCOV_EXCL_START
237 void ModelHighAPI_Selection::setTransparency(double theValue)
238 {
239   if (myVariantType != VT_ResultSubShapePair)
240     return;
241
242   AttributeDoublePtr aTransparencyAttr =
243     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
244
245   aTransparencyAttr->setValue(theValue);
246 }
247 // LCOV_EXCL_STOP
248
249 int ModelHighAPI_Selection::numberOfSubs() const
250 {
251   if (myVariantType != VT_ResultSubShapePair)
252     return 0;
253
254   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
255   if (!aBody.get())
256     return 0;
257
258   return aBody->numberOfSubs();
259 }
260
261 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
262 {
263   if (myVariantType != VT_ResultSubShapePair)
264     return ModelHighAPI_Selection();
265
266   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
267   if (!aBody)
268     return ModelHighAPI_Selection();
269   if (theIndex >= aBody->numberOfSubs())
270     return ModelHighAPI_Selection();
271
272   ResultBodyPtr aResult = aBody->subResult(theIndex);
273   return ModelHighAPI_Selection(aResult, aResult->shape());
274 }