Salome HOME
Update incorrect units tests and add missing tests (issues #2838, #2839, #2840, ...
[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(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
576   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
577   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
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(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
592   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
593   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
594   aFeature->execute();
595   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
596 }
597
598 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
599     const ModelHighAPI_RefAttr & theLine1,
600     const ModelHighAPI_RefAttr & theLine2,
601     const ModelHighAPI_Double & theValue)
602 {
603   std::shared_ptr<ModelAPI_Feature> aFeature =
604       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
605   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
606       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
607   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
608   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
609   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
610   aFeature->execute();
611   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
612 }
613
614 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
615     const ModelHighAPI_RefAttr & thePoint1,
616     const ModelHighAPI_RefAttr & thePoint2)
617 {
618   std::shared_ptr<ModelAPI_Feature> aFeature =
619       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
620   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
621   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
622   aFeature->execute();
623   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
624 }
625
626 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
627     const ModelHighAPI_RefAttr & theLine1,
628     const ModelHighAPI_RefAttr & theLine2)
629 {
630   std::shared_ptr<ModelAPI_Feature> aFeature =
631       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
632   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
633   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
634   aFeature->execute();
635   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
636 }
637
638 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
639     const ModelHighAPI_RefAttr & thePoint,
640     const ModelHighAPI_RefAttr & thePointOrLine,
641     const ModelHighAPI_Double & theValue,
642     bool isSigned)
643 {
644   std::shared_ptr<ModelAPI_Feature> aFeature =
645       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
646   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
647   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
648   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
649   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
650   aFeature->execute();
651   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
652 }
653
654 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
655     const ModelHighAPI_RefAttr & thePoint,
656     const ModelHighAPI_RefAttr & thePointOrLine,
657     const ModelHighAPI_Double & theValue)
658 {
659   return setDistance(thePoint, thePointOrLine, theValue, true);
660 }
661
662 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
663     const ModelHighAPI_RefAttr & thePoint,
664     const ModelHighAPI_RefAttr & thePointOrLine,
665     const ModelHighAPI_Double & theValue)
666 {
667   return setDistance(thePoint, thePointOrLine, theValue, false);
668 }
669
670 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
671     const ModelHighAPI_RefAttr & thePoint1,
672     const ModelHighAPI_RefAttr & thePoint2,
673     const ModelHighAPI_Double & theValue)
674 {
675   std::shared_ptr<ModelAPI_Feature> aFeature =
676       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
677   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
678   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
679   fillAttribute(theValue,
680       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
681   aFeature->execute();
682   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
683 }
684
685 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
686     const ModelHighAPI_RefAttr & thePoint1,
687     const ModelHighAPI_RefAttr & thePoint2,
688     const ModelHighAPI_Double & theValue)
689 {
690   std::shared_ptr<ModelAPI_Feature> aFeature =
691       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
692   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
693   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
694   fillAttribute(theValue,
695       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
696   aFeature->execute();
697   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
698 }
699
700 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
701     const ModelHighAPI_RefAttr & theObject1,
702     const ModelHighAPI_RefAttr & theObject2)
703 {
704   std::shared_ptr<ModelAPI_Feature> aFeature =
705       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
706   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
707   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
708   aFeature->execute();
709   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
710 }
711
712 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
713     const ModelHighAPI_RefAttr & thePoint)
714 {
715   std::shared_ptr<ModelAPI_Feature> aFeature =
716       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
717   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
718   apply(); // finish operation to remove Fillet feature correcly
719   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
720 }
721
722 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
723     const ModelHighAPI_RefAttr & thePoint,
724     const ModelHighAPI_Double & theRadius)
725 {
726   CompositeFeaturePtr aSketch = compositeFeature();
727   int aNbSubs = aSketch->numberOfSubs();
728
729   // create fillet
730   InterfacePtr aFilletFeature = setFillet(thePoint);
731
732   // set radius for just created arc
733   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
734   if (anArc->getKind() == SketchPlugin_Arc::ID())
735     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
736
737   return aFilletFeature;
738 }
739
740 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
741     const ModelHighAPI_RefAttr & theObject)
742 {
743   std::shared_ptr<ModelAPI_Feature> aFeature =
744       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
745   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
746   aFeature->execute();
747   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
748 }
749
750 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
751     const ModelHighAPI_RefAttr & theLine)
752 {
753   std::shared_ptr<ModelAPI_Feature> aFeature =
754       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
755   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
756   aFeature->execute();
757   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
758 }
759
760 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
761     const ModelHighAPI_RefAttr & theLine,
762     const ModelHighAPI_Double & theValue)
763 {
764   std::shared_ptr<ModelAPI_Feature> aFeature =
765       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
766   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
767   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
768   aFeature->execute();
769   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
770 }
771
772 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
773     const ModelHighAPI_RefAttr & thePoint,
774     const ModelHighAPI_RefAttr & theLine)
775 {
776   std::shared_ptr<ModelAPI_Feature> aFeature =
777       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
778   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
779   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
780   aFeature->execute();
781   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
782 }
783
784 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
785     const ModelHighAPI_RefAttr & theLine1,
786     const ModelHighAPI_RefAttr & theLine2)
787 {
788   std::shared_ptr<ModelAPI_Feature> aFeature =
789       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
790   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
791   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
792   aFeature->execute();
793   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
794 }
795
796 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
797     const ModelHighAPI_RefAttr & theLine1,
798     const ModelHighAPI_RefAttr & theLine2)
799 {
800   std::shared_ptr<ModelAPI_Feature> aFeature =
801       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
802   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
803   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
804   aFeature->execute();
805   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
806 }
807
808 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
809     const ModelHighAPI_RefAttr & theCircleOrArc,
810     const ModelHighAPI_Double & theValue)
811 {
812   std::shared_ptr<ModelAPI_Feature> aFeature =
813       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
814   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
815   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
816   aFeature->execute();
817   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
818 }
819
820 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
821     const ModelHighAPI_RefAttr & theLine,
822     const ModelHighAPI_RefAttr & theCircle)
823 {
824   std::shared_ptr<ModelAPI_Feature> aFeature =
825       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
826   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
827   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
828   aFeature->execute();
829   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
830 }
831
832 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
833     const ModelHighAPI_RefAttr & theLine)
834 {
835   std::shared_ptr<ModelAPI_Feature> aFeature =
836       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
837   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
838   aFeature->execute();
839   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
840 }
841
842 //--------------------------------------------------------------------------------------
843
844 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
845 {
846   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
847   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
848 }
849
850 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
851 {
852   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
853       theFeature->attribute(SketchPlugin_Line::START_ID()));
854   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
855       theFeature->attribute(SketchPlugin_Line::END_ID()));
856
857   if (!aStartAttr || !aEndAttr)
858     return std::shared_ptr<GeomAPI_Pnt2d>();
859
860   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
861   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
862   return std::shared_ptr<GeomAPI_Pnt2d>(
863       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
864 }
865
866 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
867 {
868   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
869       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
870   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
871
872   if (!aCenter || !aRadius)
873     return std::shared_ptr<GeomAPI_Pnt2d>();
874
875   return std::shared_ptr<GeomAPI_Pnt2d>(
876       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
877 }
878
879 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
880 {
881   static const double PI = 3.141592653589793238463;
882
883   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
884       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
885   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
886       theFeature->attribute(SketchPlugin_Arc::START_ID()));
887   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
888       theFeature->attribute(SketchPlugin_Arc::END_ID()));
889
890   if (!aCenterAttr || !aStartAttr || !aEndAttr)
891     return std::shared_ptr<GeomAPI_Pnt2d>();
892
893   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
894       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
895   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
896       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
897
898   double anAngle = aStartDir->angle(aEndDir);
899   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
900   if (isReversed && anAngle > 0.)
901     anAngle -= 2.0 * PI;
902   else if (!isReversed && anAngle <= 0.)
903     anAngle += 2.0 * PI;
904
905   double cosA = cos(anAngle);
906   double sinA = sin(anAngle);
907
908   // rotate start dir to find middle point on arc
909   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
910   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
911   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
912
913   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
914 }
915
916 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
917 {
918   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
919   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
920   if (aFeature) {
921     // move only features of the following types
922     const std::string& aFeatureKind = aFeature->getKind();
923     if (aFeatureKind == SketchPlugin_Point::ID())
924       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
925     else if (aFeatureKind == SketchPlugin_Line::ID())
926       aMiddlePoint = middlePointOnLine(aFeature);
927     else if (aFeatureKind == SketchPlugin_Circle::ID())
928       aMiddlePoint = pointOnCircle(aFeature);
929     else if (aFeatureKind == SketchPlugin_Arc::ID())
930       aMiddlePoint = middlePointOnArc(aFeature);
931   }
932   return aMiddlePoint;
933 }
934
935 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
936                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
937 {
938   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
939   theMovedEntity.fillMessage(aMessage);
940
941   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
942   if (aMessage->movedAttribute())
943     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
944   else
945     anOriginalPosition = middlePoint(aMessage->movedObject());
946
947   if (!anOriginalPosition)
948     return; // something has gone wrong, do not process movement
949
950   aMessage->setOriginalPosition(anOriginalPosition);
951   aMessage->setCurrentPosition(theTargetPoint);
952   Events_Loop::loop()->send(aMessage);
953 }
954
955 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
956                             double theTargetX, double theTargetY)
957 {
958   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
959   move(theMovedEntity, aTargetPoint);
960 }
961
962 //--------------------------------------------------------------------------------------
963
964 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
965 {
966   FeaturePtr aBase = feature();
967   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
968       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
969   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
970       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
971   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
972       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
973   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
974
975   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
976 }
977
978 //--------------------------------------------------------------------------------------
979
980 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
981 {
982   FeaturePtr aBase = feature();
983   const std::string& aDocName = theDumper.name(aBase->document());
984
985   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
986   if (anExternal->value()) {
987     theDumper << aBase << " = model.addSketch(" << aDocName <<
988       ", " << anExternal << ")" << std::endl;
989   } else {
990     // Sketch is base on a plane.
991     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
992         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
993     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
994         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
995     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
996         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
997
998     // Check the plane is coordinate plane
999     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1000     if(anExternal->context()) { // checking for selected planes
1001       if (!aPlaneName.empty()
1002           && anExternal->context()->data()
1003           && anExternal->context()->data()->name() == aPlaneName) {
1004         // dump sketch based on coordinate plane
1005         theDumper << aBase << " = model.addSketch(" << aDocName
1006                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1007       } else { // some other plane
1008         theDumper << aBase << " = model.addSketch(" << aDocName <<
1009           ", " << anExternal<< ")" << std::endl;
1010       }
1011     } else {
1012       if (aPlaneName.empty()) {
1013         // needs import additional module
1014         theDumper.importModule("GeomAPI");
1015         // dump plane parameters
1016         const std::string& aSketchName = theDumper.name(aBase);
1017         std::string anOriginName = aSketchName + "_origin";
1018         std::string aNormalName  = aSketchName + "_norm";
1019         std::string aDirXName    = aSketchName + "_dirx";
1020         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1021         // and then dumplicate dumping it in the next line
1022         theDumper << anOriginName << " = " << anOrigin << "\n"
1023                   << aNormalName  << " = " << aNormal  << "\n"
1024                   << aDirXName    << " = " << aDirX    << "\n";
1025         // dump sketch based on arbitrary plane
1026         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1027                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1028       } else {
1029         // dump sketch based on coordinate plane
1030         theDumper << aBase << " = model.addSketch(" << aDocName
1031                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1032       }
1033     }
1034   }
1035
1036   // dump sketch's subfeatures
1037   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1038   theDumper.processSubs(aCompFeat, true);
1039 }