]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Selection.cpp
Salome HOME
cb74050bce40a957fa16dba8abc967a06f830736
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.cpp
1 // Copyright (C) 2014-2019  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::string& 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::string& theContextName, const int theIndex)
76   : myVariantType(VT_WeakNamingPair)
77   , myWeakNamingPair(theType, std::pair<std::string, 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   }
106
107   if (theAttribute->isInvalid()) {
108     FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
109     aFeature->setError(
110       std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
111   }
112 }
113
114 //--------------------------------------------------------------------------------------
115 void ModelHighAPI_Selection::appendToList(
116     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
117 {
118   switch(myVariantType) {
119     case VT_Empty: return;
120     case VT_ResultSubShapePair:
121       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
122       return;
123     case VT_TypeSubShapeNamePair:
124       // Note: the reverse order (first - type, second - sub-shape name)
125       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
126       return;
127     case VT_TypeInnerPointPair:
128       // Note: the reverse order (first - type, second - selected point)
129       theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
130       return;
131     case VT_WeakNamingPair:
132       // Note: the reverse order (first - type, second - selected point)
133       theAttribute->append(
134         myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
135       return;
136   }
137 }
138
139 //==================================================================================================
140 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
141 {
142   return myVariantType;
143 }
144
145 //==================================================================================================
146 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
147 {
148   return myResultSubShapePair;
149 }
150
151 //==================================================================================================
152 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
153 {
154   return myTypeSubShapeNamePair;
155 }
156
157 //==================================================================================================
158 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
159 {
160   return myTypeInnerPointPair;
161 }
162
163 //==================================================================================================
164 TypeWeakNamingPair ModelHighAPI_Selection::typeWeakNamingPair() const
165 {
166   return myWeakNamingPair;
167 }
168
169 //==================================================================================================
170 std::string ModelHighAPI_Selection::shapeType() const
171 {
172   switch(myVariantType) {
173   case VT_ResultSubShapePair:
174     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
175                                                myResultSubShapePair.first->shape()->shapeTypeStr();
176   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
177   case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
178   }
179
180   return "SHAPE";
181 }
182
183 //==================================================================================================
184 void ModelHighAPI_Selection::setName(const std::string& theName)
185 {
186   if (myVariantType == VT_ResultSubShapePair) {
187     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
188     if(!aResult.get()) {
189       return;
190     }
191     aResult->data()->setName(theName);
192   }
193 }
194
195 std::string ModelHighAPI_Selection::name() const
196 {
197   if (myVariantType == VT_ResultSubShapePair) {
198     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
199     if (aResult.get())
200       return aResult->data()->name();
201   }
202   return std::string();
203 }
204
205 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
206 {
207   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
208     return;
209
210   AttributeIntArrayPtr aColor =
211       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
212   aColor->setSize(3);
213   aColor->setValue(0, theRed);
214   aColor->setValue(1, theGreen);
215   aColor->setValue(2, theBlue);
216 }
217
218 void ModelHighAPI_Selection::setDeflection(double theValue)
219 {
220   if (myVariantType != VT_ResultSubShapePair)
221     return;
222
223   AttributeDoublePtr aDeflectionAttr =
224     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
225
226   aDeflectionAttr->setValue(theValue);
227 }
228
229 // LCOV_EXCL_START
230 void ModelHighAPI_Selection::setTransparency(double theValue)
231 {
232   if (myVariantType != VT_ResultSubShapePair)
233     return;
234
235   AttributeDoublePtr aTransparencyAttr =
236     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
237
238   aTransparencyAttr->setValue(theValue);
239 }
240 // LCOV_EXCL_STOP
241
242 int ModelHighAPI_Selection::numberOfSubs() const
243 {
244   if (myVariantType != VT_ResultSubShapePair)
245     return 0;
246
247   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
248   if (!aBody.get())
249     return 0;
250
251   return aBody->numberOfSubs();
252 }
253
254 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
255 {
256   if (myVariantType != VT_ResultSubShapePair)
257     return ModelHighAPI_Selection();
258
259   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
260   if (!aBody)
261     return ModelHighAPI_Selection();
262   if (theIndex >= aBody->numberOfSubs())
263     return ModelHighAPI_Selection();
264
265   ResultBodyPtr aResult = aBody->subResult(theIndex);
266   return ModelHighAPI_Selection(aResult, aResult->shape());
267 }