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