Salome HOME
Copyright update 2020
[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(), ::tolower);
922     if (aTypeLC == "supplementary")
923       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
924     else if (aTypeLC == "backward")
925       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
926
927     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
928     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
929   }
930
931   aFeature->execute();
932   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
933 }
934
935 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
936     const ModelHighAPI_RefAttr & theLine1,
937     const ModelHighAPI_RefAttr & theLine2,
938     const ModelHighAPI_Double & theValue)
939 {
940   std::shared_ptr<ModelAPI_Feature> aFeature =
941       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
942   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
943       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
944   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
945       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
946   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
947   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
948   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
949   aFeature->execute();
950   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
951 }
952
953 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
954     const ModelHighAPI_RefAttr & theLine1,
955     const ModelHighAPI_RefAttr & theLine2,
956     const ModelHighAPI_Double & theValue)
957 {
958   std::shared_ptr<ModelAPI_Feature> aFeature =
959       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
960   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
961       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
962   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
963       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
964   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
965   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
966   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
967   aFeature->execute();
968   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
969 }
970
971 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
972     const ModelHighAPI_RefAttr & thePoint1,
973     const ModelHighAPI_RefAttr & thePoint2)
974 {
975   std::shared_ptr<ModelAPI_Feature> aFeature =
976       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
977   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
978   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
979   aFeature->execute();
980   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
981 }
982
983 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
984     const ModelHighAPI_RefAttr & theLine1,
985     const ModelHighAPI_RefAttr & theLine2)
986 {
987   std::shared_ptr<ModelAPI_Feature> aFeature =
988       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
989   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
990   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
991   aFeature->execute();
992   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
993 }
994
995 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
996     const ModelHighAPI_RefAttr & thePoint,
997     const ModelHighAPI_RefAttr & thePointOrLine,
998     const ModelHighAPI_Double & theValue,
999     bool isSigned)
1000 {
1001   std::shared_ptr<ModelAPI_Feature> aFeature =
1002       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1003   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1004   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1005   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1006   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1007   aFeature->execute();
1008   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1009 }
1010
1011 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1012     const ModelHighAPI_RefAttr & thePoint,
1013     const ModelHighAPI_RefAttr & thePointOrLine,
1014     const ModelHighAPI_Double & theValue)
1015 {
1016   return setDistance(thePoint, thePointOrLine, theValue, true);
1017 }
1018
1019 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1020     const ModelHighAPI_RefAttr & thePoint,
1021     const ModelHighAPI_RefAttr & thePointOrLine,
1022     const ModelHighAPI_Double & theValue)
1023 {
1024   return setDistance(thePoint, thePointOrLine, theValue, false);
1025 }
1026
1027 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1028     const ModelHighAPI_RefAttr & thePoint1,
1029     const ModelHighAPI_RefAttr & thePoint2,
1030     const ModelHighAPI_Double & theValue)
1031 {
1032   std::shared_ptr<ModelAPI_Feature> aFeature =
1033       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1034   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1035   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1036   fillAttribute(theValue,
1037       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1038   aFeature->execute();
1039   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1040 }
1041
1042 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1043     const ModelHighAPI_RefAttr & thePoint1,
1044     const ModelHighAPI_RefAttr & thePoint2,
1045     const ModelHighAPI_Double & theValue)
1046 {
1047   std::shared_ptr<ModelAPI_Feature> aFeature =
1048       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1049   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1050   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1051   fillAttribute(theValue,
1052       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1053   aFeature->execute();
1054   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1055 }
1056
1057 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1058     const ModelHighAPI_RefAttr & theObject1,
1059     const ModelHighAPI_RefAttr & theObject2)
1060 {
1061   std::shared_ptr<ModelAPI_Feature> aFeature =
1062       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1063   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1064   fillAttribute(theObject2, 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::setFillet(
1070     const ModelHighAPI_RefAttr & thePoint)
1071 {
1072   std::shared_ptr<ModelAPI_Feature> aFeature =
1073       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1074   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1075   apply(); // finish operation to remove Fillet feature correcly
1076   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1077 }
1078
1079 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1080     const ModelHighAPI_RefAttr & thePoint,
1081     const ModelHighAPI_Double & theRadius)
1082 {
1083   CompositeFeaturePtr aSketch = compositeFeature();
1084   int aNbSubs = aSketch->numberOfSubs();
1085
1086   // create fillet
1087   InterfacePtr aFilletFeature = setFillet(thePoint);
1088
1089   // set radius for just created arc
1090   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1091   if (anArc->getKind() == SketchPlugin_Arc::ID())
1092     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1093
1094   return aFilletFeature;
1095 }
1096
1097 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1098     const ModelHighAPI_RefAttr & theObject)
1099 {
1100   std::shared_ptr<ModelAPI_Feature> aFeature =
1101       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1102   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1103   aFeature->execute();
1104   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1105 }
1106
1107 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1108     const ModelHighAPI_RefAttr & theLine)
1109 {
1110   std::shared_ptr<ModelAPI_Feature> aFeature =
1111       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1112   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1113   aFeature->execute();
1114   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1115 }
1116
1117 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1118     const ModelHighAPI_RefAttr & theLine,
1119     const ModelHighAPI_Double & theValue)
1120 {
1121   std::shared_ptr<ModelAPI_Feature> aFeature =
1122       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1123   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1124   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1125   aFeature->execute();
1126   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1127 }
1128
1129 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1130     const ModelHighAPI_RefAttr & thePoint,
1131     const ModelHighAPI_RefAttr & theLine)
1132 {
1133   std::shared_ptr<ModelAPI_Feature> aFeature =
1134       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1135   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1136   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1137   aFeature->execute();
1138   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1139 }
1140
1141 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1142     const ModelHighAPI_RefAttr & theLine1,
1143     const ModelHighAPI_RefAttr & theLine2)
1144 {
1145   std::shared_ptr<ModelAPI_Feature> aFeature =
1146       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1147   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1148   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1149   aFeature->execute();
1150   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1151 }
1152
1153 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
1154     const ModelHighAPI_RefAttr & theLine1,
1155     const ModelHighAPI_RefAttr & theLine2)
1156 {
1157   std::shared_ptr<ModelAPI_Feature> aFeature =
1158       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1159   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1160   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1161   aFeature->execute();
1162   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1163 }
1164
1165 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1166     const ModelHighAPI_RefAttr & theCircleOrArc,
1167     const ModelHighAPI_Double & theValue)
1168 {
1169   std::shared_ptr<ModelAPI_Feature> aFeature =
1170       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1171   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1172   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1173   aFeature->execute();
1174   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1175 }
1176
1177 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1178     const ModelHighAPI_RefAttr & theLine,
1179     const ModelHighAPI_RefAttr & theCircle)
1180 {
1181   std::shared_ptr<ModelAPI_Feature> aFeature =
1182       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1183   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1184   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1185   aFeature->execute();
1186   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1187 }
1188
1189 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1190     const ModelHighAPI_RefAttr & theLine)
1191 {
1192   std::shared_ptr<ModelAPI_Feature> aFeature =
1193       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1194   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1195   aFeature->execute();
1196   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1197 }
1198
1199 //--------------------------------------------------------------------------------------
1200
1201 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1202 {
1203   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1204   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1205 }
1206
1207 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1208 {
1209   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1210       theFeature->attribute(SketchPlugin_Line::START_ID()));
1211   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1212       theFeature->attribute(SketchPlugin_Line::END_ID()));
1213
1214   if (!aStartAttr || !aEndAttr)
1215     return std::shared_ptr<GeomAPI_Pnt2d>();
1216
1217   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1218   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1219   return std::shared_ptr<GeomAPI_Pnt2d>(
1220       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1221 }
1222
1223 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1224 {
1225   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1226       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1227   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1228
1229   if (!aCenter || !aRadius)
1230     return std::shared_ptr<GeomAPI_Pnt2d>();
1231
1232   return std::shared_ptr<GeomAPI_Pnt2d>(
1233       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1234 }
1235
1236 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1237 {
1238   static const double PI = 3.141592653589793238463;
1239
1240   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1241       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1242   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1243       theFeature->attribute(SketchPlugin_Arc::START_ID()));
1244   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1245       theFeature->attribute(SketchPlugin_Arc::END_ID()));
1246
1247   if (!aCenterAttr || !aStartAttr || !aEndAttr)
1248     return std::shared_ptr<GeomAPI_Pnt2d>();
1249
1250   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1251       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1252   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1253       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1254
1255   double anAngle = aStartDir->angle(aEndDir);
1256   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1257   if (isReversed && anAngle > 0.)
1258     anAngle -= 2.0 * PI;
1259   else if (!isReversed && anAngle <= 0.)
1260     anAngle += 2.0 * PI;
1261
1262   double cosA = cos(anAngle);
1263   double sinA = sin(anAngle);
1264
1265   // rotate start dir to find middle point on arc
1266   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1267   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1268   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1269
1270   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1271 }
1272
1273 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1274                                                      bool isEllipse = true)
1275 {
1276   const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1277                                   SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1278   AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1279       theFeature->attribute(anAttrName));
1280   return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1281 }
1282
1283 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1284                                                            SketchAPI_Sketch* theSketch)
1285 {
1286   GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1287   GeomPointPtr aMiddle = anEdge.middlePoint();
1288   return theSketch->to2D(aMiddle);
1289 }
1290
1291 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1292                                                   SketchAPI_Sketch* theSketch)
1293 {
1294   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1295   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1296   if (aFeature) {
1297     // move only features of the following types
1298     const std::string& aFeatureKind = aFeature->getKind();
1299     if (aFeatureKind == SketchPlugin_Point::ID())
1300       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1301     else if (aFeatureKind == SketchPlugin_Line::ID())
1302       aMiddlePoint = middlePointOnLine(aFeature);
1303     else if (aFeatureKind == SketchPlugin_Circle::ID())
1304       aMiddlePoint = pointOnCircle(aFeature);
1305     else if (aFeatureKind == SketchPlugin_Arc::ID())
1306       aMiddlePoint = middlePointOnArc(aFeature);
1307     else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1308       aMiddlePoint = pointOnEllipse(aFeature);
1309     else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1310       aMiddlePoint = pointOnEllipse(aFeature, false);
1311     else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1312              aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1313       aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1314   }
1315   return aMiddlePoint;
1316 }
1317
1318 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1319                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1320 {
1321   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1322   theMovedEntity.fillMessage(aMessage);
1323
1324   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1325   if (aMessage->movedAttribute())
1326     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1327   else
1328     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1329
1330   if (!anOriginalPosition)
1331     return; // something has gone wrong, do not process movement
1332
1333   aMessage->setOriginalPosition(anOriginalPosition);
1334   aMessage->setCurrentPosition(theTargetPoint);
1335   Events_Loop::loop()->send(aMessage);
1336 }
1337
1338 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1339                             double theTargetX, double theTargetY)
1340 {
1341   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1342   move(theMovedEntity, aTargetPoint);
1343 }
1344
1345 //--------------------------------------------------------------------------------------
1346
1347 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1348 {
1349   FeaturePtr aBase = feature();
1350   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1351       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1352   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1353       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1354   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1355       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1356   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1357
1358   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1359 }
1360
1361 //--------------------------------------------------------------------------------------
1362
1363 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1364 {
1365   // collect edges of the first face
1366   std::list<GeomShapePtr> anEdges1;
1367   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1368     anEdges1.push_back(anExp.current());
1369   // compare edges of faces
1370   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1371     GeomShapePtr aCurrent = anExp.current();
1372     bool isFound = false;
1373     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1374     for (; anIt1 != anEdges1.end(); ++anIt1)
1375       if (aCurrent->isSameGeometry(*anIt1)) {
1376         isFound = true;
1377         anEdges1.erase(anIt1);
1378         break;
1379       }
1380     if (!isFound)
1381       return true;
1382   }
1383   return !anEdges1.empty();
1384 }
1385
1386 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1387 {
1388   ResultConstructionPtr aSketchResult =
1389       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1390   if (!aSketchResult)
1391     return false;
1392
1393   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1394       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1395   if (!aWires)
1396     return false;
1397
1398   // collect faces constructed by SketchBuilder algorithm
1399   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1400                                            aWires->norm(), aWires);
1401   const ListOfShape& aFaces = aSketchBuilder.faces();
1402
1403   // compare faces stored in sketch with faces generated by SketchBuilder
1404   int aNbSketchFaces = aSketchResult->facesNum();
1405   int aFaceIndex = 0;
1406   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1407        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1408        ++aFIt, ++aFaceIndex) {
1409     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1410     GeomFacePtr aCurFace = (*aFIt)->face();
1411     if (isDifferent(aSketchFace, aCurFace))
1412       return true;
1413   }
1414   return false;
1415 }
1416
1417 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1418                                std::list<std::list<ResultPtr> >& theEdges)
1419 {
1420   ResultConstructionPtr aSketchResult =
1421       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1422   if (!aSketchResult)
1423     return;
1424
1425   // collect curves of the sketch
1426   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1427   int aSubNum = theSketch->numberOfSubs();
1428   for (int a = 0; a < aSubNum; ++a) {
1429     FeaturePtr aSub = theSketch->subFeature(a);
1430     const std::list<ResultPtr>& aResults = aSub->results();
1431     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1432     for (; aRes != aResults.cend(); aRes++) {
1433       GeomShapePtr aCurShape = (*aRes)->shape();
1434       if (aCurShape && aCurShape->isEdge())
1435         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1436     }
1437   }
1438
1439   // convert each face to the list of results of its edges
1440   int aFacesNum = aSketchResult->facesNum();
1441   for (int a = 0; a < aFacesNum; ++a) {
1442     theEdges.push_back(std::list<ResultPtr>());
1443     std::list<ResultPtr>& aCurEdges = theEdges.back();
1444
1445     GeomFacePtr aFace = aSketchResult->face(a);
1446     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1447          anExp.more(); anExp.next()) {
1448       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1449       aCurEdges.push_back(aCurves[aCurrent]);
1450     }
1451   }
1452 }
1453
1454 //--------------------------------------------------------------------------------------
1455
1456 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1457 {
1458   FeaturePtr aBase = feature();
1459   const std::string& aDocName = theDumper.name(aBase->document());
1460
1461   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1462   if (anExternal->value()) {
1463     theDumper << aBase << " = model.addSketch(" << aDocName <<
1464       ", " << anExternal << ")" << std::endl;
1465   } else {
1466     // Sketch is base on a plane.
1467     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1468         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1469     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1470         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1471     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1472         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1473
1474     // Check the plane is coordinate plane
1475     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1476     if(anExternal->context()) { // checking for selected planes
1477       if (!aPlaneName.empty()
1478           && anExternal->context()->data()
1479           && anExternal->context()->data()->name() == aPlaneName) {
1480         // dump sketch based on coordinate plane
1481         theDumper << aBase << " = model.addSketch(" << aDocName
1482                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1483       } else { // some other plane
1484         theDumper << aBase << " = model.addSketch(" << aDocName <<
1485           ", " << anExternal<< ")" << std::endl;
1486       }
1487     } else {
1488       if (aPlaneName.empty()) {
1489         // needs import additional module
1490         theDumper.importModule("GeomAPI");
1491         // dump plane parameters
1492         const std::string& aSketchName = theDumper.name(aBase);
1493         std::string anOriginName = aSketchName + "_origin";
1494         std::string aNormalName  = aSketchName + "_norm";
1495         std::string aDirXName    = aSketchName + "_dirx";
1496         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1497         // and then dumplicate dumping it in the next line
1498         theDumper << anOriginName << " = " << anOrigin << "\n"
1499                   << aNormalName  << " = " << aNormal  << "\n"
1500                   << aDirXName    << " = " << aDirX    << "\n";
1501         // dump sketch based on arbitrary plane
1502         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1503                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1504       } else {
1505         // dump sketch based on coordinate plane
1506         theDumper << aBase << " = model.addSketch(" << aDocName
1507                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1508       }
1509     }
1510   }
1511
1512   // dump sketch's subfeatures
1513   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1514   theDumper.processSubs(aCompFeat, true);
1515
1516   // if face order differs to the order generated by SketchBuilder,
1517   // dump the list of faces for correct execution of the script
1518   if (isCustomFacesOrder(aCompFeat)) {
1519     std::list<std::list<ResultPtr> > aFaces;
1520     edgesOfSketchFaces(aCompFeat, aFaces);
1521
1522     const std::string& aSketchName = theDumper.name(aBase);
1523     std::string aMethodName(".changeFacesOrder");
1524     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1525
1526     theDumper << aSketchName << aMethodName << "([";
1527     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1528          aFIt != aFaces.end(); ++aFIt) {
1529       if (aFIt != aFaces.begin())
1530         theDumper << ",\n" << aSpaceShift << "  ";
1531       theDumper << *aFIt;
1532     }
1533     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1534     // call model.do() for correct update of the document's labels related to the changed faces
1535     theDumper << "model.do()" << std::endl;
1536   }
1537 }