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