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