Salome HOME
Issue #2024: Redesign of circle and arc of circle
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2 // Name   : SketchAPI_Sketch.cpp
3 // Purpose:
4 //
5 // History:
6 // 07/06/16 - Sergey POKHODENKO - Creation of the file
7
8 //--------------------------------------------------------------------------------------
9 #include "SketchAPI_Sketch.h"
10 //--------------------------------------------------------------------------------------
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_ConstraintAngle.h>
13 #include <SketchPlugin_ConstraintCoincidence.h>
14 #include <SketchPlugin_ConstraintCollinear.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintEqual.h>
17 #include <SketchPlugin_Fillet.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintMirror.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintSplit.h>
27 #include <SketchPlugin_Trim.h>
28 #include <SketchPlugin_ConstraintTangent.h>
29 #include <SketchPlugin_ConstraintVertical.h>
30 #include <SketcherPrs_Tools.h>
31 //--------------------------------------------------------------------------------------
32 #include <ModelAPI_CompositeFeature.h>
33 #include <ModelAPI_ResultConstruction.h>
34 #include <ModelHighAPI_Double.h>
35 #include <ModelHighAPI_Dumper.h>
36 #include <ModelHighAPI_RefAttr.h>
37 #include <ModelHighAPI_Selection.h>
38 #include <ModelHighAPI_Services.h>
39 #include <ModelHighAPI_Tools.h>
40 //--------------------------------------------------------------------------------------
41 #include "SketchAPI_Arc.h"
42 #include "SketchAPI_Circle.h"
43 #include "SketchAPI_IntersectionPoint.h"
44 #include "SketchAPI_Line.h"
45 #include "SketchAPI_MacroCircle.h"
46 #include "SketchAPI_Mirror.h"
47 #include "SketchAPI_Point.h"
48 #include "SketchAPI_Projection.h"
49 #include "SketchAPI_Rectangle.h"
50 #include "SketchAPI_Rotation.h"
51 #include "SketchAPI_Translation.h"
52 //--------------------------------------------------------------------------------------
53 SketchAPI_Sketch::SketchAPI_Sketch(
54     const std::shared_ptr<ModelAPI_Feature> & theFeature)
55 : ModelHighAPI_Interface(theFeature)
56 {
57   initialize();
58 }
59
60 SketchAPI_Sketch::SketchAPI_Sketch(
61     const std::shared_ptr<ModelAPI_Feature> & theFeature,
62     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
63 : ModelHighAPI_Interface(theFeature)
64 {
65   if (initialize()) {
66     setPlane(thePlane);
67   }
68 }
69
70 SketchAPI_Sketch::SketchAPI_Sketch(
71     const std::shared_ptr<ModelAPI_Feature> & theFeature,
72     const ModelHighAPI_Selection & theExternal)
73 : ModelHighAPI_Interface(theFeature)
74 {
75   if (initialize()) {
76     setExternal(theExternal);
77   }
78 }
79
80 SketchAPI_Sketch::SketchAPI_Sketch(
81     const std::shared_ptr<ModelAPI_Feature> & theFeature,
82     std::shared_ptr<ModelAPI_Object> thePlaneObject)
83 : ModelHighAPI_Interface(theFeature)
84 {
85   if (initialize()) {
86     setExternal(thePlaneObject);
87   }
88 }
89
90 SketchAPI_Sketch::~SketchAPI_Sketch()
91 {
92
93 }
94
95 //--------------------------------------------------------------------------------------
96 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
97 {
98   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
99 }
100
101 //--------------------------------------------------------------------------------------
102 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
103 {
104   fillAttribute(thePlane->origin(), myorigin);
105   fillAttribute(thePlane->dirX(), mydirX);
106   fillAttribute(thePlane->normal(), mynormal);
107
108   execute();
109 }
110
111 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
112 {
113   fillAttribute(theExternal, myexternal);
114
115   execute();
116 }
117
118 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
119 {
120   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
121   ModelHighAPI_Selection aSel(aRes);
122   setExternal(aSel);
123 }
124
125 //--------------------------------------------------------------------------------------
126 void SketchAPI_Sketch::setValue(
127     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
128     const ModelHighAPI_Double & theValue)
129 {
130   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
131
132 //  theConstraint->execute();
133 }
134
135 //--------------------------------------------------------------------------------------
136 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
137 {
138   const_cast<SketchAPI_Sketch*>(this)->execute();
139
140   std::list<ModelHighAPI_Selection> aSelectionList;
141
142   ResultConstructionPtr aResultConstruction =
143       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
144   if (aResultConstruction.get() == NULL)
145     return aSelectionList;
146
147   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
148     aSelectionList.push_back(
149         ModelHighAPI_Selection(aResultConstruction,
150                                aResultConstruction->face(anIndex)));
151   }
152
153   return aSelectionList;
154 }
155
156 //--------------------------------------------------------------------------------------
157 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
158                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
159 {
160   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
161   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
162 }
163
164 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
165                     const ModelHighAPI_Selection & theExternal)
166 {
167   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
168   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
169 }
170
171 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
172                     const std::string & theExternalName)
173 {
174   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
175   return SketchPtr(
176     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
177 }
178
179 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
180                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
181 {
182   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
183   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
184 }
185
186
187 //--------------------------------------------------------------------------------------
188 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
189     double theX, double theY)
190 {
191   std::shared_ptr<ModelAPI_Feature> aFeature =
192     compositeFeature()->addFeature(SketchPlugin_Point::ID());
193   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
194 }
195 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
196     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
197 {
198   std::shared_ptr<ModelAPI_Feature> aFeature =
199     compositeFeature()->addFeature(SketchPlugin_Point::ID());
200   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
201 }
202 std::shared_ptr<SketchAPI_Point>
203   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
204 {
205   std::shared_ptr<ModelAPI_Feature> aFeature =
206     compositeFeature()->addFeature(SketchPlugin_Point::ID());
207   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
208 }
209 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
210 {
211   std::shared_ptr<ModelAPI_Feature> aFeature =
212     compositeFeature()->addFeature(SketchPlugin_Point::ID());
213   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
214 }
215
216 //--------------------------------------------------------------------------------------
217 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
218     const ModelHighAPI_Selection & theExternal)
219 {
220   std::shared_ptr<ModelAPI_Feature> aFeature =
221     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
222   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
223 }
224 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
225     const std::string & theExternalName)
226 {
227   std::shared_ptr<ModelAPI_Feature> aFeature =
228     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
229   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
230 }
231
232 //--------------------------------------------------------------------------------------
233 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
234                                                           double theX2, double theY2)
235 {
236   std::shared_ptr<ModelAPI_Feature> aFeature =
237     compositeFeature()->addFeature(SketchPlugin_Line::ID());
238   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
239 }
240 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
241     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
242     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
243 {
244   std::shared_ptr<ModelAPI_Feature> aFeature =
245     compositeFeature()->addFeature(SketchPlugin_Line::ID());
246   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
247 }
248 std::shared_ptr<SketchAPI_Line>
249   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
250 {
251   std::shared_ptr<ModelAPI_Feature> aFeature =
252     compositeFeature()->addFeature(SketchPlugin_Line::ID());
253   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
254 }
255 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
256 {
257   std::shared_ptr<ModelAPI_Feature> aFeature =
258     compositeFeature()->addFeature(SketchPlugin_Line::ID());
259   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
260 }
261
262 //--------------------------------------------------------------------------------------
263 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
264                                                                     double theX2, double theY2)
265 {
266   std::shared_ptr<ModelAPI_Feature> aFeature =
267     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
268   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
269 }
270 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
271     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
272     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
273 {
274   std::shared_ptr<ModelAPI_Feature> aFeature =
275     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
276   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
277 }
278
279 //--------------------------------------------------------------------------------------
280 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
281                                                               double theCenterY,
282                                                               double theRadius)
283 {
284   std::shared_ptr<ModelAPI_Feature> aFeature =
285     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
286   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
287 }
288
289 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
290                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
291                                     double theRadius)
292 {
293   std::shared_ptr<ModelAPI_Feature> aFeature =
294     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
295   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
296 }
297
298 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
299                                                                    double theCenterY,
300                                                                    double thePassedX,
301                                                                    double thePassedY)
302 {
303   std::shared_ptr<ModelAPI_Feature> aFeature =
304     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
305   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
306                                                             thePassedX, thePassedY));
307 }
308
309 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
310     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
311     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
312 {
313   std::shared_ptr<ModelAPI_Feature> aFeature =
314     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
315   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
316 }
317
318 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
319                                                                    double theX2, double theY2,
320                                                                    double theX3, double theY3)
321 {
322   std::shared_ptr<ModelAPI_Feature> aFeature =
323     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
324   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
325                                                             theX2, theY2,
326                                                             theX3, theY3));
327 }
328
329 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
330     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
331     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
332     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
333 {
334   std::shared_ptr<ModelAPI_Feature> aFeature =
335     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
336   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
337 }
338
339 std::shared_ptr<SketchAPI_Circle>
340   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
341 {
342   std::shared_ptr<ModelAPI_Feature> aFeature =
343     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
344   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
345 }
346
347 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
348 {
349   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
350   std::shared_ptr<ModelAPI_Feature> aFeature =
351     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
352   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
353 }
354
355 //--------------------------------------------------------------------------------------
356 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
357                                                         double theStartX, double theStartY,
358                                                         double theEndX, double theEndY,
359                                                         bool theInversed)
360 {
361   std::shared_ptr<ModelAPI_Feature> aFeature =
362     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
363   return ArcPtr(new SketchAPI_Arc(aFeature,
364                                   theCenterX, theCenterY,
365                                   theStartX, theStartY,
366                                   theEndX, theEndY,
367                                   theInversed));
368 }
369
370 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
371                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
372                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
373                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
374                                               bool theInversed)
375 {
376   std::shared_ptr<ModelAPI_Feature> aFeature =
377     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
378   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
379 }
380
381 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
382                                                         double theEndX, double theEndY,
383                                                         double thePassedX, double thePassedY)
384 {
385   std::shared_ptr<ModelAPI_Feature> aFeature =
386     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
387   return ArcPtr(new SketchAPI_Arc(aFeature,
388                                   theStartX, theStartY,
389                                   theEndX, theEndY,
390                                   thePassedX, thePassedY));
391 }
392
393 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
394                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
395                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
396                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
397 {
398   std::shared_ptr<ModelAPI_Feature> aFeature =
399     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
400   return ArcPtr(new SketchAPI_Arc(aFeature, theStart, theEnd, thePassed));
401 }
402
403 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
404                                                 const ModelHighAPI_RefAttr& theTangentPoint,
405                                                 double theEndX, double theEndY,
406                                                 bool theInversed)
407 {
408   std::shared_ptr<ModelAPI_Feature> aFeature =
409     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
410   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEndX, theEndY, theInversed));
411 }
412
413 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
414                                               const ModelHighAPI_RefAttr& theTangentPoint,
415                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
416                                               bool theInversed)
417 {
418   std::shared_ptr<ModelAPI_Feature> aFeature =
419     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
420   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEnd, theInversed));
421 }
422
423 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
424 {
425   std::shared_ptr<ModelAPI_Feature> aFeature =
426     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
427   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
428 }
429
430 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
431 {
432   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
433   std::shared_ptr<ModelAPI_Feature> aFeature =
434     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
435   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
436 }
437
438 //--------------------------------------------------------------------------------------
439 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
440     const ModelHighAPI_Selection & theExternalFeature)
441 {
442   std::shared_ptr<ModelAPI_Feature> aFeature =
443     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
444   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
445 }
446
447 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
448     const std::string & theExternalName)
449 {
450   std::shared_ptr<ModelAPI_Feature> aFeature =
451     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
452   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalName));
453 }
454
455 //--------------------------------------------------------------------------------------
456 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
457     const ModelHighAPI_RefAttr & theMirrorLine,
458     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
459 {
460   std::shared_ptr<ModelAPI_Feature> aFeature =
461     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
462   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
463 }
464
465 //--------------------------------------------------------------------------------------
466 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
467     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
468     const ModelHighAPI_RefAttr & thePoint1,
469     const ModelHighAPI_RefAttr & thePoint2,
470     const ModelHighAPI_Integer & theNumberOfObjects,
471     bool theFullValue)
472 {
473   std::shared_ptr<ModelAPI_Feature> aFeature =
474     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
475   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
476                                                   thePoint2, theNumberOfObjects, theFullValue));
477 }
478
479 //--------------------------------------------------------------------------------------
480 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
481     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
482     const ModelHighAPI_RefAttr & theCenter,
483     const ModelHighAPI_Double & theAngle,
484     const ModelHighAPI_Integer & theNumberOfObjects,
485     bool theFullValue)
486 {
487   std::shared_ptr<ModelAPI_Feature> aFeature =
488     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
489   return RotationPtr(
490     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
491                            theAngle, theNumberOfObjects, theFullValue));
492 }
493
494 //--------------------------------------------------------------------------------------
495 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
496                                                     const ModelHighAPI_Reference& theFeature,
497                                                     const ModelHighAPI_RefAttr& thePoint1,
498                                                     const ModelHighAPI_RefAttr& thePoint2)
499 {
500   std::shared_ptr<ModelAPI_Feature> aFeature =
501     compositeFeature()->addFeature(SketchPlugin_ConstraintSplit::ID());
502   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Constraint::VALUE()));
503   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
504   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
505   //aFeature->execute();
506   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
507 }
508
509 //--------------------------------------------------------------------------------------
510 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
511                                         const ModelHighAPI_Reference& theFeature,
512                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
513 {
514   std::shared_ptr<ModelAPI_Feature> aFeature =
515     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
516   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
517
518   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
519   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
520     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
521     fillAttribute(thePositionPoint, aPointAttr);
522   }
523
524   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
525 }
526
527 //--------------------------------------------------------------------------------------
528 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
529     const ModelHighAPI_RefAttr & theLine1,
530     const ModelHighAPI_RefAttr & theLine2,
531     const ModelHighAPI_Double & theValue)
532 {
533   std::shared_ptr<ModelAPI_Feature> aFeature =
534       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
535   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
536       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
537   // fill the value before llines to avoid calculation of angle value by the Angle feature
538   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
539   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
540   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
541   aFeature->execute();
542   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
543 }
544
545 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
546     const ModelHighAPI_RefAttr & theLine1,
547     const ModelHighAPI_RefAttr & theLine2,
548     const ModelHighAPI_Double & theValue)
549 {
550   std::shared_ptr<ModelAPI_Feature> aFeature =
551       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
552   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
553       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
554   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
555   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
556   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
557 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
558   aFeature->execute();
559   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
560 }
561
562 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
563     const ModelHighAPI_RefAttr & theLine1,
564     const ModelHighAPI_RefAttr & theLine2,
565     const ModelHighAPI_Double & theValue)
566 {
567   std::shared_ptr<ModelAPI_Feature> aFeature =
568       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
569   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
570       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
571   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
572   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
573   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
574 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
575   aFeature->execute();
576   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
577 }
578
579 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
580     const ModelHighAPI_RefAttr & thePoint1,
581     const ModelHighAPI_RefAttr & thePoint2)
582 {
583   std::shared_ptr<ModelAPI_Feature> aFeature =
584       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
585   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
586   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
587   aFeature->execute();
588   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
589 }
590
591 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
592     const ModelHighAPI_RefAttr & theLine1,
593     const ModelHighAPI_RefAttr & theLine2)
594 {
595   std::shared_ptr<ModelAPI_Feature> aFeature =
596       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
597   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
598   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
599   aFeature->execute();
600   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
601 }
602
603 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
604     const ModelHighAPI_RefAttr & thePoint,
605     const ModelHighAPI_RefAttr & thePointOrLine,
606     const ModelHighAPI_Double & theValue)
607 {
608   std::shared_ptr<ModelAPI_Feature> aFeature =
609       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
610   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
611   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
612   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
613   aFeature->execute();
614   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
615 }
616
617 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
618     const ModelHighAPI_RefAttr & theObject1,
619     const ModelHighAPI_RefAttr & theObject2)
620 {
621   std::shared_ptr<ModelAPI_Feature> aFeature =
622       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
623   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
624   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
625   aFeature->execute();
626   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
627 }
628
629 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
630     const ModelHighAPI_RefAttr & thePoint)
631 {
632   std::shared_ptr<ModelAPI_Feature> aFeature =
633       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
634   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
635   apply(); // finish operation to remove Fillet feature correcly
636   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
637 }
638
639 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
640     const ModelHighAPI_RefAttr & thePoint,
641     const ModelHighAPI_Double & theRadius)
642 {
643   CompositeFeaturePtr aSketch = compositeFeature();
644   int aNbSubs = aSketch->numberOfSubs();
645
646   // create fillet
647   InterfacePtr aFilletFeature = setFillet(thePoint);
648
649   // set radius for just created arc
650   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
651   if (anArc->getKind() == SketchPlugin_Arc::ID())
652     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
653
654   return aFilletFeature;
655 }
656
657 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
658     const ModelHighAPI_RefAttr & theObject)
659 {
660   std::shared_ptr<ModelAPI_Feature> aFeature =
661       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
662   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
663   aFeature->execute();
664   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
665 }
666
667 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
668     const ModelHighAPI_RefAttr & theLine)
669 {
670   std::shared_ptr<ModelAPI_Feature> aFeature =
671       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
672   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
673   aFeature->execute();
674   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
675 }
676
677 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
678     const ModelHighAPI_RefAttr & theLine,
679     const ModelHighAPI_Double & theValue)
680 {
681   std::shared_ptr<ModelAPI_Feature> aFeature =
682       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
683   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
684   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
685   aFeature->execute();
686   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
687 }
688
689 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
690     const ModelHighAPI_RefAttr & thePoint,
691     const ModelHighAPI_RefAttr & theLine)
692 {
693   std::shared_ptr<ModelAPI_Feature> aFeature =
694       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
695   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
696   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
697   aFeature->execute();
698   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
699 }
700
701 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
702     const ModelHighAPI_RefAttr & theLine1,
703     const ModelHighAPI_RefAttr & theLine2)
704 {
705   std::shared_ptr<ModelAPI_Feature> aFeature =
706       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
707   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
708   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
709   aFeature->execute();
710   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
711 }
712
713 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
714     const ModelHighAPI_RefAttr & theLine1,
715     const ModelHighAPI_RefAttr & theLine2)
716 {
717   std::shared_ptr<ModelAPI_Feature> aFeature =
718       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
719   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
720   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
721   aFeature->execute();
722   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
723 }
724
725 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
726     const ModelHighAPI_RefAttr & theCircleOrArc,
727     const ModelHighAPI_Double & theValue)
728 {
729   std::shared_ptr<ModelAPI_Feature> aFeature =
730       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
731   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
732   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
733   aFeature->execute();
734   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
735 }
736
737 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
738     const ModelHighAPI_RefAttr & theLine,
739     const ModelHighAPI_RefAttr & theCircle)
740 {
741   std::shared_ptr<ModelAPI_Feature> aFeature =
742       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
743   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
744   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
745   aFeature->execute();
746   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
747 }
748
749 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
750     const ModelHighAPI_RefAttr & theLine)
751 {
752   std::shared_ptr<ModelAPI_Feature> aFeature =
753       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
754   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
755   aFeature->execute();
756   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
757 }
758
759 //--------------------------------------------------------------------------------------
760
761 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
762 {
763   FeaturePtr aBase = feature();
764   const std::string& aDocName = theDumper.name(aBase->document());
765
766   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
767   if (anExternal->value()) {
768     theDumper << aBase << " = model.addSketch(" << aDocName <<
769       ", " << anExternal << ")" << std::endl;
770   } else {
771     // Sketch is base on a plane.
772     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
773         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
774     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
775         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
776     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
777         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
778
779     // Check the plane is coordinate plane
780     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
781     if(anExternal->context()) { // checking for selected planes
782       if (!aPlaneName.empty()
783           && anExternal->context()->data()
784           && anExternal->context()->data()->name() == aPlaneName) {
785         // dump sketch based on coordinate plane
786         theDumper << aBase << " = model.addSketch(" << aDocName
787                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
788       } else { // some other plane
789         theDumper << aBase << " = model.addSketch(" << aDocName <<
790           ", " << anExternal<< ")" << std::endl;
791       }
792     } else {
793       if (aPlaneName.empty()) {
794         // needs import additional module
795         theDumper.importModule("GeomAPI");
796         // dump plane parameters
797         const std::string& aSketchName = theDumper.name(aBase);
798         std::string anOriginName = aSketchName + "_origin";
799         std::string aNormalName  = aSketchName + "_norm";
800         std::string aDirXName    = aSketchName + "_dirx";
801         // use "\n" instead of std::endl to avoid automatic dumping sketch here
802         // and then dumplicate dumping it in the next line
803         theDumper << anOriginName << " = " << anOrigin << "\n"
804                   << aNormalName  << " = " << aNormal  << "\n"
805                   << aDirXName    << " = " << aDirX    << "\n";
806         // dump sketch based on arbitrary plane
807         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
808                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
809       } else {
810         // dump sketch based on coordinate plane
811         theDumper << aBase << " = model.addSketch(" << aDocName
812                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
813       }
814     }
815   }
816
817   // dump sketch's subfeatures
818   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
819   theDumper.processSubs(aCompFeat, true);
820 }