Salome HOME
Task #3230: Sketcher: create a curve passing through selected points or vertices...
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2020  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_CurveFitting.h>
40 #include <SketchPlugin_Trim.h>
41 #include <SketchPlugin_Split.h>
42 #include <SketchPlugin_ConstraintTangent.h>
43 #include <SketchPlugin_ConstraintVertical.h>
44 #include <SketchPlugin_MacroBSpline.h>
45 #include <SketchPlugin_SketchCopy.h>
46 #include <SketcherPrs_Tools.h>
47 //--------------------------------------------------------------------------------------
48 #include <ModelAPI_Events.h>
49 #include <ModelAPI_CompositeFeature.h>
50 #include <ModelAPI_ResultConstruction.h>
51 #include <ModelHighAPI_Double.h>
52 #include <ModelHighAPI_Dumper.h>
53 #include <ModelHighAPI_RefAttr.h>
54 #include <ModelHighAPI_Selection.h>
55 #include <ModelHighAPI_Services.h>
56 #include <ModelHighAPI_Tools.h>
57 //--------------------------------------------------------------------------------------
58 #include "SketchAPI_Arc.h"
59 #include "SketchAPI_BSpline.h"
60 #include "SketchAPI_Circle.h"
61 #include "SketchAPI_Ellipse.h"
62 #include "SketchAPI_EllipticArc.h"
63 #include "SketchAPI_IntersectionPoint.h"
64 #include "SketchAPI_Line.h"
65 #include "SketchAPI_MacroArc.h"
66 #include "SketchAPI_MacroCircle.h"
67 #include "SketchAPI_MacroEllipse.h"
68 #include "SketchAPI_MacroEllipticArc.h"
69 #include "SketchAPI_Mirror.h"
70 #include "SketchAPI_Point.h"
71 #include "SketchAPI_Projection.h"
72 #include "SketchAPI_Rectangle.h"
73 #include "SketchAPI_Rotation.h"
74 #include "SketchAPI_Translation.h"
75 //--------------------------------------------------------------------------------------
76 #include <GeomAPI_Curve.h>
77 #include <GeomAPI_Dir2d.h>
78 #include <GeomAPI_PlanarEdges.h>
79 #include <GeomAPI_ShapeExplorer.h>
80 #include <GeomAPI_XY.h>
81 #include <GeomAlgoAPI_SketchBuilder.h>
82
83 #include <algorithm>
84 #include <cmath>
85 //--------------------------------------------------------------------------------------
86 SketchAPI_Sketch::SketchAPI_Sketch(
87     const std::shared_ptr<ModelAPI_Feature> & theFeature)
88 : ModelHighAPI_Interface(theFeature)
89 {
90   initialize();
91 }
92
93 SketchAPI_Sketch::SketchAPI_Sketch(
94     const std::shared_ptr<ModelAPI_Feature> & theFeature,
95     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
96 : ModelHighAPI_Interface(theFeature)
97 {
98   if (initialize()) {
99     setPlane(thePlane);
100   }
101 }
102
103 SketchAPI_Sketch::SketchAPI_Sketch(
104     const std::shared_ptr<ModelAPI_Feature> & theFeature,
105     const ModelHighAPI_Selection & theExternal)
106 : ModelHighAPI_Interface(theFeature)
107 {
108   if (initialize()) {
109     setExternal(theExternal);
110   }
111 }
112
113 SketchAPI_Sketch::SketchAPI_Sketch(
114     const std::shared_ptr<ModelAPI_Feature> & theFeature,
115     std::shared_ptr<ModelAPI_Object> thePlaneObject)
116 : ModelHighAPI_Interface(theFeature)
117 {
118   if (initialize()) {
119     setExternal(thePlaneObject);
120   }
121 }
122
123 SketchAPI_Sketch::~SketchAPI_Sketch()
124 {
125
126 }
127
128 //--------------------------------------------------------------------------------------
129 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
130 {
131   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
132 }
133
134 //--------------------------------------------------------------------------------------
135 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
136 {
137   fillAttribute(thePlane->origin(), myorigin);
138   fillAttribute(thePlane->dirX(), mydirX);
139   fillAttribute(thePlane->normal(), mynormal);
140
141   execute();
142 }
143
144 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
145                                 bool theRemoveExternalDependency)
146 {
147   FeaturePtr aSketch = feature();
148
149   DocumentPtr aDoc = aSketch->document();
150   bool useVisible = false;
151   FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
152   aDoc->setCurrentFeature(aSketch, useVisible);
153
154   if (theRemoveExternalDependency)
155     aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
156
157   setExternal(thePlane);
158
159   aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
160 }
161
162 //--------------------------------------------------------------------------------------
163 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
164 {
165   fillAttribute(theExternal, myexternal);
166
167   execute();
168 }
169
170 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
171 {
172   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
173   ModelHighAPI_Selection aSel(aRes);
174   setExternal(aSel);
175 }
176
177 //--------------------------------------------------------------------------------------
178 void SketchAPI_Sketch::setValue(
179     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
180     const ModelHighAPI_Double & theValue)
181 {
182   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
183
184 //  theConstraint->execute();
185 }
186
187 //--------------------------------------------------------------------------------------
188 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
189 {
190   const_cast<SketchAPI_Sketch*>(this)->execute();
191
192   std::list<ModelHighAPI_Selection> aSelectionList;
193
194   ResultConstructionPtr aResultConstruction =
195       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
196   if (aResultConstruction.get() == NULL)
197     return aSelectionList;
198
199   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
200     aSelectionList.push_back(
201         ModelHighAPI_Selection(aResultConstruction,
202                                aResultConstruction->face(anIndex)));
203   }
204
205   return aSelectionList;
206 }
207
208 //--------------------------------------------------------------------------------------
209 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
210                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
211 {
212   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
213   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
214 }
215
216 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
217                     const ModelHighAPI_Selection & theExternal)
218 {
219   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
220   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
221 }
222
223 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
224                     const std::string & theExternalName)
225 {
226   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
227   return SketchPtr(
228     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
229 }
230
231 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
232                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
233 {
234   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
235   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
236 }
237
238 //--------------------------------------------------------------------------------------
239 SketchPtr copySketch(const std::shared_ptr<ModelAPI_Document> & thePart,
240                      const SketchPtr & theSketch)
241 {
242   FeaturePtr aCopyer = thePart->addFeature(SketchPlugin_SketchCopy::ID());
243   aCopyer->reference(SketchPlugin_SketchCopy::BASE_ID())->setValue(theSketch->feature());
244   aCopyer->execute();
245
246   FeaturePtr aNewSketch = thePart->nextFeature(aCopyer);
247
248   // perform removing the macro-feature
249   thePart->removeFeature(aCopyer);
250   apply();
251
252   return SketchPtr(new SketchAPI_Sketch(aNewSketch));
253 }
254
255
256 //--------------------------------------------------------------------------------------
257 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
258 {
259   std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
260   std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
261   for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
262     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
263     PointPtr aPoint(new SketchAPI_Point(aFeature));
264     aFreePoints.push_back(aPoint);
265   }
266   return aFreePoints;
267 }
268
269 //--------------------------------------------------------------------------------------
270 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
271 {
272   GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
273   if (aCurve->isTrimmed())
274     aCurve = aCurve->basisCurve();
275   return aCurve;
276 }
277
278 void SketchAPI_Sketch::changeFacesOrder(
279     const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
280 {
281   // collect faces of the sketch
282   ResultConstructionPtr aSketchResult =
283       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
284   if (!aSketchResult) {
285     // sketch is nested to a boolean operation, thus, it has no result yet.
286     feature()->execute();
287     aSketchResult =
288         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
289   }
290   std::list<GeomFacePtr> aFaces;
291   int aFacesNum = aSketchResult->facesNum();
292   for (int i = 0; i < aFacesNum; ++i)
293     aFaces.push_back(aSketchResult->face(i));
294   // find new faces order according to the given lists of edges
295   std::list<GeomFacePtr> aNewFacesOrder;
296   std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
297   for (; anIt != theFaces.end(); ++anIt) {
298     // find the appropriate face
299     std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
300     for (; aFIt != aFaces.end(); ++aFIt) {
301       std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
302       GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
303       for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
304         ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
305         if (!aCurRes)
306           break;
307         GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
308         GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
309         if (!aCurve1->isEqual(aCurve2))
310           break;
311       }
312
313       if (aEdgeIt == anIt->end() && !aFExp.more()) {
314         // face is found
315         aNewFacesOrder.push_back(*aFIt);
316         aFaces.erase(aFIt);
317         break;
318       }
319     }
320   }
321   // place the rest faces at the end of new faces list
322   if (!aFaces.empty())
323     aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
324   // update the result of the sketch with the new order of faces
325   aSketchResult->setFacesOrder(aNewFacesOrder);
326 }
327
328 //--------------------------------------------------------------------------------------
329 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
330     double theX, double theY)
331 {
332   std::shared_ptr<ModelAPI_Feature> aFeature =
333     compositeFeature()->addFeature(SketchPlugin_Point::ID());
334   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
335 }
336 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
337     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
338 {
339   std::shared_ptr<ModelAPI_Feature> aFeature =
340     compositeFeature()->addFeature(SketchPlugin_Point::ID());
341   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
342 }
343 std::shared_ptr<SketchAPI_Point>
344   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
345 {
346   std::shared_ptr<ModelAPI_Feature> aFeature =
347     compositeFeature()->addFeature(SketchPlugin_Point::ID());
348   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
349 }
350 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
351 {
352   std::shared_ptr<ModelAPI_Feature> aFeature =
353     compositeFeature()->addFeature(SketchPlugin_Point::ID());
354   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
355 }
356
357 //--------------------------------------------------------------------------------------
358 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
359     const ModelHighAPI_Selection & theExternal,
360     bool theKeepResult)
361 {
362   std::shared_ptr<ModelAPI_Feature> aFeature =
363     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
364   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
365   anIntersection->setIncludeToResult(theKeepResult);
366   return anIntersection;
367 }
368 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
369     const std::string & theExternalName,
370     bool theKeepResult)
371 {
372   std::shared_ptr<ModelAPI_Feature> aFeature =
373     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
374   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
375   anIntersection->setIncludeToResult(theKeepResult);
376   return anIntersection;
377 }
378
379 //--------------------------------------------------------------------------------------
380 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
381                                                           double theX2, double theY2)
382 {
383   std::shared_ptr<ModelAPI_Feature> aFeature =
384     compositeFeature()->addFeature(SketchPlugin_Line::ID());
385   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
386 }
387 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
388     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
389     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
390 {
391   std::shared_ptr<ModelAPI_Feature> aFeature =
392     compositeFeature()->addFeature(SketchPlugin_Line::ID());
393   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
394 }
395 std::shared_ptr<SketchAPI_Line>
396   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
397 {
398   std::shared_ptr<ModelAPI_Feature> aFeature =
399     compositeFeature()->addFeature(SketchPlugin_Line::ID());
400   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
401 }
402 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
403 {
404   std::shared_ptr<ModelAPI_Feature> aFeature =
405     compositeFeature()->addFeature(SketchPlugin_Line::ID());
406   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
407 }
408
409 //--------------------------------------------------------------------------------------
410 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
411                                                                     double theX2, double theY2)
412 {
413   std::shared_ptr<ModelAPI_Feature> aFeature =
414     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
415   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
416 }
417 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
418     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
419     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
420 {
421   std::shared_ptr<ModelAPI_Feature> aFeature =
422     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
423   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
424 }
425
426 //--------------------------------------------------------------------------------------
427 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
428                                                               double theCenterY,
429                                                               double theRadius)
430 {
431   std::shared_ptr<ModelAPI_Feature> aFeature =
432     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
433   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
434 }
435
436 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
437                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
438                                     double theRadius)
439 {
440   std::shared_ptr<ModelAPI_Feature> aFeature =
441     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
442   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
443 }
444
445 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
446                                                                    double theCenterY,
447                                                                    double thePassedX,
448                                                                    double thePassedY)
449 {
450   std::shared_ptr<ModelAPI_Feature> aFeature =
451     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
452   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
453                                                             thePassedX, thePassedY));
454 }
455
456 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
457     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
458     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
459 {
460   std::shared_ptr<ModelAPI_Feature> aFeature =
461     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
462   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
463 }
464
465 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
466                                                                    double theX2, double theY2,
467                                                                    double theX3, double theY3)
468 {
469   std::shared_ptr<ModelAPI_Feature> aFeature =
470     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
471   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
472                                                             theX2, theY2,
473                                                             theX3, theY3));
474 }
475
476 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
477     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
478     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
479     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
480 {
481   std::shared_ptr<ModelAPI_Feature> aFeature =
482     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
483   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
484 }
485
486 std::shared_ptr<SketchAPI_Circle>
487   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
488 {
489   std::shared_ptr<ModelAPI_Feature> aFeature =
490     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
491   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
492 }
493
494 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
495 {
496   std::shared_ptr<ModelAPI_Feature> aFeature =
497     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
498   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
499 }
500
501 //--------------------------------------------------------------------------------------
502 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
503                                                         double theStartX, double theStartY,
504                                                         double theEndX, double theEndY,
505                                                         bool theInversed)
506 {
507   std::shared_ptr<ModelAPI_Feature> aFeature =
508     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
509   return ArcPtr(new SketchAPI_Arc(aFeature,
510                                   theCenterX, theCenterY,
511                                   theStartX, theStartY,
512                                   theEndX, theEndY,
513                                   theInversed));
514 }
515
516 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
517                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
518                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
519                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
520                                               bool theInversed)
521 {
522   std::shared_ptr<ModelAPI_Feature> aFeature =
523     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
524   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
525 }
526
527 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
528                                                         double theEndX, double theEndY,
529                                                         double thePassedX, double thePassedY)
530 {
531   std::shared_ptr<ModelAPI_Feature> aFeature =
532     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
533   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
534                                        theStartX, theStartY,
535                                        theEndX, theEndY,
536                                        thePassedX, thePassedY));
537 }
538
539 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
540                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
541                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
542                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
543 {
544   std::shared_ptr<ModelAPI_Feature> aFeature =
545     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
546   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
547 }
548
549 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
550                                                 const ModelHighAPI_RefAttr& theTangentPoint,
551                                                 double theEndX, double theEndY,
552                                                 bool theInversed,
553                                                 bool theTransversal)
554 {
555   std::shared_ptr<ModelAPI_Feature> aFeature =
556     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
557   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
558   if (theTransversal)
559     aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
560   else
561     aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
562   return aMacroArc;
563 }
564
565 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
566                                               const ModelHighAPI_RefAttr& theTangentPoint,
567                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
568                                               bool theInversed,
569                                               bool theTransversal)
570 {
571   std::shared_ptr<ModelAPI_Feature> aFeature =
572     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
573   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
574   if (theTransversal)
575     aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
576   else
577     aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
578   return aMacroArc;
579 }
580
581 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
582 {
583   std::shared_ptr<ModelAPI_Feature> aFeature =
584     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
585   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
586 }
587
588 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
589 {
590   std::shared_ptr<ModelAPI_Feature> aFeature =
591     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
592   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
593 }
594
595 //--------------------------------------------------------------------------------------
596 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
597     double theCenterX, double theCenterY,
598     double theFocusX, double theFocusY,
599     double theMinorRadius)
600 {
601   std::shared_ptr<ModelAPI_Feature> aFeature =
602       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
603   return EllipsePtr(new SketchAPI_Ellipse(aFeature,
604       theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
605 }
606
607 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
608     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
609     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
610     double theMinorRadius)
611 {
612   std::shared_ptr<ModelAPI_Feature> aFeature =
613       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
614   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
615 }
616
617 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
618     double thePoint1X, double thePoint1Y,
619     double thePoint2X, double thePoint2Y,
620     double thePassedX, double thePassedY,
621     bool isPoint1Center)
622 {
623   std::shared_ptr<ModelAPI_Feature> aFeature =
624       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
625   return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
626       thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
627 }
628
629 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
630     const PointOrReference& thePoint1,
631     const PointOrReference& thePoint2,
632     const PointOrReference& thePassedPoint,
633     bool isPoint1Center)
634 {
635   std::shared_ptr<ModelAPI_Feature> aFeature =
636       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
637   MacroEllipsePtr anEllipse;
638   if (thePoint1.second.isEmpty() &&
639       thePoint2.second.isEmpty() &&
640       thePassedPoint.second.isEmpty()) {
641     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
642         thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
643   }
644   else {
645     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
646         thePoint1.first, thePoint1.second,
647         thePoint2.first, thePoint2.second,
648         thePassedPoint.first, thePassedPoint.second,
649         isPoint1Center));
650   }
651   return anEllipse;
652 }
653
654 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
655     const ModelHighAPI_Selection & theExternal)
656 {
657   std::shared_ptr<ModelAPI_Feature> aFeature =
658       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
659   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
660 }
661
662 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
663     const std::string & theExternalName)
664 {
665   std::shared_ptr<ModelAPI_Feature> aFeature =
666       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
667   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
668 }
669
670 //--------------------------------------------------------------------------------------
671 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
672     double theCenterX, double theCenterY,
673     double theFocusX, double theFocusY,
674     double theStartX, double theStartY,
675     double theEndX, double theEndY,
676     bool theInversed)
677 {
678   std::shared_ptr<ModelAPI_Feature> aFeature =
679       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
680   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
681       theCenterX, theCenterY,
682       theFocusX, theFocusY,
683       theStartX, theStartY,
684       theEndX, theEndY,
685       theInversed));
686 }
687
688 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
689     const PointOrReference& theCenter,
690     const PointOrReference& theMajorAxisPoint,
691     const PointOrReference& theStartPoint,
692     const PointOrReference& theEndPoint,
693     bool theInversed)
694 {
695   std::shared_ptr<ModelAPI_Feature> aFeature =
696       compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
697   return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
698       theCenter.first, theCenter.second,
699       theMajorAxisPoint.first, theMajorAxisPoint.second,
700       theStartPoint.first, theStartPoint.second,
701       theEndPoint.first, theEndPoint.second,
702       theInversed));
703 }
704
705 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
706     const ModelHighAPI_Selection & theExternal)
707 {
708   std::shared_ptr<ModelAPI_Feature> aFeature =
709       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
710   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
711 }
712
713 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
714     const std::string & theExternalName)
715 {
716   std::shared_ptr<ModelAPI_Feature> aFeature =
717       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
718   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
719 }
720
721 //--------------------------------------------------------------------------------------
722
723 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
724     const ModelHighAPI_Selection & external,
725     const int degree,
726     const std::list<PointOrReference>& poles,
727     const std::list<ModelHighAPI_Double>& weights,
728     const std::list<ModelHighAPI_Double>& knots,
729     const std::list<ModelHighAPI_Integer>& multiplicities,
730     const bool periodic)
731 {
732   // split poles and references to other shapes
733   bool hasReference = false;
734   std::list<GeomPnt2dPtr> aPoints;
735   std::list<ModelHighAPI_RefAttr> aReferences;
736   for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
737     aPoints.push_back(it->first);
738     aReferences.push_back(it->second);
739     if (!it->second.isEmpty())
740       hasReference = true;
741   }
742
743   BSplinePtr aBSpline;
744   CompositeFeaturePtr aSketch = compositeFeature();
745   if (hasReference) {
746     // use macro-feature to create coincidences to referred features
747     FeaturePtr aMacroFeature = aSketch->addFeature(
748         periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
749     AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
750         aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
751     AttributeDoubleArrayPtr aWeightsAttr =
752         aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
753     AttributeRefAttrListPtr aPolesRefAttr =
754         aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
755     // always generate a control polygon to apply coincidences correctly
756     aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
757     // initialize B-spline attributes
758     fillAttribute(aPoints, aPolesAttr);
759     if (weights.empty())
760       fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
761     else
762       fillAttribute(weights, aWeightsAttr);
763     fillAttribute(aReferences, aPolesRefAttr);
764     apply(); // to kill macro-feature
765
766     // find created B-spline feature
767     const std::string& aKindToFind =
768         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
769     int aNbSubs = aSketch->numberOfSubs();
770     for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
771       FeaturePtr aFeature = aSketch->subFeature(anIndex);
772       if (aFeature->getKind() == aKindToFind) {
773         aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
774                                 : new SketchAPI_BSpline(aFeature));
775         aBSpline->execute();
776         break;
777       }
778     }
779   }
780   else {
781     // compute B-spline by parameters
782     FeaturePtr aFeature = aSketch->addFeature(
783         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
784
785     aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
786                             : new SketchAPI_BSpline(aFeature));
787     if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
788       aBSpline->setByExternal(external);
789     else if (knots.empty() || multiplicities.empty())
790       aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
791     else
792       aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
793   }
794   return aBSpline;
795 }
796
797 //--------------------------------------------------------------------------------------
798 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
799     const CompositeFeaturePtr& theSketch,
800     const FeaturePtr& theCurveFittingFeature,
801     const std::list<ModelHighAPI_RefAttr>& points,
802     const bool periodic,
803     const bool closed)
804 {
805   AttributeBooleanPtr aPeriodicAttr =
806       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
807   fillAttribute(periodic, aPeriodicAttr);
808   AttributeBooleanPtr aClosedAttr =
809       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
810   fillAttribute(closed, aClosedAttr);
811   AttributeRefAttrListPtr aPointsAttr =
812       theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
813   fillAttribute(points, aPointsAttr);
814   apply(); // to execute and kill the macro-feature
815
816   // find created B-spline feature
817   BSplinePtr aBSpline;
818   const std::string& aKindToFind =
819       periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
820   int aNbSubs = theSketch->numberOfSubs();
821   for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
822     FeaturePtr aFeature = theSketch->subFeature(anIndex);
823     if (aFeature->getKind() == aKindToFind) {
824       aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
825         : new SketchAPI_BSpline(aFeature));
826       aBSpline->execute();
827       break;
828     }
829   }
830   return aBSpline;
831 }
832
833 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
834     const std::list<ModelHighAPI_RefAttr>& points,
835     const bool periodic,
836     const bool closed)
837 {
838   CompositeFeaturePtr aSketch = compositeFeature();
839   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
840   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
841       ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
842   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
843 }
844
845 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
846     const std::list<ModelHighAPI_RefAttr>& points,
847     const ModelHighAPI_Double& precision,
848     const bool periodic,
849     const bool closed)
850 {
851   CompositeFeaturePtr aSketch = compositeFeature();
852   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
853   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
854       ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
855   fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
856   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
857 }
858
859 //--------------------------------------------------------------------------------------
860 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
861     const ModelHighAPI_Selection & theExternalFeature,
862     bool theKeepResult)
863 {
864   std::shared_ptr<ModelAPI_Feature> aFeature =
865     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
866   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
867   aProjection->setIncludeToResult(theKeepResult);
868   return aProjection;
869 }
870
871 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
872     const std::string & theExternalName,
873     bool theKeepResult)
874 {
875   std::shared_ptr<ModelAPI_Feature> aFeature =
876     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
877   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
878   aProjection->setIncludeToResult(theKeepResult);
879   return aProjection;
880 }
881
882 //--------------------------------------------------------------------------------------
883 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
884     const ModelHighAPI_RefAttr & theMirrorLine,
885     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
886 {
887   std::shared_ptr<ModelAPI_Feature> aFeature =
888     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
889   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
890 }
891
892 //--------------------------------------------------------------------------------------
893 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
894     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
895     const ModelHighAPI_RefAttr & thePoint1,
896     const ModelHighAPI_RefAttr & thePoint2,
897     const ModelHighAPI_Integer & theNumberOfObjects,
898     bool theFullValue)
899 {
900   std::shared_ptr<ModelAPI_Feature> aFeature =
901     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
902   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
903                                                   thePoint2, theNumberOfObjects, theFullValue));
904 }
905
906 //--------------------------------------------------------------------------------------
907 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
908     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
909     const ModelHighAPI_RefAttr & theCenter,
910     const ModelHighAPI_Double & theAngle,
911     const ModelHighAPI_Integer & theNumberOfObjects,
912     bool theFullValue,
913     bool theReversed)
914 {
915   std::shared_ptr<ModelAPI_Feature> aFeature =
916     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
917   return RotationPtr(
918     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
919                            theAngle, theNumberOfObjects, theFullValue, theReversed));
920 }
921
922 //--------------------------------------------------------------------------------------
923 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
924                                           const ModelHighAPI_Reference& theFeature,
925                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
926 {
927   std::shared_ptr<ModelAPI_Feature> aFeature =
928     compositeFeature()->addFeature(SketchPlugin_Split::ID());
929   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
930
931   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
932   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
933     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
934     fillAttribute(thePositionPoint, aPointAttr);
935   }
936
937   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
938 }
939
940 //--------------------------------------------------------------------------------------
941 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
942                                         const ModelHighAPI_Reference& theFeature,
943                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
944 {
945   std::shared_ptr<ModelAPI_Feature> aFeature =
946     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
947   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
948
949   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
950   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
951     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
952     fillAttribute(thePositionPoint, aPointAttr);
953   }
954
955   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
956 }
957
958 //--------------------------------------------------------------------------------------
959 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
960     const ModelHighAPI_RefAttr & theLine1,
961     const ModelHighAPI_RefAttr & theLine2,
962     const ModelHighAPI_Double & theValue,
963     const std::string& theType)
964 {
965   std::shared_ptr<ModelAPI_Feature> aFeature =
966       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
967
968   const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
969                                        : SketchPlugin_ConstraintAngle::THE_VERSION_1;
970   fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
971
972   int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
973   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
974   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
975
976   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
977     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
978
979   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
980   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
981
982   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
983     std::string aTypeLC = theType;
984     std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
985                    [](char c) { return static_cast<char>(::tolower(c)); });
986     if (aTypeLC == "supplementary")
987       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
988     else if (aTypeLC == "backward")
989       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
990
991     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
992     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
993   }
994
995   aFeature->execute();
996   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
997 }
998
999 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1000     const ModelHighAPI_RefAttr & theLine1,
1001     const ModelHighAPI_RefAttr & theLine2,
1002     const ModelHighAPI_Double & theValue)
1003 {
1004   std::shared_ptr<ModelAPI_Feature> aFeature =
1005       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1006   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1007       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1008   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1009       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1010   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1011   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1012   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1013   aFeature->execute();
1014   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1015 }
1016
1017 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1018     const ModelHighAPI_RefAttr & theLine1,
1019     const ModelHighAPI_RefAttr & theLine2,
1020     const ModelHighAPI_Double & theValue)
1021 {
1022   std::shared_ptr<ModelAPI_Feature> aFeature =
1023       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1024   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1025       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1026   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1027       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1028   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1029   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1030   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1031   aFeature->execute();
1032   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1033 }
1034
1035 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1036     const ModelHighAPI_RefAttr & thePoint1,
1037     const ModelHighAPI_RefAttr & thePoint2)
1038 {
1039   std::shared_ptr<ModelAPI_Feature> aFeature =
1040       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1041   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1042   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1043   aFeature->execute();
1044   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1045 }
1046
1047 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1048     const ModelHighAPI_RefAttr & theLine1,
1049     const ModelHighAPI_RefAttr & theLine2)
1050 {
1051   std::shared_ptr<ModelAPI_Feature> aFeature =
1052       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1053   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1054   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1055   aFeature->execute();
1056   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1057 }
1058
1059 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1060     const ModelHighAPI_RefAttr & thePoint,
1061     const ModelHighAPI_RefAttr & thePointOrLine,
1062     const ModelHighAPI_Double & theValue,
1063     bool isSigned)
1064 {
1065   std::shared_ptr<ModelAPI_Feature> aFeature =
1066       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1067   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1068   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1069   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1070   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1071   aFeature->execute();
1072   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1073 }
1074
1075 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1076     const ModelHighAPI_RefAttr & thePoint,
1077     const ModelHighAPI_RefAttr & thePointOrLine,
1078     const ModelHighAPI_Double & theValue)
1079 {
1080   return setDistance(thePoint, thePointOrLine, theValue, true);
1081 }
1082
1083 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1084     const ModelHighAPI_RefAttr & thePoint,
1085     const ModelHighAPI_RefAttr & thePointOrLine,
1086     const ModelHighAPI_Double & theValue)
1087 {
1088   return setDistance(thePoint, thePointOrLine, theValue, false);
1089 }
1090
1091 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1092     const ModelHighAPI_RefAttr & thePoint1,
1093     const ModelHighAPI_RefAttr & thePoint2,
1094     const ModelHighAPI_Double & theValue)
1095 {
1096   std::shared_ptr<ModelAPI_Feature> aFeature =
1097       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1098   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1099   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1100   fillAttribute(theValue,
1101       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1102   aFeature->execute();
1103   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1104 }
1105
1106 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1107     const ModelHighAPI_RefAttr & thePoint1,
1108     const ModelHighAPI_RefAttr & thePoint2,
1109     const ModelHighAPI_Double & theValue)
1110 {
1111   std::shared_ptr<ModelAPI_Feature> aFeature =
1112       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1113   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1114   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1115   fillAttribute(theValue,
1116       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1117   aFeature->execute();
1118   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1119 }
1120
1121 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1122     const ModelHighAPI_RefAttr & theObject1,
1123     const ModelHighAPI_RefAttr & theObject2)
1124 {
1125   std::shared_ptr<ModelAPI_Feature> aFeature =
1126       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1127   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1128   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1129   aFeature->execute();
1130   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1131 }
1132
1133 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1134     const ModelHighAPI_RefAttr & thePoint)
1135 {
1136   std::shared_ptr<ModelAPI_Feature> aFeature =
1137       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1138   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1139   apply(); // finish operation to remove Fillet feature correcly
1140   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1141 }
1142
1143 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1144     const ModelHighAPI_RefAttr & thePoint,
1145     const ModelHighAPI_Double & theRadius)
1146 {
1147   CompositeFeaturePtr aSketch = compositeFeature();
1148   int aNbSubs = aSketch->numberOfSubs();
1149
1150   // create fillet
1151   InterfacePtr aFilletFeature = setFillet(thePoint);
1152
1153   // set radius for just created arc
1154   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1155   if (anArc->getKind() == SketchPlugin_Arc::ID())
1156     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1157
1158   return aFilletFeature;
1159 }
1160
1161 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1162     const ModelHighAPI_RefAttr & theObject)
1163 {
1164   std::shared_ptr<ModelAPI_Feature> aFeature =
1165       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1166   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1167   aFeature->execute();
1168   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1169 }
1170
1171 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1172     const ModelHighAPI_RefAttr & theLine)
1173 {
1174   std::shared_ptr<ModelAPI_Feature> aFeature =
1175       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1176   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1177   aFeature->execute();
1178   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1179 }
1180
1181 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1182     const ModelHighAPI_RefAttr & theLine,
1183     const ModelHighAPI_Double & theValue)
1184 {
1185   std::shared_ptr<ModelAPI_Feature> aFeature =
1186       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1187   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1188   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1189   aFeature->execute();
1190   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1191 }
1192
1193 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1194     const ModelHighAPI_RefAttr & thePoint,
1195     const ModelHighAPI_RefAttr & theLine)
1196 {
1197   std::shared_ptr<ModelAPI_Feature> aFeature =
1198       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1199   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1200   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1201   aFeature->execute();
1202   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1203 }
1204
1205 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1206     const ModelHighAPI_RefAttr & theLine1,
1207     const ModelHighAPI_RefAttr & theLine2)
1208 {
1209   std::shared_ptr<ModelAPI_Feature> aFeature =
1210       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1211   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1212   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1213   aFeature->execute();
1214   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1215 }
1216
1217 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1218     const ModelHighAPI_RefAttr & theLine1,
1219     const ModelHighAPI_RefAttr & theLine2)
1220 {
1221   std::shared_ptr<ModelAPI_Feature> aFeature =
1222       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1223   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1224   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1225   aFeature->execute();
1226   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1227 }
1228
1229 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1230     const ModelHighAPI_RefAttr & theCircleOrArc,
1231     const ModelHighAPI_Double & theValue)
1232 {
1233   std::shared_ptr<ModelAPI_Feature> aFeature =
1234       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1235   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1236   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1237   aFeature->execute();
1238   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1239 }
1240
1241 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1242     const ModelHighAPI_RefAttr & theLine,
1243     const ModelHighAPI_RefAttr & theCircle)
1244 {
1245   std::shared_ptr<ModelAPI_Feature> aFeature =
1246       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1247   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1248   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1249   aFeature->execute();
1250   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1251 }
1252
1253 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1254     const ModelHighAPI_RefAttr & theLine)
1255 {
1256   std::shared_ptr<ModelAPI_Feature> aFeature =
1257       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1258   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1259   aFeature->execute();
1260   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1261 }
1262
1263 //--------------------------------------------------------------------------------------
1264
1265 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1266 {
1267   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1268   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1269 }
1270
1271 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1272 {
1273   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1274       theFeature->attribute(SketchPlugin_Line::START_ID()));
1275   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1276       theFeature->attribute(SketchPlugin_Line::END_ID()));
1277
1278   if (!aStartAttr || !aEndAttr)
1279     return std::shared_ptr<GeomAPI_Pnt2d>();
1280
1281   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1282   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1283   return std::shared_ptr<GeomAPI_Pnt2d>(
1284       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1285 }
1286
1287 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1288 {
1289   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1290       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1291   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1292
1293   if (!aCenter || !aRadius)
1294     return std::shared_ptr<GeomAPI_Pnt2d>();
1295
1296   return std::shared_ptr<GeomAPI_Pnt2d>(
1297       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1298 }
1299
1300 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1301 {
1302   static const double PI = 3.141592653589793238463;
1303
1304   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1305       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1306   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1307       theFeature->attribute(SketchPlugin_Arc::START_ID()));
1308   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1309       theFeature->attribute(SketchPlugin_Arc::END_ID()));
1310
1311   if (!aCenterAttr || !aStartAttr || !aEndAttr)
1312     return std::shared_ptr<GeomAPI_Pnt2d>();
1313
1314   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1315       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1316   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1317       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1318
1319   double anAngle = aStartDir->angle(aEndDir);
1320   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1321   if (isReversed && anAngle > 0.)
1322     anAngle -= 2.0 * PI;
1323   else if (!isReversed && anAngle <= 0.)
1324     anAngle += 2.0 * PI;
1325
1326   double cosA = cos(anAngle);
1327   double sinA = sin(anAngle);
1328
1329   // rotate start dir to find middle point on arc
1330   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1331   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1332   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1333
1334   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1335 }
1336
1337 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1338                                                      bool isEllipse = true)
1339 {
1340   const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1341                                   SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1342   AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1343       theFeature->attribute(anAttrName));
1344   return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1345 }
1346
1347 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1348                                                            SketchAPI_Sketch* theSketch)
1349 {
1350   GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1351   GeomPointPtr aMiddle = anEdge.middlePoint();
1352   return theSketch->to2D(aMiddle);
1353 }
1354
1355 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1356                                                   SketchAPI_Sketch* theSketch)
1357 {
1358   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1359   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1360   if (aFeature) {
1361     // move only features of the following types
1362     const std::string& aFeatureKind = aFeature->getKind();
1363     if (aFeatureKind == SketchPlugin_Point::ID())
1364       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1365     else if (aFeatureKind == SketchPlugin_Line::ID())
1366       aMiddlePoint = middlePointOnLine(aFeature);
1367     else if (aFeatureKind == SketchPlugin_Circle::ID())
1368       aMiddlePoint = pointOnCircle(aFeature);
1369     else if (aFeatureKind == SketchPlugin_Arc::ID())
1370       aMiddlePoint = middlePointOnArc(aFeature);
1371     else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1372       aMiddlePoint = pointOnEllipse(aFeature);
1373     else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1374       aMiddlePoint = pointOnEllipse(aFeature, false);
1375     else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1376              aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1377       aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1378   }
1379   return aMiddlePoint;
1380 }
1381
1382 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1383                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1384 {
1385   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1386   theMovedEntity.fillMessage(aMessage);
1387
1388   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1389   if (aMessage->movedAttribute())
1390     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1391   else
1392     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1393
1394   if (!anOriginalPosition)
1395     return; // something has gone wrong, do not process movement
1396
1397   aMessage->setOriginalPosition(anOriginalPosition);
1398   aMessage->setCurrentPosition(theTargetPoint);
1399   Events_Loop::loop()->send(aMessage);
1400 }
1401
1402 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1403                             double theTargetX, double theTargetY)
1404 {
1405   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1406   move(theMovedEntity, aTargetPoint);
1407 }
1408
1409 //--------------------------------------------------------------------------------------
1410
1411 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1412 {
1413   FeaturePtr aBase = feature();
1414   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1415       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1416   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1417       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1418   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1419       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1420   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1421
1422   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1423 }
1424
1425 //--------------------------------------------------------------------------------------
1426
1427 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1428 {
1429   // collect edges of the first face
1430   std::list<GeomShapePtr> anEdges1;
1431   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1432     anEdges1.push_back(anExp.current());
1433   // compare edges of faces
1434   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1435     GeomShapePtr aCurrent = anExp.current();
1436     bool isFound = false;
1437     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1438     for (; anIt1 != anEdges1.end(); ++anIt1)
1439       if (aCurrent->isSameGeometry(*anIt1)) {
1440         isFound = true;
1441         anEdges1.erase(anIt1);
1442         break;
1443       }
1444     if (!isFound)
1445       return true;
1446   }
1447   return !anEdges1.empty();
1448 }
1449
1450 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1451 {
1452   ResultConstructionPtr aSketchResult =
1453       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1454   if (!aSketchResult)
1455     return false;
1456
1457   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1458       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1459   if (!aWires)
1460     return false;
1461
1462   // collect faces constructed by SketchBuilder algorithm
1463   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1464                                            aWires->norm(), aWires);
1465   const ListOfShape& aFaces = aSketchBuilder.faces();
1466
1467   // compare faces stored in sketch with faces generated by SketchBuilder
1468   int aNbSketchFaces = aSketchResult->facesNum();
1469   int aFaceIndex = 0;
1470   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1471        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1472        ++aFIt, ++aFaceIndex) {
1473     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1474     GeomFacePtr aCurFace = (*aFIt)->face();
1475     if (isDifferent(aSketchFace, aCurFace))
1476       return true;
1477   }
1478   return false;
1479 }
1480
1481 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1482                                std::list<std::list<ResultPtr> >& theEdges)
1483 {
1484   ResultConstructionPtr aSketchResult =
1485       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1486   if (!aSketchResult)
1487     return;
1488
1489   // collect curves of the sketch
1490   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1491   int aSubNum = theSketch->numberOfSubs();
1492   for (int a = 0; a < aSubNum; ++a) {
1493     FeaturePtr aSub = theSketch->subFeature(a);
1494     const std::list<ResultPtr>& aResults = aSub->results();
1495     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1496     for (; aRes != aResults.cend(); aRes++) {
1497       GeomShapePtr aCurShape = (*aRes)->shape();
1498       if (aCurShape && aCurShape->isEdge())
1499         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1500     }
1501   }
1502
1503   // convert each face to the list of results of its edges
1504   int aFacesNum = aSketchResult->facesNum();
1505   for (int a = 0; a < aFacesNum; ++a) {
1506     theEdges.push_back(std::list<ResultPtr>());
1507     std::list<ResultPtr>& aCurEdges = theEdges.back();
1508
1509     GeomFacePtr aFace = aSketchResult->face(a);
1510     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1511          anExp.more(); anExp.next()) {
1512       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1513       aCurEdges.push_back(aCurves[aCurrent]);
1514     }
1515   }
1516 }
1517
1518 //--------------------------------------------------------------------------------------
1519
1520 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1521 {
1522   FeaturePtr aBase = feature();
1523   const std::string& aDocName = theDumper.name(aBase->document());
1524
1525   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1526   if (anExternal->value()) {
1527     theDumper << aBase << " = model.addSketch(" << aDocName <<
1528       ", " << anExternal << ")" << std::endl;
1529   } else {
1530     // Sketch is base on a plane.
1531     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1532         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1533     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1534         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1535     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1536         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1537
1538     // Check the plane is coordinate plane
1539     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1540     if(anExternal->context()) { // checking for selected planes
1541       if (!aPlaneName.empty()
1542           && anExternal->context()->data()
1543           && anExternal->context()->data()->name() == aPlaneName) {
1544         // dump sketch based on coordinate plane
1545         theDumper << aBase << " = model.addSketch(" << aDocName
1546                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1547       } else { // some other plane
1548         theDumper << aBase << " = model.addSketch(" << aDocName <<
1549           ", " << anExternal<< ")" << std::endl;
1550       }
1551     } else {
1552       if (aPlaneName.empty()) {
1553         // needs import additional module
1554         theDumper.importModule("GeomAPI");
1555         // dump plane parameters
1556         const std::string& aSketchName = theDumper.name(aBase);
1557         std::string anOriginName = aSketchName + "_origin";
1558         std::string aNormalName  = aSketchName + "_norm";
1559         std::string aDirXName    = aSketchName + "_dirx";
1560         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1561         // and then dumplicate dumping it in the next line
1562         theDumper << anOriginName << " = " << anOrigin << "\n"
1563                   << aNormalName  << " = " << aNormal  << "\n"
1564                   << aDirXName    << " = " << aDirX    << "\n";
1565         // dump sketch based on arbitrary plane
1566         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1567                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1568       } else {
1569         // dump sketch based on coordinate plane
1570         theDumper << aBase << " = model.addSketch(" << aDocName
1571                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1572       }
1573     }
1574   }
1575
1576   // dump sketch's subfeatures
1577   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1578   theDumper.processSubs(aCompFeat, true);
1579
1580   // if face order differs to the order generated by SketchBuilder,
1581   // dump the list of faces for correct execution of the script
1582   if (isCustomFacesOrder(aCompFeat)) {
1583     std::list<std::list<ResultPtr> > aFaces;
1584     edgesOfSketchFaces(aCompFeat, aFaces);
1585
1586     const std::string& aSketchName = theDumper.name(aBase);
1587     std::string aMethodName(".changeFacesOrder");
1588     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1589
1590     theDumper << aSketchName << aMethodName << "([";
1591     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1592          aFIt != aFaces.end(); ++aFIt) {
1593       if (aFIt != aFaces.begin())
1594         theDumper << ",\n" << aSpaceShift << "  ";
1595       theDumper << *aFIt;
1596     }
1597     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1598     // call model.do() for correct update of the document's labels related to the changed faces
1599     theDumper << "model.do()" << std::endl;
1600   }
1601 }