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