Salome HOME
Fixed the crash on moving Group down and edit it in the issue #1862
[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   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
495   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
496   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
497   aFeature->execute();
498   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
499 }
500
501 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
502     const ModelHighAPI_RefAttr & theLine1,
503     const ModelHighAPI_RefAttr & theLine2,
504     const ModelHighAPI_Double & theValue)
505 {
506   std::shared_ptr<ModelAPI_Feature> aFeature =
507       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
508   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
509       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
510   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
511   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
512   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
513 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
514   aFeature->execute();
515   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
516 }
517
518 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
519     const ModelHighAPI_RefAttr & theLine1,
520     const ModelHighAPI_RefAttr & theLine2,
521     const ModelHighAPI_Double & theValue)
522 {
523   std::shared_ptr<ModelAPI_Feature> aFeature =
524       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
525   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
526       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
527   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
528   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
529   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
530 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
531   aFeature->execute();
532   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
533 }
534
535 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
536     const ModelHighAPI_RefAttr & thePoint1,
537     const ModelHighAPI_RefAttr & thePoint2)
538 {
539   std::shared_ptr<ModelAPI_Feature> aFeature =
540       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
541   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
542   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
543   aFeature->execute();
544   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
545 }
546
547 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
548     const ModelHighAPI_RefAttr & theLine1,
549     const ModelHighAPI_RefAttr & theLine2)
550 {
551   std::shared_ptr<ModelAPI_Feature> aFeature =
552       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
553   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
554   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
555   aFeature->execute();
556   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
557 }
558
559 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
560     const ModelHighAPI_RefAttr & thePoint,
561     const ModelHighAPI_RefAttr & thePointOrLine,
562     const ModelHighAPI_Double & theValue)
563 {
564   std::shared_ptr<ModelAPI_Feature> aFeature =
565       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
566   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
567   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
568   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
569   aFeature->execute();
570   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
571 }
572
573 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
574     const ModelHighAPI_RefAttr & theObject1,
575     const ModelHighAPI_RefAttr & theObject2)
576 {
577   std::shared_ptr<ModelAPI_Feature> aFeature =
578       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
579   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
580   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
581   aFeature->execute();
582   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
583 }
584
585 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
586     const std::list<ModelHighAPI_RefAttr> & thePoints,
587     const ModelHighAPI_Double & theRadius)
588 {
589   std::shared_ptr<ModelAPI_Feature> aFeature =
590       compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
591   fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
592   fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
593   aFeature->execute();
594   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
595 }
596
597 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
598     const ModelHighAPI_RefAttr & theObject)
599 {
600   std::shared_ptr<ModelAPI_Feature> aFeature =
601       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
602   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
603   aFeature->execute();
604   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
605 }
606
607 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
608     const ModelHighAPI_RefAttr & theLine)
609 {
610   std::shared_ptr<ModelAPI_Feature> aFeature =
611       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
612   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
613   aFeature->execute();
614   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
615 }
616
617 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
618     const ModelHighAPI_RefAttr & theLine,
619     const ModelHighAPI_Double & theValue)
620 {
621   std::shared_ptr<ModelAPI_Feature> aFeature =
622       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
623   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
624   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
625   aFeature->execute();
626   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
627 }
628
629 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
630     const ModelHighAPI_RefAttr & thePoint,
631     const ModelHighAPI_RefAttr & theLine)
632 {
633   std::shared_ptr<ModelAPI_Feature> aFeature =
634       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
635   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
636   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
637   aFeature->execute();
638   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
639 }
640
641 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
642     const ModelHighAPI_RefAttr & theLine1,
643     const ModelHighAPI_RefAttr & theLine2)
644 {
645   std::shared_ptr<ModelAPI_Feature> aFeature =
646       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
647   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
648   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
649   aFeature->execute();
650   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
651 }
652
653 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
654     const ModelHighAPI_RefAttr & theLine1,
655     const ModelHighAPI_RefAttr & theLine2)
656 {
657   std::shared_ptr<ModelAPI_Feature> aFeature =
658       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
659   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
660   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
661   aFeature->execute();
662   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
663 }
664
665 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
666     const ModelHighAPI_RefAttr & theCircleOrArc,
667     const ModelHighAPI_Double & theValue)
668 {
669   std::shared_ptr<ModelAPI_Feature> aFeature =
670       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
671   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
672   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
673   aFeature->execute();
674   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
675 }
676
677 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
678     const ModelHighAPI_RefAttr & theLine,
679     const ModelHighAPI_RefAttr & theCircle)
680 {
681   std::shared_ptr<ModelAPI_Feature> aFeature =
682       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
683   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
684   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
685   aFeature->execute();
686   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
687 }
688
689 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
690     const ModelHighAPI_RefAttr & theLine)
691 {
692   std::shared_ptr<ModelAPI_Feature> aFeature =
693       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
694   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
695   aFeature->execute();
696   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
697 }
698
699 //--------------------------------------------------------------------------------------
700
701 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
702 {
703   FeaturePtr aBase = feature();
704   const std::string& aDocName = theDumper.name(aBase->document());
705
706   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
707   if (anExternal->value()) {
708     theDumper << aBase << " = model.addSketch(" << aDocName <<
709       ", " << anExternal << ")" << std::endl;
710   } else {
711     // Sketch is base on a plane.
712     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
713         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
714     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
715         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
716     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
717         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
718
719     // Check the plane is coordinate plane
720     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
721     if (anExternal->context()) { // checking for selected planes
722       if (!aPlaneName.empty()) {
723         // dump sketch based on coordinate plane
724         theDumper << aBase << " = model.addSketch(" << aDocName
725                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
726       } else { // some other plane
727         theDumper << aBase << " = model.addSketch(" << aDocName <<
728           ", " << anExternal<< ")" << std::endl;
729       }
730     } else {
731       if (aPlaneName.empty()) {
732         // needs import additional module
733         theDumper.importModule("GeomAPI");
734         // dump plane parameters
735         const std::string& aSketchName = theDumper.name(aBase);
736         std::string anOriginName = aSketchName + "_origin";
737         std::string aNormalName  = aSketchName + "_norm";
738         std::string aDirXName    = aSketchName + "_dirx";
739         // use "\n" instead of std::endl to avoid automatic dumping sketch here
740         // and then dumplicate dumping it in the next line
741         theDumper << anOriginName << " = " << anOrigin << "\n"
742                   << aNormalName  << " = " << aNormal  << "\n"
743                   << aDirXName    << " = " << aDirX    << "\n";
744         // dump sketch based on arbitrary plane
745         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
746                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
747       } else {
748         // dump sketch based on coordinate plane
749         theDumper << aBase << " = model.addSketch(" << aDocName
750                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
751       }
752     }
753   }
754
755   // dump sketch's subfeatures
756   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
757   theDumper.processSubs(aCompFeat, true);
758 }