Salome HOME
2af8499870ea1142e74d4fd4b7429b30e6190f56
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : SketchAPI_Sketch.cpp
3 // Purpose:
4 //
5 // History:
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "SketchAPI_Sketch.h"
10 //--------------------------------------------------------------------------------------
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_ConstraintAngle.h>
13 #include <SketchPlugin_ConstraintCoincidence.h>
14 #include <SketchPlugin_ConstraintCollinear.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
17 #include <SketchPlugin_ConstraintDistanceVertical.h>
18 #include <SketchPlugin_ConstraintEqual.h>
19 #include <SketchPlugin_Fillet.h>
20 #include <SketchPlugin_ConstraintHorizontal.h>
21 #include <SketchPlugin_ConstraintLength.h>
22 #include <SketchPlugin_ConstraintMiddle.h>
23 #include <SketchPlugin_ConstraintMirror.h>
24 #include <SketchPlugin_ConstraintParallel.h>
25 #include <SketchPlugin_ConstraintPerpendicular.h>
26 #include <SketchPlugin_ConstraintRadius.h>
27 #include <SketchPlugin_ConstraintRigid.h>
28 #include <SketchPlugin_ConstraintSplit.h>
29 #include <SketchPlugin_Trim.h>
30 #include <SketchPlugin_ConstraintTangent.h>
31 #include <SketchPlugin_ConstraintVertical.h>
32 #include <SketcherPrs_Tools.h>
33 //--------------------------------------------------------------------------------------
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_CompositeFeature.h>
36 #include <ModelAPI_ResultConstruction.h>
37 #include <ModelHighAPI_Double.h>
38 #include <ModelHighAPI_Dumper.h>
39 #include <ModelHighAPI_RefAttr.h>
40 #include <ModelHighAPI_Selection.h>
41 #include <ModelHighAPI_Services.h>
42 #include <ModelHighAPI_Tools.h>
43 //--------------------------------------------------------------------------------------
44 #include "SketchAPI_Arc.h"
45 #include "SketchAPI_MacroArc.h"
46 #include "SketchAPI_Circle.h"
47 #include "SketchAPI_IntersectionPoint.h"
48 #include "SketchAPI_Line.h"
49 #include "SketchAPI_MacroCircle.h"
50 #include "SketchAPI_Mirror.h"
51 #include "SketchAPI_Point.h"
52 #include "SketchAPI_Projection.h"
53 #include "SketchAPI_Rectangle.h"
54 #include "SketchAPI_Rotation.h"
55 #include "SketchAPI_Translation.h"
56 //--------------------------------------------------------------------------------------
57 #include <GeomAPI_Dir2d.h>
58 #include <GeomAPI_XY.h>
59 #include <cmath>
60 //--------------------------------------------------------------------------------------
61 SketchAPI_Sketch::SketchAPI_Sketch(
62     const std::shared_ptr<ModelAPI_Feature> & theFeature)
63 : ModelHighAPI_Interface(theFeature)
64 {
65   initialize();
66 }
67
68 SketchAPI_Sketch::SketchAPI_Sketch(
69     const std::shared_ptr<ModelAPI_Feature> & theFeature,
70     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
71 : ModelHighAPI_Interface(theFeature)
72 {
73   if (initialize()) {
74     setPlane(thePlane);
75   }
76 }
77
78 SketchAPI_Sketch::SketchAPI_Sketch(
79     const std::shared_ptr<ModelAPI_Feature> & theFeature,
80     const ModelHighAPI_Selection & theExternal)
81 : ModelHighAPI_Interface(theFeature)
82 {
83   if (initialize()) {
84     setExternal(theExternal);
85   }
86 }
87
88 SketchAPI_Sketch::SketchAPI_Sketch(
89     const std::shared_ptr<ModelAPI_Feature> & theFeature,
90     std::shared_ptr<ModelAPI_Object> thePlaneObject)
91 : ModelHighAPI_Interface(theFeature)
92 {
93   if (initialize()) {
94     setExternal(thePlaneObject);
95   }
96 }
97
98 SketchAPI_Sketch::~SketchAPI_Sketch()
99 {
100
101 }
102
103 //--------------------------------------------------------------------------------------
104 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
105 {
106   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
107 }
108
109 //--------------------------------------------------------------------------------------
110 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
111 {
112   fillAttribute(thePlane->origin(), myorigin);
113   fillAttribute(thePlane->dirX(), mydirX);
114   fillAttribute(thePlane->normal(), mynormal);
115
116   execute();
117 }
118
119 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
120 {
121   fillAttribute(theExternal, myexternal);
122
123   execute();
124 }
125
126 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
127 {
128   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
129   ModelHighAPI_Selection aSel(aRes);
130   setExternal(aSel);
131 }
132
133 //--------------------------------------------------------------------------------------
134 void SketchAPI_Sketch::setValue(
135     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
136     const ModelHighAPI_Double & theValue)
137 {
138   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
139
140 //  theConstraint->execute();
141 }
142
143 //--------------------------------------------------------------------------------------
144 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
145 {
146   const_cast<SketchAPI_Sketch*>(this)->execute();
147
148   std::list<ModelHighAPI_Selection> aSelectionList;
149
150   ResultConstructionPtr aResultConstruction =
151       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
152   if (aResultConstruction.get() == NULL)
153     return aSelectionList;
154
155   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
156     aSelectionList.push_back(
157         ModelHighAPI_Selection(aResultConstruction,
158                                aResultConstruction->face(anIndex)));
159   }
160
161   return aSelectionList;
162 }
163
164 //--------------------------------------------------------------------------------------
165 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
166                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
167 {
168   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
169   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
170 }
171
172 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
173                     const ModelHighAPI_Selection & theExternal)
174 {
175   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
176   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
177 }
178
179 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
180                     const std::string & theExternalName)
181 {
182   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
183   return SketchPtr(
184     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
185 }
186
187 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
188                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
189 {
190   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
191   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
192 }
193
194
195 //--------------------------------------------------------------------------------------
196 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
197     double theX, double theY)
198 {
199   std::shared_ptr<ModelAPI_Feature> aFeature =
200     compositeFeature()->addFeature(SketchPlugin_Point::ID());
201   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
202 }
203 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
204     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
205 {
206   std::shared_ptr<ModelAPI_Feature> aFeature =
207     compositeFeature()->addFeature(SketchPlugin_Point::ID());
208   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
209 }
210 std::shared_ptr<SketchAPI_Point>
211   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
212 {
213   std::shared_ptr<ModelAPI_Feature> aFeature =
214     compositeFeature()->addFeature(SketchPlugin_Point::ID());
215   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
216 }
217 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
218 {
219   std::shared_ptr<ModelAPI_Feature> aFeature =
220     compositeFeature()->addFeature(SketchPlugin_Point::ID());
221   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
222 }
223
224 //--------------------------------------------------------------------------------------
225 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
226     const ModelHighAPI_Selection & theExternal)
227 {
228   std::shared_ptr<ModelAPI_Feature> aFeature =
229     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
230   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
231 }
232 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
233     const std::string & theExternalName)
234 {
235   std::shared_ptr<ModelAPI_Feature> aFeature =
236     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
237   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
238 }
239
240 //--------------------------------------------------------------------------------------
241 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
242                                                           double theX2, double theY2)
243 {
244   std::shared_ptr<ModelAPI_Feature> aFeature =
245     compositeFeature()->addFeature(SketchPlugin_Line::ID());
246   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
247 }
248 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
249     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
250     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
251 {
252   std::shared_ptr<ModelAPI_Feature> aFeature =
253     compositeFeature()->addFeature(SketchPlugin_Line::ID());
254   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
255 }
256 std::shared_ptr<SketchAPI_Line>
257   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
258 {
259   std::shared_ptr<ModelAPI_Feature> aFeature =
260     compositeFeature()->addFeature(SketchPlugin_Line::ID());
261   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
262 }
263 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
264 {
265   std::shared_ptr<ModelAPI_Feature> aFeature =
266     compositeFeature()->addFeature(SketchPlugin_Line::ID());
267   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
268 }
269
270 //--------------------------------------------------------------------------------------
271 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
272                                                                     double theX2, double theY2)
273 {
274   std::shared_ptr<ModelAPI_Feature> aFeature =
275     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
276   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
277 }
278 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
279     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
280     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
281 {
282   std::shared_ptr<ModelAPI_Feature> aFeature =
283     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
284   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
285 }
286
287 //--------------------------------------------------------------------------------------
288 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
289                                                               double theCenterY,
290                                                               double theRadius)
291 {
292   std::shared_ptr<ModelAPI_Feature> aFeature =
293     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
294   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
295 }
296
297 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
298                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
299                                     double theRadius)
300 {
301   std::shared_ptr<ModelAPI_Feature> aFeature =
302     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
303   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
304 }
305
306 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
307                                                                    double theCenterY,
308                                                                    double thePassedX,
309                                                                    double thePassedY)
310 {
311   std::shared_ptr<ModelAPI_Feature> aFeature =
312     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
313   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
314                                                             thePassedX, thePassedY));
315 }
316
317 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
318     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
319     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
320 {
321   std::shared_ptr<ModelAPI_Feature> aFeature =
322     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
323   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
324 }
325
326 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
327                                                                    double theX2, double theY2,
328                                                                    double theX3, double theY3)
329 {
330   std::shared_ptr<ModelAPI_Feature> aFeature =
331     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
332   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
333                                                             theX2, theY2,
334                                                             theX3, theY3));
335 }
336
337 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
338     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
339     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
340     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
341 {
342   std::shared_ptr<ModelAPI_Feature> aFeature =
343     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
344   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
345 }
346
347 std::shared_ptr<SketchAPI_Circle>
348   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
349 {
350   std::shared_ptr<ModelAPI_Feature> aFeature =
351     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
352   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
353 }
354
355 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
356 {
357   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
358   std::shared_ptr<ModelAPI_Feature> aFeature =
359     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
360   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
361 }
362
363 //--------------------------------------------------------------------------------------
364 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
365                                                         double theStartX, double theStartY,
366                                                         double theEndX, double theEndY,
367                                                         bool theInversed)
368 {
369   std::shared_ptr<ModelAPI_Feature> aFeature =
370     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
371   return ArcPtr(new SketchAPI_Arc(aFeature,
372                                   theCenterX, theCenterY,
373                                   theStartX, theStartY,
374                                   theEndX, theEndY,
375                                   theInversed));
376 }
377
378 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
379                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
380                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
381                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
382                                               bool theInversed)
383 {
384   std::shared_ptr<ModelAPI_Feature> aFeature =
385     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
386   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
387 }
388
389 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
390                                                         double theEndX, double theEndY,
391                                                         double thePassedX, double thePassedY)
392 {
393   std::shared_ptr<ModelAPI_Feature> aFeature =
394     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
395   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
396                                        theStartX, theStartY,
397                                        theEndX, theEndY,
398                                        thePassedX, thePassedY));
399 }
400
401 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
402                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
403                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
404                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
405 {
406   std::shared_ptr<ModelAPI_Feature> aFeature =
407     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
408   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
409 }
410
411 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
412                                                 const ModelHighAPI_RefAttr& theTangentPoint,
413                                                 double theEndX, double theEndY,
414                                                 bool theInversed)
415 {
416   std::shared_ptr<ModelAPI_Feature> aFeature =
417     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
418   return MacroArcPtr(new SketchAPI_MacroArc(
419     aFeature, theTangentPoint, theEndX, theEndY, theInversed));
420 }
421
422 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
423                                               const ModelHighAPI_RefAttr& theTangentPoint,
424                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
425                                               bool theInversed)
426 {
427   std::shared_ptr<ModelAPI_Feature> aFeature =
428     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
429   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theTangentPoint, theEnd, theInversed));
430 }
431
432 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
433 {
434   std::shared_ptr<ModelAPI_Feature> aFeature =
435     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
436   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
437 }
438
439 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
440 {
441   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
442   std::shared_ptr<ModelAPI_Feature> aFeature =
443     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
444   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
445 }
446
447 //--------------------------------------------------------------------------------------
448 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
449     const ModelHighAPI_Selection & theExternalFeature)
450 {
451   std::shared_ptr<ModelAPI_Feature> aFeature =
452     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
453   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
454 }
455
456 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
457     const std::string & theExternalName)
458 {
459   std::shared_ptr<ModelAPI_Feature> aFeature =
460     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
461   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalName));
462 }
463
464 //--------------------------------------------------------------------------------------
465 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
466     const ModelHighAPI_RefAttr & theMirrorLine,
467     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
468 {
469   std::shared_ptr<ModelAPI_Feature> aFeature =
470     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
471   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
472 }
473
474 //--------------------------------------------------------------------------------------
475 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
476     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
477     const ModelHighAPI_RefAttr & thePoint1,
478     const ModelHighAPI_RefAttr & thePoint2,
479     const ModelHighAPI_Integer & theNumberOfObjects,
480     bool theFullValue)
481 {
482   std::shared_ptr<ModelAPI_Feature> aFeature =
483     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
484   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
485                                                   thePoint2, theNumberOfObjects, theFullValue));
486 }
487
488 //--------------------------------------------------------------------------------------
489 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
490     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
491     const ModelHighAPI_RefAttr & theCenter,
492     const ModelHighAPI_Double & theAngle,
493     const ModelHighAPI_Integer & theNumberOfObjects,
494     bool theFullValue)
495 {
496   std::shared_ptr<ModelAPI_Feature> aFeature =
497     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
498   return RotationPtr(
499     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
500                            theAngle, theNumberOfObjects, theFullValue));
501 }
502
503 //--------------------------------------------------------------------------------------
504 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
505                                                     const ModelHighAPI_Reference& theFeature,
506                                                     const ModelHighAPI_RefAttr& thePoint1,
507                                                     const ModelHighAPI_RefAttr& thePoint2)
508 {
509   std::shared_ptr<ModelAPI_Feature> aFeature =
510     compositeFeature()->addFeature(SketchPlugin_ConstraintSplit::ID());
511   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Constraint::VALUE()));
512   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
513   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
514   //aFeature->execute();
515   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
516 }
517
518 //--------------------------------------------------------------------------------------
519 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
520                                         const ModelHighAPI_Reference& theFeature,
521                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
522 {
523   std::shared_ptr<ModelAPI_Feature> aFeature =
524     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
525   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
526
527   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
528   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
529     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
530     fillAttribute(thePositionPoint, aPointAttr);
531   }
532
533   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
534 }
535
536 //--------------------------------------------------------------------------------------
537 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
538     const ModelHighAPI_RefAttr & theLine1,
539     const ModelHighAPI_RefAttr & theLine2,
540     const ModelHighAPI_Double & theValue)
541 {
542   std::shared_ptr<ModelAPI_Feature> aFeature =
543       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
544   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
545       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
546   // fill the value before llines to avoid calculation of angle value by the Angle feature
547   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
548   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
549   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
550   aFeature->execute();
551   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
552 }
553
554 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
555     const ModelHighAPI_RefAttr & theLine1,
556     const ModelHighAPI_RefAttr & theLine2,
557     const ModelHighAPI_Double & theValue)
558 {
559   std::shared_ptr<ModelAPI_Feature> aFeature =
560       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
561   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
562       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
563   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
564   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
565   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
566 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
567   aFeature->execute();
568   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
569 }
570
571 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
572     const ModelHighAPI_RefAttr & theLine1,
573     const ModelHighAPI_RefAttr & theLine2,
574     const ModelHighAPI_Double & theValue)
575 {
576   std::shared_ptr<ModelAPI_Feature> aFeature =
577       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
578   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
579       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
580   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
581   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
582   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
583 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
584   aFeature->execute();
585   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
586 }
587
588 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
589     const ModelHighAPI_RefAttr & thePoint1,
590     const ModelHighAPI_RefAttr & thePoint2)
591 {
592   std::shared_ptr<ModelAPI_Feature> aFeature =
593       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
594   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
595   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
596   aFeature->execute();
597   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
598 }
599
600 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
601     const ModelHighAPI_RefAttr & theLine1,
602     const ModelHighAPI_RefAttr & theLine2)
603 {
604   std::shared_ptr<ModelAPI_Feature> aFeature =
605       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
606   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
607   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
608   aFeature->execute();
609   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
610 }
611
612 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
613     const ModelHighAPI_RefAttr & thePoint,
614     const ModelHighAPI_RefAttr & thePointOrLine,
615     const ModelHighAPI_Double & theValue)
616 {
617   std::shared_ptr<ModelAPI_Feature> aFeature =
618       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
619   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
620   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
621   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
622   aFeature->execute();
623   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
624 }
625
626 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
627     const ModelHighAPI_RefAttr & thePoint1,
628     const ModelHighAPI_RefAttr & thePoint2,
629     const ModelHighAPI_Double & theValue)
630 {
631   std::shared_ptr<ModelAPI_Feature> aFeature =
632       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
633   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
634   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
635   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
636   aFeature->execute();
637   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
638 }
639
640 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
641     const ModelHighAPI_RefAttr & thePoint1,
642     const ModelHighAPI_RefAttr & thePoint2,
643     const ModelHighAPI_Double & theValue)
644 {
645   std::shared_ptr<ModelAPI_Feature> aFeature =
646       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
647   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
648   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
649   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
650   aFeature->execute();
651   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
652 }
653
654 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
655     const ModelHighAPI_RefAttr & theObject1,
656     const ModelHighAPI_RefAttr & theObject2)
657 {
658   std::shared_ptr<ModelAPI_Feature> aFeature =
659       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
660   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
661   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
662   aFeature->execute();
663   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
664 }
665
666 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
667     const ModelHighAPI_RefAttr & thePoint)
668 {
669   std::shared_ptr<ModelAPI_Feature> aFeature =
670       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
671   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
672   apply(); // finish operation to remove Fillet feature correcly
673   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
674 }
675
676 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
677     const ModelHighAPI_RefAttr & thePoint,
678     const ModelHighAPI_Double & theRadius)
679 {
680   CompositeFeaturePtr aSketch = compositeFeature();
681   int aNbSubs = aSketch->numberOfSubs();
682
683   // create fillet
684   InterfacePtr aFilletFeature = setFillet(thePoint);
685
686   // set radius for just created arc
687   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
688   if (anArc->getKind() == SketchPlugin_Arc::ID())
689     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
690
691   return aFilletFeature;
692 }
693
694 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
695     const ModelHighAPI_RefAttr & theObject)
696 {
697   std::shared_ptr<ModelAPI_Feature> aFeature =
698       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
699   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
700   aFeature->execute();
701   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
702 }
703
704 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
705     const ModelHighAPI_RefAttr & theLine)
706 {
707   std::shared_ptr<ModelAPI_Feature> aFeature =
708       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
709   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
710   aFeature->execute();
711   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
712 }
713
714 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
715     const ModelHighAPI_RefAttr & theLine,
716     const ModelHighAPI_Double & theValue)
717 {
718   std::shared_ptr<ModelAPI_Feature> aFeature =
719       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
720   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
721   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
722   aFeature->execute();
723   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
724 }
725
726 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
727     const ModelHighAPI_RefAttr & thePoint,
728     const ModelHighAPI_RefAttr & theLine)
729 {
730   std::shared_ptr<ModelAPI_Feature> aFeature =
731       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
732   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
733   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
734   aFeature->execute();
735   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
736 }
737
738 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
739     const ModelHighAPI_RefAttr & theLine1,
740     const ModelHighAPI_RefAttr & theLine2)
741 {
742   std::shared_ptr<ModelAPI_Feature> aFeature =
743       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
744   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
745   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
746   aFeature->execute();
747   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
748 }
749
750 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
751     const ModelHighAPI_RefAttr & theLine1,
752     const ModelHighAPI_RefAttr & theLine2)
753 {
754   std::shared_ptr<ModelAPI_Feature> aFeature =
755       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
756   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
757   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
758   aFeature->execute();
759   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
760 }
761
762 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
763     const ModelHighAPI_RefAttr & theCircleOrArc,
764     const ModelHighAPI_Double & theValue)
765 {
766   std::shared_ptr<ModelAPI_Feature> aFeature =
767       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
768   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
769   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
770   aFeature->execute();
771   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
772 }
773
774 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
775     const ModelHighAPI_RefAttr & theLine,
776     const ModelHighAPI_RefAttr & theCircle)
777 {
778   std::shared_ptr<ModelAPI_Feature> aFeature =
779       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
780   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
781   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
782   aFeature->execute();
783   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
784 }
785
786 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
787     const ModelHighAPI_RefAttr & theLine)
788 {
789   std::shared_ptr<ModelAPI_Feature> aFeature =
790       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
791   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
792   aFeature->execute();
793   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
794 }
795
796 //--------------------------------------------------------------------------------------
797
798 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
799 {
800   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
801   if (aPnt)
802     return aPnt->pnt();
803   return std::shared_ptr<GeomAPI_Pnt2d>();
804 }
805
806 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
807 {
808   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
809       theFeature->attribute(SketchPlugin_Line::START_ID()));
810   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
811       theFeature->attribute(SketchPlugin_Line::END_ID()));
812
813   if (!aStartAttr || !aEndAttr)
814     return std::shared_ptr<GeomAPI_Pnt2d>();
815
816   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
817   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
818   return std::shared_ptr<GeomAPI_Pnt2d>(
819       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
820 }
821
822 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
823 {
824   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
825       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
826   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
827
828   if (!aCenter || !aRadius)
829     return std::shared_ptr<GeomAPI_Pnt2d>();
830
831   return std::shared_ptr<GeomAPI_Pnt2d>(
832       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
833 }
834
835 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
836 {
837   static const double PI = 3.141592653589793238463;
838
839   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
840       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
841   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
842       theFeature->attribute(SketchPlugin_Arc::START_ID()));
843   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
844       theFeature->attribute(SketchPlugin_Arc::END_ID()));
845
846   if (!aCenterAttr || !aStartAttr || !aEndAttr)
847     return std::shared_ptr<GeomAPI_Pnt2d>();
848
849   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
850       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
851   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
852       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
853
854   double anAngle = aStartDir->angle(aEndDir);
855   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
856   if (isReversed && anAngle > 0.)
857     anAngle -= 2.0 * PI;
858   else if (!isReversed && anAngle <= 0.)
859     anAngle += 2.0 * PI;
860
861   double cosA = cos(anAngle);
862   double sinA = sin(anAngle);
863
864   // rotate start dir to find middle point on arc
865   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
866   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
867   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
868
869   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
870 }
871
872 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
873 {
874   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
875   if (aFeature) {
876     const std::string& aFeatureKind = aFeature->getKind();
877     if (aFeatureKind == SketchPlugin_Point::ID())
878       return pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
879     else if (aFeatureKind == SketchPlugin_Line::ID())
880       return middlePointOnLine(aFeature);
881     else if (aFeatureKind == SketchPlugin_Circle::ID())
882       return pointOnCircle(aFeature);
883     else if (aFeatureKind == SketchPlugin_Arc::ID())
884       return middlePointOnArc(aFeature);
885   }
886   // do not move other types of features
887   return std::shared_ptr<GeomAPI_Pnt2d>();
888 }
889
890 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
891                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
892 {
893   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
894   theMovedEntity.fillMessage(aMessage);
895
896   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
897   if (aMessage->movedAttribute())
898     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
899   else
900     anOriginalPosition = middlePoint(aMessage->movedObject());
901
902   if (!anOriginalPosition)
903     return; // something has gone wrong, do not process movement
904
905   aMessage->setOriginalPosition(anOriginalPosition);
906   aMessage->setCurrentPosition(theTargetPoint);
907   Events_Loop::loop()->send(aMessage);
908 }
909
910 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
911                             double theTargetX, double theTargetY)
912 {
913   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
914   move(theMovedEntity, aTargetPoint);
915 }
916
917 //--------------------------------------------------------------------------------------
918
919 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
920 {
921   FeaturePtr aBase = feature();
922   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
923       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
924   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
925       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
926   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
927       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
928   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
929
930   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
931 }
932
933 //--------------------------------------------------------------------------------------
934
935 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
936 {
937   FeaturePtr aBase = feature();
938   const std::string& aDocName = theDumper.name(aBase->document());
939
940   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
941   if (anExternal->value()) {
942     theDumper << aBase << " = model.addSketch(" << aDocName <<
943       ", " << anExternal << ")" << std::endl;
944   } else {
945     // Sketch is base on a plane.
946     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
947         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
948     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
949         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
950     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
951         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
952
953     // Check the plane is coordinate plane
954     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
955     if(anExternal->context()) { // checking for selected planes
956       if (!aPlaneName.empty()
957           && anExternal->context()->data()
958           && anExternal->context()->data()->name() == aPlaneName) {
959         // dump sketch based on coordinate plane
960         theDumper << aBase << " = model.addSketch(" << aDocName
961                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
962       } else { // some other plane
963         theDumper << aBase << " = model.addSketch(" << aDocName <<
964           ", " << anExternal<< ")" << std::endl;
965       }
966     } else {
967       if (aPlaneName.empty()) {
968         // needs import additional module
969         theDumper.importModule("GeomAPI");
970         // dump plane parameters
971         const std::string& aSketchName = theDumper.name(aBase);
972         std::string anOriginName = aSketchName + "_origin";
973         std::string aNormalName  = aSketchName + "_norm";
974         std::string aDirXName    = aSketchName + "_dirx";
975         // use "\n" instead of std::endl to avoid automatic dumping sketch here
976         // and then dumplicate dumping it in the next line
977         theDumper << anOriginName << " = " << anOrigin << "\n"
978                   << aNormalName  << " = " << aNormal  << "\n"
979                   << aDirXName    << " = " << aDirX    << "\n";
980         // dump sketch based on arbitrary plane
981         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
982                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
983       } else {
984         // dump sketch based on coordinate plane
985         theDumper << aBase << " = model.addSketch(" << aDocName
986                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
987       }
988     }
989   }
990
991   // dump sketch's subfeatures
992   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
993   theDumper.processSubs(aCompFeat, true);
994 }