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