Salome HOME
Issue #2148: Moving an arc displays a circle
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : ModelHighAPI_Selection.cpp
3 // Purpose:
4 //
5 // History:
6 // 06/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "ModelHighAPI_Selection.h"
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeIntArray.h>
13 #include <ModelAPI_AttributeSelection.h>
14 #include <ModelAPI_AttributeSelectionList.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_ResultCompSolid.h>
17 //--------------------------------------------------------------------------------------
18
19 //--------------------------------------------------------------------------------------
20 ModelHighAPI_Selection::ModelHighAPI_Selection()
21 : myVariantType(VT_Empty)
22 {
23 }
24
25 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
26                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
27 : myVariantType(VT_ResultSubShapePair)
28 , myResultSubShapePair(theContext, theSubShape)
29 {
30 }
31
32 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
33                                                const std::string& theSubShapeName)
34 : myVariantType(VT_TypeSubShapeNamePair)
35 , myTypeSubShapeNamePair(theType, theSubShapeName)
36 {
37 }
38
39 ModelHighAPI_Selection::~ModelHighAPI_Selection()
40 {
41 }
42
43 //--------------------------------------------------------------------------------------
44 void ModelHighAPI_Selection::fillAttribute(
45     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
46 {
47   switch(myVariantType) {
48     case VT_Empty: return;
49     case VT_ResultSubShapePair:
50       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
51       return;
52     case VT_TypeSubShapeNamePair:
53       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
54       if(theAttribute->isInvalid()) {
55         FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
56         aFeature->setError(
57           std::string("Error: attribute \"") + theAttribute->id() + std::string("\" is invalid."));
58       }
59       return;
60   }
61 }
62
63 //--------------------------------------------------------------------------------------
64 void ModelHighAPI_Selection::appendToList(
65     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
66 {
67   switch(myVariantType) {
68     case VT_Empty: return;
69     case VT_ResultSubShapePair:
70       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
71       return;
72     case VT_TypeSubShapeNamePair:
73       // Note: the reverse order (first - type, second - sub-shape name)
74       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
75       return;
76   }
77 }
78
79 //==================================================================================================
80 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
81 {
82   return myVariantType;
83 }
84
85 //==================================================================================================
86 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
87 {
88   return myResultSubShapePair;
89 }
90
91 //==================================================================================================
92 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
93 {
94   return myTypeSubShapeNamePair;
95 }
96
97 //==================================================================================================
98 std::string ModelHighAPI_Selection::shapeType() const
99 {
100   switch(myVariantType) {
101   case VT_ResultSubShapePair:
102     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
103                                                myResultSubShapePair.first->shape()->shapeTypeStr();
104   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
105   }
106
107   return "SHAPE";
108 }
109
110 //==================================================================================================
111 void ModelHighAPI_Selection::setName(const std::string& theName)
112 {
113   if (myVariantType == VT_ResultSubShapePair) {
114     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
115     if(!aResult.get()) {
116       return;
117     }
118     aResult->data()->setName(theName);
119   }
120 }
121
122 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
123 {
124   if (myVariantType != VT_ResultSubShapePair)
125     return;
126
127   AttributeIntArrayPtr aColor =
128       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
129   aColor->setSize(3);
130   aColor->setValue(0, theRed);
131   aColor->setValue(1, theGreen);
132   aColor->setValue(2, theBlue);
133 }
134
135 void ModelHighAPI_Selection::setDeflection(double theValue)
136 {
137   if (myVariantType != VT_ResultSubShapePair)
138     return;
139
140   AttributeDoublePtr aDeflectionAttr =
141     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
142
143   aDeflectionAttr->setValue(theValue);
144 }
145
146 int ModelHighAPI_Selection::numberOfSubs() const
147 {
148   if (myVariantType != VT_ResultSubShapePair)
149     return 0;
150
151   ResultCompSolidPtr aCompSolid =
152       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
153   if (!aCompSolid)
154     return 0;
155
156   return aCompSolid->numberOfSubs();
157 }
158
159 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
160 {
161   if (myVariantType != VT_ResultSubShapePair)
162     return ModelHighAPI_Selection();
163
164   ResultCompSolidPtr aCompSolid =
165       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
166   if (!aCompSolid)
167     return ModelHighAPI_Selection();
168
169   ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
170   return ModelHighAPI_Selection(aResult, aResult->shape());
171 }