Salome HOME
Fix crash on importing salome.shaper.model
[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
703 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
704     const ModelHighAPI_Selection & external,
705     const int degree,
706     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& poles,
707     const std::list<ModelHighAPI_Double>& weights,
708     const std::list<ModelHighAPI_Double>& knots,
709     const std::list<ModelHighAPI_Integer>& multiplicities,
710     const bool periodic)
711 {
712   FeaturePtr aFeature = compositeFeature()->addFeature(
713     periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
714
715   BSplinePtr aBSpline(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
716                                : new SketchAPI_BSpline(aFeature));
717   if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
718     aBSpline->setByExternal(external);
719   else if (knots.empty() || multiplicities.empty())
720     aBSpline->setByDegreePolesAndWeights(degree, poles, weights);
721   else
722     aBSpline->setByParameters(degree, poles, weights, knots, multiplicities);
723
724   return aBSpline;
725 }
726
727 //--------------------------------------------------------------------------------------
728 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
729     const ModelHighAPI_Selection & theExternalFeature,
730     bool theKeepResult)
731 {
732   std::shared_ptr<ModelAPI_Feature> aFeature =
733     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
734   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
735   aProjection->setIncludeToResult(theKeepResult);
736   return aProjection;
737 }
738
739 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
740     const std::string & theExternalName,
741     bool theKeepResult)
742 {
743   std::shared_ptr<ModelAPI_Feature> aFeature =
744     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
745   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
746   aProjection->setIncludeToResult(theKeepResult);
747   return aProjection;
748 }
749
750 //--------------------------------------------------------------------------------------
751 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
752     const ModelHighAPI_RefAttr & theMirrorLine,
753     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
754 {
755   std::shared_ptr<ModelAPI_Feature> aFeature =
756     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
757   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
758 }
759
760 //--------------------------------------------------------------------------------------
761 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
762     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
763     const ModelHighAPI_RefAttr & thePoint1,
764     const ModelHighAPI_RefAttr & thePoint2,
765     const ModelHighAPI_Integer & theNumberOfObjects,
766     bool theFullValue)
767 {
768   std::shared_ptr<ModelAPI_Feature> aFeature =
769     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
770   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
771                                                   thePoint2, theNumberOfObjects, theFullValue));
772 }
773
774 //--------------------------------------------------------------------------------------
775 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
776     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
777     const ModelHighAPI_RefAttr & theCenter,
778     const ModelHighAPI_Double & theAngle,
779     const ModelHighAPI_Integer & theNumberOfObjects,
780     bool theFullValue,
781     bool theReversed)
782 {
783   std::shared_ptr<ModelAPI_Feature> aFeature =
784     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
785   return RotationPtr(
786     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
787                            theAngle, theNumberOfObjects, theFullValue, theReversed));
788 }
789
790 //--------------------------------------------------------------------------------------
791 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
792                                           const ModelHighAPI_Reference& theFeature,
793                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
794 {
795   std::shared_ptr<ModelAPI_Feature> aFeature =
796     compositeFeature()->addFeature(SketchPlugin_Split::ID());
797   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
798
799   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
800   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
801     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
802     fillAttribute(thePositionPoint, aPointAttr);
803   }
804
805   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
806 }
807
808 //--------------------------------------------------------------------------------------
809 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
810                                         const ModelHighAPI_Reference& theFeature,
811                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
812 {
813   std::shared_ptr<ModelAPI_Feature> aFeature =
814     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
815   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
816
817   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
818   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
819     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
820     fillAttribute(thePositionPoint, aPointAttr);
821   }
822
823   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
824 }
825
826 //--------------------------------------------------------------------------------------
827 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
828     const ModelHighAPI_RefAttr & theLine1,
829     const ModelHighAPI_RefAttr & theLine2,
830     const ModelHighAPI_Double & theValue,
831     const std::string& theType)
832 {
833   std::shared_ptr<ModelAPI_Feature> aFeature =
834       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
835
836   const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
837                                        : SketchPlugin_ConstraintAngle::THE_VERSION_1;
838   fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
839
840   int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
841   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
842   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
843
844   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
845     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
846
847   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
848   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
849
850   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
851     std::string aTypeLC = theType;
852     std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(), ::tolower);
853     if (aTypeLC == "supplementary")
854       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
855     else if (aTypeLC == "backward")
856       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
857
858     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
859     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
860   }
861
862   aFeature->execute();
863   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
864 }
865
866 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
867     const ModelHighAPI_RefAttr & theLine1,
868     const ModelHighAPI_RefAttr & theLine2,
869     const ModelHighAPI_Double & theValue)
870 {
871   std::shared_ptr<ModelAPI_Feature> aFeature =
872       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
873   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
874       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
875   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
876       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
877   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
878   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
879   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
880   aFeature->execute();
881   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
882 }
883
884 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
885     const ModelHighAPI_RefAttr & theLine1,
886     const ModelHighAPI_RefAttr & theLine2,
887     const ModelHighAPI_Double & theValue)
888 {
889   std::shared_ptr<ModelAPI_Feature> aFeature =
890       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
891   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
892       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
893   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
894       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
895   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
896   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
897   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
898   aFeature->execute();
899   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
900 }
901
902 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
903     const ModelHighAPI_RefAttr & thePoint1,
904     const ModelHighAPI_RefAttr & thePoint2)
905 {
906   std::shared_ptr<ModelAPI_Feature> aFeature =
907       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
908   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
909   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
910   aFeature->execute();
911   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
912 }
913
914 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
915     const ModelHighAPI_RefAttr & theLine1,
916     const ModelHighAPI_RefAttr & theLine2)
917 {
918   std::shared_ptr<ModelAPI_Feature> aFeature =
919       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
920   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
921   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
922   aFeature->execute();
923   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
924 }
925
926 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
927     const ModelHighAPI_RefAttr & thePoint,
928     const ModelHighAPI_RefAttr & thePointOrLine,
929     const ModelHighAPI_Double & theValue,
930     bool isSigned)
931 {
932   std::shared_ptr<ModelAPI_Feature> aFeature =
933       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
934   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
935   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
936   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
937   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
938   aFeature->execute();
939   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
940 }
941
942 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
943     const ModelHighAPI_RefAttr & thePoint,
944     const ModelHighAPI_RefAttr & thePointOrLine,
945     const ModelHighAPI_Double & theValue)
946 {
947   return setDistance(thePoint, thePointOrLine, theValue, true);
948 }
949
950 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
951     const ModelHighAPI_RefAttr & thePoint,
952     const ModelHighAPI_RefAttr & thePointOrLine,
953     const ModelHighAPI_Double & theValue)
954 {
955   return setDistance(thePoint, thePointOrLine, theValue, false);
956 }
957
958 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
959     const ModelHighAPI_RefAttr & thePoint1,
960     const ModelHighAPI_RefAttr & thePoint2,
961     const ModelHighAPI_Double & theValue)
962 {
963   std::shared_ptr<ModelAPI_Feature> aFeature =
964       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
965   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
966   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
967   fillAttribute(theValue,
968       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
969   aFeature->execute();
970   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
971 }
972
973 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
974     const ModelHighAPI_RefAttr & thePoint1,
975     const ModelHighAPI_RefAttr & thePoint2,
976     const ModelHighAPI_Double & theValue)
977 {
978   std::shared_ptr<ModelAPI_Feature> aFeature =
979       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
980   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
981   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
982   fillAttribute(theValue,
983       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
984   aFeature->execute();
985   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
986 }
987
988 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
989     const ModelHighAPI_RefAttr & theObject1,
990     const ModelHighAPI_RefAttr & theObject2)
991 {
992   std::shared_ptr<ModelAPI_Feature> aFeature =
993       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
994   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
995   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
996   aFeature->execute();
997   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
998 }
999
1000 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1001     const ModelHighAPI_RefAttr & thePoint)
1002 {
1003   std::shared_ptr<ModelAPI_Feature> aFeature =
1004       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1005   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1006   apply(); // finish operation to remove Fillet feature correcly
1007   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1008 }
1009
1010 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1011     const ModelHighAPI_RefAttr & thePoint,
1012     const ModelHighAPI_Double & theRadius)
1013 {
1014   CompositeFeaturePtr aSketch = compositeFeature();
1015   int aNbSubs = aSketch->numberOfSubs();
1016
1017   // create fillet
1018   InterfacePtr aFilletFeature = setFillet(thePoint);
1019
1020   // set radius for just created arc
1021   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1022   if (anArc->getKind() == SketchPlugin_Arc::ID())
1023     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1024
1025   return aFilletFeature;
1026 }
1027
1028 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1029     const ModelHighAPI_RefAttr & theObject)
1030 {
1031   std::shared_ptr<ModelAPI_Feature> aFeature =
1032       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1033   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1034   aFeature->execute();
1035   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1036 }
1037
1038 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1039     const ModelHighAPI_RefAttr & theLine)
1040 {
1041   std::shared_ptr<ModelAPI_Feature> aFeature =
1042       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1043   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1044   aFeature->execute();
1045   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1046 }
1047
1048 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1049     const ModelHighAPI_RefAttr & theLine,
1050     const ModelHighAPI_Double & theValue)
1051 {
1052   std::shared_ptr<ModelAPI_Feature> aFeature =
1053       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1054   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1055   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1056   aFeature->execute();
1057   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1058 }
1059
1060 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1061     const ModelHighAPI_RefAttr & thePoint,
1062     const ModelHighAPI_RefAttr & theLine)
1063 {
1064   std::shared_ptr<ModelAPI_Feature> aFeature =
1065       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1066   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1067   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1068   aFeature->execute();
1069   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1070 }
1071
1072 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1073     const ModelHighAPI_RefAttr & theLine1,
1074     const ModelHighAPI_RefAttr & theLine2)
1075 {
1076   std::shared_ptr<ModelAPI_Feature> aFeature =
1077       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1078   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1079   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1080   aFeature->execute();
1081   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1082 }
1083
1084 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1085     const ModelHighAPI_RefAttr & theLine1,
1086     const ModelHighAPI_RefAttr & theLine2)
1087 {
1088   std::shared_ptr<ModelAPI_Feature> aFeature =
1089       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1090   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1091   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1092   aFeature->execute();
1093   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1094 }
1095
1096 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1097     const ModelHighAPI_RefAttr & theCircleOrArc,
1098     const ModelHighAPI_Double & theValue)
1099 {
1100   std::shared_ptr<ModelAPI_Feature> aFeature =
1101       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1102   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1103   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1104   aFeature->execute();
1105   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1106 }
1107
1108 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1109     const ModelHighAPI_RefAttr & theLine,
1110     const ModelHighAPI_RefAttr & theCircle)
1111 {
1112   std::shared_ptr<ModelAPI_Feature> aFeature =
1113       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1114   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1115   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1116   aFeature->execute();
1117   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1118 }
1119
1120 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1121     const ModelHighAPI_RefAttr & theLine)
1122 {
1123   std::shared_ptr<ModelAPI_Feature> aFeature =
1124       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1125   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1126   aFeature->execute();
1127   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1128 }
1129
1130 //--------------------------------------------------------------------------------------
1131
1132 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1133 {
1134   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1135   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1136 }
1137
1138 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1139 {
1140   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1141       theFeature->attribute(SketchPlugin_Line::START_ID()));
1142   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1143       theFeature->attribute(SketchPlugin_Line::END_ID()));
1144
1145   if (!aStartAttr || !aEndAttr)
1146     return std::shared_ptr<GeomAPI_Pnt2d>();
1147
1148   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1149   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1150   return std::shared_ptr<GeomAPI_Pnt2d>(
1151       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1152 }
1153
1154 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1155 {
1156   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1157       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1158   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1159
1160   if (!aCenter || !aRadius)
1161     return std::shared_ptr<GeomAPI_Pnt2d>();
1162
1163   return std::shared_ptr<GeomAPI_Pnt2d>(
1164       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1165 }
1166
1167 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1168 {
1169   static const double PI = 3.141592653589793238463;
1170
1171   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1172       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1173   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1174       theFeature->attribute(SketchPlugin_Arc::START_ID()));
1175   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1176       theFeature->attribute(SketchPlugin_Arc::END_ID()));
1177
1178   if (!aCenterAttr || !aStartAttr || !aEndAttr)
1179     return std::shared_ptr<GeomAPI_Pnt2d>();
1180
1181   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1182       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1183   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1184       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1185
1186   double anAngle = aStartDir->angle(aEndDir);
1187   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1188   if (isReversed && anAngle > 0.)
1189     anAngle -= 2.0 * PI;
1190   else if (!isReversed && anAngle <= 0.)
1191     anAngle += 2.0 * PI;
1192
1193   double cosA = cos(anAngle);
1194   double sinA = sin(anAngle);
1195
1196   // rotate start dir to find middle point on arc
1197   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1198   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1199   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1200
1201   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1202 }
1203
1204 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1205                                                      bool isEllipse = true)
1206 {
1207   const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1208                                   SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1209   AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1210       theFeature->attribute(anAttrName));
1211   return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1212 }
1213
1214 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1215                                                            SketchAPI_Sketch* theSketch)
1216 {
1217   GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1218   GeomPointPtr aMiddle = anEdge.middlePoint();
1219   return theSketch->to2D(aMiddle);
1220 }
1221
1222 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1223                                                   SketchAPI_Sketch* theSketch)
1224 {
1225   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1226   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1227   if (aFeature) {
1228     // move only features of the following types
1229     const std::string& aFeatureKind = aFeature->getKind();
1230     if (aFeatureKind == SketchPlugin_Point::ID())
1231       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1232     else if (aFeatureKind == SketchPlugin_Line::ID())
1233       aMiddlePoint = middlePointOnLine(aFeature);
1234     else if (aFeatureKind == SketchPlugin_Circle::ID())
1235       aMiddlePoint = pointOnCircle(aFeature);
1236     else if (aFeatureKind == SketchPlugin_Arc::ID())
1237       aMiddlePoint = middlePointOnArc(aFeature);
1238     else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1239       aMiddlePoint = pointOnEllipse(aFeature);
1240     else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1241       aMiddlePoint = pointOnEllipse(aFeature, false);
1242     else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1243              aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1244       aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1245   }
1246   return aMiddlePoint;
1247 }
1248
1249 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1250                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1251 {
1252   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1253   theMovedEntity.fillMessage(aMessage);
1254
1255   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1256   if (aMessage->movedAttribute())
1257     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1258   else
1259     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1260
1261   if (!anOriginalPosition)
1262     return; // something has gone wrong, do not process movement
1263
1264   aMessage->setOriginalPosition(anOriginalPosition);
1265   aMessage->setCurrentPosition(theTargetPoint);
1266   Events_Loop::loop()->send(aMessage);
1267 }
1268
1269 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1270                             double theTargetX, double theTargetY)
1271 {
1272   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1273   move(theMovedEntity, aTargetPoint);
1274 }
1275
1276 //--------------------------------------------------------------------------------------
1277
1278 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1279 {
1280   FeaturePtr aBase = feature();
1281   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1282       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1283   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1284       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1285   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1286       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1287   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1288
1289   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1290 }
1291
1292 //--------------------------------------------------------------------------------------
1293
1294 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1295 {
1296   // collect edges of the first face
1297   std::list<GeomShapePtr> anEdges1;
1298   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1299     anEdges1.push_back(anExp.current());
1300   // compare edges of faces
1301   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1302     GeomShapePtr aCurrent = anExp.current();
1303     bool isFound = false;
1304     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1305     for (; anIt1 != anEdges1.end(); ++anIt1)
1306       if (aCurrent->isSameGeometry(*anIt1)) {
1307         isFound = true;
1308         anEdges1.erase(anIt1);
1309         break;
1310       }
1311     if (!isFound)
1312       return true;
1313   }
1314   return !anEdges1.empty();
1315 }
1316
1317 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1318 {
1319   ResultConstructionPtr aSketchResult =
1320       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1321   if (!aSketchResult)
1322     return false;
1323
1324   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1325       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1326   if (!aWires)
1327     return false;
1328
1329   // collect faces constructed by SketchBuilder algorithm
1330   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1331                                            aWires->norm(), aWires);
1332   const ListOfShape& aFaces = aSketchBuilder.faces();
1333
1334   // compare faces stored in sketch with faces generated by SketchBuilder
1335   int aNbSketchFaces = aSketchResult->facesNum();
1336   int aFaceIndex = 0;
1337   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1338        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1339        ++aFIt, ++aFaceIndex) {
1340     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1341     GeomFacePtr aCurFace = (*aFIt)->face();
1342     if (isDifferent(aSketchFace, aCurFace))
1343       return true;
1344   }
1345   return false;
1346 }
1347
1348 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1349                                std::list<std::list<ResultPtr> >& theEdges)
1350 {
1351   ResultConstructionPtr aSketchResult =
1352       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1353   if (!aSketchResult)
1354     return;
1355
1356   // collect curves of the sketch
1357   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1358   int aSubNum = theSketch->numberOfSubs();
1359   for (int a = 0; a < aSubNum; ++a) {
1360     FeaturePtr aSub = theSketch->subFeature(a);
1361     const std::list<ResultPtr>& aResults = aSub->results();
1362     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1363     for (; aRes != aResults.cend(); aRes++) {
1364       GeomShapePtr aCurShape = (*aRes)->shape();
1365       if (aCurShape && aCurShape->isEdge())
1366         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1367     }
1368   }
1369
1370   // convert each face to the list of results of its edges
1371   int aFacesNum = aSketchResult->facesNum();
1372   for (int a = 0; a < aFacesNum; ++a) {
1373     theEdges.push_back(std::list<ResultPtr>());
1374     std::list<ResultPtr>& aCurEdges = theEdges.back();
1375
1376     GeomFacePtr aFace = aSketchResult->face(a);
1377     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1378          anExp.more(); anExp.next()) {
1379       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1380       aCurEdges.push_back(aCurves[aCurrent]);
1381     }
1382   }
1383 }
1384
1385 //--------------------------------------------------------------------------------------
1386
1387 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1388 {
1389   FeaturePtr aBase = feature();
1390   const std::string& aDocName = theDumper.name(aBase->document());
1391
1392   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1393   if (anExternal->value()) {
1394     theDumper << aBase << " = model.addSketch(" << aDocName <<
1395       ", " << anExternal << ")" << std::endl;
1396   } else {
1397     // Sketch is base on a plane.
1398     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1399         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1400     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1401         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1402     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1403         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1404
1405     // Check the plane is coordinate plane
1406     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1407     if(anExternal->context()) { // checking for selected planes
1408       if (!aPlaneName.empty()
1409           && anExternal->context()->data()
1410           && anExternal->context()->data()->name() == aPlaneName) {
1411         // dump sketch based on coordinate plane
1412         theDumper << aBase << " = model.addSketch(" << aDocName
1413                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1414       } else { // some other plane
1415         theDumper << aBase << " = model.addSketch(" << aDocName <<
1416           ", " << anExternal<< ")" << std::endl;
1417       }
1418     } else {
1419       if (aPlaneName.empty()) {
1420         // needs import additional module
1421         theDumper.importModule("GeomAPI");
1422         // dump plane parameters
1423         const std::string& aSketchName = theDumper.name(aBase);
1424         std::string anOriginName = aSketchName + "_origin";
1425         std::string aNormalName  = aSketchName + "_norm";
1426         std::string aDirXName    = aSketchName + "_dirx";
1427         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1428         // and then dumplicate dumping it in the next line
1429         theDumper << anOriginName << " = " << anOrigin << "\n"
1430                   << aNormalName  << " = " << aNormal  << "\n"
1431                   << aDirXName    << " = " << aDirX    << "\n";
1432         // dump sketch based on arbitrary plane
1433         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1434                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1435       } else {
1436         // dump sketch based on coordinate plane
1437         theDumper << aBase << " = model.addSketch(" << aDocName
1438                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1439       }
1440     }
1441   }
1442
1443   // dump sketch's subfeatures
1444   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1445   theDumper.processSubs(aCompFeat, true);
1446
1447   // if face order differs to the order generated by SketchBuilder,
1448   // dump the list of faces for correct execution of the script
1449   if (isCustomFacesOrder(aCompFeat)) {
1450     std::list<std::list<ResultPtr> > aFaces;
1451     edgesOfSketchFaces(aCompFeat, aFaces);
1452
1453     const std::string& aSketchName = theDumper.name(aBase);
1454     std::string aMethodName(".changeFacesOrder");
1455     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1456
1457     theDumper << aSketchName << aMethodName << "([";
1458     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1459          aFIt != aFaces.end(); ++aFIt) {
1460       if (aFIt != aFaces.begin())
1461         theDumper << ",\n" << aSpaceShift << "  ";
1462       theDumper << *aFIt;
1463     }
1464     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1465     // call model.do() for correct update of the document's labels related to the changed faces
1466     theDumper << "model.do()" << std::endl;
1467   }
1468 }