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