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