Salome HOME
[EDF] (2023-T1) Sketch middle point constrain should create point if missing
[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 //--------------------------------------------------------------------------------------
593 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
594                                                               double theCenterY,
595                                                               double theRadius)
596 {
597   std::shared_ptr<ModelAPI_Feature> aFeature =
598     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
599   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
600 }
601
602 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
603                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
604                                     double theRadius)
605 {
606   std::shared_ptr<ModelAPI_Feature> aFeature =
607     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
608   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
609 }
610
611 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
612                                                                    double theCenterY,
613                                                                    double thePassedX,
614                                                                    double thePassedY)
615 {
616   std::shared_ptr<ModelAPI_Feature> aFeature =
617     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
618   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
619                                                             thePassedX, thePassedY));
620 }
621
622 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
623     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
624     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
625 {
626   std::shared_ptr<ModelAPI_Feature> aFeature =
627     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
628   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
629 }
630
631 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
632                                                                    double theX2, double theY2,
633                                                                    double theX3, double theY3)
634 {
635   std::shared_ptr<ModelAPI_Feature> aFeature =
636     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
637   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
638                                                             theX2, theY2,
639                                                             theX3, theY3));
640 }
641
642 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
643     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
644     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
645     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
646 {
647   std::shared_ptr<ModelAPI_Feature> aFeature =
648     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
649   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
650 }
651
652 std::shared_ptr<SketchAPI_Circle>
653   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
654 {
655   std::shared_ptr<ModelAPI_Feature> aFeature =
656     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
657   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
658 }
659
660 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::wstring & theExternalName)
661 {
662   std::shared_ptr<ModelAPI_Feature> aFeature =
663     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
664   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
665 }
666
667 //--------------------------------------------------------------------------------------
668 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
669                                                         double theStartX, double theStartY,
670                                                         double theEndX, double theEndY,
671                                                         bool theInversed)
672 {
673   std::shared_ptr<ModelAPI_Feature> aFeature =
674     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
675   return ArcPtr(new SketchAPI_Arc(aFeature,
676                                   theCenterX, theCenterY,
677                                   theStartX, theStartY,
678                                   theEndX, theEndY,
679                                   theInversed));
680 }
681
682 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
683                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
684                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
685                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
686                                               bool theInversed)
687 {
688   std::shared_ptr<ModelAPI_Feature> aFeature =
689     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
690   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
691 }
692
693 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
694                                                         double theEndX, double theEndY,
695                                                         double thePassedX, double thePassedY)
696 {
697   std::shared_ptr<ModelAPI_Feature> aFeature =
698     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
699   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
700                                        theStartX, theStartY,
701                                        theEndX, theEndY,
702                                        thePassedX, thePassedY));
703 }
704
705 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
706                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
707                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
708                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
709 {
710   std::shared_ptr<ModelAPI_Feature> aFeature =
711     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
712   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
713 }
714
715 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
716                                                 const ModelHighAPI_RefAttr& theTangentPoint,
717                                                 double theEndX, double theEndY,
718                                                 bool theInversed,
719                                                 bool theTransversal)
720 {
721   std::shared_ptr<ModelAPI_Feature> aFeature =
722     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
723   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
724   if (theTransversal)
725     aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
726   else
727     aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
728   return aMacroArc;
729 }
730
731 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
732                                               const ModelHighAPI_RefAttr& theTangentPoint,
733                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
734                                               bool theInversed,
735                                               bool theTransversal)
736 {
737   std::shared_ptr<ModelAPI_Feature> aFeature =
738     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
739   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
740   if (theTransversal)
741     aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
742   else
743     aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
744   return aMacroArc;
745 }
746
747 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
748 {
749   std::shared_ptr<ModelAPI_Feature> aFeature =
750     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
751   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
752 }
753
754 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::wstring & theExternalName)
755 {
756   std::shared_ptr<ModelAPI_Feature> aFeature =
757     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
758   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
759 }
760
761 //--------------------------------------------------------------------------------------
762 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
763     double theCenterX, double theCenterY,
764     double theFocusX, double theFocusY,
765     double theMinorRadius)
766 {
767   std::shared_ptr<ModelAPI_Feature> aFeature =
768       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
769   return EllipsePtr(new SketchAPI_Ellipse(aFeature,
770       theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
771 }
772
773 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
774     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
775     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
776     double theMinorRadius)
777 {
778   std::shared_ptr<ModelAPI_Feature> aFeature =
779       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
780   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
781 }
782
783 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
784     double thePoint1X, double thePoint1Y,
785     double thePoint2X, double thePoint2Y,
786     double thePassedX, double thePassedY,
787     bool isPoint1Center)
788 {
789   std::shared_ptr<ModelAPI_Feature> aFeature =
790       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
791   return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
792       thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
793 }
794
795 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
796     const PointOrReference& thePoint1,
797     const PointOrReference& thePoint2,
798     const PointOrReference& thePassedPoint,
799     bool isPoint1Center)
800 {
801   std::shared_ptr<ModelAPI_Feature> aFeature =
802       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
803   MacroEllipsePtr anEllipse;
804   if (thePoint1.second.isEmpty() &&
805       thePoint2.second.isEmpty() &&
806       thePassedPoint.second.isEmpty()) {
807     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
808         thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
809   }
810   else {
811     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
812         thePoint1.first, thePoint1.second,
813         thePoint2.first, thePoint2.second,
814         thePassedPoint.first, thePassedPoint.second,
815         isPoint1Center));
816   }
817   return anEllipse;
818 }
819
820 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
821     const ModelHighAPI_Selection & theExternal)
822 {
823   std::shared_ptr<ModelAPI_Feature> aFeature =
824       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
825   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
826 }
827
828 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
829     const std::wstring & theExternalName)
830 {
831   std::shared_ptr<ModelAPI_Feature> aFeature =
832       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
833   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
834 }
835
836 //--------------------------------------------------------------------------------------
837 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
838     double theCenterX, double theCenterY,
839     double theFocusX, double theFocusY,
840     double theStartX, double theStartY,
841     double theEndX, double theEndY,
842     bool theInversed)
843 {
844   std::shared_ptr<ModelAPI_Feature> aFeature =
845       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
846   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
847       theCenterX, theCenterY,
848       theFocusX, theFocusY,
849       theStartX, theStartY,
850       theEndX, theEndY,
851       theInversed));
852 }
853
854 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
855     const PointOrReference& theCenter,
856     const PointOrReference& theMajorAxisPoint,
857     const PointOrReference& theStartPoint,
858     const PointOrReference& theEndPoint,
859     bool theInversed)
860 {
861   std::shared_ptr<ModelAPI_Feature> aFeature =
862       compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
863   return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
864       theCenter.first, theCenter.second,
865       theMajorAxisPoint.first, theMajorAxisPoint.second,
866       theStartPoint.first, theStartPoint.second,
867       theEndPoint.first, theEndPoint.second,
868       theInversed));
869 }
870
871 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
872     const ModelHighAPI_Selection & theExternal)
873 {
874   std::shared_ptr<ModelAPI_Feature> aFeature =
875       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
876   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
877 }
878
879 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
880     const std::wstring & theExternalName)
881 {
882   std::shared_ptr<ModelAPI_Feature> aFeature =
883       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
884   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
885 }
886
887 //--------------------------------------------------------------------------------------
888
889 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
890     const ModelHighAPI_Selection & external,
891     const int degree,
892     const std::list<PointOrReference>& poles,
893     const std::list<ModelHighAPI_Double>& weights,
894     const std::list<ModelHighAPI_Double>& knots,
895     const std::list<ModelHighAPI_Integer>& multiplicities,
896     const bool periodic)
897 {
898   // split poles and references to other shapes
899   bool hasReference = false;
900   std::list<GeomPnt2dPtr> aPoints;
901   std::list<ModelHighAPI_RefAttr> aReferences;
902   for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
903     aPoints.push_back(it->first);
904     aReferences.push_back(it->second);
905     if (!it->second.isEmpty())
906       hasReference = true;
907   }
908
909   BSplinePtr aBSpline;
910   CompositeFeaturePtr aSketch = compositeFeature();
911   if (hasReference) {
912     // use macro-feature to create coincidences to referred features
913     FeaturePtr aMacroFeature = aSketch->addFeature(
914         periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
915     AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
916         aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
917     AttributeDoubleArrayPtr aWeightsAttr =
918         aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
919     AttributeRefAttrListPtr aPolesRefAttr =
920         aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
921     // always generate a control polygon to apply coincidences correctly
922     aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
923     // initialize B-spline attributes
924     fillAttribute(aPoints, aPolesAttr);
925     if (weights.empty())
926       fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
927     else
928       fillAttribute(weights, aWeightsAttr);
929     fillAttribute(aReferences, aPolesRefAttr);
930     apply(); // to kill macro-feature
931
932     // find created B-spline feature
933     const std::string& aKindToFind =
934         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
935     int aNbSubs = aSketch->numberOfSubs();
936     for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
937       FeaturePtr aFeature = aSketch->subFeature(anIndex);
938       if (aFeature->getKind() == aKindToFind) {
939         aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
940                                 : new SketchAPI_BSpline(aFeature));
941         aBSpline->execute();
942         break;
943       }
944     }
945   }
946   else {
947     // compute B-spline by parameters
948     FeaturePtr aFeature = aSketch->addFeature(
949         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
950
951     aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
952                             : new SketchAPI_BSpline(aFeature));
953     if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
954       aBSpline->setByExternal(external);
955     else if (knots.empty() || multiplicities.empty())
956       aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
957     else
958       aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
959   }
960   return aBSpline;
961 }
962
963 //--------------------------------------------------------------------------------------
964 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
965     const CompositeFeaturePtr& theSketch,
966     const FeaturePtr& theCurveFittingFeature,
967     const std::list<ModelHighAPI_RefAttr>& points,
968     const bool periodic,
969     const bool closed)
970 {
971   AttributeBooleanPtr aPeriodicAttr =
972       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
973   fillAttribute(periodic, aPeriodicAttr);
974   AttributeBooleanPtr aClosedAttr =
975       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
976   fillAttribute(closed, aClosedAttr);
977   AttributeRefAttrListPtr aPointsAttr =
978       theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
979   fillAttribute(points, aPointsAttr);
980   apply(); // to execute and kill the macro-feature
981
982   // find created B-spline feature
983   BSplinePtr aBSpline;
984   const std::string& aKindToFind =
985       periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
986   int aNbSubs = theSketch->numberOfSubs();
987   for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
988     FeaturePtr aFeature = theSketch->subFeature(anIndex);
989     if (aFeature->getKind() == aKindToFind) {
990       aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
991         : new SketchAPI_BSpline(aFeature));
992       aBSpline->execute();
993       break;
994     }
995   }
996   return aBSpline;
997 }
998
999 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
1000     const std::list<ModelHighAPI_RefAttr>& points,
1001     const bool periodic,
1002     const bool closed)
1003 {
1004   CompositeFeaturePtr aSketch = compositeFeature();
1005   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1006   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1007       ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
1008   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1009 }
1010
1011 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
1012     const std::list<ModelHighAPI_RefAttr>& points,
1013     const ModelHighAPI_Double& precision,
1014     const bool periodic,
1015     const bool closed)
1016 {
1017   CompositeFeaturePtr aSketch = compositeFeature();
1018   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
1019   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
1020       ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
1021   fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
1022   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
1023 }
1024
1025 //--------------------------------------------------------------------------------------
1026 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
1027     const ModelHighAPI_Selection & theExternalFeature,
1028     bool keepResult,
1029     bool keepRefToOriginal)
1030 {
1031   std::shared_ptr<ModelAPI_Feature> aFeature =
1032     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
1033   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature));
1034   aProjection->setIncludeToResult(keepResult);
1035   aProjection->setKeepReferenceToOriginal(keepRefToOriginal);
1036   aProjection->setExternalFeature(theExternalFeature);
1037   return aProjection;
1038 }
1039
1040 //--------------------------------------------------------------------------------------
1041 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
1042     const ModelHighAPI_RefAttr & theMirrorLine,
1043     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
1044 {
1045   std::shared_ptr<ModelAPI_Feature> aFeature =
1046     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
1047   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
1048 }
1049
1050 //--------------------------------------------------------------------------------------
1051 std::shared_ptr<SketchAPI_Offset> SketchAPI_Sketch::addOffset(
1052     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1053     const ModelHighAPI_Double & theValue,
1054     const bool theReversed,
1055     const std::string & theJointType,
1056     const bool theApprox)
1057 {
1058   std::shared_ptr<ModelAPI_Feature> aFeature =
1059     compositeFeature()->addFeature(SketchPlugin_Offset::ID());
1060   return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed, theJointType, theApprox));
1061 }
1062
1063 //--------------------------------------------------------------------------------------
1064 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
1065     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1066     const ModelHighAPI_RefAttr & thePoint1,
1067     const ModelHighAPI_RefAttr & thePoint2,
1068     const ModelHighAPI_Integer & theNumberOfObjects,
1069     bool theFullValue)
1070 {
1071   std::shared_ptr<ModelAPI_Feature> aFeature =
1072     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
1073   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
1074                                                   thePoint2, theNumberOfObjects, theFullValue));
1075 }
1076
1077 //--------------------------------------------------------------------------------------
1078 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
1079     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
1080     const ModelHighAPI_RefAttr & theCenter,
1081     const ModelHighAPI_Double & theAngle,
1082     const ModelHighAPI_Integer & theNumberOfObjects,
1083     bool theFullValue,
1084     bool theReversed)
1085 {
1086   std::shared_ptr<ModelAPI_Feature> aFeature =
1087     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
1088   return RotationPtr(
1089     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
1090                            theAngle, theNumberOfObjects, theFullValue, theReversed));
1091 }
1092
1093 //--------------------------------------------------------------------------------------
1094 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
1095                                           const ModelHighAPI_Reference& theFeature,
1096                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1097 {
1098   std::shared_ptr<ModelAPI_Feature> aFeature =
1099     compositeFeature()->addFeature(SketchPlugin_Split::ID());
1100   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
1101
1102   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
1103   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1104     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1105     fillAttribute(thePositionPoint, aPointAttr);
1106   }
1107
1108   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1109 }
1110
1111 //--------------------------------------------------------------------------------------
1112 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
1113                                         const ModelHighAPI_Reference& theFeature,
1114                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
1115 {
1116   std::shared_ptr<ModelAPI_Feature> aFeature =
1117     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
1118   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
1119
1120   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
1121   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1122     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1123     fillAttribute(thePositionPoint, aPointAttr);
1124   }
1125
1126   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1127 }
1128
1129 //--------------------------------------------------------------------------------------
1130 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
1131     const ModelHighAPI_RefAttr & theLine1,
1132     const ModelHighAPI_RefAttr & theLine2,
1133     const ModelHighAPI_Double & theValue,
1134     const std::string& theType)
1135 {
1136   std::shared_ptr<ModelAPI_Feature> aFeature =
1137       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1138
1139   const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
1140                                        : SketchPlugin_ConstraintAngle::THE_VERSION_1;
1141   fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1142
1143   int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
1144   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
1145   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1146
1147   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
1148     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1149
1150   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1151   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1152
1153   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
1154     std::string aTypeLC = theType;
1155     std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
1156                    [](char c) { return static_cast<char>(::tolower(c)); });
1157     if (aTypeLC == "supplementary")
1158       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
1159     else if (aTypeLC == "backward")
1160       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
1161
1162     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1163     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1164   }
1165
1166   aFeature->execute();
1167   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1168 }
1169
1170 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1171     const ModelHighAPI_RefAttr & theLine1,
1172     const ModelHighAPI_RefAttr & theLine2,
1173     const ModelHighAPI_Double & theValue)
1174 {
1175   std::shared_ptr<ModelAPI_Feature> aFeature =
1176       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1177   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1178       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1179   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1180       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1181   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1182   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1183   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1184   aFeature->execute();
1185   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1186 }
1187
1188 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1189     const ModelHighAPI_RefAttr & theLine1,
1190     const ModelHighAPI_RefAttr & theLine2,
1191     const ModelHighAPI_Double & theValue)
1192 {
1193   std::shared_ptr<ModelAPI_Feature> aFeature =
1194       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1195   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1196       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1197   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1198       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1199   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1200   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1201   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1202   aFeature->execute();
1203   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1204 }
1205
1206 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1207     const ModelHighAPI_RefAttr & thePoint1,
1208     const ModelHighAPI_RefAttr & thePoint2)
1209 {
1210   std::shared_ptr<ModelAPI_Feature> aFeature =
1211       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1212   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1213   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1214   aFeature->execute();
1215   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1216 }
1217
1218 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1219     const ModelHighAPI_RefAttr & theLine1,
1220     const ModelHighAPI_RefAttr & theLine2)
1221 {
1222   std::shared_ptr<ModelAPI_Feature> aFeature =
1223       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1224   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1225   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1226   aFeature->execute();
1227   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1228 }
1229
1230 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1231     const ModelHighAPI_RefAttr & thePoint,
1232     const ModelHighAPI_RefAttr & thePointOrLine,
1233     const ModelHighAPI_Double & theValue,
1234     bool isSigned)
1235 {
1236   std::shared_ptr<ModelAPI_Feature> aFeature =
1237       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1238   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1239   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1240   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1241   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1242   aFeature->execute();
1243   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1244 }
1245
1246 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1247     const ModelHighAPI_RefAttr & thePoint,
1248     const ModelHighAPI_RefAttr & thePointOrLine,
1249     const ModelHighAPI_Double & theValue)
1250 {
1251   return setDistance(thePoint, thePointOrLine, theValue, true);
1252 }
1253
1254 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1255     const ModelHighAPI_RefAttr & thePoint,
1256     const ModelHighAPI_RefAttr & thePointOrLine,
1257     const ModelHighAPI_Double & theValue)
1258 {
1259   return setDistance(thePoint, thePointOrLine, theValue, false);
1260 }
1261
1262 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1263     const ModelHighAPI_RefAttr & thePoint1,
1264     const ModelHighAPI_RefAttr & thePoint2,
1265     const ModelHighAPI_Double & theValue)
1266 {
1267   std::shared_ptr<ModelAPI_Feature> aFeature =
1268       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1269   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1270   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1271   fillAttribute(theValue,
1272       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1273   aFeature->execute();
1274   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1275 }
1276
1277 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1278     const ModelHighAPI_RefAttr & thePoint1,
1279     const ModelHighAPI_RefAttr & thePoint2,
1280     const ModelHighAPI_Double & theValue)
1281 {
1282   std::shared_ptr<ModelAPI_Feature> aFeature =
1283       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1284   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1285   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1286   fillAttribute(theValue,
1287       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1288   aFeature->execute();
1289   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1290 }
1291
1292 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1293     const ModelHighAPI_RefAttr & theObject1,
1294     const ModelHighAPI_RefAttr & theObject2)
1295 {
1296   std::shared_ptr<ModelAPI_Feature> aFeature =
1297       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1298   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1299   fillAttribute(theObject2, 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::setFillet(
1305     const ModelHighAPI_RefAttr & thePoint)
1306 {
1307   std::shared_ptr<ModelAPI_Feature> aFeature =
1308       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1309   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1310   apply(); // finish operation to remove Fillet feature correcly
1311   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1312 }
1313
1314 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1315     const ModelHighAPI_RefAttr & thePoint,
1316     const ModelHighAPI_Double & theRadius)
1317 {
1318   CompositeFeaturePtr aSketch = compositeFeature();
1319   int aNbSubs = aSketch->numberOfSubs();
1320
1321   // create fillet
1322   InterfacePtr aFilletFeature = setFillet(thePoint);
1323
1324   // set radius for just created arc
1325   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1326   if (anArc->getKind() == SketchPlugin_Arc::ID())
1327     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1328
1329   return aFilletFeature;
1330 }
1331
1332 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1333     const ModelHighAPI_RefAttr & theObject)
1334 {
1335   std::shared_ptr<ModelAPI_Feature> aFeature =
1336       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1337   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1338   aFeature->execute();
1339   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1340 }
1341
1342 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1343     const ModelHighAPI_RefAttr & theLine)
1344 {
1345   std::shared_ptr<ModelAPI_Feature> aFeature =
1346       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1347   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1348   aFeature->execute();
1349   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1350 }
1351
1352 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1353     const ModelHighAPI_RefAttr & theLine,
1354     const ModelHighAPI_Double & theValue)
1355 {
1356   std::shared_ptr<ModelAPI_Feature> aFeature =
1357       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1358   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1359   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1360   aFeature->execute();
1361   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1362 }
1363
1364 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1365     const ModelHighAPI_RefAttr & thePoint,
1366     const ModelHighAPI_RefAttr & theLine)
1367 {
1368   std::shared_ptr<ModelAPI_Feature> aFeature =
1369       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1370   auto aType = aFeature->data()->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE());
1371   fillAttribute(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE_AND_POINT(), aType);
1372
1373   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1374   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1375
1376   aFeature->execute();
1377   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1378 }
1379
1380 std::shared_ptr<SketchAPI_MacroMiddlePoint> SketchAPI_Sketch::setMiddlePoint(
1381   const ModelHighAPI_RefAttr& theLine)
1382 {
1383   std::shared_ptr<ModelAPI_Feature> aFeature =
1384     compositeFeature()->addFeature(SketchPlugin_Point::ID());
1385
1386   ObjectPtr anObj = theLine.object();
1387   auto aPoint = middlePoint(anObj, this);
1388
1389   return std::shared_ptr<SketchAPI_MacroMiddlePoint>
1390     (new SketchAPI_MacroMiddlePoint(aFeature, theLine, aPoint));
1391 }
1392
1393 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1394     const ModelHighAPI_RefAttr & theLine1,
1395     const ModelHighAPI_RefAttr & theLine2)
1396 {
1397   std::shared_ptr<ModelAPI_Feature> aFeature =
1398       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1399   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1400   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1401   aFeature->execute();
1402   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1403 }
1404
1405 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1406     const ModelHighAPI_RefAttr & theLine1,
1407     const ModelHighAPI_RefAttr & theLine2)
1408 {
1409   std::shared_ptr<ModelAPI_Feature> aFeature =
1410       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1411   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1412   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1413   aFeature->execute();
1414   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1415 }
1416
1417 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1418     const ModelHighAPI_RefAttr & theCircleOrArc,
1419     const ModelHighAPI_Double & theValue)
1420 {
1421   std::shared_ptr<ModelAPI_Feature> aFeature =
1422       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1423   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1424   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1425   aFeature->execute();
1426   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1427 }
1428
1429 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1430     const ModelHighAPI_RefAttr & theLine,
1431     const ModelHighAPI_RefAttr & theCircle)
1432 {
1433   std::shared_ptr<ModelAPI_Feature> aFeature =
1434       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1435   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1436   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1437   aFeature->execute();
1438   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1439 }
1440
1441 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1442     const ModelHighAPI_RefAttr & theLine)
1443 {
1444   std::shared_ptr<ModelAPI_Feature> aFeature =
1445       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1446   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1447   aFeature->execute();
1448   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1449 }
1450
1451 //--------------------------------------------------------------------------------------
1452
1453 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1454                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1455 {
1456   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1457   theMovedEntity.fillMessage(aMessage);
1458
1459   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1460   if (aMessage->movedAttribute())
1461     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1462   else
1463     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1464
1465   if (!anOriginalPosition)
1466     return; // something has gone wrong, do not process movement
1467
1468   aMessage->setOriginalPosition(anOriginalPosition);
1469   aMessage->setCurrentPosition(theTargetPoint);
1470   Events_Loop::loop()->send(aMessage);
1471 }
1472
1473 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1474                             double theTargetX, double theTargetY)
1475 {
1476   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1477   move(theMovedEntity, aTargetPoint);
1478 }
1479
1480 //--------------------------------------------------------------------------------------
1481
1482 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1483 {
1484   FeaturePtr aBase = feature();
1485   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1486       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1487   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1488       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1489   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1490       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1491   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1492
1493   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1494 }
1495
1496 //--------------------------------------------------------------------------------------
1497
1498 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1499 {
1500   // collect edges of the first face
1501   std::list<GeomShapePtr> anEdges1;
1502   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1503     anEdges1.push_back(anExp.current());
1504   // compare edges of faces
1505   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1506     GeomShapePtr aCurrent = anExp.current();
1507     bool isFound = false;
1508     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1509     for (; anIt1 != anEdges1.end(); ++anIt1)
1510       if (aCurrent->isSameGeometry(*anIt1)) {
1511         isFound = true;
1512         anEdges1.erase(anIt1);
1513         break;
1514       }
1515     if (!isFound)
1516       return true;
1517   }
1518   return !anEdges1.empty();
1519 }
1520
1521 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1522 {
1523   ResultConstructionPtr aSketchResult =
1524       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1525   if (!aSketchResult)
1526     return false;
1527
1528   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1529       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1530   if (!aWires)
1531     return false;
1532
1533   // collect faces constructed by SketchBuilder algorithm
1534   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1535                                            aWires->norm(), aWires);
1536   const ListOfShape& aFaces = aSketchBuilder.faces();
1537
1538   // compare faces stored in sketch with faces generated by SketchBuilder
1539   int aNbSketchFaces = aSketchResult->facesNum();
1540   int aFaceIndex = 0;
1541   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1542        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1543        ++aFIt, ++aFaceIndex) {
1544     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1545     GeomFacePtr aCurFace = (*aFIt)->face();
1546     if (isDifferent(aSketchFace, aCurFace))
1547       return true;
1548   }
1549   return false;
1550 }
1551
1552 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1553                                std::list<std::list<ResultPtr> >& theEdges)
1554 {
1555   ResultConstructionPtr aSketchResult =
1556       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1557   if (!aSketchResult)
1558     return;
1559
1560   // collect curves of the sketch
1561   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1562   int aSubNum = theSketch->numberOfSubs();
1563   for (int a = 0; a < aSubNum; ++a) {
1564     FeaturePtr aSub = theSketch->subFeature(a);
1565     const std::list<ResultPtr>& aResults = aSub->results();
1566     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1567     for (; aRes != aResults.cend(); aRes++) {
1568       GeomShapePtr aCurShape = (*aRes)->shape();
1569       if (aCurShape && aCurShape->isEdge())
1570         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1571     }
1572   }
1573
1574   // convert each face to the list of results of its edges
1575   int aFacesNum = aSketchResult->facesNum();
1576   for (int a = 0; a < aFacesNum; ++a) {
1577     theEdges.push_back(std::list<ResultPtr>());
1578     std::list<ResultPtr>& aCurEdges = theEdges.back();
1579
1580     GeomFacePtr aFace = aSketchResult->face(a);
1581     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1582          anExp.more(); anExp.next()) {
1583       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1584       aCurEdges.push_back(aCurves[aCurrent]);
1585     }
1586   }
1587 }
1588
1589 //--------------------------------------------------------------------------------------
1590
1591 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1592 {
1593   FeaturePtr aBase = feature();
1594   const std::string& aDocName = theDumper.name(aBase->document());
1595
1596   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1597   if (anExternal->value()) {
1598     theDumper << aBase << " = model.addSketch(" << aDocName <<
1599       ", " << anExternal << ")" << std::endl;
1600   } else {
1601     // Sketch is base on a plane.
1602     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1603         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1604     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1605         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1606     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1607         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1608
1609     // Check the plane is coordinate plane
1610     std::wstring aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1611     if(anExternal->context()) { // checking for selected planes
1612       if (!aPlaneName.empty()
1613           && anExternal->context()->data()
1614           && anExternal->context()->data()->name() == aPlaneName) {
1615         // dump sketch based on coordinate plane
1616         theDumper << aBase << " = model.addSketch(" << aDocName
1617                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1618       } else { // some other plane
1619         theDumper << aBase << " = model.addSketch(" << aDocName <<
1620           ", " << anExternal<< ")" << std::endl;
1621       }
1622     } else {
1623       if (aPlaneName.empty()) {
1624         // needs import additional module
1625         theDumper.importModule("GeomAPI");
1626         // dump plane parameters
1627         const std::string& aSketchName = theDumper.name(aBase);
1628         std::string anOriginName = aSketchName + "_origin";
1629         std::string aNormalName  = aSketchName + "_norm";
1630         std::string aDirXName    = aSketchName + "_dirx";
1631         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1632         // and then dumplicate dumping it in the next line
1633         theDumper << anOriginName << " = " << anOrigin << "\n"
1634                   << aNormalName  << " = " << aNormal  << "\n"
1635                   << aDirXName    << " = " << aDirX    << "\n";
1636         // dump sketch based on arbitrary plane
1637         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1638                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1639       } else {
1640         // dump sketch based on coordinate plane
1641         theDumper << aBase << " = model.addSketch(" << aDocName
1642                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1643       }
1644     }
1645   }
1646
1647   // dump sketch's subfeatures
1648   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1649   theDumper.processSubs(aCompFeat, true);
1650
1651   // if face order differs to the order generated by SketchBuilder,
1652   // dump the list of faces for correct execution of the script
1653   if (isCustomFacesOrder(aCompFeat)) {
1654     std::list<std::list<ResultPtr> > aFaces;
1655     edgesOfSketchFaces(aCompFeat, aFaces);
1656
1657     const std::string& aSketchName = theDumper.name(aBase);
1658     std::string aMethodName(".changeFacesOrder");
1659     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1660
1661     theDumper << aSketchName << aMethodName << "([";
1662     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1663          aFIt != aFaces.end(); ++aFIt) {
1664       if (aFIt != aFaces.begin())
1665         theDumper << ",\n" << aSpaceShift << "  ";
1666       theDumper << *aFIt;
1667     }
1668     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1669     // call model.do() for correct update of the document's labels related to the changed faces
1670     theDumper << "model.do()" << std::endl;
1671   }
1672 }