]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Selection.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto: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_ResultCompSolid.h>
28 //--------------------------------------------------------------------------------------
29
30 //--------------------------------------------------------------------------------------
31 ModelHighAPI_Selection::ModelHighAPI_Selection()
32 : myVariantType(VT_Empty)
33 {
34 }
35
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)
40 {
41 }
42
43 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
44                                                const std::string& theSubShapeName)
45 : myVariantType(VT_TypeSubShapeNamePair)
46 , myTypeSubShapeNamePair(theType, theSubShapeName)
47 {
48 }
49
50 ModelHighAPI_Selection::~ModelHighAPI_Selection()
51 {
52 }
53
54 //--------------------------------------------------------------------------------------
55 void ModelHighAPI_Selection::fillAttribute(
56     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
57 {
58   switch(myVariantType) {
59     case VT_Empty: return;
60     case VT_ResultSubShapePair:
61       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
62       return;
63     case VT_TypeSubShapeNamePair:
64       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
65       if(theAttribute->isInvalid()) {
66         FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
67         aFeature->setError(
68           std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
69       }
70       return;
71   }
72 }
73
74 //--------------------------------------------------------------------------------------
75 void ModelHighAPI_Selection::appendToList(
76     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
77 {
78   switch(myVariantType) {
79     case VT_Empty: return;
80     case VT_ResultSubShapePair:
81       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
82       return;
83     case VT_TypeSubShapeNamePair:
84       // Note: the reverse order (first - type, second - sub-shape name)
85       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
86       return;
87   }
88 }
89
90 //==================================================================================================
91 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
92 {
93   return myVariantType;
94 }
95
96 //==================================================================================================
97 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
98 {
99   return myResultSubShapePair;
100 }
101
102 //==================================================================================================
103 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
104 {
105   return myTypeSubShapeNamePair;
106 }
107
108 //==================================================================================================
109 std::string ModelHighAPI_Selection::shapeType() const
110 {
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;
116   }
117
118   return "SHAPE";
119 }
120
121 //==================================================================================================
122 void ModelHighAPI_Selection::setName(const std::string& theName)
123 {
124   if (myVariantType == VT_ResultSubShapePair) {
125     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
126     if(!aResult.get()) {
127       return;
128     }
129     aResult->data()->setName(theName);
130   }
131 }
132
133 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
134 {
135   if (myVariantType != VT_ResultSubShapePair)
136     return;
137
138   AttributeIntArrayPtr aColor =
139       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
140   aColor->setSize(3);
141   aColor->setValue(0, theRed);
142   aColor->setValue(1, theGreen);
143   aColor->setValue(2, theBlue);
144 }
145
146 void ModelHighAPI_Selection::setDeflection(double theValue)
147 {
148   if (myVariantType != VT_ResultSubShapePair)
149     return;
150
151   AttributeDoublePtr aDeflectionAttr =
152     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
153
154   aDeflectionAttr->setValue(theValue);
155 }
156
157 int ModelHighAPI_Selection::numberOfSubs() const
158 {
159   if (myVariantType != VT_ResultSubShapePair)
160     return 0;
161
162   ResultCompSolidPtr aCompSolid =
163       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
164   if (!aCompSolid)
165     return 0;
166
167   return aCompSolid->numberOfSubs();
168 }
169
170 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
171 {
172   if (myVariantType != VT_ResultSubShapePair)
173     return ModelHighAPI_Selection();
174
175   ResultCompSolidPtr aCompSolid =
176       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
177   if (!aCompSolid)
178     return ModelHighAPI_Selection();
179
180   ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
181   return ModelHighAPI_Selection(aResult, aResult->shape());
182 }