Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Name   : SketchAPI_Sketch.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "SketchAPI_Sketch.h"
9 //--------------------------------------------------------------------------------------
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintCollinear.h>
14 #include <SketchPlugin_ConstraintDistance.h>
15 #include <SketchPlugin_ConstraintEqual.h>
16 #include <SketchPlugin_ConstraintFillet.h>
17 #include <SketchPlugin_ConstraintHorizontal.h>
18 #include <SketchPlugin_ConstraintLength.h>
19 #include <SketchPlugin_ConstraintMiddle.h>
20 #include <SketchPlugin_ConstraintMirror.h>
21 #include <SketchPlugin_ConstraintParallel.h>
22 #include <SketchPlugin_ConstraintPerpendicular.h>
23 #include <SketchPlugin_ConstraintRadius.h>
24 #include <SketchPlugin_ConstraintRigid.h>
25 #include <SketchPlugin_ConstraintTangent.h>
26 #include <SketchPlugin_ConstraintVertical.h>
27 //--------------------------------------------------------------------------------------
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelHighAPI_Dumper.h>
31 #include <ModelHighAPI_RefAttr.h>
32 #include <ModelHighAPI_Selection.h>
33 #include <ModelHighAPI_Services.h>
34 #include <ModelHighAPI_Tools.h>
35 //--------------------------------------------------------------------------------------
36 #include "SketchAPI_Arc.h"
37 #include "SketchAPI_Circle.h"
38 #include "SketchAPI_IntersectionPoint.h"
39 #include "SketchAPI_Line.h"
40 #include "SketchAPI_Mirror.h"
41 #include "SketchAPI_Point.h"
42 #include "SketchAPI_Projection.h"
43 #include "SketchAPI_Rectangle.h"
44 #include "SketchAPI_Rotation.h"
45 #include "SketchAPI_Translation.h"
46 //--------------------------------------------------------------------------------------
47 SketchAPI_Sketch::SketchAPI_Sketch(
48     const std::shared_ptr<ModelAPI_Feature> & theFeature)
49 : ModelHighAPI_Interface(theFeature)
50 {
51   initialize();
52 }
53
54 SketchAPI_Sketch::SketchAPI_Sketch(
55     const std::shared_ptr<ModelAPI_Feature> & theFeature,
56     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
57 : ModelHighAPI_Interface(theFeature)
58 {
59   if (initialize()) {
60     setPlane(thePlane);
61   }
62 }
63
64 SketchAPI_Sketch::SketchAPI_Sketch(
65     const std::shared_ptr<ModelAPI_Feature> & theFeature,
66     const ModelHighAPI_Selection & theExternal)
67 : ModelHighAPI_Interface(theFeature)
68 {
69   if (initialize()) {
70     setExternal(theExternal);
71   }
72 }
73
74 SketchAPI_Sketch::~SketchAPI_Sketch()
75 {
76
77 }
78
79 //--------------------------------------------------------------------------------------
80 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
81 {
82   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
83 }
84
85 //--------------------------------------------------------------------------------------
86 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
87 {
88   fillAttribute(thePlane->origin(), myorigin);
89   fillAttribute(thePlane->dirX(), mydirX);
90   fillAttribute(thePlane->normal(), mynormal);
91
92   execute();
93 }
94
95 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
96 {
97   fillAttribute(theExternal, myexternal);
98
99   execute();
100 }
101
102 //--------------------------------------------------------------------------------------
103 void SketchAPI_Sketch::setValue(
104     const std::shared_ptr<ModelAPI_Feature> & theConstraint,
105     const ModelHighAPI_Double & theValue)
106 {
107   // TODO(spo): check somehow that the feature is a constraint or eliminate crash if the feature have no real attribute VALUE
108   fillAttribute(theValue, theConstraint->real(SketchPlugin_Constraint::VALUE()));
109
110 //  theConstraint->execute();
111 }
112
113 //--------------------------------------------------------------------------------------
114 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
115 {
116   const_cast<SketchAPI_Sketch*>(this)->execute();
117
118   std::list<ModelHighAPI_Selection> aSelectionList;
119
120   ResultConstructionPtr aResultConstruction =
121       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
122   if (aResultConstruction.get() == NULL)
123     return aSelectionList;
124
125   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
126     aSelectionList.push_back(
127         ModelHighAPI_Selection(aResultConstruction,
128                                aResultConstruction->face(anIndex)));
129   }
130
131   return aSelectionList;
132 }
133
134 //--------------------------------------------------------------------------------------
135 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
136                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
137 {
138   // TODO(spo): check that thePart is not empty
139   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
140   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
141 }
142
143 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
144                     const ModelHighAPI_Selection & theExternal)
145 {
146   // TODO(spo): check that thePart is not empty
147   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
148   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
149 }
150
151 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
152                     const std::string & theExternalName)
153 {
154   // TODO(spo): check that thePart is not empty
155   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
156   return SketchPtr(new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
157 }
158
159 //--------------------------------------------------------------------------------------
160 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
161     double theX, double theY)
162 {
163   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
164   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
165 }
166 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
167     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
168 {
169   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
170   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
171 }
172 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
173 {
174   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
175   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
176 }
177 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
178 {
179   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Point::ID());
180   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
181 }
182
183 //--------------------------------------------------------------------------------------
184 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
185     const ModelHighAPI_Selection & theExternal)
186 {
187   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
188   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
189 }
190 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
191     const std::string & theExternalName)
192 {
193   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
194   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
195 }
196
197 //--------------------------------------------------------------------------------------
198 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1, double theX2, double theY2)
199 {
200   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
201   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
202 }
203 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
204     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
205     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
206 {
207   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
208   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
209 }
210 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
211 {
212   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
213   LinePtr aLine(new SketchAPI_Line(aFeature, theExternal));
214   setFixed(InterfacePtr(aLine));
215   return aLine;
216 }
217 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
218 {
219   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Line::ID());
220   LinePtr aLine(new SketchAPI_Line(aFeature, theExternalName));
221   setFixed(InterfacePtr(aLine));
222   return aLine;
223 }
224
225 //--------------------------------------------------------------------------------------
226 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1, double theX2, double theY2)
227 {
228   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
229   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
230 }
231 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
232     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
233     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
234 {
235   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
236   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
237 }
238
239 //--------------------------------------------------------------------------------------
240 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
241                                                               double theCenterY,
242                                                               double theRadius)
243 {
244   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
245   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
246 }
247
248 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
249                                                               double theRadius)
250 {
251   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
252   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
253 }
254
255 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
256                                                               double theX2, double theY2,
257                                                               double theX3, double theY3)
258 {
259   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
260   return CirclePtr(new SketchAPI_Circle(aFeature, theX1, theY1, theX2, theY2, theX3, theY3));
261 }
262
263 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
264                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
265                                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
266 {
267   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
268   return CirclePtr(new SketchAPI_Circle(aFeature, thePoint1, thePoint2, thePoint3));
269 }
270
271 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
272 {
273   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
274   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
275 }
276
277 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
278 {
279   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
280   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Circle::ID());
281   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
282 }
283
284 //--------------------------------------------------------------------------------------
285 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
286                                                         double theStartX, double theStartY,
287                                                         double theEndX, double theEndY,
288                                                         bool theInversed)
289 {
290   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
291   return ArcPtr(new SketchAPI_Arc(aFeature,
292                                   theCenterX, theCenterY,
293                                   theStartX, theStartY,
294                                   theEndX, theEndY,
295                                   theInversed));
296 }
297
298 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
299                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
300                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
301                                                         bool theInversed)
302 {
303   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
304   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
305 }
306
307 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
308                                                         double theEndX, double theEndY,
309                                                         double thePassedX, double thePassedY)
310 {
311   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
312   return ArcPtr(new SketchAPI_Arc(aFeature,
313                                   theStartX, theStartY,
314                                   theEndX, theEndY,
315                                   thePassedX, thePassedY));
316 }
317
318 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
319                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
320                                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
321 {
322   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
323   return ArcPtr(new SketchAPI_Arc(aFeature, theStart, theEnd, thePassed));
324 }
325
326 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
327                                                         double theEndX, double theEndY,
328                                                         bool theInversed)
329 {
330   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
331   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEndX, theEndY, theInversed));
332 }
333
334 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_RefAttr& theTangentPoint,
335                                                         const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
336                                                         bool theInversed)
337 {
338   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
339   return ArcPtr(new SketchAPI_Arc(aFeature, theTangentPoint, theEnd, theInversed));
340 }
341
342 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
343 {
344   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
345   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
346 }
347
348 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
349 {
350   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
351   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Arc::ID());
352   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
353 }
354
355 //--------------------------------------------------------------------------------------
356 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
357     const ModelHighAPI_Selection & theExternalFeature)
358 {
359   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_Projection::ID());
360   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
361 }
362
363 //--------------------------------------------------------------------------------------
364 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
365     const ModelHighAPI_RefAttr & theMirrorLine,
366     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
367 {
368   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
369   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
370 }
371
372 //--------------------------------------------------------------------------------------
373 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
374     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
375     const ModelHighAPI_RefAttr & thePoint1,
376     const ModelHighAPI_RefAttr & thePoint2,
377     const ModelHighAPI_Integer & theNumberOfObjects,
378     bool theFullValue)
379 {
380   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
381   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1, thePoint2, theNumberOfObjects, theFullValue));
382 }
383
384 //--------------------------------------------------------------------------------------
385 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
386     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
387     const ModelHighAPI_RefAttr & theCenter,
388     const ModelHighAPI_Double & theAngle,
389     const ModelHighAPI_Integer & theNumberOfObjects,
390     bool theFullValue)
391 {
392   std::shared_ptr<ModelAPI_Feature> aFeature = compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
393   return RotationPtr(new SketchAPI_Rotation(aFeature, theObjects, theCenter, theAngle, theNumberOfObjects, theFullValue));
394 }
395
396 //--------------------------------------------------------------------------------------
397 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setAngle(
398     const ModelHighAPI_RefAttr & theLine1,
399     const ModelHighAPI_RefAttr & theLine2,
400     const ModelHighAPI_Double & theValue)
401 {
402   // TODO(spo): is support of angle type necessary?
403   std::shared_ptr<ModelAPI_Feature> aFeature =
404       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
405   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
406   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
407   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
408   aFeature->execute();
409   return aFeature;
410 }
411
412 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCoincident(
413     const ModelHighAPI_RefAttr & thePoint1,
414     const ModelHighAPI_RefAttr & thePoint2)
415 {
416   std::shared_ptr<ModelAPI_Feature> aFeature =
417       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
418   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
419   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
420   aFeature->execute();
421   return aFeature;
422 }
423
424 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setCollinear(
425     const ModelHighAPI_RefAttr & theLine1,
426     const ModelHighAPI_RefAttr & theLine2)
427 {
428   std::shared_ptr<ModelAPI_Feature> aFeature =
429       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
430   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
431   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
432   aFeature->execute();
433   return aFeature;
434 }
435
436 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setDistance(
437     const ModelHighAPI_RefAttr & thePoint,
438     const ModelHighAPI_RefAttr & thePointOrLine,
439     const ModelHighAPI_Double & theValue)
440 {
441   std::shared_ptr<ModelAPI_Feature> aFeature =
442       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
443   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
444   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
445   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
446   aFeature->execute();
447   return aFeature;
448 }
449
450 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setEqual(
451     const ModelHighAPI_RefAttr & theObject1,
452     const ModelHighAPI_RefAttr & theObject2)
453 {
454   std::shared_ptr<ModelAPI_Feature> aFeature =
455       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
456   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
457   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
458   aFeature->execute();
459   return aFeature;
460 }
461
462 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFillet(
463     const std::list<ModelHighAPI_RefAttr> & thePoints,
464     const ModelHighAPI_Double & theRadius)
465 {
466   std::shared_ptr<ModelAPI_Feature> aFeature =
467       compositeFeature()->addFeature(SketchPlugin_ConstraintFillet::ID());
468   fillAttribute(thePoints, aFeature->data()->refattrlist(SketchPlugin_Constraint::ENTITY_A()));
469   fillAttribute(theRadius, aFeature->real(SketchPlugin_Constraint::VALUE()));
470   aFeature->execute();
471   return aFeature;
472 }
473
474 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setFixed(
475     const ModelHighAPI_RefAttr & theObject)
476 {
477   std::shared_ptr<ModelAPI_Feature> aFeature =
478       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
479   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
480   aFeature->execute();
481   return aFeature;
482 }
483
484 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setHorizontal(
485     const ModelHighAPI_RefAttr & theLine)
486 {
487   std::shared_ptr<ModelAPI_Feature> aFeature =
488       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
489   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
490   aFeature->execute();
491   return aFeature;
492 }
493
494 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setLength(
495     const ModelHighAPI_RefAttr & theLine,
496     const ModelHighAPI_Double & theValue)
497 {
498   std::shared_ptr<ModelAPI_Feature> aFeature =
499       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
500   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
501   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
502   aFeature->execute();
503   return aFeature;
504 }
505
506 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setMiddlePoint(
507     const ModelHighAPI_RefAttr & thePoint,
508     const ModelHighAPI_RefAttr & theLine)
509 {
510   std::shared_ptr<ModelAPI_Feature> aFeature =
511       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
512   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
513   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
514   aFeature->execute();
515   return aFeature;
516 }
517
518 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setParallel(
519     const ModelHighAPI_RefAttr & theLine1,
520     const ModelHighAPI_RefAttr & theLine2)
521 {
522   std::shared_ptr<ModelAPI_Feature> aFeature =
523       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
524   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
525   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
526   aFeature->execute();
527   return aFeature;
528 }
529
530 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setPerpendicular(
531     const ModelHighAPI_RefAttr & theLine1,
532     const ModelHighAPI_RefAttr & theLine2)
533 {
534   std::shared_ptr<ModelAPI_Feature> aFeature =
535       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
536   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
537   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
538   aFeature->execute();
539   return aFeature;
540 }
541
542 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setRadius(
543     const ModelHighAPI_RefAttr & theCircleOrArc,
544     const ModelHighAPI_Double & theValue)
545 {
546   std::shared_ptr<ModelAPI_Feature> aFeature =
547       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
548   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
549   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
550   aFeature->execute();
551   return aFeature;
552 }
553
554 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setTangent(
555     const ModelHighAPI_RefAttr & theLine,
556     const ModelHighAPI_RefAttr & theCircle)
557 {
558   std::shared_ptr<ModelAPI_Feature> aFeature =
559       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
560   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
561   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
562   aFeature->execute();
563   return aFeature;
564 }
565
566 std::shared_ptr<ModelAPI_Feature> SketchAPI_Sketch::setVertical(
567     const ModelHighAPI_RefAttr & theLine)
568 {
569   std::shared_ptr<ModelAPI_Feature> aFeature =
570       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
571   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
572   aFeature->execute();
573   return aFeature;
574 }
575
576 //--------------------------------------------------------------------------------------
577
578 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
579 {
580   FeaturePtr aBase = feature();
581   const std::string& aDocName = theDumper.name(aBase->document());
582
583   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
584   if (anExternal->value()) {
585     FeaturePtr aPlnFeature = ModelAPI_Feature::feature(anExternal->context()->data()->owner());
586     const std::string& aPlaneName = theDumper.name(aPlnFeature);
587     theDumper << aBase << " = model.addSketch(" << aDocName << ", \"" << aPlaneName << "\")" << std::endl;
588   } else {
589     // Sketch is base on a plane.
590     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
591         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
592     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
593         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
594     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
595         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
596
597     // Check the plane is coordinate plane
598     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
599     if (aPlaneName.empty()) {
600       // needs import additional module
601       theDumper.importModule("GeomAPI");
602       // dump plane parameters
603       const std::string& aSketchName = theDumper.name(aBase);
604       std::string anOriginName = aSketchName + "_origin";
605       std::string aNormalName  = aSketchName + "_norm";
606       std::string aDirXName    = aSketchName + "_dirx";
607       theDumper << anOriginName << " = " << anOrigin << std::endl
608                 << aNormalName  << " = " << aNormal  << std::endl
609                 << aDirXName    << " = " << aDirX    << std::endl;
610       // dump sketch based on arbitrary plane
611       theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
612                 << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
613     } else {
614       // dump sketch based on coordinate plane
615       theDumper << aBase << " = model.addSketch(" << aDocName
616                 << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
617     }
618   }
619 }