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