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