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 email : webmaster.salome@opencascade.com<mailto: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_ResultCompSolid.h>
28 //--------------------------------------------------------------------------------------
30 //--------------------------------------------------------------------------------------
31 ModelHighAPI_Selection::ModelHighAPI_Selection()
32 : myVariantType(VT_Empty)
36 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
37 const std::shared_ptr<GeomAPI_Shape>& theSubShape)
38 : myVariantType(VT_ResultSubShapePair)
39 , myResultSubShapePair(theContext, theSubShape)
43 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
44 const std::string& theSubShapeName)
45 : myVariantType(VT_TypeSubShapeNamePair)
46 , myTypeSubShapeNamePair(theType, theSubShapeName)
50 ModelHighAPI_Selection::~ModelHighAPI_Selection()
54 //--------------------------------------------------------------------------------------
55 void ModelHighAPI_Selection::fillAttribute(
56 const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
58 switch(myVariantType) {
59 case VT_Empty: return;
60 case VT_ResultSubShapePair:
61 theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
63 case VT_TypeSubShapeNamePair:
64 theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
65 if(theAttribute->isInvalid()) {
66 FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
68 std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
74 //--------------------------------------------------------------------------------------
75 void ModelHighAPI_Selection::appendToList(
76 const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
78 switch(myVariantType) {
79 case VT_Empty: return;
80 case VT_ResultSubShapePair:
81 theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
83 case VT_TypeSubShapeNamePair:
84 // Note: the reverse order (first - type, second - sub-shape name)
85 theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
90 //==================================================================================================
91 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
96 //==================================================================================================
97 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
99 return myResultSubShapePair;
102 //==================================================================================================
103 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
105 return myTypeSubShapeNamePair;
108 //==================================================================================================
109 std::string ModelHighAPI_Selection::shapeType() const
111 switch(myVariantType) {
112 case VT_ResultSubShapePair:
113 return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
114 myResultSubShapePair.first->shape()->shapeTypeStr();
115 case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
121 //==================================================================================================
122 void ModelHighAPI_Selection::setName(const std::string& theName)
124 if (myVariantType == VT_ResultSubShapePair) {
125 std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
129 aResult->data()->setName(theName);
133 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
135 if (myVariantType != VT_ResultSubShapePair)
138 AttributeIntArrayPtr aColor =
139 myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
141 aColor->setValue(0, theRed);
142 aColor->setValue(1, theGreen);
143 aColor->setValue(2, theBlue);
146 void ModelHighAPI_Selection::setDeflection(double theValue)
148 if (myVariantType != VT_ResultSubShapePair)
151 AttributeDoublePtr aDeflectionAttr =
152 myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
154 aDeflectionAttr->setValue(theValue);
157 int ModelHighAPI_Selection::numberOfSubs() const
159 if (myVariantType != VT_ResultSubShapePair)
162 ResultCompSolidPtr aCompSolid =
163 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
167 return aCompSolid->numberOfSubs();
170 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
172 if (myVariantType != VT_ResultSubShapePair)
173 return ModelHighAPI_Selection();
175 ResultCompSolidPtr aCompSolid =
176 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
178 return ModelHighAPI_Selection();
180 ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
181 return ModelHighAPI_Selection(aResult, aResult->shape());