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