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