Salome HOME
Task 2.2. New arcs creation mode: perpendicular to a straight edge
[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                                                 bool theTransversal)
465 {
466   std::shared_ptr<ModelAPI_Feature> aFeature =
467     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
468   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
469   if (theTransversal)
470     aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
471   else
472     aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
473   return aMacroArc;
474 }
475
476 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
477                                               const ModelHighAPI_RefAttr& theTangentPoint,
478                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
479                                               bool theInversed,
480                                               bool theTransversal)
481 {
482   std::shared_ptr<ModelAPI_Feature> aFeature =
483     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
484   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
485   if (theTransversal)
486     aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
487   else
488     aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
489   return aMacroArc;
490 }
491
492 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
493 {
494   std::shared_ptr<ModelAPI_Feature> aFeature =
495     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
496   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
497 }
498
499 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
500 {
501   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
502   std::shared_ptr<ModelAPI_Feature> aFeature =
503     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
504   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
505 }
506
507 //--------------------------------------------------------------------------------------
508 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
509     const ModelHighAPI_Selection & theExternalFeature,
510     bool theKeepResult)
511 {
512   std::shared_ptr<ModelAPI_Feature> aFeature =
513     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
514   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
515   aProjection->setIncludeToResult(theKeepResult);
516   return aProjection;
517 }
518
519 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
520     const std::string & theExternalName,
521     bool theKeepResult)
522 {
523   std::shared_ptr<ModelAPI_Feature> aFeature =
524     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
525   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
526   aProjection->setIncludeToResult(theKeepResult);
527   return aProjection;
528 }
529
530 //--------------------------------------------------------------------------------------
531 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
532     const ModelHighAPI_RefAttr & theMirrorLine,
533     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
534 {
535   std::shared_ptr<ModelAPI_Feature> aFeature =
536     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
537   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
538 }
539
540 //--------------------------------------------------------------------------------------
541 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
542     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
543     const ModelHighAPI_RefAttr & thePoint1,
544     const ModelHighAPI_RefAttr & thePoint2,
545     const ModelHighAPI_Integer & theNumberOfObjects,
546     bool theFullValue)
547 {
548   std::shared_ptr<ModelAPI_Feature> aFeature =
549     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
550   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
551                                                   thePoint2, theNumberOfObjects, theFullValue));
552 }
553
554 //--------------------------------------------------------------------------------------
555 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
556     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
557     const ModelHighAPI_RefAttr & theCenter,
558     const ModelHighAPI_Double & theAngle,
559     const ModelHighAPI_Integer & theNumberOfObjects,
560     bool theFullValue,
561     bool theReversed)
562 {
563   std::shared_ptr<ModelAPI_Feature> aFeature =
564     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
565   return RotationPtr(
566     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
567                            theAngle, theNumberOfObjects, theFullValue, theReversed));
568 }
569
570 //--------------------------------------------------------------------------------------
571 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
572                                           const ModelHighAPI_Reference& theFeature,
573                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
574 {
575   std::shared_ptr<ModelAPI_Feature> aFeature =
576     compositeFeature()->addFeature(SketchPlugin_Split::ID());
577   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
578
579   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
580   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
581     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
582     fillAttribute(thePositionPoint, aPointAttr);
583   }
584
585   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
586 }
587
588 //--------------------------------------------------------------------------------------
589 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
590                                         const ModelHighAPI_Reference& theFeature,
591                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
592 {
593   std::shared_ptr<ModelAPI_Feature> aFeature =
594     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
595   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
596
597   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
598   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
599     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
600     fillAttribute(thePositionPoint, aPointAttr);
601   }
602
603   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
604 }
605
606 //--------------------------------------------------------------------------------------
607 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
608     const ModelHighAPI_RefAttr & theLine1,
609     const ModelHighAPI_RefAttr & theLine2,
610     const ModelHighAPI_Double & theValue)
611 {
612   std::shared_ptr<ModelAPI_Feature> aFeature =
613       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
614   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
615       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
616   // fill the value before llines to avoid calculation of angle value by the Angle feature
617   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
618   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
619   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
620   aFeature->execute();
621   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
622 }
623
624 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
625     const ModelHighAPI_RefAttr & theLine1,
626     const ModelHighAPI_RefAttr & theLine2,
627     const ModelHighAPI_Double & theValue)
628 {
629   std::shared_ptr<ModelAPI_Feature> aFeature =
630       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
631   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
632       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
633   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
634   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
635   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
636   aFeature->execute();
637   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
638 }
639
640 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
641     const ModelHighAPI_RefAttr & theLine1,
642     const ModelHighAPI_RefAttr & theLine2,
643     const ModelHighAPI_Double & theValue)
644 {
645   std::shared_ptr<ModelAPI_Feature> aFeature =
646       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
647   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
648       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
649   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
650   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
651   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
652   aFeature->execute();
653   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
654 }
655
656 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
657     const ModelHighAPI_RefAttr & thePoint1,
658     const ModelHighAPI_RefAttr & thePoint2)
659 {
660   std::shared_ptr<ModelAPI_Feature> aFeature =
661       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
662   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
663   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
664   aFeature->execute();
665   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
666 }
667
668 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
669     const ModelHighAPI_RefAttr & theLine1,
670     const ModelHighAPI_RefAttr & theLine2)
671 {
672   std::shared_ptr<ModelAPI_Feature> aFeature =
673       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
674   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
675   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
676   aFeature->execute();
677   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
678 }
679
680 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
681     const ModelHighAPI_RefAttr & thePoint,
682     const ModelHighAPI_RefAttr & thePointOrLine,
683     const ModelHighAPI_Double & theValue,
684     bool isSigned)
685 {
686   std::shared_ptr<ModelAPI_Feature> aFeature =
687       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
688   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
689   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
690   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
691   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
692   aFeature->execute();
693   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
694 }
695
696 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
697     const ModelHighAPI_RefAttr & thePoint,
698     const ModelHighAPI_RefAttr & thePointOrLine,
699     const ModelHighAPI_Double & theValue)
700 {
701   return setDistance(thePoint, thePointOrLine, theValue, true);
702 }
703
704 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
705     const ModelHighAPI_RefAttr & thePoint,
706     const ModelHighAPI_RefAttr & thePointOrLine,
707     const ModelHighAPI_Double & theValue)
708 {
709   return setDistance(thePoint, thePointOrLine, theValue, false);
710 }
711
712 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
713     const ModelHighAPI_RefAttr & thePoint1,
714     const ModelHighAPI_RefAttr & thePoint2,
715     const ModelHighAPI_Double & theValue)
716 {
717   std::shared_ptr<ModelAPI_Feature> aFeature =
718       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
719   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
720   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
721   fillAttribute(theValue,
722       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
723   aFeature->execute();
724   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
725 }
726
727 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
728     const ModelHighAPI_RefAttr & thePoint1,
729     const ModelHighAPI_RefAttr & thePoint2,
730     const ModelHighAPI_Double & theValue)
731 {
732   std::shared_ptr<ModelAPI_Feature> aFeature =
733       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
734   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
735   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
736   fillAttribute(theValue,
737       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
738   aFeature->execute();
739   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
740 }
741
742 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
743     const ModelHighAPI_RefAttr & theObject1,
744     const ModelHighAPI_RefAttr & theObject2)
745 {
746   std::shared_ptr<ModelAPI_Feature> aFeature =
747       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
748   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
749   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
750   aFeature->execute();
751   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
752 }
753
754 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
755     const ModelHighAPI_RefAttr & thePoint)
756 {
757   std::shared_ptr<ModelAPI_Feature> aFeature =
758       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
759   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
760   apply(); // finish operation to remove Fillet feature correcly
761   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
762 }
763
764 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
765     const ModelHighAPI_RefAttr & thePoint,
766     const ModelHighAPI_Double & theRadius)
767 {
768   CompositeFeaturePtr aSketch = compositeFeature();
769   int aNbSubs = aSketch->numberOfSubs();
770
771   // create fillet
772   InterfacePtr aFilletFeature = setFillet(thePoint);
773
774   // set radius for just created arc
775   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
776   if (anArc->getKind() == SketchPlugin_Arc::ID())
777     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
778
779   return aFilletFeature;
780 }
781
782 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
783     const ModelHighAPI_RefAttr & theObject)
784 {
785   std::shared_ptr<ModelAPI_Feature> aFeature =
786       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
787   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
788   aFeature->execute();
789   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
790 }
791
792 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
793     const ModelHighAPI_RefAttr & theLine)
794 {
795   std::shared_ptr<ModelAPI_Feature> aFeature =
796       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
797   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
798   aFeature->execute();
799   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
800 }
801
802 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
803     const ModelHighAPI_RefAttr & theLine,
804     const ModelHighAPI_Double & theValue)
805 {
806   std::shared_ptr<ModelAPI_Feature> aFeature =
807       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
808   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
809   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
810   aFeature->execute();
811   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
812 }
813
814 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
815     const ModelHighAPI_RefAttr & thePoint,
816     const ModelHighAPI_RefAttr & theLine)
817 {
818   std::shared_ptr<ModelAPI_Feature> aFeature =
819       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
820   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
821   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
822   aFeature->execute();
823   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
824 }
825
826 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
827     const ModelHighAPI_RefAttr & theLine1,
828     const ModelHighAPI_RefAttr & theLine2)
829 {
830   std::shared_ptr<ModelAPI_Feature> aFeature =
831       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
832   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
833   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
834   aFeature->execute();
835   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
836 }
837
838 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
839     const ModelHighAPI_RefAttr & theLine1,
840     const ModelHighAPI_RefAttr & theLine2)
841 {
842   std::shared_ptr<ModelAPI_Feature> aFeature =
843       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
844   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
845   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
846   aFeature->execute();
847   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
848 }
849
850 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
851     const ModelHighAPI_RefAttr & theCircleOrArc,
852     const ModelHighAPI_Double & theValue)
853 {
854   std::shared_ptr<ModelAPI_Feature> aFeature =
855       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
856   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
857   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
858   aFeature->execute();
859   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
860 }
861
862 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
863     const ModelHighAPI_RefAttr & theLine,
864     const ModelHighAPI_RefAttr & theCircle)
865 {
866   std::shared_ptr<ModelAPI_Feature> aFeature =
867       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
868   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
869   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
870   aFeature->execute();
871   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
872 }
873
874 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
875     const ModelHighAPI_RefAttr & theLine)
876 {
877   std::shared_ptr<ModelAPI_Feature> aFeature =
878       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
879   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
880   aFeature->execute();
881   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
882 }
883
884 //--------------------------------------------------------------------------------------
885
886 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
887 {
888   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
889   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
890 }
891
892 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
893 {
894   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
895       theFeature->attribute(SketchPlugin_Line::START_ID()));
896   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
897       theFeature->attribute(SketchPlugin_Line::END_ID()));
898
899   if (!aStartAttr || !aEndAttr)
900     return std::shared_ptr<GeomAPI_Pnt2d>();
901
902   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
903   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
904   return std::shared_ptr<GeomAPI_Pnt2d>(
905       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
906 }
907
908 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
909 {
910   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
911       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
912   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
913
914   if (!aCenter || !aRadius)
915     return std::shared_ptr<GeomAPI_Pnt2d>();
916
917   return std::shared_ptr<GeomAPI_Pnt2d>(
918       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
919 }
920
921 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
922 {
923   static const double PI = 3.141592653589793238463;
924
925   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
926       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
927   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
928       theFeature->attribute(SketchPlugin_Arc::START_ID()));
929   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
930       theFeature->attribute(SketchPlugin_Arc::END_ID()));
931
932   if (!aCenterAttr || !aStartAttr || !aEndAttr)
933     return std::shared_ptr<GeomAPI_Pnt2d>();
934
935   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
936       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
937   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
938       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
939
940   double anAngle = aStartDir->angle(aEndDir);
941   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
942   if (isReversed && anAngle > 0.)
943     anAngle -= 2.0 * PI;
944   else if (!isReversed && anAngle <= 0.)
945     anAngle += 2.0 * PI;
946
947   double cosA = cos(anAngle);
948   double sinA = sin(anAngle);
949
950   // rotate start dir to find middle point on arc
951   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
952   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
953   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
954
955   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
956 }
957
958 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
959 {
960   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
961   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
962   if (aFeature) {
963     // move only features of the following types
964     const std::string& aFeatureKind = aFeature->getKind();
965     if (aFeatureKind == SketchPlugin_Point::ID())
966       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
967     else if (aFeatureKind == SketchPlugin_Line::ID())
968       aMiddlePoint = middlePointOnLine(aFeature);
969     else if (aFeatureKind == SketchPlugin_Circle::ID())
970       aMiddlePoint = pointOnCircle(aFeature);
971     else if (aFeatureKind == SketchPlugin_Arc::ID())
972       aMiddlePoint = middlePointOnArc(aFeature);
973   }
974   return aMiddlePoint;
975 }
976
977 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
978                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
979 {
980   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
981   theMovedEntity.fillMessage(aMessage);
982
983   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
984   if (aMessage->movedAttribute())
985     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
986   else
987     anOriginalPosition = middlePoint(aMessage->movedObject());
988
989   if (!anOriginalPosition)
990     return; // something has gone wrong, do not process movement
991
992   aMessage->setOriginalPosition(anOriginalPosition);
993   aMessage->setCurrentPosition(theTargetPoint);
994   Events_Loop::loop()->send(aMessage);
995 }
996
997 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
998                             double theTargetX, double theTargetY)
999 {
1000   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1001   move(theMovedEntity, aTargetPoint);
1002 }
1003
1004 //--------------------------------------------------------------------------------------
1005
1006 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1007 {
1008   FeaturePtr aBase = feature();
1009   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1010       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1011   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1012       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1013   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1014       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1015   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1016
1017   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1018 }
1019
1020 //--------------------------------------------------------------------------------------
1021
1022 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1023 {
1024   FeaturePtr aBase = feature();
1025   const std::string& aDocName = theDumper.name(aBase->document());
1026
1027   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1028   if (anExternal->value()) {
1029     theDumper << aBase << " = model.addSketch(" << aDocName <<
1030       ", " << anExternal << ")" << std::endl;
1031   } else {
1032     // Sketch is base on a plane.
1033     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1034         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1035     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1036         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1037     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1038         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1039
1040     // Check the plane is coordinate plane
1041     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1042     if(anExternal->context()) { // checking for selected planes
1043       if (!aPlaneName.empty()
1044           && anExternal->context()->data()
1045           && anExternal->context()->data()->name() == aPlaneName) {
1046         // dump sketch based on coordinate plane
1047         theDumper << aBase << " = model.addSketch(" << aDocName
1048                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1049       } else { // some other plane
1050         theDumper << aBase << " = model.addSketch(" << aDocName <<
1051           ", " << anExternal<< ")" << std::endl;
1052       }
1053     } else {
1054       if (aPlaneName.empty()) {
1055         // needs import additional module
1056         theDumper.importModule("GeomAPI");
1057         // dump plane parameters
1058         const std::string& aSketchName = theDumper.name(aBase);
1059         std::string anOriginName = aSketchName + "_origin";
1060         std::string aNormalName  = aSketchName + "_norm";
1061         std::string aDirXName    = aSketchName + "_dirx";
1062         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1063         // and then dumplicate dumping it in the next line
1064         theDumper << anOriginName << " = " << anOrigin << "\n"
1065                   << aNormalName  << " = " << aNormal  << "\n"
1066                   << aDirXName    << " = " << aDirX    << "\n";
1067         // dump sketch based on arbitrary plane
1068         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1069                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1070       } else {
1071         // dump sketch based on coordinate plane
1072         theDumper << aBase << " = model.addSketch(" << aDocName
1073                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1074       }
1075     }
1076   }
1077
1078   // dump sketch's subfeatures
1079   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1080   theDumper.processSubs(aCompFeat, true);
1081 }