1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModelHighAPI_Selection.h"
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>
30 #include <GeomAPI_Pnt.h>
31 //--------------------------------------------------------------------------------------
33 //--------------------------------------------------------------------------------------
34 ModelHighAPI_Selection::ModelHighAPI_Selection()
35 : myVariantType(VT_Empty)
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)
46 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
47 const std::string& theSubShapeName)
48 : myVariantType(VT_TypeSubShapeNamePair)
49 , myTypeSubShapeNamePair(theType, theSubShapeName)
53 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
54 const GeomPointPtr& theSubShapeInnerPoint)
55 : myVariantType(VT_TypeInnerPointPair)
56 , myTypeInnerPointPair(theType, theSubShapeInnerPoint)
60 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
61 const std::list<double>& theSubShapeInnerPoint)
62 : myVariantType(VT_TypeInnerPointPair)
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)
70 GeomPointPtr anInnerPoint(new GeomAPI_Pnt(aCoordinates[0], aCoordinates[1], aCoordinates[2]));
71 myTypeInnerPointPair = std::pair<std::string, GeomPointPtr>(theType, anInnerPoint);
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))
82 ModelHighAPI_Selection::~ModelHighAPI_Selection()
86 //--------------------------------------------------------------------------------------
87 void ModelHighAPI_Selection::fillAttribute(
88 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
90 switch(myVariantType) {
91 case VT_Empty: return;
92 case VT_ResultSubShapePair:
93 theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
95 case VT_TypeSubShapeNamePair:
96 theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
98 case VT_TypeInnerPointPair:
99 theAttribute->selectSubShape(myTypeInnerPointPair.first, myTypeInnerPointPair.second);
101 case VT_WeakNamingPair:
102 theAttribute->selectSubShape(
103 myWeakNamingPair.first, myWeakNamingPair.second.first, myWeakNamingPair.second.second);
107 if (theAttribute->isInvalid()) {
108 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
110 std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
114 //--------------------------------------------------------------------------------------
115 void ModelHighAPI_Selection::appendToList(
116 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
118 switch(myVariantType) {
119 case VT_Empty: return;
120 case VT_ResultSubShapePair:
121 theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
123 case VT_TypeSubShapeNamePair:
124 // Note: the reverse order (first - type, second - sub-shape name)
125 theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
127 case VT_TypeInnerPointPair:
128 // Note: the reverse order (first - type, second - selected point)
129 theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
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);
137 theAttribute->setFilters(myFilterFeature);
142 //==================================================================================================
143 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
145 return myVariantType;
148 //==================================================================================================
149 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
151 return myResultSubShapePair;
154 //==================================================================================================
155 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
157 return myTypeSubShapeNamePair;
160 //==================================================================================================
161 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
163 return myTypeInnerPointPair;
166 //==================================================================================================
167 TypeWeakNamingPair ModelHighAPI_Selection::typeWeakNamingPair() const
169 return myWeakNamingPair;
172 //==================================================================================================
173 std::string ModelHighAPI_Selection::shapeType() const
175 switch(myVariantType) {
176 case VT_ResultSubShapePair:
177 return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
178 myResultSubShapePair.first->shape()->shapeTypeStr();
179 case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
180 case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
186 //==================================================================================================
187 void ModelHighAPI_Selection::setName(const std::string& theName)
189 if (myVariantType == VT_ResultSubShapePair) {
190 std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
194 aResult->data()->setName(theName);
198 std::string ModelHighAPI_Selection::name() const
200 if (myVariantType == VT_ResultSubShapePair) {
201 std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
203 return aResult->data()->name();
205 return std::string();
208 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
210 if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
213 AttributeIntArrayPtr aColor =
214 myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
216 aColor->setValue(0, theRed);
217 aColor->setValue(1, theGreen);
218 aColor->setValue(2, theBlue);
221 void ModelHighAPI_Selection::setDeflection(double theValue)
223 if (myVariantType != VT_ResultSubShapePair)
226 AttributeDoublePtr aDeflectionAttr =
227 myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
229 aDeflectionAttr->setValue(theValue);
233 void ModelHighAPI_Selection::setTransparency(double theValue)
235 if (myVariantType != VT_ResultSubShapePair)
238 AttributeDoublePtr aTransparencyAttr =
239 myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
241 aTransparencyAttr->setValue(theValue);
245 int ModelHighAPI_Selection::numberOfSubs() const
247 if (myVariantType != VT_ResultSubShapePair)
250 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
254 return aBody->numberOfSubs();
257 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
259 if (myVariantType != VT_ResultSubShapePair)
260 return ModelHighAPI_Selection();
262 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
264 return ModelHighAPI_Selection();
265 if (theIndex >= aBody->numberOfSubs())
266 return ModelHighAPI_Selection();
268 ResultBodyPtr aResult = aBody->subResult(theIndex);
269 return ModelHighAPI_Selection(aResult, aResult->shape());