Salome HOME
b207823768c79c5295fb0764603af98862beb291
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Sketch.h"
22 //--------------------------------------------------------------------------------------
23 #include <SketchPlugin_Constraint.h>
24 #include <SketchPlugin_ConstraintAngle.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintCollinear.h>
27 #include <SketchPlugin_ConstraintDistance.h>
28 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
29 #include <SketchPlugin_ConstraintDistanceVertical.h>
30 #include <SketchPlugin_ConstraintEqual.h>
31 #include <SketchPlugin_Fillet.h>
32 #include <SketchPlugin_ConstraintHorizontal.h>
33 #include <SketchPlugin_ConstraintLength.h>
34 #include <SketchPlugin_ConstraintMiddle.h>
35 #include <SketchPlugin_ConstraintMirror.h>
36 #include <SketchPlugin_ConstraintParallel.h>
37 #include <SketchPlugin_ConstraintPerpendicular.h>
38 #include <SketchPlugin_ConstraintRadius.h>
39 #include <SketchPlugin_ConstraintRigid.h>
40 #include <SketchPlugin_Trim.h>
41 #include <SketchPlugin_Split.h>
42 #include <SketchPlugin_ConstraintTangent.h>
43 #include <SketchPlugin_ConstraintVertical.h>
44 #include <SketcherPrs_Tools.h>
45 //--------------------------------------------------------------------------------------
46 #include <ModelAPI_Events.h>
47 #include <ModelAPI_CompositeFeature.h>
48 #include <ModelAPI_ResultConstruction.h>
49 #include <ModelHighAPI_Double.h>
50 #include <ModelHighAPI_Dumper.h>
51 #include <ModelHighAPI_RefAttr.h>
52 #include <ModelHighAPI_Selection.h>
53 #include <ModelHighAPI_Services.h>
54 #include <ModelHighAPI_Tools.h>
55 //--------------------------------------------------------------------------------------
56 #include "SketchAPI_Arc.h"
57 #include "SketchAPI_MacroArc.h"
58 #include "SketchAPI_Circle.h"
59 #include "SketchAPI_IntersectionPoint.h"
60 #include "SketchAPI_Line.h"
61 #include "SketchAPI_MacroCircle.h"
62 #include "SketchAPI_Mirror.h"
63 #include "SketchAPI_Point.h"
64 #include "SketchAPI_Projection.h"
65 #include "SketchAPI_Rectangle.h"
66 #include "SketchAPI_Rotation.h"
67 #include "SketchAPI_Translation.h"
68 //--------------------------------------------------------------------------------------
69 #include <GeomAPI_Dir2d.h>
70 #include <GeomAPI_XY.h>
71 #include <cmath>
72 //--------------------------------------------------------------------------------------
73 SketchAPI_Sketch::SketchAPI_Sketch(
74     const std::shared_ptr<ModelAPI_Feature> & theFeature)
75 : ModelHighAPI_Interface(theFeature)
76 {
77   initialize();
78 }
79
80 SketchAPI_Sketch::SketchAPI_Sketch(
81     const std::shared_ptr<ModelAPI_Feature> & theFeature,
82     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
83 : ModelHighAPI_Interface(theFeature)
84 {
85   if (initialize()) {
86     setPlane(thePlane);
87   }
88 }
89
90 SketchAPI_Sketch::SketchAPI_Sketch(
91     const std::shared_ptr<ModelAPI_Feature> & theFeature,
92     const ModelHighAPI_Selection & theExternal)
93 : ModelHighAPI_Interface(theFeature)
94 {
95   if (initialize()) {
96     setExternal(theExternal);
97   }
98 }
99
100 SketchAPI_Sketch::SketchAPI_Sketch(
101     const std::shared_ptr<ModelAPI_Feature> & theFeature,
102     std::shared_ptr<ModelAPI_Object> thePlaneObject)
103 : ModelHighAPI_Interface(theFeature)
104 {
105   if (initialize()) {
106     setExternal(thePlaneObject);
107   }
108 }
109
110 SketchAPI_Sketch::~SketchAPI_Sketch()
111 {
112
113 }
114
115 //--------------------------------------------------------------------------------------
116 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
117 {
118   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
119 }
120
121 //--------------------------------------------------------------------------------------
122 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
123 {
124   fillAttribute(thePlane->origin(), myorigin);
125   fillAttribute(thePlane->dirX(), mydirX);
126   fillAttribute(thePlane->normal(), mynormal);
127
128   execute();
129 }
130
131 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
132 {
133   fillAttribute(theExternal, myexternal);
134
135   execute();
136 }
137
138 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
139 {
140   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
141   ModelHighAPI_Selection aSel(aRes);
142   setExternal(aSel);
143 }
144
145 //--------------------------------------------------------------------------------------
146 void SketchAPI_Sketch::setValue(
147     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
148     const ModelHighAPI_Double & theValue)
149 {
150   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
151
152 //  theConstraint->execute();
153 }
154
155 //--------------------------------------------------------------------------------------
156 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
157 {
158   const_cast<SketchAPI_Sketch*>(this)->execute();
159
160   std::list<ModelHighAPI_Selection> aSelectionList;
161
162   ResultConstructionPtr aResultConstruction =
163       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
164   if (aResultConstruction.get() == NULL)
165     return aSelectionList;
166
167   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
168     aSelectionList.push_back(
169         ModelHighAPI_Selection(aResultConstruction,
170                                aResultConstruction->face(anIndex)));
171   }
172
173   return aSelectionList;
174 }
175
176 //--------------------------------------------------------------------------------------
177 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
178                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
179 {
180   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
181   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
182 }
183
184 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
185                     const ModelHighAPI_Selection & theExternal)
186 {
187   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
188   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
189 }
190
191 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
192                     const std::string & theExternalName)
193 {
194   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
195   return SketchPtr(
196     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
197 }
198
199 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
200                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
201 {
202   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
203   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
204 }
205
206
207 //--------------------------------------------------------------------------------------
208 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
209     double theX, double theY)
210 {
211   std::shared_ptr<ModelAPI_Feature> aFeature =
212     compositeFeature()->addFeature(SketchPlugin_Point::ID());
213   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
214 }
215 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
216     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
217 {
218   std::shared_ptr<ModelAPI_Feature> aFeature =
219     compositeFeature()->addFeature(SketchPlugin_Point::ID());
220   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
221 }
222 std::shared_ptr<SketchAPI_Point>
223   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
224 {
225   std::shared_ptr<ModelAPI_Feature> aFeature =
226     compositeFeature()->addFeature(SketchPlugin_Point::ID());
227   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
228 }
229 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
230 {
231   std::shared_ptr<ModelAPI_Feature> aFeature =
232     compositeFeature()->addFeature(SketchPlugin_Point::ID());
233   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
234 }
235
236 //--------------------------------------------------------------------------------------
237 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
238     const ModelHighAPI_Selection & theExternal)
239 {
240   std::shared_ptr<ModelAPI_Feature> aFeature =
241     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
242   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternal));
243 }
244 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
245     const std::string & theExternalName)
246 {
247   std::shared_ptr<ModelAPI_Feature> aFeature =
248     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
249   return IntersectionPointPtr(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
250 }
251
252 //--------------------------------------------------------------------------------------
253 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
254                                                           double theX2, double theY2)
255 {
256   std::shared_ptr<ModelAPI_Feature> aFeature =
257     compositeFeature()->addFeature(SketchPlugin_Line::ID());
258   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
259 }
260 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
261     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
262     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
263 {
264   std::shared_ptr<ModelAPI_Feature> aFeature =
265     compositeFeature()->addFeature(SketchPlugin_Line::ID());
266   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
267 }
268 std::shared_ptr<SketchAPI_Line>
269   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
270 {
271   std::shared_ptr<ModelAPI_Feature> aFeature =
272     compositeFeature()->addFeature(SketchPlugin_Line::ID());
273   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
274 }
275 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
276 {
277   std::shared_ptr<ModelAPI_Feature> aFeature =
278     compositeFeature()->addFeature(SketchPlugin_Line::ID());
279   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
280 }
281
282 //--------------------------------------------------------------------------------------
283 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
284                                                                     double theX2, double theY2)
285 {
286   std::shared_ptr<ModelAPI_Feature> aFeature =
287     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
288   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
289 }
290 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
291     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
292     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
293 {
294   std::shared_ptr<ModelAPI_Feature> aFeature =
295     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
296   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
297 }
298
299 //--------------------------------------------------------------------------------------
300 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
301                                                               double theCenterY,
302                                                               double theRadius)
303 {
304   std::shared_ptr<ModelAPI_Feature> aFeature =
305     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
306   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
307 }
308
309 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
310                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
311                                     double theRadius)
312 {
313   std::shared_ptr<ModelAPI_Feature> aFeature =
314     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
315   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
316 }
317
318 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
319                                                                    double theCenterY,
320                                                                    double thePassedX,
321                                                                    double thePassedY)
322 {
323   std::shared_ptr<ModelAPI_Feature> aFeature =
324     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
325   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
326                                                             thePassedX, thePassedY));
327 }
328
329 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
330     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
331     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
332 {
333   std::shared_ptr<ModelAPI_Feature> aFeature =
334     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
335   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
336 }
337
338 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
339                                                                    double theX2, double theY2,
340                                                                    double theX3, double theY3)
341 {
342   std::shared_ptr<ModelAPI_Feature> aFeature =
343     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
344   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
345                                                             theX2, theY2,
346                                                             theX3, theY3));
347 }
348
349 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
350     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
351     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
352     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
353 {
354   std::shared_ptr<ModelAPI_Feature> aFeature =
355     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
356   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
357 }
358
359 std::shared_ptr<SketchAPI_Circle>
360   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
361 {
362   std::shared_ptr<ModelAPI_Feature> aFeature =
363     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
364   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
365 }
366
367 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
368 {
369   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
370   std::shared_ptr<ModelAPI_Feature> aFeature =
371     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
372   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
373 }
374
375 //--------------------------------------------------------------------------------------
376 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
377                                                         double theStartX, double theStartY,
378                                                         double theEndX, double theEndY,
379                                                         bool theInversed)
380 {
381   std::shared_ptr<ModelAPI_Feature> aFeature =
382     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
383   return ArcPtr(new SketchAPI_Arc(aFeature,
384                                   theCenterX, theCenterY,
385                                   theStartX, theStartY,
386                                   theEndX, theEndY,
387                                   theInversed));
388 }
389
390 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
391                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
392                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
393                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
394                                               bool theInversed)
395 {
396   std::shared_ptr<ModelAPI_Feature> aFeature =
397     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
398   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
399 }
400
401 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
402                                                         double theEndX, double theEndY,
403                                                         double thePassedX, double thePassedY)
404 {
405   std::shared_ptr<ModelAPI_Feature> aFeature =
406     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
407   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
408                                        theStartX, theStartY,
409                                        theEndX, theEndY,
410                                        thePassedX, thePassedY));
411 }
412
413 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
414                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
415                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
416                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
417 {
418   std::shared_ptr<ModelAPI_Feature> aFeature =
419     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
420   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
421 }
422
423 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
424                                                 const ModelHighAPI_RefAttr& theTangentPoint,
425                                                 double theEndX, double theEndY,
426                                                 bool theInversed)
427 {
428   std::shared_ptr<ModelAPI_Feature> aFeature =
429     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
430   return MacroArcPtr(new SketchAPI_MacroArc(
431     aFeature, theTangentPoint, theEndX, theEndY, theInversed));
432 }
433
434 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
435                                               const ModelHighAPI_RefAttr& theTangentPoint,
436                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
437                                               bool theInversed)
438 {
439   std::shared_ptr<ModelAPI_Feature> aFeature =
440     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
441   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theTangentPoint, theEnd, theInversed));
442 }
443
444 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
445 {
446   std::shared_ptr<ModelAPI_Feature> aFeature =
447     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
448   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
449 }
450
451 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
452 {
453   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
454   std::shared_ptr<ModelAPI_Feature> aFeature =
455     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
456   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
457 }
458
459 //--------------------------------------------------------------------------------------
460 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
461     const ModelHighAPI_Selection & theExternalFeature)
462 {
463   std::shared_ptr<ModelAPI_Feature> aFeature =
464     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
465   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalFeature));
466 }
467
468 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
469     const std::string & theExternalName)
470 {
471   std::shared_ptr<ModelAPI_Feature> aFeature =
472     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
473   return ProjectionPtr(new SketchAPI_Projection(aFeature, theExternalName));
474 }
475
476 //--------------------------------------------------------------------------------------
477 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
478     const ModelHighAPI_RefAttr & theMirrorLine,
479     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
480 {
481   std::shared_ptr<ModelAPI_Feature> aFeature =
482     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
483   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
484 }
485
486 //--------------------------------------------------------------------------------------
487 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
488     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
489     const ModelHighAPI_RefAttr & thePoint1,
490     const ModelHighAPI_RefAttr & thePoint2,
491     const ModelHighAPI_Integer & theNumberOfObjects,
492     bool theFullValue)
493 {
494   std::shared_ptr<ModelAPI_Feature> aFeature =
495     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
496   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
497                                                   thePoint2, theNumberOfObjects, theFullValue));
498 }
499
500 //--------------------------------------------------------------------------------------
501 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
502     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
503     const ModelHighAPI_RefAttr & theCenter,
504     const ModelHighAPI_Double & theAngle,
505     const ModelHighAPI_Integer & theNumberOfObjects,
506     bool theFullValue)
507 {
508   std::shared_ptr<ModelAPI_Feature> aFeature =
509     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
510   return RotationPtr(
511     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
512                            theAngle, theNumberOfObjects, theFullValue));
513 }
514
515 //--------------------------------------------------------------------------------------
516 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
517                                           const ModelHighAPI_Reference& theFeature,
518                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
519 {
520   std::shared_ptr<ModelAPI_Feature> aFeature =
521     compositeFeature()->addFeature(SketchPlugin_Split::ID());
522   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
523
524   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
525   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
526     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
527     fillAttribute(thePositionPoint, aPointAttr);
528   }
529
530   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
531 }
532
533 //--------------------------------------------------------------------------------------
534 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
535                                         const ModelHighAPI_Reference& theFeature,
536                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
537 {
538   std::shared_ptr<ModelAPI_Feature> aFeature =
539     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
540   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
541
542   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
543   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
544     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
545     fillAttribute(thePositionPoint, aPointAttr);
546   }
547
548   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
549 }
550
551 //--------------------------------------------------------------------------------------
552 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
553     const ModelHighAPI_RefAttr & theLine1,
554     const ModelHighAPI_RefAttr & theLine2,
555     const ModelHighAPI_Double & theValue)
556 {
557   std::shared_ptr<ModelAPI_Feature> aFeature =
558       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
559   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
560       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
561   // fill the value before llines to avoid calculation of angle value by the Angle feature
562   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
563   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
564   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
565   aFeature->execute();
566   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
567 }
568
569 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
570     const ModelHighAPI_RefAttr & theLine1,
571     const ModelHighAPI_RefAttr & theLine2,
572     const ModelHighAPI_Double & theValue)
573 {
574   std::shared_ptr<ModelAPI_Feature> aFeature =
575       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
576   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
577       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
578   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
579   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
580   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
581 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
582   aFeature->execute();
583   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
584 }
585
586 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
587     const ModelHighAPI_RefAttr & theLine1,
588     const ModelHighAPI_RefAttr & theLine2,
589     const ModelHighAPI_Double & theValue)
590 {
591   std::shared_ptr<ModelAPI_Feature> aFeature =
592       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
593   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
594       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
595   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
596   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
597   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
598 //  fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
599   aFeature->execute();
600   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
601 }
602
603 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
604     const ModelHighAPI_RefAttr & thePoint1,
605     const ModelHighAPI_RefAttr & thePoint2)
606 {
607   std::shared_ptr<ModelAPI_Feature> aFeature =
608       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
609   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
610   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
611   aFeature->execute();
612   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
613 }
614
615 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
616     const ModelHighAPI_RefAttr & theLine1,
617     const ModelHighAPI_RefAttr & theLine2)
618 {
619   std::shared_ptr<ModelAPI_Feature> aFeature =
620       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
621   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
622   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
623   aFeature->execute();
624   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
625 }
626
627 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
628     const ModelHighAPI_RefAttr & thePoint,
629     const ModelHighAPI_RefAttr & thePointOrLine,
630     const ModelHighAPI_Double & theValue,
631     bool isSigned)
632 {
633   std::shared_ptr<ModelAPI_Feature> aFeature =
634       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
635   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
636   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
637   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
638   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
639   aFeature->execute();
640   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
641 }
642
643 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
644     const ModelHighAPI_RefAttr & thePoint,
645     const ModelHighAPI_RefAttr & thePointOrLine,
646     const ModelHighAPI_Double & theValue)
647 {
648   return setDistance(thePoint, thePointOrLine, theValue, true);
649 }
650
651 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
652     const ModelHighAPI_RefAttr & thePoint,
653     const ModelHighAPI_RefAttr & thePointOrLine,
654     const ModelHighAPI_Double & theValue)
655 {
656   return setDistance(thePoint, thePointOrLine, theValue, false);
657 }
658
659 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
660     const ModelHighAPI_RefAttr & thePoint1,
661     const ModelHighAPI_RefAttr & thePoint2,
662     const ModelHighAPI_Double & theValue)
663 {
664   std::shared_ptr<ModelAPI_Feature> aFeature =
665       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
666   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
667   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
668   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
669   aFeature->execute();
670   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
671 }
672
673 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
674     const ModelHighAPI_RefAttr & thePoint1,
675     const ModelHighAPI_RefAttr & thePoint2,
676     const ModelHighAPI_Double & theValue)
677 {
678   std::shared_ptr<ModelAPI_Feature> aFeature =
679       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
680   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
681   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
682   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
683   aFeature->execute();
684   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
685 }
686
687 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
688     const ModelHighAPI_RefAttr & theObject1,
689     const ModelHighAPI_RefAttr & theObject2)
690 {
691   std::shared_ptr<ModelAPI_Feature> aFeature =
692       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
693   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
694   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
695   aFeature->execute();
696   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
697 }
698
699 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
700     const ModelHighAPI_RefAttr & thePoint)
701 {
702   std::shared_ptr<ModelAPI_Feature> aFeature =
703       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
704   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
705   apply(); // finish operation to remove Fillet feature correcly
706   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
707 }
708
709 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
710     const ModelHighAPI_RefAttr & thePoint,
711     const ModelHighAPI_Double & theRadius)
712 {
713   CompositeFeaturePtr aSketch = compositeFeature();
714   int aNbSubs = aSketch->numberOfSubs();
715
716   // create fillet
717   InterfacePtr aFilletFeature = setFillet(thePoint);
718
719   // set radius for just created arc
720   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
721   if (anArc->getKind() == SketchPlugin_Arc::ID())
722     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
723
724   return aFilletFeature;
725 }
726
727 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
728     const ModelHighAPI_RefAttr & theObject)
729 {
730   std::shared_ptr<ModelAPI_Feature> aFeature =
731       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
732   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
733   aFeature->execute();
734   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
735 }
736
737 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
738     const ModelHighAPI_RefAttr & theLine)
739 {
740   std::shared_ptr<ModelAPI_Feature> aFeature =
741       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
742   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
743   aFeature->execute();
744   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
745 }
746
747 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
748     const ModelHighAPI_RefAttr & theLine,
749     const ModelHighAPI_Double & theValue)
750 {
751   std::shared_ptr<ModelAPI_Feature> aFeature =
752       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
753   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
754   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
755   aFeature->execute();
756   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
757 }
758
759 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
760     const ModelHighAPI_RefAttr & thePoint,
761     const ModelHighAPI_RefAttr & theLine)
762 {
763   std::shared_ptr<ModelAPI_Feature> aFeature =
764       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
765   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
766   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
767   aFeature->execute();
768   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
769 }
770
771 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
772     const ModelHighAPI_RefAttr & theLine1,
773     const ModelHighAPI_RefAttr & theLine2)
774 {
775   std::shared_ptr<ModelAPI_Feature> aFeature =
776       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
777   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
778   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
779   aFeature->execute();
780   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
781 }
782
783 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
784     const ModelHighAPI_RefAttr & theLine1,
785     const ModelHighAPI_RefAttr & theLine2)
786 {
787   std::shared_ptr<ModelAPI_Feature> aFeature =
788       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
789   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
790   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
791   aFeature->execute();
792   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
793 }
794
795 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
796     const ModelHighAPI_RefAttr & theCircleOrArc,
797     const ModelHighAPI_Double & theValue)
798 {
799   std::shared_ptr<ModelAPI_Feature> aFeature =
800       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
801   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
802   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
803   aFeature->execute();
804   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
805 }
806
807 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
808     const ModelHighAPI_RefAttr & theLine,
809     const ModelHighAPI_RefAttr & theCircle)
810 {
811   std::shared_ptr<ModelAPI_Feature> aFeature =
812       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
813   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
814   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
815   aFeature->execute();
816   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
817 }
818
819 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
820     const ModelHighAPI_RefAttr & theLine)
821 {
822   std::shared_ptr<ModelAPI_Feature> aFeature =
823       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
824   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
825   aFeature->execute();
826   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
827 }
828
829 //--------------------------------------------------------------------------------------
830
831 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
832 {
833   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
834   if (aPnt)
835     return aPnt->pnt();
836   return std::shared_ptr<GeomAPI_Pnt2d>();
837 }
838
839 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
840 {
841   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
842       theFeature->attribute(SketchPlugin_Line::START_ID()));
843   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
844       theFeature->attribute(SketchPlugin_Line::END_ID()));
845
846   if (!aStartAttr || !aEndAttr)
847     return std::shared_ptr<GeomAPI_Pnt2d>();
848
849   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
850   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
851   return std::shared_ptr<GeomAPI_Pnt2d>(
852       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
853 }
854
855 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
856 {
857   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
858       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
859   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
860
861   if (!aCenter || !aRadius)
862     return std::shared_ptr<GeomAPI_Pnt2d>();
863
864   return std::shared_ptr<GeomAPI_Pnt2d>(
865       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
866 }
867
868 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
869 {
870   static const double PI = 3.141592653589793238463;
871
872   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
873       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
874   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
875       theFeature->attribute(SketchPlugin_Arc::START_ID()));
876   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
877       theFeature->attribute(SketchPlugin_Arc::END_ID()));
878
879   if (!aCenterAttr || !aStartAttr || !aEndAttr)
880     return std::shared_ptr<GeomAPI_Pnt2d>();
881
882   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
883       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
884   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
885       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
886
887   double anAngle = aStartDir->angle(aEndDir);
888   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
889   if (isReversed && anAngle > 0.)
890     anAngle -= 2.0 * PI;
891   else if (!isReversed && anAngle <= 0.)
892     anAngle += 2.0 * PI;
893
894   double cosA = cos(anAngle);
895   double sinA = sin(anAngle);
896
897   // rotate start dir to find middle point on arc
898   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
899   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
900   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
901
902   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
903 }
904
905 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
906 {
907   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
908   if (aFeature) {
909     const std::string& aFeatureKind = aFeature->getKind();
910     if (aFeatureKind == SketchPlugin_Point::ID())
911       return pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
912     else if (aFeatureKind == SketchPlugin_Line::ID())
913       return middlePointOnLine(aFeature);
914     else if (aFeatureKind == SketchPlugin_Circle::ID())
915       return pointOnCircle(aFeature);
916     else if (aFeatureKind == SketchPlugin_Arc::ID())
917       return middlePointOnArc(aFeature);
918   }
919   // do not move other types of features
920   return std::shared_ptr<GeomAPI_Pnt2d>();
921 }
922
923 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
924                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
925 {
926   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
927   theMovedEntity.fillMessage(aMessage);
928
929   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
930   if (aMessage->movedAttribute())
931     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
932   else
933     anOriginalPosition = middlePoint(aMessage->movedObject());
934
935   if (!anOriginalPosition)
936     return; // something has gone wrong, do not process movement
937
938   aMessage->setOriginalPosition(anOriginalPosition);
939   aMessage->setCurrentPosition(theTargetPoint);
940   Events_Loop::loop()->send(aMessage);
941 }
942
943 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
944                             double theTargetX, double theTargetY)
945 {
946   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
947   move(theMovedEntity, aTargetPoint);
948 }
949
950 //--------------------------------------------------------------------------------------
951
952 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
953 {
954   FeaturePtr aBase = feature();
955   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
956       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
957   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
958       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
959   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
960       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
961   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
962
963   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
964 }
965
966 //--------------------------------------------------------------------------------------
967
968 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
969 {
970   FeaturePtr aBase = feature();
971   const std::string& aDocName = theDumper.name(aBase->document());
972
973   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
974   if (anExternal->value()) {
975     theDumper << aBase << " = model.addSketch(" << aDocName <<
976       ", " << anExternal << ")" << std::endl;
977   } else {
978     // Sketch is base on a plane.
979     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
980         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
981     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
982         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
983     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
984         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
985
986     // Check the plane is coordinate plane
987     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
988     if(anExternal->context()) { // checking for selected planes
989       if (!aPlaneName.empty()
990           && anExternal->context()->data()
991           && anExternal->context()->data()->name() == aPlaneName) {
992         // dump sketch based on coordinate plane
993         theDumper << aBase << " = model.addSketch(" << aDocName
994                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
995       } else { // some other plane
996         theDumper << aBase << " = model.addSketch(" << aDocName <<
997           ", " << anExternal<< ")" << std::endl;
998       }
999     } else {
1000       if (aPlaneName.empty()) {
1001         // needs import additional module
1002         theDumper.importModule("GeomAPI");
1003         // dump plane parameters
1004         const std::string& aSketchName = theDumper.name(aBase);
1005         std::string anOriginName = aSketchName + "_origin";
1006         std::string aNormalName  = aSketchName + "_norm";
1007         std::string aDirXName    = aSketchName + "_dirx";
1008         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1009         // and then dumplicate dumping it in the next line
1010         theDumper << anOriginName << " = " << anOrigin << "\n"
1011                   << aNormalName  << " = " << aNormal  << "\n"
1012                   << aDirXName    << " = " << aDirX    << "\n";
1013         // dump sketch based on arbitrary plane
1014         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1015                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1016       } else {
1017         // dump sketch based on coordinate plane
1018         theDumper << aBase << " = model.addSketch(" << aDocName
1019                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1020       }
1021     }
1022   }
1023
1024   // dump sketch's subfeatures
1025   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1026   theDumper.processSubs(aCompFeat, true);
1027 }