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