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