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