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