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