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