Salome HOME
Issue #1648: Dump Python in the High Level Parameterized Geometry API
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Tools.cpp
1 // Name   : ModelHighAPI_Tools.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Tools.h"
9 //--------------------------------------------------------------------------------------
10 #include <GeomAPI_Dir.h>
11 #include <GeomAPI_Pnt.h>
12 #include <GeomAPI_Pnt2d.h>
13 //--------------------------------------------------------------------------------------
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point.h>
16 #include <GeomDataAPI_Point2D.h>
17 //--------------------------------------------------------------------------------------
18 #include <ModelAPI_AttributeBoolean.h>
19 #include <ModelAPI_AttributeDocRef.h>
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_AttributeIntArray.h>
22 #include <ModelAPI_AttributeInteger.h>
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_AttributeRefAttrList.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeRefList.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeString.h>
30 //--------------------------------------------------------------------------------------
31 #include "ModelHighAPI_Double.h"
32 #include "ModelHighAPI_Integer.h"
33 #include "ModelHighAPI_RefAttr.h"
34 #include "ModelHighAPI_Reference.h"
35 #include "ModelHighAPI_Selection.h"
36
37 #include <algorithm>
38
39 //--------------------------------------------------------------------------------------
40 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
41                    const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
42 {
43   theAttribute->setValue(theValue);
44 }
45
46 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
47                    double theX, double theY)
48 {
49   theAttribute->setValue(theX, theY);
50 }
51
52 //--------------------------------------------------------------------------------------
53 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
54                    const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
55 {
56   theAttribute->setValue(theValue);
57 }
58
59 //--------------------------------------------------------------------------------------
60 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
61                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
62 {
63   theAttribute->setValue(theValue);
64 }
65
66 //--------------------------------------------------------------------------------------
67 void fillAttribute(bool theValue,
68                    const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
69 {
70   theAttribute->setValue(theValue);
71 }
72
73 //--------------------------------------------------------------------------------------
74 void fillAttribute(const ModelHighAPI_Double & theValue,
75                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
76 {
77   theValue.fillAttribute(theAttribute);
78 }
79 void fillAttribute(double theValue,
80                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
81 {
82   theAttribute->setValue(theValue);
83 }
84
85 //--------------------------------------------------------------------------------------
86 void fillAttribute(const ModelHighAPI_Integer & theValue,
87                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
88 {
89   theValue.fillAttribute(theAttribute);
90 }
91
92 //--------------------------------------------------------------------------------------
93 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
94                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
95 {
96   theValue.fillAttribute(theAttribute);
97 }
98
99 //--------------------------------------------------------------------------------------
100 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
101                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
102 {
103   theAttribute->clear();
104   for (auto it = theValue.begin(); it != theValue.end(); ++it)
105     it->appendToList(theAttribute);
106 }
107
108 //--------------------------------------------------------------------------------------
109 void fillAttribute(const ModelHighAPI_Reference & theValue,
110                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
111 {
112   theValue.fillAttribute(theAttribute);
113 }
114
115 //--------------------------------------------------------------------------------------
116 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
117                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
118 {
119   theAttribute->clear();
120   for (auto it = theValue.begin(); it != theValue.end(); ++it)
121     it->appendToList(theAttribute);
122 }
123
124 //--------------------------------------------------------------------------------------
125 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
126                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
127 {
128   theAttribute->setValue(theValue);
129 }
130
131 //--------------------------------------------------------------------------------------
132 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
133                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
134 {
135   theAttribute->clear();
136   for (auto it = theValue.begin(); it != theValue.end(); ++it)
137     theAttribute->append(*it);
138 }
139
140 MODELHIGHAPI_EXPORT
141 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
142                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
143 {
144   theAttribute->clear();
145   for (auto it = theValue.begin(); it != theValue.end(); ++it)
146     theAttribute->append(it->resultSubShapePair().first); // use only context
147 }
148
149 //--------------------------------------------------------------------------------------
150 void fillAttribute(const ModelHighAPI_Selection & theValue,
151                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
152 {
153   theValue.fillAttribute(theAttribute);
154 }
155
156 //--------------------------------------------------------------------------------------
157 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
158                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
159 {
160   theAttribute->clear();
161   for (auto it = theValue.begin(); it != theValue.end(); ++it)
162     it->appendToList(theAttribute);
163 }
164
165 //--------------------------------------------------------------------------------------
166 void fillAttribute(const std::string & theValue,
167                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
168 {
169   theAttribute->setValue(theValue);
170 }
171 void fillAttribute(const char * theValue,
172                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
173 {
174   theAttribute->setValue(theValue);
175 }
176
177 //==================================================================================================
178 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
179 {
180   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
181
182   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(), ::tolower);
183
184   if(theShapeTypeStr == "compound") {
185     aShapeType = GeomAPI_Shape::COMPOUND;
186   } else if(theShapeTypeStr == "compsolid") {
187     aShapeType = GeomAPI_Shape::COMPSOLID;
188   } else if(theShapeTypeStr == "solid") {
189     aShapeType = GeomAPI_Shape::SOLID;
190   } else if(theShapeTypeStr == "shell") {
191     aShapeType = GeomAPI_Shape::SHELL;
192   } else if(theShapeTypeStr == "face") {
193     aShapeType = GeomAPI_Shape::FACE;
194   } else if(theShapeTypeStr == "wire") {
195     aShapeType = GeomAPI_Shape::WIRE;
196   } else if(theShapeTypeStr == "edge") {
197     aShapeType = GeomAPI_Shape::EDGE;
198   } else if(theShapeTypeStr == "vertex") {
199     aShapeType = GeomAPI_Shape::VERTEX;
200   } else if(theShapeTypeStr == "shape") {
201     aShapeType = GeomAPI_Shape::SHAPE;
202   }
203
204   return aShapeType;
205 }
206
207 //==================================================================================================
208 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
209 {
210   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
211
212   switch(theSelection.variantType()) {
213     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
214       ResultSubShapePair aPair = theSelection.resultSubShapePair();
215       GeomShapePtr aShape = aPair.second;
216       if(!aShape.get()) {
217         aShape = aPair.first->shape();
218       }
219       if(!aShape.get()) {
220         return aShapeType;
221       }
222       aShapeType = aShape->shapeType();
223       break;
224     }
225     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
226       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
227       std::string aType = aPair.first;
228       aShapeType = shapeTypeByStr(aType);
229       break;
230     }
231   }
232
233   return aShapeType;
234 }
235
236 //--------------------------------------------------------------------------------------