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