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