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