]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Selection.cpp
Salome HOME
Initial implementation of support of any level of hierarchy in Result Bodies.
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModelHighAPI_Selection.h"
22
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>
29
30 //--------------------------------------------------------------------------------------
31
32 //--------------------------------------------------------------------------------------
33 ModelHighAPI_Selection::ModelHighAPI_Selection()
34 : myVariantType(VT_Empty)
35 {
36 }
37
38 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
39                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
40 : myVariantType(VT_ResultSubShapePair)
41 , myResultSubShapePair(theContext, theSubShape)
42 {
43 }
44
45 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
46                                                const std::string& theSubShapeName)
47 : myVariantType(VT_TypeSubShapeNamePair)
48 , myTypeSubShapeNamePair(theType, theSubShapeName)
49 {
50 }
51
52 ModelHighAPI_Selection::~ModelHighAPI_Selection()
53 {
54 }
55
56 //--------------------------------------------------------------------------------------
57 void ModelHighAPI_Selection::fillAttribute(
58     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
59 {
60   switch(myVariantType) {
61     case VT_Empty: return;
62     case VT_ResultSubShapePair:
63       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
64       return;
65     case VT_TypeSubShapeNamePair:
66       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
67       if(theAttribute->isInvalid()) {
68         FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
69         aFeature->setError(
70           std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
71       }
72       return;
73   }
74 }
75
76 //--------------------------------------------------------------------------------------
77 void ModelHighAPI_Selection::appendToList(
78     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
79 {
80   switch(myVariantType) {
81     case VT_Empty: return;
82     case VT_ResultSubShapePair:
83       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
84       return;
85     case VT_TypeSubShapeNamePair:
86       // Note: the reverse order (first - type, second - sub-shape name)
87       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
88       return;
89   }
90 }
91
92 //==================================================================================================
93 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
94 {
95   return myVariantType;
96 }
97
98 //==================================================================================================
99 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
100 {
101   return myResultSubShapePair;
102 }
103
104 //==================================================================================================
105 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
106 {
107   return myTypeSubShapeNamePair;
108 }
109
110 //==================================================================================================
111 std::string ModelHighAPI_Selection::shapeType() const
112 {
113   switch(myVariantType) {
114   case VT_ResultSubShapePair:
115     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
116                                                myResultSubShapePair.first->shape()->shapeTypeStr();
117   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
118   }
119
120   return "SHAPE";
121 }
122
123 //==================================================================================================
124 void ModelHighAPI_Selection::setName(const std::string& theName)
125 {
126   if (myVariantType == VT_ResultSubShapePair) {
127     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
128     if(!aResult.get()) {
129       return;
130     }
131     aResult->data()->setName(theName);
132   }
133 }
134
135 std::string ModelHighAPI_Selection::name() const
136 {
137   if (myVariantType == VT_ResultSubShapePair) {
138     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
139     if (aResult.get())
140       return aResult->data()->name();
141   }
142   return std::string();
143 }
144
145 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
146 {
147   if (myVariantType != VT_ResultSubShapePair || !myResultSubShapePair.first.get())
148     return;
149
150   AttributeIntArrayPtr aColor =
151       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
152   aColor->setSize(3);
153   aColor->setValue(0, theRed);
154   aColor->setValue(1, theGreen);
155   aColor->setValue(2, theBlue);
156 }
157
158 void ModelHighAPI_Selection::setDeflection(double theValue)
159 {
160   if (myVariantType != VT_ResultSubShapePair)
161     return;
162
163   AttributeDoublePtr aDeflectionAttr =
164     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
165
166   aDeflectionAttr->setValue(theValue);
167 }
168
169 void ModelHighAPI_Selection::setTransparency(double theValue)
170 {
171   if (myVariantType != VT_ResultSubShapePair)
172     return;
173
174   AttributeDoublePtr aTransparencyAttr =
175     myResultSubShapePair.first->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
176
177   aTransparencyAttr->setValue(theValue);
178 }
179
180 int ModelHighAPI_Selection::numberOfSubs() const
181 {
182   if (myVariantType != VT_ResultSubShapePair)
183     return 0;
184
185   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
186   if (!aBody.get())
187     return 0;
188
189   return aBody->numberOfSubs();
190 }
191
192 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
193 {
194   if (myVariantType != VT_ResultSubShapePair)
195     return ModelHighAPI_Selection();
196
197   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myResultSubShapePair.first);
198   if (!aBody)
199     return ModelHighAPI_Selection();
200   if (theIndex >= aBody->numberOfSubs())
201     return ModelHighAPI_Selection();
202
203   ResultBodyPtr aResult = aBody->subResult(theIndex);
204   return ModelHighAPI_Selection(aResult, aResult->shape());
205 }