Salome HOME
Issue #1650: Added CPP High API for feature Axis;
[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_Selection.h"
35
36 #include <algorithm>
37
38 //--------------------------------------------------------------------------------------
39 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt2d> & theValue,
40                    const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute)
41 {
42   theAttribute->setValue(theValue);
43 }
44
45 void fillAttribute(const std::shared_ptr<GeomDataAPI_Point2D> & theAttribute,
46                    double theX, double theY)
47 {
48   theAttribute->setValue(theX, theY);
49 }
50
51 //--------------------------------------------------------------------------------------
52 void fillAttribute(const std::shared_ptr<GeomAPI_Dir> & theValue,
53                    const std::shared_ptr<GeomDataAPI_Dir> & theAttribute)
54 {
55   theAttribute->setValue(theValue);
56 }
57
58 //--------------------------------------------------------------------------------------
59 void fillAttribute(const std::shared_ptr<GeomAPI_Pnt> & theValue,
60                    const std::shared_ptr<GeomDataAPI_Point> & theAttribute)
61 {
62   theAttribute->setValue(theValue);
63 }
64
65 //--------------------------------------------------------------------------------------
66 void fillAttribute(bool theValue,
67                    const std::shared_ptr<ModelAPI_AttributeBoolean> & theAttribute)
68 {
69   theAttribute->setValue(theValue);
70 }
71
72 //--------------------------------------------------------------------------------------
73 void fillAttribute(const ModelHighAPI_Double & theValue,
74                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
75 {
76   theValue.fillAttribute(theAttribute);
77 }
78 void fillAttribute(double theValue,
79                    const std::shared_ptr<ModelAPI_AttributeDouble> & theAttribute)
80 {
81   theAttribute->setValue(theValue);
82 }
83
84 //--------------------------------------------------------------------------------------
85 void fillAttribute(const ModelHighAPI_Integer & theValue,
86                    const std::shared_ptr<ModelAPI_AttributeInteger> & theAttribute)
87 {
88   theValue.fillAttribute(theAttribute);
89 }
90
91 //--------------------------------------------------------------------------------------
92 void fillAttribute(const ModelHighAPI_RefAttr & theValue,
93                    const std::shared_ptr<ModelAPI_AttributeRefAttr> & theAttribute)
94 {
95   theValue.fillAttribute(theAttribute);
96 }
97
98 //--------------------------------------------------------------------------------------
99 void fillAttribute(const std::list<ModelHighAPI_RefAttr> & theValue,
100                    const std::shared_ptr<ModelAPI_AttributeRefAttrList> & theAttribute)
101 {
102   theAttribute->clear();
103   for (auto it = theValue.begin(); it != theValue.end(); ++it)
104     it->appendToList(theAttribute);
105 }
106
107 //--------------------------------------------------------------------------------------
108 void fillAttribute(const std::shared_ptr<ModelAPI_Object> & theValue,
109                    const std::shared_ptr<ModelAPI_AttributeReference> & theAttribute)
110 {
111   theAttribute->setValue(theValue);
112 }
113
114 //--------------------------------------------------------------------------------------
115 void fillAttribute(const std::list<std::shared_ptr<ModelAPI_Object> > & theValue,
116                    const std::shared_ptr<ModelAPI_AttributeRefList> & theAttribute)
117 {
118   theAttribute->clear();
119   for (auto it = theValue.begin(); it != theValue.end(); ++it)
120     theAttribute->append(*it);
121 }
122
123 //--------------------------------------------------------------------------------------
124 void fillAttribute(const ModelHighAPI_Selection & theValue,
125                    const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
126 {
127   theValue.fillAttribute(theAttribute);
128 }
129
130 //--------------------------------------------------------------------------------------
131 void fillAttribute(const std::list<ModelHighAPI_Selection> & theValue,
132                    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
133 {
134   theAttribute->clear();
135   for (auto it = theValue.begin(); it != theValue.end(); ++it)
136     it->appendToList(theAttribute);
137 }
138
139 //--------------------------------------------------------------------------------------
140 void fillAttribute(const std::string & theValue,
141                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
142 {
143   theAttribute->setValue(theValue);
144 }
145 void fillAttribute(const char * theValue,
146                    const std::shared_ptr<ModelAPI_AttributeString> & theAttribute)
147 {
148   theAttribute->setValue(theValue);
149 }
150
151 //==================================================================================================
152 GeomAPI_Shape::ShapeType shapeTypeByStr(const std::string& theShapeTypeStr)
153 {
154   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
155
156   std::string aShapeTypeStr = theShapeTypeStr;
157   std::transform(aShapeTypeStr.begin(), aShapeTypeStr.end(), aShapeTypeStr.begin(), ::tolower);
158
159   if(theShapeTypeStr == "compound") {
160     aShapeType = GeomAPI_Shape::COMPOUND;
161   } else if(theShapeTypeStr == "compsolid") {
162     aShapeType = GeomAPI_Shape::COMPSOLID;
163   } else if(theShapeTypeStr == "solid") {
164     aShapeType = GeomAPI_Shape::SOLID;
165   } else if(theShapeTypeStr == "shell") {
166     aShapeType = GeomAPI_Shape::SHELL;
167   } else if(theShapeTypeStr == "face") {
168     aShapeType = GeomAPI_Shape::FACE;
169   } else if(theShapeTypeStr == "wire") {
170     aShapeType = GeomAPI_Shape::WIRE;
171   } else if(theShapeTypeStr == "edge") {
172     aShapeType = GeomAPI_Shape::EDGE;
173   } else if(theShapeTypeStr == "vertex") {
174     aShapeType = GeomAPI_Shape::VERTEX;
175   } else if(theShapeTypeStr == "shape") {
176     aShapeType = GeomAPI_Shape::SHAPE;
177   }
178
179   return aShapeType;
180 }
181
182 //==================================================================================================
183 GeomAPI_Shape::ShapeType getShapeType(const ModelHighAPI_Selection& theSelection)
184 {
185   GeomAPI_Shape::ShapeType aShapeType = GeomAPI_Shape::SHAPE;
186
187   switch(theSelection.variantType()) {
188     case ModelHighAPI_Selection::VT_ResultSubShapePair: {
189       ResultSubShapePair aPair = theSelection.resultSubShapePair();
190       GeomShapePtr aShape = aPair.second;
191       if(!aShape.get()) {
192         aShape = aPair.first->shape();
193       }
194       if(!aShape.get()) {
195         return aShapeType;
196       }
197       aShapeType = aShape->shapeType();
198       break;
199     }
200     case ModelHighAPI_Selection::VT_TypeSubShapeNamePair: {
201       TypeSubShapeNamePair aPair = theSelection.typeSubShapeNamePair();
202       std::string aType = aPair.first;
203       aShapeType = shapeTypeByStr(aType);
204       break;
205     }
206   }
207
208   return aShapeType;
209 }
210
211 //--------------------------------------------------------------------------------------