]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MacroArc.cpp
Salome HOME
Issue #2083: Creation of sketch arc by center have unexpected behavior
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroArc.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_MacroArc.cpp
4 // Created:     26 Apr 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_MacroArc.h"
8
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_ConstraintTangent.h"
11 #include "SketchPlugin_Sketch.h"
12 #include "SketchPlugin_Tools.h"
13 #include "SketchPlugin_MacroArcReentrantMessage.h"
14
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <GeomAPI_Circ.h>
23 #include <GeomAPI_Circ2d.h>
24 #include <GeomAPI_Curve.h>
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_Lin2d.h>
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_Vertex.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomAPI_ShapeIterator.h>
33
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomDataAPI_Dir.h>
36
37 #include <GeomAlgoAPI_Circ2dBuilder.h>
38 #include <GeomAlgoAPI_EdgeBuilder.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_PointBuilder.h>
41
42 // for sqrt on Linux
43 #include <math.h>
44
45 const double tolerance = 1e-7;
46 const double paramTolerance = 1.e-4;
47 const double PI = 3.141592653589793238463;
48
49 static void projectPointOnCircle(AttributePoint2DPtr& thePoint, const GeomAPI_Circ2d& theCircle)
50 {
51   std::shared_ptr<GeomAPI_Pnt2d> aProjection = theCircle.project(thePoint->pnt());
52   if(aProjection.get())
53     thePoint->setValue(aProjection);
54 }
55
56 static void intersectShapeAndCircle(const GeomShapePtr& theShape,
57                                     const GeomAPI_Circ2d& theCircle,
58                                     const SketchPlugin_Sketch* theSketch,
59                                     AttributePoint2DPtr& theIntersection)
60 {
61   if (!theShape->isEdge())
62     return projectPointOnCircle(theIntersection, theCircle);
63
64   // convert shape to unbounded
65   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(theShape));
66   if (anEdge->isLine()) {
67     static const double HALF_SIZE = 1.e6;
68     std::shared_ptr<GeomAPI_XYZ> aLoc = anEdge->line()->location()->xyz();
69     std::shared_ptr<GeomAPI_XYZ> aDir = anEdge->line()->direction()->xyz();
70
71     std::shared_ptr<GeomAPI_Pnt> aStart(
72         new GeomAPI_Pnt(aLoc->added(aDir->multiplied(-HALF_SIZE))));
73     std::shared_ptr<GeomAPI_Pnt> aEnd(
74         new GeomAPI_Pnt(aLoc->added(aDir->multiplied(HALF_SIZE))));
75     anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, aEnd);
76   } else if (anEdge->isArc()) {
77     std::shared_ptr<GeomAPI_Circ> aCircle = anEdge->circle();
78     anEdge = GeomAlgoAPI_EdgeBuilder::lineCircle(
79         aCircle->center(), aCircle->normal(), aCircle->radius());
80   }
81
82   // convert 2D circle to 3D object
83   std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = theCircle.center();
84   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aCenter2d->x(), aCenter2d->y()));
85   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86       const_cast<SketchPlugin_Sketch*>(theSketch)->attribute(SketchPlugin_Sketch::NORM_ID()));
87   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
88
89   GeomShapePtr aCircleShape =
90       GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, theCircle.radius());
91
92   GeomShapePtr anInter = anEdge->intersect(aCircleShape);
93   std::shared_ptr<GeomAPI_Pnt2d> anInterPnt;
94   if (!anInter)
95     return projectPointOnCircle(theIntersection, theCircle);
96   if (anInter->isVertex()) {
97     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
98     anInterPnt = theSketch->to2D(aVertex->point());
99   } else if (anInter->isCompound()) {
100     double aMinDist = 1.e300;
101
102     GeomAPI_ShapeIterator anIt(anInter);
103     for (; anIt.more(); anIt.next()) {
104       GeomShapePtr aCurrent = anIt.current();
105       if (!aCurrent->isVertex())
106         continue;
107       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aCurrent));
108       std::shared_ptr<GeomAPI_Pnt2d> aPnt = theSketch->to2D(aVertex->point());
109       double aDist = aPnt->distance(theIntersection->pnt());
110       if (aDist < aMinDist) {
111         aMinDist = aDist;
112         anInterPnt = aPnt;
113       }
114     }
115   }
116   theIntersection->setValue(anInterPnt);
117 }
118
119
120 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
121 : SketchPlugin_SketchEntity(),
122   myParamBefore(0.0)
123 {
124 }
125
126 void SketchPlugin_MacroArc::initAttributes()
127 {
128   data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
129
130   data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
131   data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
132   data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
133
134   data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
135   data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
136   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
137
138   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
139   data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
140
141   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
142
143   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
144   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
145
146   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
147
148   data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
149   data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
150   data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
151   data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
152
153   data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
154
155   boolean(REVERSED_ID())->setValue(false);
156   string(EDIT_ARC_TYPE_ID())->setValue("");
157
158   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
159   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
160   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
161   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
162   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
163 }
164
165 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
166 {
167   std::string anArcType = string(ARC_TYPE())->value();
168
169   // If arc type switched reset according attributes.
170   if(theID == ARC_TYPE()) {
171     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
172     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
173     SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
174     SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
175     SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
176     SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
177     SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
178     SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
179     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
180     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
181     SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
182     SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
183     SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
184     SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
185     SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
186
187     myCenter.reset();
188     myStart.reset();
189     myEnd.reset();
190     boolean(REVERSED_ID())->setValue(false);
191     myParamBefore = 0.0;
192   } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
193     fillByCenterAndTwoPassed();
194   else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
195     fillByThreePassedPoints();
196   else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
197     fillByTangentEdge();
198
199   double aRadius = 0;
200   double anAngle = 0;
201   if(myCenter.get() && myStart.get()) {
202     aRadius = myCenter->distance(myStart);
203     if(myEnd.get()) {
204       if(myStart->isEqual(myEnd)) {
205         anAngle = 360;
206       } else {
207         GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
208         double aStartParam, anEndParam;
209         aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
210         aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
211         anAngle = (anEndParam - aStartParam) / PI * 180.0;
212         if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
213       }
214     }
215   }
216
217   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
218   real(RADIUS_ID())->setValue(aRadius);
219   real(ANGLE_ID())->setValue(anAngle);
220   data()->blockSendAttributeUpdated(aWasBlocked, false);
221 }
222
223 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
224 {
225   if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
226     return GeomShapePtr();
227   }
228
229   SketchPlugin_Sketch* aSketch = sketch();
230   if(!aSketch) {
231     return GeomShapePtr();
232   }
233
234   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
235   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
236   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
237   std::shared_ptr<GeomDataAPI_Dir> aNDir =
238     std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
239   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
240
241   GeomShapePtr anArcShape;
242   if (isBound) {
243     anArcShape = boolean(REVERSED_ID())->value() ?
244         GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
245       : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
246   } else {
247     double aRadius = aCenter->distance(aStart);
248     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
249   }
250
251   return anArcShape;
252 }
253
254 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
255 {
256   if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
257     return AISObjectPtr();
258   }
259
260   SketchPlugin_Sketch* aSketch = sketch();
261   if(!aSketch) {
262     return AISObjectPtr();
263   }
264
265   GeomShapePtr anArcShape = getArcShape();
266   std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
267   GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
268
269   if(!anArcShape.get() || !aCenterPointShape.get()) {
270     return AISObjectPtr();
271   }
272
273   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
274   aShapes.push_back(anArcShape);
275   aShapes.push_back(aCenterPointShape);
276
277   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
278   AISObjectPtr anAIS = thePrevious;
279   if(!anAIS.get()) {
280     anAIS.reset(new GeomAPI_AISObject());
281   }
282   anAIS->createShape(aCompound);
283   return anAIS;
284 }
285
286 void SketchPlugin_MacroArc::execute()
287 {
288   FeaturePtr anArcFeature = createArcFeature();
289
290   myCenter.reset();
291   myStart.reset();
292   myEnd.reset();
293
294   // Create constraints.
295   std::string anArcType = string(ARC_TYPE())->value();
296   if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
297     SketchPlugin_Tools::createConstraint(this,
298                                          CENTER_POINT_REF_ID(),
299                                          anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
300                                          ObjectPtr(),
301                                          false);
302     SketchPlugin_Tools::createConstraint(this,
303                                          START_POINT_REF_ID(),
304                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
305                                          ObjectPtr(),
306                                          false);
307     SketchPlugin_Tools::createConstraint(this,
308                                          END_POINT_REF_ID(),
309                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
310                                          ObjectPtr(),
311                                          false);
312   } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
313     SketchPlugin_Tools::createConstraint(this,
314                                          START_POINT_REF_ID(),
315                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
316                                          ObjectPtr(),
317                                          false);
318     SketchPlugin_Tools::createConstraint(this,
319                                          END_POINT_REF_ID(),
320                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
321                                          ObjectPtr(),
322                                          false);
323     SketchPlugin_Tools::createConstraint(this,
324                                          PASSED_POINT_REF_ID(),
325                                          AttributePtr(),
326                                          anArcFeature->lastResult(),
327                                          true);
328   } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
329     // constraints for tangent arc
330     SketchPlugin_Tools::createConstraint(this,
331                                          TANGENT_POINT_ID(),
332                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
333                                          ObjectPtr(),
334                                          false);
335     FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
336     AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
337     AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
338     FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
339     aRefAttrA->setObject(aTgFeature->lastResult());
340     AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
341     aRefAttrB->setObject(anArcFeature->lastResult());
342     // constraint for end point
343     SketchPlugin_Tools::createConstraint(this,
344                                          END_POINT_REF_ID(),
345                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
346                                          ObjectPtr(),
347                                          false);
348   }
349
350   // message to init reentrant operation
351   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
352   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
353     <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, 0));
354
355   std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
356   aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
357   aMessage->setCreatedFeature(anArcFeature);
358   Events_Loop::loop()->send(aMessage);
359 }
360
361 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
362 {
363   std::string aFilledAttributeName;
364   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
365         std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
366   if (aReentrantMessage.get()) {
367     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
368     std::string anArcType = aReentrantMessage->typeOfCreation();
369
370     string(ARC_TYPE())->setValue(anArcType);
371
372     aFilledAttributeName = ARC_TYPE();
373     if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
374       aFilledAttributeName = TANGENT_POINT_ID();
375       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
376                                                         attribute(aFilledAttributeName));
377       FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
378       aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
379     }
380     else {
381       ObjectPtr anObject = aReentrantMessage->selectedObject();
382       AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
383       std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
384
385       if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
386         if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
387             anArcType == ARC_TYPE_BY_THREE_POINTS()) {
388           std::string aReferenceAttributeName;
389           if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
390             aFilledAttributeName = CENTER_POINT_ID();
391             aReferenceAttributeName = CENTER_POINT_REF_ID();
392           }
393           else {
394             aFilledAttributeName = START_POINT_2_ID();
395             aReferenceAttributeName = START_POINT_REF_ID();
396           }
397           // fill 2d point attribute
398           AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
399                                                             attribute(aFilledAttributeName));
400           aPointAttr->setValue(aClickedPoint);
401           // fill reference attribute
402           AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
403                                                             attribute(aReferenceAttributeName));
404           if (aRefAttr.get()) {
405             if (anAttribute.get())
406               aRefAttr->setAttr(anAttribute);
407             else if (anObject.get()) {
408               // if presentation of previous reentrant macro arc is used, the object is invalid,
409               // we should use result of previous feature of the message(Arc)
410               if (!anObject->data()->isValid()) {
411                 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
412                 anObject = aCreatedFeature->lastResult();
413               }
414               aRefAttr->setObject(anObject);
415             }
416           }
417         }
418       }
419     }
420     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
421   }
422   return aFilledAttributeName;
423 }
424
425 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
426 {
427   FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
428   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
429       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
430   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
431       anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
432   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
433       anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
434   anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
435                 ->setValue(boolean(REVERSED_ID())->value());
436   anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
437                 ->setValue(boolean(AUXILIARY_ID())->value());
438   anArcFeature->execute();
439
440   return anArcFeature;
441 }
442
443 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
444 {
445   AttributePoint2DPtr aCenterPointAttr =
446       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
447   if (!aCenterPointAttr->isInitialized())
448       return;
449
450   AttributePoint2DPtr aStartPointAttr =
451       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
452   if (!aStartPointAttr->isInitialized())
453     return;
454
455   myCenter = aCenterPointAttr->pnt();
456   myStart = aStartPointAttr->pnt();
457   myEnd = myStart;
458
459   AttributePoint2DPtr anEndPointAttr =
460       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
461   if (!anEndPointAttr->isInitialized())
462     return;
463
464   GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
465
466   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
467   // check the end point is referred to another feature
468   GeomShapePtr aRefShape;
469   AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
470   if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
471     if (aEndPointRefAttr->isObject()) {
472       FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
473       if (aFeature)
474         aRefShape = aFeature->lastResult()->shape();
475     }
476   }
477   if (aRefShape) {
478     // Calculate end point as an intersection between circle and another shape
479     intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
480   } else {
481     // End point should be a projection on circle.
482     projectPointOnCircle(anEndPointAttr, aCircleForArc);
483   }
484   data()->blockSendAttributeUpdated(aWasBlocked, false);
485   myEnd = anEndPointAttr->pnt();
486
487   // update the REVERSED flag
488   recalculateReversedFlagByEnd(aCircleForArc);
489 }
490
491 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
492 {
493   double aParameterNew = 0.0;
494   if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
495     if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
496       boolean(REVERSED_ID())->setValue(true);
497     } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
498       boolean(REVERSED_ID())->setValue(false);
499     }
500   }
501   myParamBefore = aParameterNew;
502 }
503
504 void SketchPlugin_MacroArc::fillByThreePassedPoints()
505 {
506   AttributePoint2DPtr aStartPointAttr =
507       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
508   if (!aStartPointAttr->isInitialized())
509     return;
510
511   AttributePoint2DPtr anEndPointAttr =
512       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
513   if (!anEndPointAttr->isInitialized())
514     return;
515
516   myStart = aStartPointAttr->pnt();
517   myEnd = anEndPointAttr->pnt();
518
519   AttributePoint2DPtr aPassedPointAttr =
520       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
521   if (aPassedPointAttr->isInitialized()) {
522     std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
523     std::shared_ptr<GeomAPI_Shape> aTangentCurve;
524     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
525         refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
526
527     GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
528     aCircBuilder.addPassingPoint(myStart);
529     aCircBuilder.addPassingPoint(myEnd);
530     if (aTangentCurve) {
531       aCircBuilder.addTangentCurve(aTangentCurve);
532       aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
533     } else
534       aCircBuilder.addPassingPoint(aPassedPnt);
535
536     std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
537     if (!aCircle)
538       return;
539     myCenter = aCircle->center();
540
541     aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
542     recalculateReversedFlagByPassed(*aCircle);
543   } else
544     myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
545 }
546
547 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
548     const GeomAPI_Circ2d& theCurrentCircular)
549 {
550   AttributePoint2DPtr aPassedAttr =
551       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
552   std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
553
554   double aEndParam, aPassedParam;
555   theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
556   theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
557
558   if(aPassedParam > aEndParam)
559     boolean(REVERSED_ID())->setValue(true);
560   else
561     boolean(REVERSED_ID())->setValue(false);
562
563   myParamBefore = aEndParam;
564 }
565
566 void SketchPlugin_MacroArc::fillByTangentEdge()
567 {
568   AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
569   if (!aTangentAttr->isInitialized())
570     return;
571
572   AttributePoint2DPtr aTangentPointAttr =
573       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
574   if (!aTangentPointAttr->isInitialized())
575     return;
576
577   AttributePoint2DPtr anEndPointAttr =
578       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
579   if (!anEndPointAttr->isInitialized())
580     return;
581
582   myStart = aTangentPointAttr->pnt();
583   myEnd = anEndPointAttr->pnt();
584   if (myStart->isEqual(myEnd))
585     return;
586
587   // obtain a shape the tangent point belongs to
588   FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
589   std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
590
591   GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
592   aCircBuilder.addPassingPoint(myStart);
593   aCircBuilder.addPassingPoint(myEnd);
594   aCircBuilder.addTangentCurve(aTangentShape);
595
596   std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
597   myCenter = aCircle->center();
598
599   // rebuild circle to set start point equal to zero parameter
600   aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
601   recalculateReversedFlagByEnd(*aCircle);
602 }