Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2023  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 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(
429     const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & thePoint)
430 {
431   if (thePoint.first)
432     return thePoint.first;
433
434   AttributePtr anAttr = thePoint.second.attr();
435   if (thePoint.second.object()) {
436     FeaturePtr aFeature = ModelAPI_Feature::feature(thePoint.second.object());
437     if (aFeature)
438       anAttr = aFeature->attribute(SketchPlugin_Point::COORD_ID());
439   }
440
441   std::shared_ptr<GeomDataAPI_Point2D> aPntAttr =
442       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
443   if (aPntAttr)
444     return aPntAttr->pnt();
445   return std::shared_ptr<GeomAPI_Pnt2d>();
446 }
447
448 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangleCentered(
449     const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & theCenter,
450     const std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> & theCorner)
451 {
452   std::shared_ptr<ModelAPI_Feature> aFeature =
453     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
454   RectanglePtr aRect(new SketchAPI_Rectangle(aFeature));
455   fillAttribute("RectangleTypeCentered", aRect->type());
456   if (!theCenter.second.isEmpty())
457     fillAttribute(theCenter.second, aRect->centerPointRef());
458   fillAttribute(pointCoordinates(theCenter), aRect->centerPoint());
459   fillAttribute(pointCoordinates(theCorner), aRect->cornerPoint());
460   aRect->execute();
461
462   if (!theCorner.second.isEmpty() && aRect->linesList()->size() > 1) {
463     // get start point of the last line in rectangle and apply coindidence constraint
464     FeaturePtr aLine = ModelAPI_Feature::feature(aRect->linesList()->object(3));
465     AttributePtr aEndPnt = aLine->attribute(SketchPlugin_Line::START_ID());
466     setCoincident(ModelHighAPI_RefAttr(aEndPnt), theCorner.second);
467   }
468   return aRect;
469 }
470
471 //--------------------------------------------------------------------------------------
472 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
473                                                               double theCenterY,
474                                                               double theRadius)
475 {
476   std::shared_ptr<ModelAPI_Feature> aFeature =
477     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
478   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
479 }
480
481 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
482                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
483                                     double theRadius)
484 {
485   std::shared_ptr<ModelAPI_Feature> aFeature =
486     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
487   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
488 }
489
490 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
491                                                                    double theCenterY,
492                                                                    double thePassedX,
493                                                                    double thePassedY)
494 {
495   std::shared_ptr<ModelAPI_Feature> aFeature =
496     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
497   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
498                                                             thePassedX, thePassedY));
499 }
500
501 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
502     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
503     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
504 {
505   std::shared_ptr<ModelAPI_Feature> aFeature =
506     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
507   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
508 }
509
510 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
511                                                                    double theX2, double theY2,
512                                                                    double theX3, double theY3)
513 {
514   std::shared_ptr<ModelAPI_Feature> aFeature =
515     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
516   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
517                                                             theX2, theY2,
518                                                             theX3, theY3));
519 }
520
521 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
522     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
523     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
524     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
525 {
526   std::shared_ptr<ModelAPI_Feature> aFeature =
527     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
528   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
529 }
530
531 std::shared_ptr<SketchAPI_Circle>
532   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
533 {
534   std::shared_ptr<ModelAPI_Feature> aFeature =
535     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
536   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
537 }
538
539 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::wstring & theExternalName)
540 {
541   std::shared_ptr<ModelAPI_Feature> aFeature =
542     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
543   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
544 }
545
546 //--------------------------------------------------------------------------------------
547 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
548                                                         double theStartX, double theStartY,
549                                                         double theEndX, double theEndY,
550                                                         bool theInversed)
551 {
552   std::shared_ptr<ModelAPI_Feature> aFeature =
553     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
554   return ArcPtr(new SketchAPI_Arc(aFeature,
555                                   theCenterX, theCenterY,
556                                   theStartX, theStartY,
557                                   theEndX, theEndY,
558                                   theInversed));
559 }
560
561 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
562                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
563                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
564                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
565                                               bool theInversed)
566 {
567   std::shared_ptr<ModelAPI_Feature> aFeature =
568     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
569   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
570 }
571
572 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
573                                                         double theEndX, double theEndY,
574                                                         double thePassedX, double thePassedY)
575 {
576   std::shared_ptr<ModelAPI_Feature> aFeature =
577     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
578   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
579                                        theStartX, theStartY,
580                                        theEndX, theEndY,
581                                        thePassedX, thePassedY));
582 }
583
584 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
585                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
586                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
587                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
588 {
589   std::shared_ptr<ModelAPI_Feature> aFeature =
590     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
591   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
592 }
593
594 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
595                                                 const ModelHighAPI_RefAttr& theTangentPoint,
596                                                 double theEndX, double theEndY,
597                                                 bool theInversed,
598                                                 bool theTransversal)
599 {
600   std::shared_ptr<ModelAPI_Feature> aFeature =
601     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
602   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
603   if (theTransversal)
604     aMacroArc->setByTransversal(theTangentPoint, theEndX, theEndY, theInversed);
605   else
606     aMacroArc->setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
607   return aMacroArc;
608 }
609
610 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
611                                               const ModelHighAPI_RefAttr& theTangentPoint,
612                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
613                                               bool theInversed,
614                                               bool theTransversal)
615 {
616   std::shared_ptr<ModelAPI_Feature> aFeature =
617     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
618   MacroArcPtr aMacroArc(new SketchAPI_MacroArc(aFeature));
619   if (theTransversal)
620     aMacroArc->setByTransversal(theTangentPoint, theEnd, theInversed);
621   else
622     aMacroArc->setByTangent(theTangentPoint, theEnd, theInversed);
623   return aMacroArc;
624 }
625
626 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
627 {
628   std::shared_ptr<ModelAPI_Feature> aFeature =
629     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
630   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
631 }
632
633 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::wstring & theExternalName)
634 {
635   std::shared_ptr<ModelAPI_Feature> aFeature =
636     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
637   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
638 }
639
640 //--------------------------------------------------------------------------------------
641 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
642     double theCenterX, double theCenterY,
643     double theFocusX, double theFocusY,
644     double theMinorRadius)
645 {
646   std::shared_ptr<ModelAPI_Feature> aFeature =
647       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
648   return EllipsePtr(new SketchAPI_Ellipse(aFeature,
649       theCenterX, theCenterY, theFocusX, theFocusY, theMinorRadius));
650 }
651
652 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
653     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
654     const std::shared_ptr<GeomAPI_Pnt2d>& theFocus,
655     double theMinorRadius)
656 {
657   std::shared_ptr<ModelAPI_Feature> aFeature =
658       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
659   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theCenter, theFocus, theMinorRadius));
660 }
661
662 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
663     double thePoint1X, double thePoint1Y,
664     double thePoint2X, double thePoint2Y,
665     double thePassedX, double thePassedY,
666     bool isPoint1Center)
667 {
668   std::shared_ptr<ModelAPI_Feature> aFeature =
669       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
670   return MacroEllipsePtr(new SketchAPI_MacroEllipse(aFeature,
671       thePoint1X, thePoint1Y, thePoint2X, thePoint2Y, thePassedX, thePassedY, isPoint1Center));
672 }
673
674 std::shared_ptr<SketchAPI_MacroEllipse> SketchAPI_Sketch::addEllipse(
675     const PointOrReference& thePoint1,
676     const PointOrReference& thePoint2,
677     const PointOrReference& thePassedPoint,
678     bool isPoint1Center)
679 {
680   std::shared_ptr<ModelAPI_Feature> aFeature =
681       compositeFeature()->addFeature(SketchPlugin_MacroEllipse::ID());
682   MacroEllipsePtr anEllipse;
683   if (thePoint1.second.isEmpty() &&
684       thePoint2.second.isEmpty() &&
685       thePassedPoint.second.isEmpty()) {
686     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
687         thePoint1.first, thePoint2.first, thePassedPoint.first, isPoint1Center));
688   }
689   else {
690     anEllipse.reset(new SketchAPI_MacroEllipse(aFeature,
691         thePoint1.first, thePoint1.second,
692         thePoint2.first, thePoint2.second,
693         thePassedPoint.first, thePassedPoint.second,
694         isPoint1Center));
695   }
696   return anEllipse;
697 }
698
699 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
700     const ModelHighAPI_Selection & theExternal)
701 {
702   std::shared_ptr<ModelAPI_Feature> aFeature =
703       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
704   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternal));
705 }
706
707 std::shared_ptr<SketchAPI_Ellipse> SketchAPI_Sketch::addEllipse(
708     const std::wstring & theExternalName)
709 {
710   std::shared_ptr<ModelAPI_Feature> aFeature =
711       compositeFeature()->addFeature(SketchPlugin_Ellipse::ID());
712   return EllipsePtr(new SketchAPI_Ellipse(aFeature, theExternalName));
713 }
714
715 //--------------------------------------------------------------------------------------
716 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
717     double theCenterX, double theCenterY,
718     double theFocusX, double theFocusY,
719     double theStartX, double theStartY,
720     double theEndX, double theEndY,
721     bool theInversed)
722 {
723   std::shared_ptr<ModelAPI_Feature> aFeature =
724       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
725   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature,
726       theCenterX, theCenterY,
727       theFocusX, theFocusY,
728       theStartX, theStartY,
729       theEndX, theEndY,
730       theInversed));
731 }
732
733 std::shared_ptr<SketchAPI_MacroEllipticArc> SketchAPI_Sketch::addEllipticArc(
734     const PointOrReference& theCenter,
735     const PointOrReference& theMajorAxisPoint,
736     const PointOrReference& theStartPoint,
737     const PointOrReference& theEndPoint,
738     bool theInversed)
739 {
740   std::shared_ptr<ModelAPI_Feature> aFeature =
741       compositeFeature()->addFeature(SketchPlugin_MacroEllipticArc::ID());
742   return MacroEllipticArcPtr(new SketchAPI_MacroEllipticArc(aFeature,
743       theCenter.first, theCenter.second,
744       theMajorAxisPoint.first, theMajorAxisPoint.second,
745       theStartPoint.first, theStartPoint.second,
746       theEndPoint.first, theEndPoint.second,
747       theInversed));
748 }
749
750 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
751     const ModelHighAPI_Selection & theExternal)
752 {
753   std::shared_ptr<ModelAPI_Feature> aFeature =
754       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
755   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternal));
756 }
757
758 std::shared_ptr<SketchAPI_EllipticArc> SketchAPI_Sketch::addEllipticArc(
759     const std::wstring & theExternalName)
760 {
761   std::shared_ptr<ModelAPI_Feature> aFeature =
762       compositeFeature()->addFeature(SketchPlugin_EllipticArc::ID());
763   return EllipticArcPtr(new SketchAPI_EllipticArc(aFeature, theExternalName));
764 }
765
766 //--------------------------------------------------------------------------------------
767
768 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addSpline(
769     const ModelHighAPI_Selection & external,
770     const int degree,
771     const std::list<PointOrReference>& poles,
772     const std::list<ModelHighAPI_Double>& weights,
773     const std::list<ModelHighAPI_Double>& knots,
774     const std::list<ModelHighAPI_Integer>& multiplicities,
775     const bool periodic)
776 {
777   // split poles and references to other shapes
778   bool hasReference = false;
779   std::list<GeomPnt2dPtr> aPoints;
780   std::list<ModelHighAPI_RefAttr> aReferences;
781   for (std::list<PointOrReference>::const_iterator it = poles.begin(); it != poles.end(); ++it) {
782     aPoints.push_back(it->first);
783     aReferences.push_back(it->second);
784     if (!it->second.isEmpty())
785       hasReference = true;
786   }
787
788   BSplinePtr aBSpline;
789   CompositeFeaturePtr aSketch = compositeFeature();
790   if (hasReference) {
791     // use macro-feature to create coincidences to referred features
792     FeaturePtr aMacroFeature = aSketch->addFeature(
793         periodic ? SketchPlugin_MacroBSplinePeriodic::ID() : SketchPlugin_MacroBSpline::ID());
794     AttributePoint2DArrayPtr aPolesAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
795         aMacroFeature->attribute(SketchPlugin_MacroBSpline::POLES_ID()));
796     AttributeDoubleArrayPtr aWeightsAttr =
797         aMacroFeature->data()->realArray(SketchPlugin_MacroBSpline::WEIGHTS_ID());
798     AttributeRefAttrListPtr aPolesRefAttr =
799         aMacroFeature->data()->refattrlist(SketchPlugin_MacroBSpline::REF_POLES_ID());
800     // always generate a control polygon to apply coincidences correctly
801     aMacroFeature->boolean(SketchPlugin_MacroBSpline::CONTROL_POLYGON_ID())->setValue(true);
802     // initialize B-spline attributes
803     fillAttribute(aPoints, aPolesAttr);
804     if (weights.empty())
805       fillAttribute(std::list<ModelHighAPI_Double>(poles.size(), 1.0), aWeightsAttr);
806     else
807       fillAttribute(weights, aWeightsAttr);
808     fillAttribute(aReferences, aPolesRefAttr);
809     apply(); // to kill macro-feature
810
811     // find created B-spline feature
812     const std::string& aKindToFind =
813         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
814     int aNbSubs = aSketch->numberOfSubs();
815     for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
816       FeaturePtr aFeature = aSketch->subFeature(anIndex);
817       if (aFeature->getKind() == aKindToFind) {
818         aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
819                                 : new SketchAPI_BSpline(aFeature));
820         aBSpline->execute();
821         break;
822       }
823     }
824   }
825   else {
826     // compute B-spline by parameters
827     FeaturePtr aFeature = aSketch->addFeature(
828         periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
829
830     aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
831                             : new SketchAPI_BSpline(aFeature));
832     if (external.variantType() != ModelHighAPI_Selection::VT_Empty)
833       aBSpline->setByExternal(external);
834     else if (knots.empty() || multiplicities.empty())
835       aBSpline->setByDegreePolesAndWeights(degree, aPoints, weights);
836     else
837       aBSpline->setByParameters(degree, aPoints, weights, knots, multiplicities);
838   }
839   return aBSpline;
840 }
841
842 //--------------------------------------------------------------------------------------
843 static std::shared_ptr<SketchAPI_BSpline> buildInterpolation(
844     const CompositeFeaturePtr& theSketch,
845     const FeaturePtr& theCurveFittingFeature,
846     const std::list<ModelHighAPI_RefAttr>& points,
847     const bool periodic,
848     const bool closed)
849 {
850   AttributeBooleanPtr aPeriodicAttr =
851       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::PERIODIC_ID());
852   fillAttribute(periodic, aPeriodicAttr);
853   AttributeBooleanPtr aClosedAttr =
854       theCurveFittingFeature->boolean(SketchPlugin_CurveFitting::CLOSED_ID());
855   fillAttribute(closed, aClosedAttr);
856   AttributeRefAttrListPtr aPointsAttr =
857       theCurveFittingFeature->refattrlist(SketchPlugin_CurveFitting::POINTS_ID());
858   fillAttribute(points, aPointsAttr);
859   apply(); // to execute and kill the macro-feature
860
861   // find created B-spline feature
862   BSplinePtr aBSpline;
863   const std::string& aKindToFind =
864       periodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID();
865   int aNbSubs = theSketch->numberOfSubs();
866   for (int anIndex = aNbSubs - 1; anIndex >= 0; --anIndex) {
867     FeaturePtr aFeature = theSketch->subFeature(anIndex);
868     if (aFeature->getKind() == aKindToFind) {
869       aBSpline.reset(periodic ? new SketchAPI_BSplinePeriodic(aFeature)
870         : new SketchAPI_BSpline(aFeature));
871       aBSpline->execute();
872       break;
873     }
874   }
875   return aBSpline;
876 }
877
878 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addInterpolation(
879     const std::list<ModelHighAPI_RefAttr>& points,
880     const bool periodic,
881     const bool closed)
882 {
883   CompositeFeaturePtr aSketch = compositeFeature();
884   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
885   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
886       ->setValue(SketchPlugin_CurveFitting::TYPE_INTERPOLATION_ID());
887   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
888 }
889
890 std::shared_ptr<SketchAPI_BSpline> SketchAPI_Sketch::addApproximation(
891     const std::list<ModelHighAPI_RefAttr>& points,
892     const ModelHighAPI_Double& precision,
893     const bool periodic,
894     const bool closed)
895 {
896   CompositeFeaturePtr aSketch = compositeFeature();
897   FeaturePtr anInterpFeature = aSketch->addFeature(SketchPlugin_CurveFitting::ID());
898   anInterpFeature->string(SketchPlugin_CurveFitting::TYPE_ID())
899       ->setValue(SketchPlugin_CurveFitting::TYPE_APPROXIMATION_ID());
900   fillAttribute(precision, anInterpFeature->real(SketchPlugin_CurveFitting::PRECISION_ID()));
901   return buildInterpolation(aSketch, anInterpFeature, points, periodic, closed);
902 }
903
904 //--------------------------------------------------------------------------------------
905 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
906     const ModelHighAPI_Selection & theExternalFeature,
907     bool keepResult,
908     bool keepRefToOriginal)
909 {
910   std::shared_ptr<ModelAPI_Feature> aFeature =
911     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
912   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature));
913   aProjection->setIncludeToResult(keepResult);
914   aProjection->setKeepReferenceToOriginal(keepRefToOriginal);
915   aProjection->setExternalFeature(theExternalFeature);
916   return aProjection;
917 }
918
919 //--------------------------------------------------------------------------------------
920 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
921     const ModelHighAPI_RefAttr & theMirrorLine,
922     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
923 {
924   std::shared_ptr<ModelAPI_Feature> aFeature =
925     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
926   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
927 }
928
929 //--------------------------------------------------------------------------------------
930 std::shared_ptr<SketchAPI_Offset> SketchAPI_Sketch::addOffset(
931     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
932     const ModelHighAPI_Double & theValue,
933     const bool theReversed,
934     const std::string & theJointType)
935 {
936   std::shared_ptr<ModelAPI_Feature> aFeature =
937     compositeFeature()->addFeature(SketchPlugin_Offset::ID());
938   return OffsetPtr(new SketchAPI_Offset(aFeature, theObjects, theValue, theReversed, theJointType));
939 }
940
941 //--------------------------------------------------------------------------------------
942 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
943     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
944     const ModelHighAPI_RefAttr & thePoint1,
945     const ModelHighAPI_RefAttr & thePoint2,
946     const ModelHighAPI_Integer & theNumberOfObjects,
947     bool theFullValue)
948 {
949   std::shared_ptr<ModelAPI_Feature> aFeature =
950     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
951   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
952                                                   thePoint2, theNumberOfObjects, theFullValue));
953 }
954
955 //--------------------------------------------------------------------------------------
956 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
957     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
958     const ModelHighAPI_RefAttr & theCenter,
959     const ModelHighAPI_Double & theAngle,
960     const ModelHighAPI_Integer & theNumberOfObjects,
961     bool theFullValue,
962     bool theReversed)
963 {
964   std::shared_ptr<ModelAPI_Feature> aFeature =
965     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
966   return RotationPtr(
967     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
968                            theAngle, theNumberOfObjects, theFullValue, theReversed));
969 }
970
971 //--------------------------------------------------------------------------------------
972 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
973                                           const ModelHighAPI_Reference& theFeature,
974                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
975 {
976   std::shared_ptr<ModelAPI_Feature> aFeature =
977     compositeFeature()->addFeature(SketchPlugin_Split::ID());
978   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
979
980   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
981   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
982     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
983     fillAttribute(thePositionPoint, aPointAttr);
984   }
985
986   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
987 }
988
989 //--------------------------------------------------------------------------------------
990 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
991                                         const ModelHighAPI_Reference& theFeature,
992                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
993 {
994   std::shared_ptr<ModelAPI_Feature> aFeature =
995     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
996   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
997
998   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
999   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
1000     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
1001     fillAttribute(thePositionPoint, aPointAttr);
1002   }
1003
1004   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1005 }
1006
1007 //--------------------------------------------------------------------------------------
1008 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
1009     const ModelHighAPI_RefAttr & theLine1,
1010     const ModelHighAPI_RefAttr & theLine2,
1011     const ModelHighAPI_Double & theValue,
1012     const std::string& theType)
1013 {
1014   std::shared_ptr<ModelAPI_Feature> aFeature =
1015       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1016
1017   const int aVersion = theType.empty() ? SketchPlugin_ConstraintAngle::THE_VERSION_0
1018                                        : SketchPlugin_ConstraintAngle::THE_VERSION_1;
1019   fillAttribute(aVersion, aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1020
1021   int aType = (int)SketcherPrs_Tools::ANGLE_DIRECT;
1022   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::PREV_TYPE_ID()));
1023   fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1024
1025   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_0)
1026     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1027
1028   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1029   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1030
1031   if (aVersion == SketchPlugin_ConstraintAngle::THE_VERSION_1) {
1032     std::string aTypeLC = theType;
1033     std::transform(aTypeLC.begin(), aTypeLC.end(), aTypeLC.begin(),
1034                    [](char c) { return static_cast<char>(::tolower(c)); });
1035     if (aTypeLC == "supplementary")
1036       aType = (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
1037     else if (aTypeLC == "backward")
1038       aType = (int)SketcherPrs_Tools::ANGLE_BACKWARD;
1039
1040     fillAttribute(aType, aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1041     fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1042   }
1043
1044   aFeature->execute();
1045   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1046 }
1047
1048 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
1049     const ModelHighAPI_RefAttr & theLine1,
1050     const ModelHighAPI_RefAttr & theLine2,
1051     const ModelHighAPI_Double & theValue)
1052 {
1053   std::shared_ptr<ModelAPI_Feature> aFeature =
1054       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1055   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1056       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1057   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
1058       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1059   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1060   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1061   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1062   aFeature->execute();
1063   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1064 }
1065
1066 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
1067     const ModelHighAPI_RefAttr & theLine1,
1068     const ModelHighAPI_RefAttr & theLine2,
1069     const ModelHighAPI_Double & theValue)
1070 {
1071   std::shared_ptr<ModelAPI_Feature> aFeature =
1072       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
1073   fillAttribute(SketchPlugin_ConstraintAngle::THE_VERSION_0,
1074       aFeature->integer(SketchPlugin_ConstraintAngle::VERSION_ID()));
1075   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
1076       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
1077   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
1078   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1079   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1080   aFeature->execute();
1081   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1082 }
1083
1084 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
1085     const ModelHighAPI_RefAttr & thePoint1,
1086     const ModelHighAPI_RefAttr & thePoint2)
1087 {
1088   std::shared_ptr<ModelAPI_Feature> aFeature =
1089       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
1090   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1091   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1092   aFeature->execute();
1093   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1094 }
1095
1096 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
1097     const ModelHighAPI_RefAttr & theLine1,
1098     const ModelHighAPI_RefAttr & theLine2)
1099 {
1100   std::shared_ptr<ModelAPI_Feature> aFeature =
1101       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
1102   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1103   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1104   aFeature->execute();
1105   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1106 }
1107
1108 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
1109     const ModelHighAPI_RefAttr & thePoint,
1110     const ModelHighAPI_RefAttr & thePointOrLine,
1111     const ModelHighAPI_Double & theValue,
1112     bool isSigned)
1113 {
1114   std::shared_ptr<ModelAPI_Feature> aFeature =
1115       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
1116   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1117   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1118   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1119   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
1120   aFeature->execute();
1121   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1122 }
1123
1124 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
1125     const ModelHighAPI_RefAttr & thePoint,
1126     const ModelHighAPI_RefAttr & thePointOrLine,
1127     const ModelHighAPI_Double & theValue)
1128 {
1129   return setDistance(thePoint, thePointOrLine, theValue, true);
1130 }
1131
1132 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
1133     const ModelHighAPI_RefAttr & thePoint,
1134     const ModelHighAPI_RefAttr & thePointOrLine,
1135     const ModelHighAPI_Double & theValue)
1136 {
1137   return setDistance(thePoint, thePointOrLine, theValue, false);
1138 }
1139
1140 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
1141     const ModelHighAPI_RefAttr & thePoint1,
1142     const ModelHighAPI_RefAttr & thePoint2,
1143     const ModelHighAPI_Double & theValue)
1144 {
1145   std::shared_ptr<ModelAPI_Feature> aFeature =
1146       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
1147   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1148   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1149   fillAttribute(theValue,
1150       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1151   aFeature->execute();
1152   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1153 }
1154
1155 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
1156     const ModelHighAPI_RefAttr & thePoint1,
1157     const ModelHighAPI_RefAttr & thePoint2,
1158     const ModelHighAPI_Double & theValue)
1159 {
1160   std::shared_ptr<ModelAPI_Feature> aFeature =
1161       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
1162   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1163   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1164   fillAttribute(theValue,
1165       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
1166   aFeature->execute();
1167   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1168 }
1169
1170 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
1171     const ModelHighAPI_RefAttr & theObject1,
1172     const ModelHighAPI_RefAttr & theObject2)
1173 {
1174   std::shared_ptr<ModelAPI_Feature> aFeature =
1175       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
1176   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1177   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1178   aFeature->execute();
1179   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1180 }
1181
1182 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
1183     const ModelHighAPI_RefAttr & thePoint)
1184 {
1185   std::shared_ptr<ModelAPI_Feature> aFeature =
1186       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
1187   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
1188   apply(); // finish operation to remove Fillet feature correcly
1189   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1190 }
1191
1192 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
1193     const ModelHighAPI_RefAttr & thePoint,
1194     const ModelHighAPI_Double & theRadius)
1195 {
1196   CompositeFeaturePtr aSketch = compositeFeature();
1197   int aNbSubs = aSketch->numberOfSubs();
1198
1199   // create fillet
1200   InterfacePtr aFilletFeature = setFillet(thePoint);
1201
1202   // set radius for just created arc
1203   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
1204   if (anArc->getKind() == SketchPlugin_Arc::ID())
1205     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
1206
1207   return aFilletFeature;
1208 }
1209
1210 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
1211     const ModelHighAPI_RefAttr & theObject)
1212 {
1213   std::shared_ptr<ModelAPI_Feature> aFeature =
1214       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
1215   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1216   aFeature->execute();
1217   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1218 }
1219
1220 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
1221     const ModelHighAPI_RefAttr & theLine)
1222 {
1223   std::shared_ptr<ModelAPI_Feature> aFeature =
1224       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
1225   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1226   aFeature->execute();
1227   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1228 }
1229
1230 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
1231     const ModelHighAPI_RefAttr & theLine,
1232     const ModelHighAPI_Double & theValue)
1233 {
1234   std::shared_ptr<ModelAPI_Feature> aFeature =
1235       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
1236   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1237   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1238   aFeature->execute();
1239   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1240 }
1241
1242 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
1243     const ModelHighAPI_RefAttr & thePoint,
1244     const ModelHighAPI_RefAttr & theLine)
1245 {
1246   std::shared_ptr<ModelAPI_Feature> aFeature =
1247       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
1248   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1249   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1250   aFeature->execute();
1251   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1252 }
1253
1254 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
1255     const ModelHighAPI_RefAttr & theLine1,
1256     const ModelHighAPI_RefAttr & theLine2)
1257 {
1258   std::shared_ptr<ModelAPI_Feature> aFeature =
1259       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
1260   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1261   fillAttribute(theLine2, 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::setPerpendicular(
1267     const ModelHighAPI_RefAttr & theLine1,
1268     const ModelHighAPI_RefAttr & theLine2)
1269 {
1270   std::shared_ptr<ModelAPI_Feature> aFeature =
1271       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
1272   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1273   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1274   aFeature->execute();
1275   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1276 }
1277
1278 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
1279     const ModelHighAPI_RefAttr & theCircleOrArc,
1280     const ModelHighAPI_Double & theValue)
1281 {
1282   std::shared_ptr<ModelAPI_Feature> aFeature =
1283       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
1284   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1285   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
1286   aFeature->execute();
1287   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1288 }
1289
1290 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
1291     const ModelHighAPI_RefAttr & theLine,
1292     const ModelHighAPI_RefAttr & theCircle)
1293 {
1294   std::shared_ptr<ModelAPI_Feature> aFeature =
1295       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
1296   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1297   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
1298   aFeature->execute();
1299   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1300 }
1301
1302 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
1303     const ModelHighAPI_RefAttr & theLine)
1304 {
1305   std::shared_ptr<ModelAPI_Feature> aFeature =
1306       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
1307   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
1308   aFeature->execute();
1309   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
1310 }
1311
1312 //--------------------------------------------------------------------------------------
1313
1314 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
1315 {
1316   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
1317   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1318 }
1319
1320 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
1321 {
1322   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1323       theFeature->attribute(SketchPlugin_Line::START_ID()));
1324   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1325       theFeature->attribute(SketchPlugin_Line::END_ID()));
1326
1327   if (!aStartAttr || !aEndAttr)
1328     return std::shared_ptr<GeomAPI_Pnt2d>();
1329
1330   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
1331   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
1332   return std::shared_ptr<GeomAPI_Pnt2d>(
1333       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
1334 }
1335
1336 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
1337 {
1338   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1339       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
1340   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
1341
1342   if (!aCenter || !aRadius)
1343     return std::shared_ptr<GeomAPI_Pnt2d>();
1344
1345   return std::shared_ptr<GeomAPI_Pnt2d>(
1346       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
1347 }
1348
1349 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
1350 {
1351   static const double PI = 3.141592653589793238463;
1352
1353   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1354       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1355   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1356       theFeature->attribute(SketchPlugin_Arc::START_ID()));
1357   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1358       theFeature->attribute(SketchPlugin_Arc::END_ID()));
1359
1360   if (!aCenterAttr || !aStartAttr || !aEndAttr)
1361     return std::shared_ptr<GeomAPI_Pnt2d>();
1362
1363   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
1364       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
1365   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
1366       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
1367
1368   double anAngle = aStartDir->angle(aEndDir);
1369   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1370   if (isReversed && anAngle > 0.)
1371     anAngle -= 2.0 * PI;
1372   else if (!isReversed && anAngle <= 0.)
1373     anAngle += 2.0 * PI;
1374
1375   double cosA = cos(anAngle);
1376   double sinA = sin(anAngle);
1377
1378   // rotate start dir to find middle point on arc
1379   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1380   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1381   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1382
1383   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1384 }
1385
1386 static std::shared_ptr<GeomAPI_Pnt2d> pointOnEllipse(const FeaturePtr& theFeature,
1387                                                      bool isEllipse = true)
1388 {
1389   const std::string& anAttrName = isEllipse ? SketchPlugin_Ellipse::MAJOR_AXIS_END_ID() :
1390                                   SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
1391   AttributePoint2DPtr aMajorAxisEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1392       theFeature->attribute(anAttrName));
1393   return aMajorAxisEnd ? aMajorAxisEnd->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
1394 }
1395
1396 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnBSpline(const FeaturePtr& theFeature,
1397                                                            SketchAPI_Sketch* theSketch)
1398 {
1399   GeomAPI_Edge anEdge(theFeature->lastResult()->shape());
1400   GeomPointPtr aMiddle = anEdge.middlePoint();
1401   return theSketch->to2D(aMiddle);
1402 }
1403
1404 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject,
1405                                                   SketchAPI_Sketch* theSketch)
1406 {
1407   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1408   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1409   if (aFeature) {
1410     // move only features of the following types
1411     const std::string& aFeatureKind = aFeature->getKind();
1412     if (aFeatureKind == SketchPlugin_Point::ID())
1413       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1414     else if (aFeatureKind == SketchPlugin_Line::ID())
1415       aMiddlePoint = middlePointOnLine(aFeature);
1416     else if (aFeatureKind == SketchPlugin_Circle::ID())
1417       aMiddlePoint = pointOnCircle(aFeature);
1418     else if (aFeatureKind == SketchPlugin_Arc::ID())
1419       aMiddlePoint = middlePointOnArc(aFeature);
1420     else if (aFeatureKind == SketchPlugin_Ellipse::ID())
1421       aMiddlePoint = pointOnEllipse(aFeature);
1422     else if (aFeatureKind == SketchPlugin_EllipticArc::ID())
1423       aMiddlePoint = pointOnEllipse(aFeature, false);
1424     else if (aFeatureKind == SketchPlugin_BSpline::ID() ||
1425              aFeatureKind == SketchPlugin_BSplinePeriodic::ID())
1426       aMiddlePoint = middlePointOnBSpline(aFeature, theSketch);
1427   }
1428   return aMiddlePoint;
1429 }
1430
1431 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1432                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1433 {
1434   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1435   theMovedEntity.fillMessage(aMessage);
1436
1437   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1438   if (aMessage->movedAttribute())
1439     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1440   else
1441     anOriginalPosition = middlePoint(aMessage->movedObject(), this);
1442
1443   if (!anOriginalPosition)
1444     return; // something has gone wrong, do not process movement
1445
1446   aMessage->setOriginalPosition(anOriginalPosition);
1447   aMessage->setCurrentPosition(theTargetPoint);
1448   Events_Loop::loop()->send(aMessage);
1449 }
1450
1451 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1452                             double theTargetX, double theTargetY)
1453 {
1454   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1455   move(theMovedEntity, aTargetPoint);
1456 }
1457
1458 //--------------------------------------------------------------------------------------
1459
1460 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1461 {
1462   FeaturePtr aBase = feature();
1463   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1464       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1465   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1466       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1467   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1468       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1469   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1470
1471   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1472 }
1473
1474 //--------------------------------------------------------------------------------------
1475
1476 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1477 {
1478   // collect edges of the first face
1479   std::list<GeomShapePtr> anEdges1;
1480   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1481     anEdges1.push_back(anExp.current());
1482   // compare edges of faces
1483   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1484     GeomShapePtr aCurrent = anExp.current();
1485     bool isFound = false;
1486     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1487     for (; anIt1 != anEdges1.end(); ++anIt1)
1488       if (aCurrent->isSameGeometry(*anIt1)) {
1489         isFound = true;
1490         anEdges1.erase(anIt1);
1491         break;
1492       }
1493     if (!isFound)
1494       return true;
1495   }
1496   return !anEdges1.empty();
1497 }
1498
1499 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1500 {
1501   ResultConstructionPtr aSketchResult =
1502       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1503   if (!aSketchResult)
1504     return false;
1505
1506   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1507       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1508   if (!aWires)
1509     return false;
1510
1511   // collect faces constructed by SketchBuilder algorithm
1512   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1513                                            aWires->norm(), aWires);
1514   const ListOfShape& aFaces = aSketchBuilder.faces();
1515
1516   // compare faces stored in sketch with faces generated by SketchBuilder
1517   int aNbSketchFaces = aSketchResult->facesNum();
1518   int aFaceIndex = 0;
1519   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1520        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1521        ++aFIt, ++aFaceIndex) {
1522     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1523     GeomFacePtr aCurFace = (*aFIt)->face();
1524     if (isDifferent(aSketchFace, aCurFace))
1525       return true;
1526   }
1527   return false;
1528 }
1529
1530 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1531                                std::list<std::list<ResultPtr> >& theEdges)
1532 {
1533   ResultConstructionPtr aSketchResult =
1534       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1535   if (!aSketchResult)
1536     return;
1537
1538   // collect curves of the sketch
1539   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1540   int aSubNum = theSketch->numberOfSubs();
1541   for (int a = 0; a < aSubNum; ++a) {
1542     FeaturePtr aSub = theSketch->subFeature(a);
1543     const std::list<ResultPtr>& aResults = aSub->results();
1544     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1545     for (; aRes != aResults.cend(); aRes++) {
1546       GeomShapePtr aCurShape = (*aRes)->shape();
1547       if (aCurShape && aCurShape->isEdge())
1548         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1549     }
1550   }
1551
1552   // convert each face to the list of results of its edges
1553   int aFacesNum = aSketchResult->facesNum();
1554   for (int a = 0; a < aFacesNum; ++a) {
1555     theEdges.push_back(std::list<ResultPtr>());
1556     std::list<ResultPtr>& aCurEdges = theEdges.back();
1557
1558     GeomFacePtr aFace = aSketchResult->face(a);
1559     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1560          anExp.more(); anExp.next()) {
1561       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1562       aCurEdges.push_back(aCurves[aCurrent]);
1563     }
1564   }
1565 }
1566
1567 //--------------------------------------------------------------------------------------
1568
1569 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1570 {
1571   FeaturePtr aBase = feature();
1572   const std::string& aDocName = theDumper.name(aBase->document());
1573
1574   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1575   if (anExternal->value()) {
1576     theDumper << aBase << " = model.addSketch(" << aDocName <<
1577       ", " << anExternal << ")" << std::endl;
1578   } else {
1579     // Sketch is base on a plane.
1580     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1581         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1582     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1583         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1584     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1585         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1586
1587     // Check the plane is coordinate plane
1588     std::wstring aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1589     if(anExternal->context()) { // checking for selected planes
1590       if (!aPlaneName.empty()
1591           && anExternal->context()->data()
1592           && anExternal->context()->data()->name() == aPlaneName) {
1593         // dump sketch based on coordinate plane
1594         theDumper << aBase << " = model.addSketch(" << aDocName
1595                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1596       } else { // some other plane
1597         theDumper << aBase << " = model.addSketch(" << aDocName <<
1598           ", " << anExternal<< ")" << std::endl;
1599       }
1600     } else {
1601       if (aPlaneName.empty()) {
1602         // needs import additional module
1603         theDumper.importModule("GeomAPI");
1604         // dump plane parameters
1605         const std::string& aSketchName = theDumper.name(aBase);
1606         std::string anOriginName = aSketchName + "_origin";
1607         std::string aNormalName  = aSketchName + "_norm";
1608         std::string aDirXName    = aSketchName + "_dirx";
1609         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1610         // and then dumplicate dumping it in the next line
1611         theDumper << anOriginName << " = " << anOrigin << "\n"
1612                   << aNormalName  << " = " << aNormal  << "\n"
1613                   << aDirXName    << " = " << aDirX    << "\n";
1614         // dump sketch based on arbitrary plane
1615         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1616                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1617       } else {
1618         // dump sketch based on coordinate plane
1619         theDumper << aBase << " = model.addSketch(" << aDocName
1620                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1621       }
1622     }
1623   }
1624
1625   // dump sketch's subfeatures
1626   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1627   theDumper.processSubs(aCompFeat, true);
1628
1629   // if face order differs to the order generated by SketchBuilder,
1630   // dump the list of faces for correct execution of the script
1631   if (isCustomFacesOrder(aCompFeat)) {
1632     std::list<std::list<ResultPtr> > aFaces;
1633     edgesOfSketchFaces(aCompFeat, aFaces);
1634
1635     const std::string& aSketchName = theDumper.name(aBase);
1636     std::string aMethodName(".changeFacesOrder");
1637     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1638
1639     theDumper << aSketchName << aMethodName << "([";
1640     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1641          aFIt != aFaces.end(); ++aFIt) {
1642       if (aFIt != aFaces.begin())
1643         theDumper << ",\n" << aSpaceShift << "  ";
1644       theDumper << *aFIt;
1645     }
1646     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1647     // call model.do() for correct update of the document's labels related to the changed faces
1648     theDumper << "model.do()" << std::endl;
1649   }
1650 }