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