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