Salome HOME
Merge remote-tracking branch 'origin/ngo/Lot5'
[modules/shaper.git] / src / SketchAPI / SketchAPI_Sketch.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchAPI_Sketch.h"
21 //--------------------------------------------------------------------------------------
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_ConstraintAngle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintCollinear.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
28 #include <SketchPlugin_ConstraintDistanceVertical.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_Fillet.h>
31 #include <SketchPlugin_ConstraintHorizontal.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMiddle.h>
34 #include <SketchPlugin_ConstraintMirror.h>
35 #include <SketchPlugin_ConstraintParallel.h>
36 #include <SketchPlugin_ConstraintPerpendicular.h>
37 #include <SketchPlugin_ConstraintRadius.h>
38 #include <SketchPlugin_ConstraintRigid.h>
39 #include <SketchPlugin_Trim.h>
40 #include <SketchPlugin_Split.h>
41 #include <SketchPlugin_ConstraintTangent.h>
42 #include <SketchPlugin_ConstraintVertical.h>
43 #include <SketcherPrs_Tools.h>
44 //--------------------------------------------------------------------------------------
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_ResultConstruction.h>
48 #include <ModelHighAPI_Double.h>
49 #include <ModelHighAPI_Dumper.h>
50 #include <ModelHighAPI_RefAttr.h>
51 #include <ModelHighAPI_Selection.h>
52 #include <ModelHighAPI_Services.h>
53 #include <ModelHighAPI_Tools.h>
54 //--------------------------------------------------------------------------------------
55 #include "SketchAPI_Arc.h"
56 #include "SketchAPI_MacroArc.h"
57 #include "SketchAPI_Circle.h"
58 #include "SketchAPI_IntersectionPoint.h"
59 #include "SketchAPI_Line.h"
60 #include "SketchAPI_MacroCircle.h"
61 #include "SketchAPI_Mirror.h"
62 #include "SketchAPI_Point.h"
63 #include "SketchAPI_Projection.h"
64 #include "SketchAPI_Rectangle.h"
65 #include "SketchAPI_Rotation.h"
66 #include "SketchAPI_Translation.h"
67 //--------------------------------------------------------------------------------------
68 #include <GeomAPI_Curve.h>
69 #include <GeomAPI_Dir2d.h>
70 #include <GeomAPI_PlanarEdges.h>
71 #include <GeomAPI_ShapeExplorer.h>
72 #include <GeomAPI_XY.h>
73 #include <GeomAlgoAPI_SketchBuilder.h>
74 #include <cmath>
75 //--------------------------------------------------------------------------------------
76 SketchAPI_Sketch::SketchAPI_Sketch(
77     const std::shared_ptr<ModelAPI_Feature> & theFeature)
78 : ModelHighAPI_Interface(theFeature)
79 {
80   initialize();
81 }
82
83 SketchAPI_Sketch::SketchAPI_Sketch(
84     const std::shared_ptr<ModelAPI_Feature> & theFeature,
85     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
86 : ModelHighAPI_Interface(theFeature)
87 {
88   if (initialize()) {
89     setPlane(thePlane);
90   }
91 }
92
93 SketchAPI_Sketch::SketchAPI_Sketch(
94     const std::shared_ptr<ModelAPI_Feature> & theFeature,
95     const ModelHighAPI_Selection & theExternal)
96 : ModelHighAPI_Interface(theFeature)
97 {
98   if (initialize()) {
99     setExternal(theExternal);
100   }
101 }
102
103 SketchAPI_Sketch::SketchAPI_Sketch(
104     const std::shared_ptr<ModelAPI_Feature> & theFeature,
105     std::shared_ptr<ModelAPI_Object> thePlaneObject)
106 : ModelHighAPI_Interface(theFeature)
107 {
108   if (initialize()) {
109     setExternal(thePlaneObject);
110   }
111 }
112
113 SketchAPI_Sketch::~SketchAPI_Sketch()
114 {
115
116 }
117
118 //--------------------------------------------------------------------------------------
119 std::shared_ptr<ModelAPI_CompositeFeature> SketchAPI_Sketch::compositeFeature() const
120 {
121   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
122 }
123
124 //--------------------------------------------------------------------------------------
125 void SketchAPI_Sketch::setPlane(const std::shared_ptr<GeomAPI_Ax3> & thePlane)
126 {
127   fillAttribute(thePlane->origin(), myorigin);
128   fillAttribute(thePlane->dirX(), mydirX);
129   fillAttribute(thePlane->normal(), mynormal);
130
131   execute();
132 }
133
134 void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane,
135                                 bool theRemoveExternalDependency)
136 {
137   FeaturePtr aSketch = feature();
138
139   DocumentPtr aDoc = aSketch->document();
140   bool useVisible = false;
141   FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible);
142   aDoc->setCurrentFeature(aSketch, useVisible);
143
144   if (theRemoveExternalDependency)
145     aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
146
147   setExternal(thePlane);
148
149   aDoc->setCurrentFeature(aCurFeatureBefore, useVisible);
150 }
151
152 //--------------------------------------------------------------------------------------
153 void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal)
154 {
155   fillAttribute(theExternal, myexternal);
156
157   execute();
158 }
159
160 void SketchAPI_Sketch::setExternal(std::shared_ptr<ModelAPI_Object> thePlaneObject)
161 {
162   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePlaneObject);
163   ModelHighAPI_Selection aSel(aRes);
164   setExternal(aSel);
165 }
166
167 //--------------------------------------------------------------------------------------
168 void SketchAPI_Sketch::setValue(
169     const std::shared_ptr<ModelHighAPI_Interface> & theConstraint,
170     const ModelHighAPI_Double & theValue)
171 {
172   fillAttribute(theValue, theConstraint->feature()->real(SketchPlugin_Constraint::VALUE()));
173
174 //  theConstraint->execute();
175 }
176
177 //--------------------------------------------------------------------------------------
178 std::list<ModelHighAPI_Selection> SketchAPI_Sketch::selectFace() const
179 {
180   const_cast<SketchAPI_Sketch*>(this)->execute();
181
182   std::list<ModelHighAPI_Selection> aSelectionList;
183
184   ResultConstructionPtr aResultConstruction =
185       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->firstResult());
186   if (aResultConstruction.get() == NULL)
187     return aSelectionList;
188
189   for (int anIndex = 0; anIndex < aResultConstruction->facesNum(); ++anIndex) {
190     aSelectionList.push_back(
191         ModelHighAPI_Selection(aResultConstruction,
192                                aResultConstruction->face(anIndex)));
193   }
194
195   return aSelectionList;
196 }
197
198 //--------------------------------------------------------------------------------------
199 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
200                     const std::shared_ptr<GeomAPI_Ax3> & thePlane)
201 {
202   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
203   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlane));
204 }
205
206 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
207                     const ModelHighAPI_Selection & theExternal)
208 {
209   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
210   return SketchPtr(new SketchAPI_Sketch(aFeature, theExternal));
211 }
212
213 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
214                     const std::string & theExternalName)
215 {
216   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
217   return SketchPtr(
218     new SketchAPI_Sketch(aFeature, ModelHighAPI_Selection("FACE", theExternalName)));
219 }
220
221 SketchPtr addSketch(const std::shared_ptr<ModelAPI_Document> & thePart,
222                     std::shared_ptr<ModelAPI_Object> thePlaneObject)
223 {
224   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(SketchAPI_Sketch::ID());
225   return SketchPtr(new SketchAPI_Sketch(aFeature, thePlaneObject));
226 }
227
228
229 //--------------------------------------------------------------------------------------
230 std::list< std::shared_ptr<SketchAPI_Point> > SketchAPI_Sketch::getFreePoints()
231 {
232   std::list< std::shared_ptr<SketchAPI_Point> > aFreePoints;
233   std::list<ResultPtr> aPoints = SketcherPrs_Tools::getFreePoints(compositeFeature());
234   for (std::list<ResultPtr>::iterator anIt = aPoints.begin(); anIt != aPoints.end(); ++anIt) {
235     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
236     PointPtr aPoint(new SketchAPI_Point(aFeature));
237     aFreePoints.push_back(aPoint);
238   }
239   return aFreePoints;
240 }
241
242 //--------------------------------------------------------------------------------------
243 static GeomCurvePtr untrimmedCurve(GeomShapePtr theShape)
244 {
245   GeomCurvePtr aCurve(new GeomAPI_Curve(theShape));
246   if (aCurve->isTrimmed())
247     aCurve = aCurve->basisCurve();
248   return aCurve;
249 }
250
251 void SketchAPI_Sketch::changeFacesOrder(
252     const std::list<std::list<ModelHighAPI_Selection> >& theFaces)
253 {
254   // collect faces of the sketch
255   ResultConstructionPtr aSketchResult =
256       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
257   if (!aSketchResult) {
258     // sketch is nested to a boolean operation, thus, it has no result yet.
259     feature()->execute();
260     aSketchResult =
261         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(feature()->lastResult());
262   }
263   std::list<GeomFacePtr> aFaces;
264   int aFacesNum = aSketchResult->facesNum();
265   for (int i = 0; i < aFacesNum; ++i)
266     aFaces.push_back(aSketchResult->face(i));
267   // find new faces order according to the given lists of edges
268   std::list<GeomFacePtr> aNewFacesOrder;
269   std::list<std::list<ModelHighAPI_Selection> >::const_iterator anIt = theFaces.begin();
270   for (; anIt != theFaces.end(); ++anIt) {
271     // find the appropriate face
272     std::list<GeomFacePtr>::iterator aFIt = aFaces.begin();
273     for (; aFIt != aFaces.end(); ++aFIt) {
274       std::list<ModelHighAPI_Selection>::const_iterator aEdgeIt = anIt->begin();
275       GeomAPI_ShapeExplorer aFExp(*aFIt, GeomAPI_Shape::EDGE);
276       for (; aEdgeIt != anIt->end() && aFExp.more(); ++aEdgeIt, aFExp.next()) {
277         ResultPtr aCurRes = aEdgeIt->resultSubShapePair().first;
278         if (!aCurRes)
279           break;
280         GeomCurvePtr aCurve1 = untrimmedCurve(aCurRes->shape());
281         GeomCurvePtr aCurve2 = untrimmedCurve(aFExp.current());
282         if (!aCurve1->isEqual(aCurve2))
283           break;
284       }
285
286       if (aEdgeIt == anIt->end() && !aFExp.more()) {
287         // face is found
288         aNewFacesOrder.push_back(*aFIt);
289         aFaces.erase(aFIt);
290         break;
291       }
292     }
293   }
294   // place the rest faces at the end of new faces list
295   if (!aFaces.empty())
296     aNewFacesOrder.insert(aNewFacesOrder.end(), aFaces.begin(), aFaces.end());
297   // update the result of the sketch with the new order of faces
298   aSketchResult->setFacesOrder(aNewFacesOrder);
299 }
300
301 //--------------------------------------------------------------------------------------
302 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
303     double theX, double theY)
304 {
305   std::shared_ptr<ModelAPI_Feature> aFeature =
306     compositeFeature()->addFeature(SketchPlugin_Point::ID());
307   return PointPtr(new SketchAPI_Point(aFeature, theX, theY));
308 }
309 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(
310     const std::shared_ptr<GeomAPI_Pnt2d> & thePoint)
311 {
312   std::shared_ptr<ModelAPI_Feature> aFeature =
313     compositeFeature()->addFeature(SketchPlugin_Point::ID());
314   return PointPtr(new SketchAPI_Point(aFeature, thePoint));
315 }
316 std::shared_ptr<SketchAPI_Point>
317   SketchAPI_Sketch::addPoint(const ModelHighAPI_Selection & theExternal)
318 {
319   std::shared_ptr<ModelAPI_Feature> aFeature =
320     compositeFeature()->addFeature(SketchPlugin_Point::ID());
321   return PointPtr(new SketchAPI_Point(aFeature, theExternal));
322 }
323 std::shared_ptr<SketchAPI_Point> SketchAPI_Sketch::addPoint(const std::string & theExternalName)
324 {
325   std::shared_ptr<ModelAPI_Feature> aFeature =
326     compositeFeature()->addFeature(SketchPlugin_Point::ID());
327   return PointPtr(new SketchAPI_Point(aFeature, theExternalName));
328 }
329
330 //--------------------------------------------------------------------------------------
331 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
332     const ModelHighAPI_Selection & theExternal,
333     bool theKeepResult)
334 {
335   std::shared_ptr<ModelAPI_Feature> aFeature =
336     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
337   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternal));
338   anIntersection->setIncludeToResult(theKeepResult);
339   return anIntersection;
340 }
341 std::shared_ptr<SketchAPI_IntersectionPoint> SketchAPI_Sketch::addIntersectionPoint(
342     const std::string & theExternalName,
343     bool theKeepResult)
344 {
345   std::shared_ptr<ModelAPI_Feature> aFeature =
346     compositeFeature()->addFeature(SketchPlugin_IntersectionPoint::ID());
347   IntersectionPointPtr anIntersection(new SketchAPI_IntersectionPoint(aFeature, theExternalName));
348   anIntersection->setIncludeToResult(theKeepResult);
349   return anIntersection;
350 }
351
352 //--------------------------------------------------------------------------------------
353 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(double theX1, double theY1,
354                                                           double theX2, double theY2)
355 {
356   std::shared_ptr<ModelAPI_Feature> aFeature =
357     compositeFeature()->addFeature(SketchPlugin_Line::ID());
358   return LinePtr(new SketchAPI_Line(aFeature, theX1, theY1, theX2, theY2));
359 }
360 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(
361     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
362     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
363 {
364   std::shared_ptr<ModelAPI_Feature> aFeature =
365     compositeFeature()->addFeature(SketchPlugin_Line::ID());
366   return LinePtr(new SketchAPI_Line(aFeature, theStartPoint, theEndPoint));
367 }
368 std::shared_ptr<SketchAPI_Line>
369   SketchAPI_Sketch::addLine(const ModelHighAPI_Selection & theExternal)
370 {
371   std::shared_ptr<ModelAPI_Feature> aFeature =
372     compositeFeature()->addFeature(SketchPlugin_Line::ID());
373   return LinePtr(new SketchAPI_Line(aFeature, theExternal));
374 }
375 std::shared_ptr<SketchAPI_Line> SketchAPI_Sketch::addLine(const std::string & theExternalName)
376 {
377   std::shared_ptr<ModelAPI_Feature> aFeature =
378     compositeFeature()->addFeature(SketchPlugin_Line::ID());
379   return LinePtr(new SketchAPI_Line(aFeature, theExternalName));
380 }
381
382 //--------------------------------------------------------------------------------------
383 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(double theX1, double theY1,
384                                                                     double theX2, double theY2)
385 {
386   std::shared_ptr<ModelAPI_Feature> aFeature =
387     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
388   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2));
389 }
390 std::shared_ptr<SketchAPI_Rectangle> SketchAPI_Sketch::addRectangle(
391     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
392     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
393 {
394   std::shared_ptr<ModelAPI_Feature> aFeature =
395     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
396   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
397 }
398
399 //--------------------------------------------------------------------------------------
400 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
401                                                               double theCenterY,
402                                                               double theRadius)
403 {
404   std::shared_ptr<ModelAPI_Feature> aFeature =
405     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
406   return CirclePtr(new SketchAPI_Circle(aFeature, theCenterX, theCenterY, theRadius));
407 }
408
409 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(
410                                     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
411                                     double theRadius)
412 {
413   std::shared_ptr<ModelAPI_Feature> aFeature =
414     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
415   return CirclePtr(new SketchAPI_Circle(aFeature, theCenter, theRadius));
416 }
417
418 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theCenterX,
419                                                                    double theCenterY,
420                                                                    double thePassedX,
421                                                                    double thePassedY)
422 {
423   std::shared_ptr<ModelAPI_Feature> aFeature =
424     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
425   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterX, theCenterY,
426                                                             thePassedX, thePassedY));
427 }
428
429 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
430     const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint,
431     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint)
432 {
433   std::shared_ptr<ModelAPI_Feature> aFeature =
434     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
435   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theCenterPoint, thePassedPoint));
436 }
437
438 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(double theX1, double theY1,
439                                                                    double theX2, double theY2,
440                                                                    double theX3, double theY3)
441 {
442   std::shared_ptr<ModelAPI_Feature> aFeature =
443     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
444   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, theX1, theY1,
445                                                             theX2, theY2,
446                                                             theX3, theY3));
447 }
448
449 std::shared_ptr<SketchAPI_MacroCircle> SketchAPI_Sketch::addCircle(
450     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint1,
451     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
452     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
453 {
454   std::shared_ptr<ModelAPI_Feature> aFeature =
455     compositeFeature()->addFeature(SketchPlugin_MacroCircle::ID());
456   return MacroCirclePtr(new SketchAPI_MacroCircle(aFeature, thePoint1, thePoint2, thePoint3));
457 }
458
459 std::shared_ptr<SketchAPI_Circle>
460   SketchAPI_Sketch::addCircle(const ModelHighAPI_Selection & theExternal)
461 {
462   std::shared_ptr<ModelAPI_Feature> aFeature =
463     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
464   return CirclePtr(new SketchAPI_Circle(aFeature, theExternal));
465 }
466
467 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(const std::string & theExternalName)
468 {
469   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
470   std::shared_ptr<ModelAPI_Feature> aFeature =
471     compositeFeature()->addFeature(SketchPlugin_Circle::ID());
472   return CirclePtr(new SketchAPI_Circle(aFeature, theExternalName));
473 }
474
475 //--------------------------------------------------------------------------------------
476 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(double theCenterX, double theCenterY,
477                                                         double theStartX, double theStartY,
478                                                         double theEndX, double theEndY,
479                                                         bool theInversed)
480 {
481   std::shared_ptr<ModelAPI_Feature> aFeature =
482     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
483   return ArcPtr(new SketchAPI_Arc(aFeature,
484                                   theCenterX, theCenterY,
485                                   theStartX, theStartY,
486                                   theEndX, theEndY,
487                                   theInversed));
488 }
489
490 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(
491                                               const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
492                                               const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
493                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
494                                               bool theInversed)
495 {
496   std::shared_ptr<ModelAPI_Feature> aFeature =
497     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
498   return ArcPtr(new SketchAPI_Arc(aFeature, theCenter, theStart, theEnd, theInversed));
499 }
500
501 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(double theStartX, double theStartY,
502                                                         double theEndX, double theEndY,
503                                                         double thePassedX, double thePassedY)
504 {
505   std::shared_ptr<ModelAPI_Feature> aFeature =
506     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
507   return MacroArcPtr(new SketchAPI_MacroArc(aFeature,
508                                        theStartX, theStartY,
509                                        theEndX, theEndY,
510                                        thePassedX, thePassedY));
511 }
512
513 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
514                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
515                                                 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
516                                                 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
517 {
518   std::shared_ptr<ModelAPI_Feature> aFeature =
519     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
520   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theStart, theEnd, thePassed));
521 }
522
523 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
524                                                 const ModelHighAPI_RefAttr& theTangentPoint,
525                                                 double theEndX, double theEndY,
526                                                 bool theInversed)
527 {
528   std::shared_ptr<ModelAPI_Feature> aFeature =
529     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
530   return MacroArcPtr(new SketchAPI_MacroArc(
531     aFeature, theTangentPoint, theEndX, theEndY, theInversed));
532 }
533
534 std::shared_ptr<SketchAPI_MacroArc> SketchAPI_Sketch::addArc(
535                                               const ModelHighAPI_RefAttr& theTangentPoint,
536                                               const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
537                                               bool theInversed)
538 {
539   std::shared_ptr<ModelAPI_Feature> aFeature =
540     compositeFeature()->addFeature(SketchPlugin_MacroArc::ID());
541   return MacroArcPtr(new SketchAPI_MacroArc(aFeature, theTangentPoint, theEnd, theInversed));
542 }
543
544 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const ModelHighAPI_Selection & theExternal)
545 {
546   std::shared_ptr<ModelAPI_Feature> aFeature =
547     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
548   return ArcPtr(new SketchAPI_Arc(aFeature, theExternal));
549 }
550
551 std::shared_ptr<SketchAPI_Arc> SketchAPI_Sketch::addArc(const std::string & theExternalName)
552 {
553   // TODO(spo): Add constraint SketchConstraintRigid like in PythonAPI. Is it necessary?
554   std::shared_ptr<ModelAPI_Feature> aFeature =
555     compositeFeature()->addFeature(SketchPlugin_Arc::ID());
556   return ArcPtr(new SketchAPI_Arc(aFeature, theExternalName));
557 }
558
559 //--------------------------------------------------------------------------------------
560 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
561     const ModelHighAPI_Selection & theExternalFeature,
562     bool theKeepResult)
563 {
564   std::shared_ptr<ModelAPI_Feature> aFeature =
565     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
566   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalFeature));
567   aProjection->setIncludeToResult(theKeepResult);
568   return aProjection;
569 }
570
571 std::shared_ptr<SketchAPI_Projection> SketchAPI_Sketch::addProjection(
572     const std::string & theExternalName,
573     bool theKeepResult)
574 {
575   std::shared_ptr<ModelAPI_Feature> aFeature =
576     compositeFeature()->addFeature(SketchPlugin_Projection::ID());
577   ProjectionPtr aProjection(new SketchAPI_Projection(aFeature, theExternalName));
578   aProjection->setIncludeToResult(theKeepResult);
579   return aProjection;
580 }
581
582 //--------------------------------------------------------------------------------------
583 std::shared_ptr<SketchAPI_Mirror> SketchAPI_Sketch::addMirror(
584     const ModelHighAPI_RefAttr & theMirrorLine,
585     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
586 {
587   std::shared_ptr<ModelAPI_Feature> aFeature =
588     compositeFeature()->addFeature(SketchPlugin_ConstraintMirror::ID());
589   return MirrorPtr(new SketchAPI_Mirror(aFeature, theMirrorLine, theObjects));
590 }
591
592 //--------------------------------------------------------------------------------------
593 std::shared_ptr<SketchAPI_Translation> SketchAPI_Sketch::addTranslation(
594     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
595     const ModelHighAPI_RefAttr & thePoint1,
596     const ModelHighAPI_RefAttr & thePoint2,
597     const ModelHighAPI_Integer & theNumberOfObjects,
598     bool theFullValue)
599 {
600   std::shared_ptr<ModelAPI_Feature> aFeature =
601     compositeFeature()->addFeature(SketchPlugin_MultiTranslation::ID());
602   return TranslationPtr(new SketchAPI_Translation(aFeature, theObjects, thePoint1,
603                                                   thePoint2, theNumberOfObjects, theFullValue));
604 }
605
606 //--------------------------------------------------------------------------------------
607 std::shared_ptr<SketchAPI_Rotation> SketchAPI_Sketch::addRotation(
608     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
609     const ModelHighAPI_RefAttr & theCenter,
610     const ModelHighAPI_Double & theAngle,
611     const ModelHighAPI_Integer & theNumberOfObjects,
612     bool theFullValue,
613     bool theReversed)
614 {
615   std::shared_ptr<ModelAPI_Feature> aFeature =
616     compositeFeature()->addFeature(SketchPlugin_MultiRotation::ID());
617   return RotationPtr(
618     new SketchAPI_Rotation(aFeature, theObjects, theCenter,
619                            theAngle, theNumberOfObjects, theFullValue, theReversed));
620 }
621
622 //--------------------------------------------------------------------------------------
623 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addSplit(
624                                           const ModelHighAPI_Reference& theFeature,
625                                           const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
626 {
627   std::shared_ptr<ModelAPI_Feature> aFeature =
628     compositeFeature()->addFeature(SketchPlugin_Split::ID());
629   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Split::SELECTED_OBJECT()));
630
631   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Split::SELECTED_POINT());
632   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
633     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
634     fillAttribute(thePositionPoint, aPointAttr);
635   }
636
637   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
638 }
639
640 //--------------------------------------------------------------------------------------
641 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::addTrim(
642                                         const ModelHighAPI_Reference& theFeature,
643                                         const std::shared_ptr<GeomAPI_Pnt2d>& thePositionPoint)
644 {
645   std::shared_ptr<ModelAPI_Feature> aFeature =
646     compositeFeature()->addFeature(SketchPlugin_Trim::ID());
647   fillAttribute(theFeature, aFeature->reference(SketchPlugin_Trim::SELECTED_OBJECT()));
648
649   AttributePtr anAttribute = aFeature->attribute(SketchPlugin_Trim::SELECTED_POINT());
650   if (anAttribute->attributeType() == GeomDataAPI_Point2D::typeId()) {
651     AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
652     fillAttribute(thePositionPoint, aPointAttr);
653   }
654
655   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
656 }
657
658 //--------------------------------------------------------------------------------------
659 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngle(
660     const ModelHighAPI_RefAttr & theLine1,
661     const ModelHighAPI_RefAttr & theLine2,
662     const ModelHighAPI_Double & theValue)
663 {
664   std::shared_ptr<ModelAPI_Feature> aFeature =
665       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
666   fillAttribute(SketcherPrs_Tools::ANGLE_DIRECT,
667       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
668   // fill the value before llines to avoid calculation of angle value by the Angle feature
669   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
670   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
671   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
672   aFeature->execute();
673   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
674 }
675
676 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleComplementary(
677     const ModelHighAPI_RefAttr & theLine1,
678     const ModelHighAPI_RefAttr & theLine2,
679     const ModelHighAPI_Double & theValue)
680 {
681   std::shared_ptr<ModelAPI_Feature> aFeature =
682       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
683   fillAttribute(SketcherPrs_Tools::ANGLE_COMPLEMENTARY,
684       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
685   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
686   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
687   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
688   aFeature->execute();
689   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
690 }
691
692 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setAngleBackward(
693     const ModelHighAPI_RefAttr & theLine1,
694     const ModelHighAPI_RefAttr & theLine2,
695     const ModelHighAPI_Double & theValue)
696 {
697   std::shared_ptr<ModelAPI_Feature> aFeature =
698       compositeFeature()->addFeature(SketchPlugin_ConstraintAngle::ID());
699   fillAttribute(SketcherPrs_Tools::ANGLE_BACKWARD,
700       aFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID()));
701   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
702   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
703   fillAttribute(theValue, aFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
704   aFeature->execute();
705   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
706 }
707
708 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCoincident(
709     const ModelHighAPI_RefAttr & thePoint1,
710     const ModelHighAPI_RefAttr & thePoint2)
711 {
712   std::shared_ptr<ModelAPI_Feature> aFeature =
713       compositeFeature()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
714   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
715   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
716   aFeature->execute();
717   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
718 }
719
720 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setCollinear(
721     const ModelHighAPI_RefAttr & theLine1,
722     const ModelHighAPI_RefAttr & theLine2)
723 {
724   std::shared_ptr<ModelAPI_Feature> aFeature =
725       compositeFeature()->addFeature(SketchPlugin_ConstraintCollinear::ID());
726   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
727   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
728   aFeature->execute();
729   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
730 }
731
732 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setDistance(
733     const ModelHighAPI_RefAttr & thePoint,
734     const ModelHighAPI_RefAttr & thePointOrLine,
735     const ModelHighAPI_Double & theValue,
736     bool isSigned)
737 {
738   std::shared_ptr<ModelAPI_Feature> aFeature =
739       compositeFeature()->addFeature(SketchPlugin_ConstraintDistance::ID());
740   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
741   fillAttribute(thePointOrLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
742   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
743   fillAttribute(isSigned, aFeature->boolean(SketchPlugin_ConstraintDistance::SIGNED()));
744   aFeature->execute();
745   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
746 }
747
748 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setSignedDistance(
749     const ModelHighAPI_RefAttr & thePoint,
750     const ModelHighAPI_RefAttr & thePointOrLine,
751     const ModelHighAPI_Double & theValue)
752 {
753   return setDistance(thePoint, thePointOrLine, theValue, true);
754 }
755
756 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setUnsignedDistance(
757     const ModelHighAPI_RefAttr & thePoint,
758     const ModelHighAPI_RefAttr & thePointOrLine,
759     const ModelHighAPI_Double & theValue)
760 {
761   return setDistance(thePoint, thePointOrLine, theValue, false);
762 }
763
764 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontalDistance(
765     const ModelHighAPI_RefAttr & thePoint1,
766     const ModelHighAPI_RefAttr & thePoint2,
767     const ModelHighAPI_Double & theValue)
768 {
769   std::shared_ptr<ModelAPI_Feature> aFeature =
770       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceHorizontal::ID());
771   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
772   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
773   fillAttribute(theValue,
774       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
775   aFeature->execute();
776   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
777 }
778
779 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVerticalDistance(
780     const ModelHighAPI_RefAttr & thePoint1,
781     const ModelHighAPI_RefAttr & thePoint2,
782     const ModelHighAPI_Double & theValue)
783 {
784   std::shared_ptr<ModelAPI_Feature> aFeature =
785       compositeFeature()->addFeature(SketchPlugin_ConstraintDistanceVertical::ID());
786   fillAttribute(thePoint1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
787   fillAttribute(thePoint2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
788   fillAttribute(theValue,
789       aFeature->real(SketchPlugin_ConstraintDistanceAlongDir::DISTANCE_VALUE_ID()));
790   aFeature->execute();
791   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
792 }
793
794 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setEqual(
795     const ModelHighAPI_RefAttr & theObject1,
796     const ModelHighAPI_RefAttr & theObject2)
797 {
798   std::shared_ptr<ModelAPI_Feature> aFeature =
799       compositeFeature()->addFeature(SketchPlugin_ConstraintEqual::ID());
800   fillAttribute(theObject1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
801   fillAttribute(theObject2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
802   aFeature->execute();
803   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
804 }
805
806 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFillet(
807     const ModelHighAPI_RefAttr & thePoint)
808 {
809   std::shared_ptr<ModelAPI_Feature> aFeature =
810       compositeFeature()->addFeature(SketchPlugin_Fillet::ID());
811   fillAttribute(thePoint, aFeature->data()->refattr(SketchPlugin_Fillet::FILLET_POINT_ID()));
812   apply(); // finish operation to remove Fillet feature correcly
813   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
814 }
815
816 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFilletWithRadius(
817     const ModelHighAPI_RefAttr & thePoint,
818     const ModelHighAPI_Double & theRadius)
819 {
820   CompositeFeaturePtr aSketch = compositeFeature();
821   int aNbSubs = aSketch->numberOfSubs();
822
823   // create fillet
824   InterfacePtr aFilletFeature = setFillet(thePoint);
825
826   // set radius for just created arc
827   FeaturePtr anArc = aSketch->subFeature(aNbSubs - 1);
828   if (anArc->getKind() == SketchPlugin_Arc::ID())
829     setRadius(ModelHighAPI_RefAttr(ObjectPtr(anArc->lastResult())), ModelHighAPI_Double(theRadius));
830
831   return aFilletFeature;
832 }
833
834 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setFixed(
835     const ModelHighAPI_RefAttr & theObject)
836 {
837   std::shared_ptr<ModelAPI_Feature> aFeature =
838       compositeFeature()->addFeature(SketchPlugin_ConstraintRigid::ID());
839   fillAttribute(theObject, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
840   aFeature->execute();
841   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
842 }
843
844 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setHorizontal(
845     const ModelHighAPI_RefAttr & theLine)
846 {
847   std::shared_ptr<ModelAPI_Feature> aFeature =
848       compositeFeature()->addFeature(SketchPlugin_ConstraintHorizontal::ID());
849   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
850   aFeature->execute();
851   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
852 }
853
854 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setLength(
855     const ModelHighAPI_RefAttr & theLine,
856     const ModelHighAPI_Double & theValue)
857 {
858   std::shared_ptr<ModelAPI_Feature> aFeature =
859       compositeFeature()->addFeature(SketchPlugin_ConstraintLength::ID());
860   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
861   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
862   aFeature->execute();
863   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
864 }
865
866 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setMiddlePoint(
867     const ModelHighAPI_RefAttr & thePoint,
868     const ModelHighAPI_RefAttr & theLine)
869 {
870   std::shared_ptr<ModelAPI_Feature> aFeature =
871       compositeFeature()->addFeature(SketchPlugin_ConstraintMiddle::ID());
872   fillAttribute(thePoint, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
873   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
874   aFeature->execute();
875   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
876 }
877
878 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setParallel(
879     const ModelHighAPI_RefAttr & theLine1,
880     const ModelHighAPI_RefAttr & theLine2)
881 {
882   std::shared_ptr<ModelAPI_Feature> aFeature =
883       compositeFeature()->addFeature(SketchPlugin_ConstraintParallel::ID());
884   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
885   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
886   aFeature->execute();
887   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
888 }
889
890 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setPerpendicular(
891     const ModelHighAPI_RefAttr & theLine1,
892     const ModelHighAPI_RefAttr & theLine2)
893 {
894   std::shared_ptr<ModelAPI_Feature> aFeature =
895       compositeFeature()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
896   fillAttribute(theLine1, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
897   fillAttribute(theLine2, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
898   aFeature->execute();
899   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
900 }
901
902 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setRadius(
903     const ModelHighAPI_RefAttr & theCircleOrArc,
904     const ModelHighAPI_Double & theValue)
905 {
906   std::shared_ptr<ModelAPI_Feature> aFeature =
907       compositeFeature()->addFeature(SketchPlugin_ConstraintRadius::ID());
908   fillAttribute(theCircleOrArc, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
909   fillAttribute(theValue, aFeature->real(SketchPlugin_Constraint::VALUE()));
910   aFeature->execute();
911   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
912 }
913
914 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setTangent(
915     const ModelHighAPI_RefAttr & theLine,
916     const ModelHighAPI_RefAttr & theCircle)
917 {
918   std::shared_ptr<ModelAPI_Feature> aFeature =
919       compositeFeature()->addFeature(SketchPlugin_ConstraintTangent::ID());
920   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
921   fillAttribute(theCircle, aFeature->refattr(SketchPlugin_Constraint::ENTITY_B()));
922   aFeature->execute();
923   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
924 }
925
926 std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
927     const ModelHighAPI_RefAttr & theLine)
928 {
929   std::shared_ptr<ModelAPI_Feature> aFeature =
930       compositeFeature()->addFeature(SketchPlugin_ConstraintVertical::ID());
931   fillAttribute(theLine, aFeature->refattr(SketchPlugin_Constraint::ENTITY_A()));
932   aFeature->execute();
933   return InterfacePtr(new ModelHighAPI_Interface(aFeature));
934 }
935
936 //--------------------------------------------------------------------------------------
937
938 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
939 {
940   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
941   return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
942 }
943
944 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
945 {
946   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
947       theFeature->attribute(SketchPlugin_Line::START_ID()));
948   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
949       theFeature->attribute(SketchPlugin_Line::END_ID()));
950
951   if (!aStartAttr || !aEndAttr)
952     return std::shared_ptr<GeomAPI_Pnt2d>();
953
954   std::shared_ptr<GeomAPI_XY> aStartPoint = aStartAttr->pnt()->xy();
955   std::shared_ptr<GeomAPI_XY> aEndPoint = aEndAttr->pnt()->xy();
956   return std::shared_ptr<GeomAPI_Pnt2d>(
957       new GeomAPI_Pnt2d(aStartPoint->added(aEndPoint)->multiplied(0.5)));
958 }
959
960 static std::shared_ptr<GeomAPI_Pnt2d> pointOnCircle(const FeaturePtr& theFeature)
961 {
962   AttributePoint2DPtr aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
963       theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
964   AttributeDoublePtr aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID());
965
966   if (!aCenter || !aRadius)
967     return std::shared_ptr<GeomAPI_Pnt2d>();
968
969   return std::shared_ptr<GeomAPI_Pnt2d>(
970       new GeomAPI_Pnt2d(aCenter->x() + aRadius->value(), aCenter->y()));
971 }
972
973 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeature)
974 {
975   static const double PI = 3.141592653589793238463;
976
977   AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
978       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
979   AttributePoint2DPtr aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
980       theFeature->attribute(SketchPlugin_Arc::START_ID()));
981   AttributePoint2DPtr aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
982       theFeature->attribute(SketchPlugin_Arc::END_ID()));
983
984   if (!aCenterAttr || !aStartAttr || !aEndAttr)
985     return std::shared_ptr<GeomAPI_Pnt2d>();
986
987   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
988       aStartAttr->x() - aCenterAttr->x(), aStartAttr->y() - aCenterAttr->y()));
989   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
990       aEndAttr->x() - aCenterAttr->x(), aEndAttr->y() - aCenterAttr->y()));
991
992   double anAngle = aStartDir->angle(aEndDir);
993   bool isReversed = theFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
994   if (isReversed && anAngle > 0.)
995     anAngle -= 2.0 * PI;
996   else if (!isReversed && anAngle <= 0.)
997     anAngle += 2.0 * PI;
998
999   double cosA = cos(anAngle);
1000   double sinA = sin(anAngle);
1001
1002   // rotate start dir to find middle point on arc
1003   double aRadius = aStartAttr->pnt()->distance(aCenterAttr->pnt());
1004   double x = aCenterAttr->x() + aRadius * (aStartDir->x() * cosA - aStartDir->y() * sinA);
1005   double y = aCenterAttr->y() + aRadius * (aStartDir->x() * sinA + aStartDir->y() * cosA);
1006
1007   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(x, y));
1008 }
1009
1010 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
1011 {
1012   std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
1013   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1014   if (aFeature) {
1015     // move only features of the following types
1016     const std::string& aFeatureKind = aFeature->getKind();
1017     if (aFeatureKind == SketchPlugin_Point::ID())
1018       aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
1019     else if (aFeatureKind == SketchPlugin_Line::ID())
1020       aMiddlePoint = middlePointOnLine(aFeature);
1021     else if (aFeatureKind == SketchPlugin_Circle::ID())
1022       aMiddlePoint = pointOnCircle(aFeature);
1023     else if (aFeatureKind == SketchPlugin_Arc::ID())
1024       aMiddlePoint = middlePointOnArc(aFeature);
1025   }
1026   return aMiddlePoint;
1027 }
1028
1029 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1030                             const std::shared_ptr<GeomAPI_Pnt2d>& theTargetPoint)
1031 {
1032   std::shared_ptr<ModelAPI_ObjectMovedMessage> aMessage(new ModelAPI_ObjectMovedMessage);
1033   theMovedEntity.fillMessage(aMessage);
1034
1035   std::shared_ptr<GeomAPI_Pnt2d> anOriginalPosition;
1036   if (aMessage->movedAttribute())
1037     anOriginalPosition = pointCoordinates(aMessage->movedAttribute());
1038   else
1039     anOriginalPosition = middlePoint(aMessage->movedObject());
1040
1041   if (!anOriginalPosition)
1042     return; // something has gone wrong, do not process movement
1043
1044   aMessage->setOriginalPosition(anOriginalPosition);
1045   aMessage->setCurrentPosition(theTargetPoint);
1046   Events_Loop::loop()->send(aMessage);
1047 }
1048
1049 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
1050                             double theTargetX, double theTargetY)
1051 {
1052   std::shared_ptr<GeomAPI_Pnt2d> aTargetPoint(new GeomAPI_Pnt2d(theTargetX, theTargetY));
1053   move(theMovedEntity, aTargetPoint);
1054 }
1055
1056 //--------------------------------------------------------------------------------------
1057
1058 std::shared_ptr<GeomAPI_Pnt2d> SketchAPI_Sketch::to2D(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
1059 {
1060   FeaturePtr aBase = feature();
1061   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1062       aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1063   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1064       aBase->attribute(SketchPlugin_Sketch::NORM_ID()));
1065   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1066       aBase->attribute(SketchPlugin_Sketch::DIRX_ID()));
1067   std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1068
1069   return thePoint->to2D(aC->pnt(), aX->dir(), aY);
1070 }
1071
1072 //--------------------------------------------------------------------------------------
1073
1074 static bool isDifferent(GeomFacePtr theFace1, GeomFacePtr theFace2)
1075 {
1076   // collect edges of the first face
1077   std::list<GeomShapePtr> anEdges1;
1078   for (GeomAPI_ShapeExplorer anExp(theFace1, GeomAPI_Shape::EDGE); anExp.more(); anExp.next())
1079     anEdges1.push_back(anExp.current());
1080   // compare edges of faces
1081   for (GeomAPI_ShapeExplorer anExp(theFace2, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
1082     GeomShapePtr aCurrent = anExp.current();
1083     bool isFound = false;
1084     std::list<GeomShapePtr>::iterator anIt1 = anEdges1.begin();
1085     for (; anIt1 != anEdges1.end(); ++anIt1)
1086       if (aCurrent->isSameGeometry(*anIt1)) {
1087         isFound = true;
1088         anEdges1.erase(anIt1);
1089         break;
1090       }
1091     if (!isFound)
1092       return true;
1093   }
1094   return !anEdges1.empty();
1095 }
1096
1097 static bool isCustomFacesOrder(CompositeFeaturePtr theSketch)
1098 {
1099   ResultConstructionPtr aSketchResult =
1100       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1101   if (!aSketchResult)
1102     return false;
1103
1104   std::shared_ptr<GeomAPI_PlanarEdges> aWires =
1105       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aSketchResult->shape());
1106   if (!aWires)
1107     return false;
1108
1109   // collect faces constructed by SketchBuilder algorithm
1110   GeomAlgoAPI_SketchBuilder aSketchBuilder(aWires->origin(), aWires->dirX(),
1111                                            aWires->norm(), aWires);
1112   const ListOfShape& aFaces = aSketchBuilder.faces();
1113
1114   // compare faces stored in sketch with faces generated by SketchBuilder
1115   int aNbSketchFaces = aSketchResult->facesNum();
1116   int aFaceIndex = 0;
1117   for (ListOfShape::const_iterator aFIt = aFaces.begin();
1118        aFIt != aFaces.end() && aFaceIndex < aNbSketchFaces;
1119        ++aFIt, ++aFaceIndex) {
1120     GeomFacePtr aSketchFace = aSketchResult->face(aFaceIndex);
1121     GeomFacePtr aCurFace = (*aFIt)->face();
1122     if (isDifferent(aSketchFace, aCurFace))
1123       return true;
1124   }
1125   return false;
1126 }
1127
1128 static void edgesOfSketchFaces(CompositeFeaturePtr theSketch,
1129                                std::list<std::list<ResultPtr> >& theEdges)
1130 {
1131   ResultConstructionPtr aSketchResult =
1132       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theSketch->lastResult());
1133   if (!aSketchResult)
1134     return;
1135
1136   // collect curves of the sketch
1137   std::map<GeomCurvePtr, ResultPtr, GeomAPI_Curve::Comparator> aCurves;
1138   int aSubNum = theSketch->numberOfSubs();
1139   for (int a = 0; a < aSubNum; ++a) {
1140     FeaturePtr aSub = theSketch->subFeature(a);
1141     const std::list<ResultPtr>& aResults = aSub->results();
1142     std::list<ResultPtr>::const_iterator aRes = aResults.cbegin();
1143     for (; aRes != aResults.cend(); aRes++) {
1144       GeomShapePtr aCurShape = (*aRes)->shape();
1145       if (aCurShape && aCurShape->isEdge())
1146         aCurves[untrimmedCurve(aCurShape)] = *aRes;
1147     }
1148   }
1149
1150   // convert each face to the list of results of its edges
1151   int aFacesNum = aSketchResult->facesNum();
1152   for (int a = 0; a < aFacesNum; ++a) {
1153     theEdges.push_back(std::list<ResultPtr>());
1154     std::list<ResultPtr>& aCurEdges = theEdges.back();
1155
1156     GeomFacePtr aFace = aSketchResult->face(a);
1157     for (GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE);
1158          anExp.more(); anExp.next()) {
1159       GeomCurvePtr aCurrent = untrimmedCurve(anExp.current());
1160       aCurEdges.push_back(aCurves[aCurrent]);
1161     }
1162   }
1163 }
1164
1165 //--------------------------------------------------------------------------------------
1166
1167 void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
1168 {
1169   FeaturePtr aBase = feature();
1170   const std::string& aDocName = theDumper.name(aBase->document());
1171
1172   AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
1173   if (anExternal->value()) {
1174     theDumper << aBase << " = model.addSketch(" << aDocName <<
1175       ", " << anExternal << ")" << std::endl;
1176   } else {
1177     // Sketch is base on a plane.
1178     std::shared_ptr<GeomAPI_Pnt> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1179         aBase->attribute(SketchPlugin_Sketch::ORIGIN_ID()))->pnt();
1180     std::shared_ptr<GeomAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1181         aBase->attribute(SketchPlugin_Sketch::NORM_ID()))->dir();
1182     std::shared_ptr<GeomAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1183         aBase->attribute(SketchPlugin_Sketch::DIRX_ID()))->dir();
1184
1185     // Check the plane is coordinate plane
1186     std::string aPlaneName = defaultPlane(anOrigin, aNormal, aDirX);
1187     if(anExternal->context()) { // checking for selected planes
1188       if (!aPlaneName.empty()
1189           && anExternal->context()->data()
1190           && anExternal->context()->data()->name() == aPlaneName) {
1191         // dump sketch based on coordinate plane
1192         theDumper << aBase << " = model.addSketch(" << aDocName
1193                   << ", model.standardPlane(\"" << aPlaneName << "\"))" << std::endl;
1194       } else { // some other plane
1195         theDumper << aBase << " = model.addSketch(" << aDocName <<
1196           ", " << anExternal<< ")" << std::endl;
1197       }
1198     } else {
1199       if (aPlaneName.empty()) {
1200         // needs import additional module
1201         theDumper.importModule("GeomAPI");
1202         // dump plane parameters
1203         const std::string& aSketchName = theDumper.name(aBase);
1204         std::string anOriginName = aSketchName + "_origin";
1205         std::string aNormalName  = aSketchName + "_norm";
1206         std::string aDirXName    = aSketchName + "_dirx";
1207         // use "\n" instead of std::endl to avoid automatic dumping sketch here
1208         // and then dumplicate dumping it in the next line
1209         theDumper << anOriginName << " = " << anOrigin << "\n"
1210                   << aNormalName  << " = " << aNormal  << "\n"
1211                   << aDirXName    << " = " << aDirX    << "\n";
1212         // dump sketch based on arbitrary plane
1213         theDumper << aBase << " = model.addSketch(" << aDocName << ", GeomAPI_Ax3("
1214                   << anOriginName << ", " << aDirXName << ", " << aNormalName << "))" << std::endl;
1215       } else {
1216         // dump sketch based on coordinate plane
1217         theDumper << aBase << " = model.addSketch(" << aDocName
1218                   << ", model.defaultPlane(\"" << aPlaneName << "\"))" << std::endl;
1219       }
1220     }
1221   }
1222
1223   // dump sketch's subfeatures
1224   CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
1225   theDumper.processSubs(aCompFeat, true);
1226
1227   // if face order differs to the order generated by SketchBuilder,
1228   // dump the list of faces for correct execution of the script
1229   if (isCustomFacesOrder(aCompFeat)) {
1230     std::list<std::list<ResultPtr> > aFaces;
1231     edgesOfSketchFaces(aCompFeat, aFaces);
1232
1233     const std::string& aSketchName = theDumper.name(aBase);
1234     std::string aMethodName(".changeFacesOrder");
1235     std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' ');
1236
1237     theDumper << aSketchName << aMethodName << "([";
1238     for (std::list<std::list<ResultPtr> >::iterator aFIt = aFaces.begin();
1239          aFIt != aFaces.end(); ++aFIt) {
1240       if (aFIt != aFaces.begin())
1241         theDumper << ",\n" << aSpaceShift << "  ";
1242       theDumper << *aFIt;
1243     }
1244     theDumper << "\n" << aSpaceShift << " ])" << std::endl;
1245   }
1246 }