]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Tools.cpp
Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[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 void fillAttribute(int theValue,
92                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
93 {
94   theAttribute->setValue(theValue);
95 }
96
97 //--------------------------------------------------------------------------------------
98 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
99                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
100 {
101   theValue.fillAttribute(theAttribute);
102 }
103
104 //--------------------------------------------------------------------------------------
105 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
106                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
107 {
108   theAttribute->clear();
109   for (auto it = theValue.begin(); it != theValue.end(); ++it)
110     it->appendToList(theAttribute);
111 }
112
113 //--------------------------------------------------------------------------------------
114 void fillAttribute(const ModelHighAPI_Reference & theValue,
115                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
116 {
117   theValue.fillAttribute(theAttribute);
118 }
119
120 //--------------------------------------------------------------------------------------
121 void fillAttribute(const std::list<ModelHighAPI_Reference> & theValue,
122                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
123 {
124   theAttribute->clear();
125   for (auto it = theValue.begin(); it != theValue.end(); ++it)
126     it->appendToList(theAttribute);
127 }
128
129 //--------------------------------------------------------------------------------------
130 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
131                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
132 {
133   theAttribute->setValue(theValue);
134 }
135
136 //--------------------------------------------------------------------------------------
137 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
138                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
139 {
140   theAttribute->clear();
141   for (auto it = theValue.begin(); it != theValue.end(); ++it)
142     theAttribute->append(*it);
143 }
144
145 MODELHIGHAPI_EXPORT
146 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
147                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
148 {
149   theAttribute->clear();
150   for (auto it = theValue.begin(); it != theValue.end(); ++it)
151     theAttribute->append(it->resultSubShapePair().first); // use only context
152 }
153
154 //--------------------------------------------------------------------------------------
155 void fillAttribute(const ModelHighAPI_Selection & theValue,
156                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
157 {
158   theValue.fillAttribute(theAttribute);
159 }
160
161 //--------------------------------------------------------------------------------------
162 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
163                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
164 {
165   theAttribute->clear();
166   for (auto it = theValue.begin(); it != theValue.end(); ++it)
167     it->appendToList(theAttribute);
168 }
169
170 //--------------------------------------------------------------------------------------
171 void fillAttribute(const std::string & theValue,
172                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
173 {
174   theAttribute->setValue(theValue);
175 }
176 void fillAttribute(const char * theValue,
177                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
178 {
179   theAttribute->setValue(theValue);
180 }
181
182 //==================================================================================================
183 GeomAPI_Shape::ShapeType shapeTypeByStr(std::string theShapeTypeStr)
184 {
185   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
186
187   std::transform(theShapeTypeStr.begin(), theShapeTypeStr.end(), theShapeTypeStr.begin(), ::tolower);
188
189   if(theShapeTypeStr == "compound") {
190     aShapeType = GeomAPI_Shape::COMPOUND;
191   } else if(theShapeTypeStr == "compsolid") {
192     aShapeType = GeomAPI_Shape::COMPSOLID;
193   } else if(theShapeTypeStr == "solid") {
194     aShapeType = GeomAPI_Shape::SOLID;
195   } else if(theShapeTypeStr == "shell") {
196     aShapeType = GeomAPI_Shape::SHELL;
197   } else if(theShapeTypeStr == "face") {
198     aShapeType = GeomAPI_Shape::FACE;
199   } else if(theShapeTypeStr == "wire") {
200     aShapeType = GeomAPI_Shape::WIRE;
201   } else if(theShapeTypeStr == "edge") {
202     aShapeType = GeomAPI_Shape::EDGE;
203   } else if(theShapeTypeStr == "vertex") {
204     aShapeType = GeomAPI_Shape::VERTEX;
205   } else if(theShapeTypeStr == "shape") {
206     aShapeType = GeomAPI_Shape::SHAPE;
207   }
208
209   return aShapeType;
210 }
211
212 //==================================================================================================
213 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
214 {
215   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
216
217   switch(theSelection.variantType()) {
218     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
219       ResultSubShapePair aPair = theSelection.resultSubShapePair();
220       GeomShapePtr aShape = aPair.second;
221       if(!aShape.get()) {
222         aShape = aPair.first->shape();
223       }
224       if(!aShape.get()) {
225         return aShapeType;
226       }
227       aShapeType = aShape->shapeType();
228       break;
229     }
230     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
231       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
232       std::string aType = aPair.first;
233       aShapeType = shapeTypeByStr(aType);
234       break;
235     }
236   }
237
238   return aShapeType;
239 }
240
241 //--------------------------------------------------------------------------------------