1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelHighAPI_Selection.h"
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>
31 #include <GeomAPI_Pnt.h>
32 //--------------------------------------------------------------------------------------
34 //--------------------------------------------------------------------------------------
35 ModelHighAPI_Selection::ModelHighAPI_Selection()
36 : myVariantType(VT_Empty)
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)
47 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
48 const std::string& theSubShapeName)
49 : myVariantType(VT_TypeSubShapeNamePair)
50 , myTypeSubShapeNamePair(theType, theSubShapeName)
54 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
55 const GeomPointPtr& theSubShapeInnerPoint)
56 : myVariantType(VT_TypeInnerPointPair)
57 , myTypeInnerPointPair(theType, theSubShapeInnerPoint)
61 ModelHighAPI_Selection::~ModelHighAPI_Selection()
65 //--------------------------------------------------------------------------------------
66 void ModelHighAPI_Selection::fillAttribute(
67 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
69 switch(myVariantType) {
70 case VT_Empty: return;
71 case VT_ResultSubShapePair:
72 theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
74 case VT_TypeSubShapeNamePair:
75 theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
77 case VT_TypeInnerPointPair:
78 theAttribute->selectSubShape(myTypeInnerPointPair.first, myTypeInnerPointPair.second);
82 if (theAttribute->isInvalid()) {
83 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
85 std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
89 //--------------------------------------------------------------------------------------
90 void ModelHighAPI_Selection::appendToList(
91 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
93 switch(myVariantType) {
94 case VT_Empty: return;
95 case VT_ResultSubShapePair:
96 theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
98 case VT_TypeSubShapeNamePair:
99 // Note: the reverse order (first - type, second - sub-shape name)
100 theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
102 case VT_TypeInnerPointPair:
103 // Note: the reverse order (first - type, second - selected point)
104 theAttribute->append(myTypeInnerPointPair.second, myTypeInnerPointPair.first);
109 //==================================================================================================
110 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
112 return myVariantType;
115 //==================================================================================================
116 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
118 return myResultSubShapePair;
121 //==================================================================================================
122 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
124 return myTypeSubShapeNamePair;
127 //==================================================================================================
128 TypeInnerPointPair ModelHighAPI_Selection::typeInnerPointPair() const
130 return myTypeInnerPointPair;
133 //==================================================================================================
134 std::string ModelHighAPI_Selection::shapeType() const
136 switch(myVariantType) {
137 case VT_ResultSubShapePair:
138 return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
139 myResultSubShapePair.first->shape()->shapeTypeStr();
140 case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
141 case VT_TypeInnerPointPair: return myTypeInnerPointPair.first;
147 //==================================================================================================
148 void ModelHighAPI_Selection::setName(const std::string& theName)
150 if (myVariantType == VT_ResultSubShapePair) {
151 std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
155 aResult->data()->setName(theName);
159 std::string ModelHighAPI_Selection::name() const
161 if (myVariantType == VT_ResultSubShapePair) {
162 std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
164 return aResult->data()->name();
166 return std::string();
169 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
171 if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
174 AttributeIntArrayPtr aColor =
175 myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
177 aColor->setValue(0, theRed);
178 aColor->setValue(1, theGreen);
179 aColor->setValue(2, theBlue);
182 void ModelHighAPI_Selection::setDeflection(double theValue)
184 if (myVariantType != VT_ResultSubShapePair)
187 AttributeDoublePtr aDeflectionAttr =
188 myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
190 aDeflectionAttr->setValue(theValue);
193 void ModelHighAPI_Selection::setTransparency(double theValue)
195 if (myVariantType != VT_ResultSubShapePair)
198 AttributeDoublePtr aTransparencyAttr =
199 myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
201 aTransparencyAttr->setValue(theValue);
204 int ModelHighAPI_Selection::numberOfSubs() const
206 if (myVariantType != VT_ResultSubShapePair)
209 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
213 return aBody->numberOfSubs();
216 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
218 if (myVariantType != VT_ResultSubShapePair)
219 return ModelHighAPI_Selection();
221 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
223 return ModelHighAPI_Selection();
224 if (theIndex >= aBody->numberOfSubs())
225 return ModelHighAPI_Selection();
227 ResultBodyPtr aResult = aBody->subResult(theIndex);
228 return ModelHighAPI_Selection(aResult, aResult->shape());