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