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