Salome HOME
27ba10f6332f3a2cb93d1020ddbb0ea2fb1fb6af
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Name   : SketchAPI_Sketch.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "SketchAPI_Sketch.h"
9 //--------------------------------------------------------------------------------------
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintFillet.h>
17 #include <SketchPlugin_ConstraintHorizontal.h>
18 #include <SketchPlugin_ConstraintLength.h>
19 #include <SketchPlugin_ConstraintMiddle.h>
20 #include <SketchPlugin_ConstraintMirror.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
27 //--------------------------------------------------------------------------------------
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelHighAPI_RefAttr.h>
31 #include <ModelHighAPI_Selection.h>
32 #include <ModelHighAPI_Tools.h>
33 //--------------------------------------------------------------------------------------
34 #include "SketchAPI_Circle.h"
35 #include "SketchAPI_Line.h"
36 //--------------------------------------------------------------------------------------
37 SketchAPI_Sketch::SketchAPI_Sketch(
38     const std::shared_ptr<ModelAPI_Feature> & theFeature)
39 : ModelHighAPI_Interface(theFeature)
40 {
41   initialize();
42 }
43
44 SketchAPI_Sketch::SketchAPI_Sketch(
45     const std::shared_ptr<ModelAPI_Feature> & theFeature,
46     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
47 : ModelHighAPI_Interface(theFeature)
48 {
49   if (initialize()) {
50     setPlane(thePlane);
51   }
52 }
53
54 SketchAPI_Sketch::SketchAPI_Sketch(
55     const std::shared_ptr<ModelAPI_Feature> & theFeature,
56     const ModelHighAPI_Selection & theExternal)
57 : ModelHighAPI_Interface(theFeature)
58 {
59   if (initialize()) {
60     setExternal(theExternal);
61   }
62 }
63
64 SketchAPI_Sketch::~SketchAPI_Sketch()
65 {
66
67 }
68
69 //--------------------------------------------------------------------------------------
70 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
71 {
72   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
73 }
74
75 //--------------------------------------------------------------------------------------
76 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
77 {
78   fillAttribute(thePlane->origin(), myorigin);
79   fillAttribute(thePlane->dirX(), mydirX);
80   fillAttribute(thePlane->normal(), mynormal);
81
82   execute();
83 }
84
85 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
86 {
87   fillAttribute(theExternal, myexternal);
88
89   execute();
90 }
91
92 //--------------------------------------------------------------------------------------
93 void SketchAPI_Sketch::setValue(
94     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
95     const ModelHighAPI_Double & theValue)
96 {
97   // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
98   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
99
100 //  theConstraint->execute();
101 }
102
103 //--------------------------------------------------------------------------------------
104 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
105 {
106   const_cast<SketchAPI_Sketch*>(this)->execute();
107
108   std::list<ModelHighAPI_Selection> aSelectionList;
109
110   ResultConstructionPtr aResultConstruction =
111       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
112   if (aResultConstruction.get() == NULL)
113     return aSelectionList;
114
115   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
116     aSelectionList.push_back(
117         ModelHighAPI_Selection(aResultConstruction,
118                                aResultConstruction->face(anIndex)));
119   }
120
121   return aSelectionList;
122 }
123
124 //--------------------------------------------------------------------------------------
125 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
126                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
127 {
128   // TODO(spo): check that thePart is not empty
129   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
130   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
131 }
132
133 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
134                     const ModelHighAPI_Selection & theExternal)
135 {
136   // TODO(spo): check that thePart is not empty
137   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
138   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
139 }
140
141 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
142                     const std::string & theExternalName)
143 {
144   // TODO(spo): check that thePart is not empty
145   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
146   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
147 }
148
149 //--------------------------------------------------------------------------------------
150 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
151 {
152   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
153   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
154 }
155 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
156     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
157     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
158 {
159   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
160   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
161 }
162 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
163 {
164   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
165   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
166 }
167 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
168 {
169   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
170   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
171   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
172 }
173
174 //--------------------------------------------------------------------------------------
175 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
176                                                               double theCenterY,
177                                                               double theRadius)
178 {
179   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
180   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
181 }
182
183 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
184                                                               double theRadius)
185 {
186   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
187   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
188 }
189
190 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
191                                                               double theX2, double theY2,
192                                                               double theX3, double theY3)
193 {
194   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
195   return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
196 }
197
198 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
199                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
200                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
201 {
202   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
203   return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
204 }
205
206 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
207 {
208   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
209   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
210 }
211
212 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
213 {
214   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
215   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
216   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
217 }
218
219 //--------------------------------------------------------------------------------------
220 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
221     const ModelHighAPI_RefAttr & thePoint1,
222     const ModelHighAPI_RefAttr & thePoint2)
223 {
224   std::shared_ptr<ModelAPI_Feature> aFeature =
225       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
226   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
227   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
228   aFeature->execute();
229   return aFeature;
230 }
231
232 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
233     const ModelHighAPI_RefAttr & thePoint,
234     const ModelHighAPI_RefAttr & theLine,
235     const ModelHighAPI_Double & theValue)
236 {
237   std::shared_ptr<ModelAPI_Feature> aFeature =
238       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
239   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
240   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
241   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
242   aFeature->execute();
243   return aFeature;
244 }
245
246 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
247     const ModelHighAPI_RefAttr & theLine,
248     const ModelHighAPI_Double & theValue)
249 {
250   std::shared_ptr<ModelAPI_Feature> aFeature =
251       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
252   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
253   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
254   aFeature->execute();
255   return aFeature;
256 }
257
258 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
259     const ModelHighAPI_RefAttr & theLine1,
260     const ModelHighAPI_RefAttr & theLine2)
261 {
262   std::shared_ptr<ModelAPI_Feature> aFeature =
263       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
264   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
265   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
266   aFeature->execute();
267   return aFeature;
268 }
269
270 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
271     const ModelHighAPI_RefAttr & theLine1,
272     const ModelHighAPI_RefAttr & theLine2)
273 {
274   std::shared_ptr<ModelAPI_Feature> aFeature =
275       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
276   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
277   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
278   aFeature->execute();
279   return aFeature;
280 }
281 //--------------------------------------------------------------------------------------