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