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