]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Selection.cpp
Salome HOME
Added model.do() after part dump for correct objects concealment.
[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_ResultCompSolid.h>
16 //--------------------------------------------------------------------------------------
17
18 //--------------------------------------------------------------------------------------
19 ModelHighAPI_Selection::ModelHighAPI_Selection()
20 : myVariantType(VT_Empty)
21 {
22 }
23
24 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
25                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
26 : myVariantType(VT_ResultSubShapePair)
27 , myResultSubShapePair(theContext, theSubShape)
28 {
29 }
30
31 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
32                                                const std::string& theSubShapeName)
33 : myVariantType(VT_TypeSubShapeNamePair)
34 , myTypeSubShapeNamePair(theType, theSubShapeName)
35 {
36 }
37
38 ModelHighAPI_Selection::~ModelHighAPI_Selection()
39 {
40 }
41
42 //--------------------------------------------------------------------------------------
43 void ModelHighAPI_Selection::fillAttribute(
44     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
45 {
46   switch(myVariantType) {
47     case VT_Empty: return;
48     case VT_ResultSubShapePair:
49       theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
50       return;
51     case VT_TypeSubShapeNamePair:
52       theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
53       return;
54   }
55 }
56
57 //--------------------------------------------------------------------------------------
58 void ModelHighAPI_Selection::appendToList(
59     const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
60 {
61   switch(myVariantType) {
62     case VT_Empty: return;
63     case VT_ResultSubShapePair:
64       theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
65       return;
66     case VT_TypeSubShapeNamePair:
67       // Note: the reverse order (first - type, second - sub-shape name)
68       theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
69       return;
70   }
71 }
72
73 //==================================================================================================
74 ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
75 {
76   return myVariantType;
77 }
78
79 //==================================================================================================
80 ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
81 {
82   return myResultSubShapePair;
83 }
84
85 //==================================================================================================
86 TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
87 {
88   return myTypeSubShapeNamePair;
89 }
90
91 //==================================================================================================
92 std::string ModelHighAPI_Selection::shapeType() const
93 {
94   switch(myVariantType) {
95   case VT_ResultSubShapePair:
96     return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
97                                                myResultSubShapePair.first->shape()->shapeTypeStr();
98   case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
99   }
100
101   return "SHAPE";
102 }
103
104 //==================================================================================================
105 void ModelHighAPI_Selection::setName(const std::string& theName)
106 {
107   if (myVariantType == VT_ResultSubShapePair) {
108     std::shared_ptr<ModelAPI_Result> aResult = myResultSubShapePair.first;
109     if(!aResult.get()) {
110       return;
111     }
112     aResult->data()->setName(theName);
113   }
114 }
115
116 void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
117 {
118   if (myVariantType != VT_ResultSubShapePair)
119     return;
120
121   AttributeIntArrayPtr aColor =
122       myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
123   aColor->setSize(3);
124   aColor->setValue(0, theRed);
125   aColor->setValue(1, theGreen);
126   aColor->setValue(2, theBlue);
127 }
128
129 void ModelHighAPI_Selection::setDeflection(double theValue)
130 {
131   if (myVariantType != VT_ResultSubShapePair)
132     return;
133
134   AttributeDoublePtr aDeflectionAttr =
135     myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
136
137   aDeflectionAttr->setValue(theValue);
138 }
139
140 int ModelHighAPI_Selection::numberOfSubs() const
141 {
142   if (myVariantType != VT_ResultSubShapePair)
143     return 0;
144
145   ResultCompSolidPtr aCompSolid =
146       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
147   if (!aCompSolid)
148     return 0;
149
150   return aCompSolid->numberOfSubs();
151 }
152
153 ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex) const
154 {
155   if (myVariantType != VT_ResultSubShapePair)
156     return ModelHighAPI_Selection();
157
158   ResultCompSolidPtr aCompSolid =
159       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
160   if (!aCompSolid)
161     return ModelHighAPI_Selection();
162
163   ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
164   return ModelHighAPI_Selection(aResult, aResult->shape());
165 }