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