Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[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_Dumper.h>
31 #include <ModelHighAPI_RefAttr.h>
32 #include <ModelHighAPI_Selection.h>
33 #include <ModelHighAPI_Services.h>
34 #include <ModelHighAPI_Tools.h>
35 //--------------------------------------------------------------------------------------
36 #include "SketchAPI_Arc.h"
37 #include "SketchAPI_Circle.h"
38 #include "SketchAPI_IntersectionPoint.h"
39 #include "SketchAPI_Line.h"
40 #include "SketchAPI_Mirror.h"
41 #include "SketchAPI_Point.h"
42 #include "SketchAPI_Projection.h"
43 #include "SketchAPI_Rectangle.h"
44 #include "SketchAPI_Rotation.h"
45 #include "SketchAPI_Translation.h"
46 //--------------------------------------------------------------------------------------
47 SketchAPI_Sketch::SketchAPI_Sketch(
48     const std::shared_ptr<ModelAPI_Feature> & theFeature)
49 : ModelHighAPI_Interface(theFeature)
50 {
51   initialize();
52 }
53
54 SketchAPI_Sketch::SketchAPI_Sketch(
55     const std::shared_ptr<ModelAPI_Feature> & theFeature,
56     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
57 : ModelHighAPI_Interface(theFeature)
58 {
59   if (initialize()) {
60     setPlane(thePlane);
61   }
62 }
63
64 SketchAPI_Sketch::SketchAPI_Sketch(
65     const std::shared_ptr<ModelAPI_Feature> & theFeature,
66     const ModelHighAPI_Selection & theExternal)
67 : ModelHighAPI_Interface(theFeature)
68 {
69   if (initialize()) {
70     setExternal(theExternal);
71   }
72 }
73
74 SketchAPI_Sketch::~SketchAPI_Sketch()
75 {
76
77 }
78
79 //--------------------------------------------------------------------------------------
80 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
81 {
82   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
83 }
84
85 //--------------------------------------------------------------------------------------
86 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
87 {
88   fillAttribute(thePlane->origin(), myorigin);
89   fillAttribute(thePlane->dirX(), mydirX);
90   fillAttribute(thePlane->normal(), mynormal);
91
92   execute();
93 }
94
95 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
96 {
97   fillAttribute(theExternal, myexternal);
98
99   execute();
100 }
101
102 //--------------------------------------------------------------------------------------
103 void SketchAPI_Sketch::setValue(
104     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
105     const ModelHighAPI_Double & theValue)
106 {
107   // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
108   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
109
110 //  theConstraint->execute();
111 }
112
113 //--------------------------------------------------------------------------------------
114 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
115 {
116   const_cast<SketchAPI_Sketch*>(this)->execute();
117
118   std::list<ModelHighAPI_Selection> aSelectionList;
119
120   ResultConstructionPtr aResultConstruction =
121       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
122   if (aResultConstruction.get() == NULL)
123     return aSelectionList;
124
125   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
126     aSelectionList.push_back(
127         ModelHighAPI_Selection(aResultConstruction,
128                                aResultConstruction->face(anIndex)));
129   }
130
131   return aSelectionList;
132 }
133
134 //--------------------------------------------------------------------------------------
135 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
136                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
137 {
138   // TODO(spo): check that thePart is not empty
139   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
140   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
141 }
142
143 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
144                     const ModelHighAPI_Selection & theExternal)
145 {
146   // TODO(spo): check that thePart is not empty
147   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
148   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
149 }
150
151 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
152                     const std::string & theExternalName)
153 {
154   // TODO(spo): check that thePart is not empty
155   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
156   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
157 }
158
159 //--------------------------------------------------------------------------------------
160 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
161     double theX, double theY)
162 {
163   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
164   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
165 }
166 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
167     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
168 {
169   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
170   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
171 }
172 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
173 {
174   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
175   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
176 }
177 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
178 {
179   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
180   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
181 }
182
183 //--------------------------------------------------------------------------------------
184 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
185     const ModelHighAPI_Selection & theExternal)
186 {
187   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
188   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
189 }
190 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
191     const std::string & theExternalName)
192 {
193   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
194   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
195 }
196
197 //--------------------------------------------------------------------------------------
198 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
199 {
200   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
201   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
202 }
203 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
204     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
205     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
206 {
207   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
208   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
209 }
210 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
211 {
212   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
213   LinePtr aLine(new SketchAPI_Line(aFeature, theExternal));
214   setFixed(InterfacePtr(aLine));
215   return aLine;
216 }
217 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
218 {
219   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
220   LinePtr aLine(new SketchAPI_Line(aFeature, theExternalName));
221   setFixed(InterfacePtr(aLine));
222   return aLine;
223 }
224
225 //--------------------------------------------------------------------------------------
226 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1, double theX2, double theY2)
227 {
228   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
229   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
230 }
231 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
232     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
233     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
234 {
235   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
236   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
237 }
238
239 //--------------------------------------------------------------------------------------
240 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
241                                                               double theCenterY,
242                                                               double theRadius)
243 {
244   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
245   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
246 }
247
248 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
249                                                               double theRadius)
250 {
251   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
252   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
253 }
254
255 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
256                                                               double theX2, double theY2,
257                                                               double theX3, double theY3)
258 {
259   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
260   return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
261 }
262
263 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
264                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
265                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
266 {
267   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
268   return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
269 }
270
271 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
272 {
273   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
274   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
275 }
276
277 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
278 {
279   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
280   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
281   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
282 }
283
284 //--------------------------------------------------------------------------------------
285 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
286                                                         double theStartX, double theStartY,
287                                                         double theEndX, double theEndY,
288                                                         bool theInversed)
289 {
290   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
291   return ArcPtr(new SketchAPI_Arc(aFeature,
292                                   theCenterX, theCenterY,
293                                   theStartX, theStartY,
294                                   theEndX, theEndY,
295                                   theInversed));
296 }
297
298 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
299                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
300                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
301                                                         bool theInversed)
302 {
303   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
304   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
305 }
306
307 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
308                                                         double theEndX, double theEndY,
309                                                         double thePassedX, double thePassedY)
310 {
311   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
312   return ArcPtr(new SketchAPI_Arc(aFeature,
313                                   theStartX, theStartY,
314                                   theEndX, theEndY,
315                                   thePassedX, thePassedY));
316 }
317
318 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
319                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
320                                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
321 {
322   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
323   return ArcPtr(new SketchAPI_Arc(aFeature, theStart, theEnd, thePassed));
324 }
325
326 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
327                                                         double theEndX, double theEndY,
328                                                         bool theInversed)
329 {
330   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
331   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEndX, theEndY, theInversed));
332 }
333
334 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
335                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
336                                                         bool theInversed)
337 {
338   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
339   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEnd, theInversed));
340 }
341
342 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
343 {
344   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
345   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
346 }
347
348 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
349 {
350   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
351   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
352   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
353 }
354
355 //--------------------------------------------------------------------------------------
356 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
357     const ModelHighAPI_Selection & theExternalFeature)
358 {
359   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Projection::ID());
360   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
361 }
362
363 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
364     const std::string & theExternalName)
365 {
366   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Projection::ID());
367   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalName));
368 }
369
370 //--------------------------------------------------------------------------------------
371 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
372     const ModelHighAPI_RefAttr & theMirrorLine,
373     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
374 {
375   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
376   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
377 }
378
379 //--------------------------------------------------------------------------------------
380 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
381     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
382     const ModelHighAPI_RefAttr & thePoint1,
383     const ModelHighAPI_RefAttr & thePoint2,
384     const ModelHighAPI_Integer & theNumberOfObjects,
385     bool theFullValue)
386 {
387   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
388   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1, thePoint2, theNumberOfObjects, theFullValue));
389 }
390
391 //--------------------------------------------------------------------------------------
392 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
393     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
394     const ModelHighAPI_RefAttr & theCenter,
395     const ModelHighAPI_Double & theAngle,
396     const ModelHighAPI_Integer & theNumberOfObjects,
397     bool theFullValue)
398 {
399   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
400   return RotationPtr(new SketchAPI_Rotation(aFeature, theObjects, theCenter, theAngle, theNumberOfObjects, theFullValue));
401 }
402
403 //--------------------------------------------------------------------------------------
404 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setAngle(
405     const ModelHighAPI_RefAttr & theLine1,
406     const ModelHighAPI_RefAttr & theLine2,
407     const ModelHighAPI_Double & theValue)
408 {
409   // TODO(spo): is support of angle type necessary?
410   std::shared_ptr<ModelAPI_Feature> aFeature =
411       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
412   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
413   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
414   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
415   aFeature->execute();
416   return aFeature;
417 }
418
419 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
420     const ModelHighAPI_RefAttr & thePoint1,
421     const ModelHighAPI_RefAttr & thePoint2)
422 {
423   std::shared_ptr<ModelAPI_Feature> aFeature =
424       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
425   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
426   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
427   aFeature->execute();
428   return aFeature;
429 }
430
431 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCollinear(
432     const ModelHighAPI_RefAttr & theLine1,
433     const ModelHighAPI_RefAttr & theLine2)
434 {
435   std::shared_ptr<ModelAPI_Feature> aFeature =
436       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
437   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
438   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
439   aFeature->execute();
440   return aFeature;
441 }
442
443 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
444     const ModelHighAPI_RefAttr & thePoint,
445     const ModelHighAPI_RefAttr & thePointOrLine,
446     const ModelHighAPI_Double & theValue)
447 {
448   std::shared_ptr<ModelAPI_Feature> aFeature =
449       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
450   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
451   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
452   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
453   aFeature->execute();
454   return aFeature;
455 }
456
457 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setEqual(
458     const ModelHighAPI_RefAttr & theObject1,
459     const ModelHighAPI_RefAttr & theObject2)
460 {
461   std::shared_ptr<ModelAPI_Feature> aFeature =
462       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
463   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
464   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
465   aFeature->execute();
466   return aFeature;
467 }
468
469 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
470     const std::list<ModelHighAPI_RefAttr> & thePoints,
471     const ModelHighAPI_Double & theRadius)
472 {
473   std::shared_ptr<ModelAPI_Feature> aFeature =
474       compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
475   fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
476   fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
477   aFeature->execute();
478   return aFeature;
479 }
480
481 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFixed(
482     const ModelHighAPI_RefAttr & theObject)
483 {
484   std::shared_ptr<ModelAPI_Feature> aFeature =
485       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
486   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
487   aFeature->execute();
488   return aFeature;
489 }
490
491 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
492     const ModelHighAPI_RefAttr & theLine)
493 {
494   std::shared_ptr<ModelAPI_Feature> aFeature =
495       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
496   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
497   aFeature->execute();
498   return aFeature;
499 }
500
501 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
502     const ModelHighAPI_RefAttr & theLine,
503     const ModelHighAPI_Double & theValue)
504 {
505   std::shared_ptr<ModelAPI_Feature> aFeature =
506       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
507   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
508   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
509   aFeature->execute();
510   return aFeature;
511 }
512
513 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setMiddlePoint(
514     const ModelHighAPI_RefAttr & thePoint,
515     const ModelHighAPI_RefAttr & theLine)
516 {
517   std::shared_ptr<ModelAPI_Feature> aFeature =
518       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
519   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
520   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
521   aFeature->execute();
522   return aFeature;
523 }
524
525 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
526     const ModelHighAPI_RefAttr & theLine1,
527     const ModelHighAPI_RefAttr & theLine2)
528 {
529   std::shared_ptr<ModelAPI_Feature> aFeature =
530       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
531   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
532   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
533   aFeature->execute();
534   return aFeature;
535 }
536
537 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
538     const ModelHighAPI_RefAttr & theLine1,
539     const ModelHighAPI_RefAttr & theLine2)
540 {
541   std::shared_ptr<ModelAPI_Feature> aFeature =
542       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
543   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
544   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
545   aFeature->execute();
546   return aFeature;
547 }
548
549 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRadius(
550     const ModelHighAPI_RefAttr & theCircleOrArc,
551     const ModelHighAPI_Double & theValue)
552 {
553   std::shared_ptr<ModelAPI_Feature> aFeature =
554       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
555   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
556   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
557   aFeature->execute();
558   return aFeature;
559 }
560
561 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setTangent(
562     const ModelHighAPI_RefAttr & theLine,
563     const ModelHighAPI_RefAttr & theCircle)
564 {
565   std::shared_ptr<ModelAPI_Feature> aFeature =
566       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
567   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
568   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
569   aFeature->execute();
570   return aFeature;
571 }
572
573 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
574     const ModelHighAPI_RefAttr & theLine)
575 {
576   std::shared_ptr<ModelAPI_Feature> aFeature =
577       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
578   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
579   aFeature->execute();
580   return aFeature;
581 }
582
583 //--------------------------------------------------------------------------------------
584
585 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
586 {
587   FeaturePtr aBase = feature();
588   const std::string& aDocName = theDumper.name(aBase->document());
589
590   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
591   if (anExternal->value()) {
592     FeaturePtr aPlnFeature = ModelAPI_Feature::feature(anExternal->context()->data()->owner());
593     const std::string& aPlaneName = theDumper.name(aPlnFeature);
594     theDumper << aBase << " = model.addSketch(" << aDocName << ", \"" << aPlaneName << "\")" << std::endl;
595   } else {
596     // Sketch is base on a plane.
597     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
598         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
599     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
600         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
601     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
602         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
603
604     // Check the plane is coordinate plane
605     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
606     if (aPlaneName.empty()) {
607       // needs import additional module
608       theDumper.importModule("GeomAPI");
609       // dump plane parameters
610       const std::string& aSketchName = theDumper.name(aBase);
611       std::string anOriginName = aSketchName + "_origin";
612       std::string aNormalName  = aSketchName + "_norm";
613       std::string aDirXName    = aSketchName + "_dirx";
614       theDumper << anOriginName << " = " << anOrigin << std::endl
615                 << aNormalName  << " = " << aNormal  << std::endl
616                 << aDirXName    << " = " << aDirX    << std::endl;
617       // dump sketch based on arbitrary plane
618       theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
619                 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
620     } else {
621       // dump sketch based on coordinate plane
622       theDumper << aBase << " = model.addSketch(" << aDocName
623                 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
624     }
625   }
626 }