Salome HOME
Fixed crash when creating arc coinciding with already created arc.
[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   if(anInterPnt.get()) {
117     theIntersection->setValue(anInterPnt);
118   }
119 }
120
121
122 SketchPlugin_MacroArc::SketchPlugin_MacroArc()
123 : SketchPlugin_SketchEntity(),
124   myParamBefore(0.0)
125 {
126 }
127
128 void SketchPlugin_MacroArc::initAttributes()
129 {
130   data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
131
132   data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
133   data()->addAttribute(START_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
134   data()->addAttribute(END_POINT_1_ID(), GeomDataAPI_Point2D::typeId());
135
136   data()->addAttribute(START_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
137   data()->addAttribute(END_POINT_2_ID(), GeomDataAPI_Point2D::typeId());
138   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
139
140   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
141   data()->addAttribute(END_POINT_3_ID(), GeomDataAPI_Point2D::typeId());
142
143   data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
144
145   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
146   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
147
148   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
149
150   data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
151   data()->addAttribute(START_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
152   data()->addAttribute(END_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
153   data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
154
155   data()->addAttribute(EDIT_ARC_TYPE_ID(), ModelAPI_AttributeString::typeId());
156
157   boolean(REVERSED_ID())->setValue(false);
158   string(EDIT_ARC_TYPE_ID())->setValue("");
159
160   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
161   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START_POINT_REF_ID());
162   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END_POINT_REF_ID());
163   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
164   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_ARC_TYPE_ID());
165 }
166
167 void SketchPlugin_MacroArc::attributeChanged(const std::string& theID)
168 {
169   std::string anArcType = string(ARC_TYPE())->value();
170
171   // If arc type switched reset according attributes.
172   if(theID == ARC_TYPE()) {
173     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_ID());
174     SketchPlugin_Tools::resetAttribute(this, CENTER_POINT_REF_ID());
175     SketchPlugin_Tools::resetAttribute(this, START_POINT_1_ID());
176     SketchPlugin_Tools::resetAttribute(this, START_POINT_REF_ID());
177     SketchPlugin_Tools::resetAttribute(this, END_POINT_1_ID());
178     SketchPlugin_Tools::resetAttribute(this, END_POINT_REF_ID());
179     SketchPlugin_Tools::resetAttribute(this, START_POINT_2_ID());
180     SketchPlugin_Tools::resetAttribute(this, END_POINT_2_ID());
181     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_ID());
182     SketchPlugin_Tools::resetAttribute(this, PASSED_POINT_REF_ID());
183     SketchPlugin_Tools::resetAttribute(this, TANGENT_POINT_ID());
184     SketchPlugin_Tools::resetAttribute(this, END_POINT_3_ID());
185     SketchPlugin_Tools::resetAttribute(this, REVERSED_ID());
186     SketchPlugin_Tools::resetAttribute(this, RADIUS_ID());
187     SketchPlugin_Tools::resetAttribute(this, ANGLE_ID());
188
189     myCenter.reset();
190     myStart.reset();
191     myEnd.reset();
192     boolean(REVERSED_ID())->setValue(false);
193     myParamBefore = 0.0;
194   } else if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS())
195     fillByCenterAndTwoPassed();
196   else if(anArcType == ARC_TYPE_BY_THREE_POINTS())
197     fillByThreePassedPoints();
198   else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE())
199     fillByTangentEdge();
200
201   double aRadius = 0;
202   double anAngle = 0;
203   if(myCenter.get() && myStart.get()) {
204     aRadius = myCenter->distance(myStart);
205     if(myEnd.get()) {
206       if(myStart->isEqual(myEnd)) {
207         anAngle = 360;
208       } else {
209         GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
210         double aStartParam, anEndParam;
211         aCircleForArc.parameter(myStart, paramTolerance, aStartParam);
212         aCircleForArc.parameter(myEnd, paramTolerance, anEndParam);
213         anAngle = (anEndParam - aStartParam) / PI * 180.0;
214         if(boolean(REVERSED_ID())->value()) anAngle = 360.0 - anAngle;
215       }
216     }
217   }
218
219   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
220   if(myCenter.get()) {
221     // center attribute is used in processEvent() to set reference to reentrant arc
222     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))
223         ->setValue(myCenter);
224   }
225   real(RADIUS_ID())->setValue(aRadius);
226   real(ANGLE_ID())->setValue(anAngle);
227   data()->blockSendAttributeUpdated(aWasBlocked, false);
228 }
229
230 GeomShapePtr SketchPlugin_MacroArc::getArcShape(bool isBound)
231 {
232   if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
233     return GeomShapePtr();
234   }
235
236   SketchPlugin_Sketch* aSketch = sketch();
237   if(!aSketch) {
238     return GeomShapePtr();
239   }
240
241   std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
242   std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(myStart->x(), myStart->y()));
243   std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(myEnd->x(), myEnd->y()));
244   std::shared_ptr<GeomDataAPI_Dir> aNDir =
245     std::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->attribute(SketchPlugin_Sketch::NORM_ID()));
246   std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
247
248   GeomShapePtr anArcShape;
249   if (isBound) {
250     anArcShape = boolean(REVERSED_ID())->value() ?
251         GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
252       : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
253   } else {
254     double aRadius = aCenter->distance(aStart);
255     anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
256   }
257
258   return anArcShape;
259 }
260
261 AISObjectPtr SketchPlugin_MacroArc::getAISObject(AISObjectPtr thePrevious)
262 {
263   if(!myStart.get() || !myEnd.get() || !myCenter.get()) {
264     return AISObjectPtr();
265   }
266
267   SketchPlugin_Sketch* aSketch = sketch();
268   if(!aSketch) {
269     return AISObjectPtr();
270   }
271
272   GeomShapePtr anArcShape = getArcShape();
273   std::shared_ptr<GeomAPI_Pnt> aCenter = aSketch->to3D(myCenter->x(), myCenter->y());;
274   GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
275
276   if(!anArcShape.get() || !aCenterPointShape.get()) {
277     return AISObjectPtr();
278   }
279
280   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
281   aShapes.push_back(anArcShape);
282   aShapes.push_back(aCenterPointShape);
283
284   std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
285   AISObjectPtr anAIS = thePrevious;
286   if(!anAIS.get()) {
287     anAIS.reset(new GeomAPI_AISObject());
288   }
289   anAIS->createShape(aCompound);
290   return anAIS;
291 }
292
293 void SketchPlugin_MacroArc::execute()
294 {
295   FeaturePtr anArcFeature = createArcFeature();
296
297   myCenter.reset();
298   myStart.reset();
299   myEnd.reset();
300
301   // Create constraints.
302   std::string anArcType = string(ARC_TYPE())->value();
303   if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
304     SketchPlugin_Tools::createConstraint(this,
305                                          CENTER_POINT_REF_ID(),
306                                          anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
307                                          ObjectPtr(),
308                                          false);
309     SketchPlugin_Tools::createConstraint(this,
310                                          START_POINT_REF_ID(),
311                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
312                                          ObjectPtr(),
313                                          false);
314     SketchPlugin_Tools::createConstraint(this,
315                                          END_POINT_REF_ID(),
316                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
317                                          ObjectPtr(),
318                                          false);
319   } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
320     SketchPlugin_Tools::createConstraint(this,
321                                          START_POINT_REF_ID(),
322                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
323                                          ObjectPtr(),
324                                          false);
325     SketchPlugin_Tools::createConstraint(this,
326                                          END_POINT_REF_ID(),
327                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
328                                          ObjectPtr(),
329                                          false);
330     SketchPlugin_Tools::createConstraint(this,
331                                          PASSED_POINT_REF_ID(),
332                                          AttributePtr(),
333                                          anArcFeature->lastResult(),
334                                          true);
335   } else if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
336     // constraints for tangent arc
337     SketchPlugin_Tools::createConstraint(this,
338                                          TANGENT_POINT_ID(),
339                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
340                                          ObjectPtr(),
341                                          false);
342     FeaturePtr aTangent = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
343     AttributeRefAttrPtr aRefAttrA = aTangent->refattr(SketchPlugin_Constraint::ENTITY_A());
344     AttributeRefAttrPtr aTgPntRefAttr = refattr(TANGENT_POINT_ID());
345     FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
346     aRefAttrA->setObject(aTgFeature->lastResult());
347     AttributeRefAttrPtr aRefAttrB = aTangent->refattr(SketchPlugin_Constraint::ENTITY_B());
348     aRefAttrB->setObject(anArcFeature->lastResult());
349     // constraint for end point
350     SketchPlugin_Tools::createConstraint(this,
351                                          END_POINT_REF_ID(),
352                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
353                                          ObjectPtr(),
354                                          false);
355   }
356
357   // message to init reentrant operation
358   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
359   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
360     <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
361
362   std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
363   aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
364   aMessage->setCreatedFeature(anArcFeature);
365   Events_Loop::loop()->send(aMessage);
366 }
367
368 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
369 {
370   std::string aFilledAttributeName;
371   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
372         std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
373   if (aReentrantMessage.get()) {
374     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
375     std::string anArcType = aReentrantMessage->typeOfCreation();
376
377     string(ARC_TYPE())->setValue(anArcType);
378
379     aFilledAttributeName = ARC_TYPE();
380     if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
381       aFilledAttributeName = TANGENT_POINT_ID();
382       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
383                                                         attribute(aFilledAttributeName));
384       FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
385       aRefAttr->setAttr(aCreatedFeature->attribute(SketchPlugin_Arc::END_ID()));
386     }
387     else {
388       ObjectPtr anObject = aReentrantMessage->selectedObject();
389       AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
390       std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
391
392       if (aClickedPoint.get() && (anObject.get() || anAttribute.get())) {
393         if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS() ||
394             anArcType == ARC_TYPE_BY_THREE_POINTS()) {
395           std::string aReferenceAttributeName;
396           if (anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
397             aFilledAttributeName = CENTER_POINT_ID();
398             aReferenceAttributeName = CENTER_POINT_REF_ID();
399           }
400           else {
401             aFilledAttributeName = START_POINT_2_ID();
402             aReferenceAttributeName = START_POINT_REF_ID();
403           }
404           // fill 2d point attribute
405           AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
406                                                             attribute(aFilledAttributeName));
407           aPointAttr->setValue(aClickedPoint);
408           // fill reference attribute
409           AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
410                                                           attribute(aReferenceAttributeName));
411           if (aRefAttr.get()) {
412             if (anAttribute.get()) {
413               if (!anAttribute->owner().get() || !anAttribute->owner()->data()->isValid()) {
414                 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
415                 if (aCreatedFeature.get()) {
416                   std::string anID = anAttribute->id();
417                   std::string anArcID;
418                   if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
419                       anID == END_POINT_3_ID())
420                     anArcID = SketchPlugin_Arc::END_ID();
421                   else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
422                     anArcID = SketchPlugin_Arc::START_ID();
423                   else if (anID == CENTER_POINT_ID())
424                     anArcID = SketchPlugin_Arc::CENTER_ID();
425                   anAttribute = aCreatedFeature->attribute(anArcID);
426                 }
427               }
428               aRefAttr->setAttr(anAttribute);
429             }
430             else if (anObject.get()) {
431               // if presentation of previous reentrant macro arc is used, the object is invalid,
432               // we should use result of previous feature of the message(Arc)
433               if (!anObject->data()->isValid()) {
434                 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
435                 if (aCreatedFeature.get())
436                   anObject = aCreatedFeature->lastResult();
437               }
438               aRefAttr->setObject(anObject);
439             }
440           }
441         }
442       }
443     }
444     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
445   }
446   return aFilledAttributeName;
447 }
448
449 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
450 {
451   FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
452   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
453       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
454   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
455       anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
456   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
457       anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
458   anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
459                 ->setValue(boolean(REVERSED_ID())->value());
460   anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
461                 ->setValue(boolean(AUXILIARY_ID())->value());
462   anArcFeature->execute();
463
464   return anArcFeature;
465 }
466
467 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
468 {
469   AttributePoint2DPtr aCenterPointAttr =
470       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
471   if (!aCenterPointAttr->isInitialized())
472       return;
473
474   AttributePoint2DPtr aStartPointAttr =
475       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
476   if (!aStartPointAttr->isInitialized())
477     return;
478
479   myCenter = aCenterPointAttr->pnt();
480   myStart = aStartPointAttr->pnt();
481   myEnd = myStart;
482
483   AttributePoint2DPtr anEndPointAttr =
484       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
485   if (!anEndPointAttr->isInitialized())
486     return;
487
488   GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
489
490   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
491   // check the end point is referred to another feature
492   GeomShapePtr aRefShape;
493   AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
494   if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
495     if (aEndPointRefAttr->isObject()) {
496       FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
497       if (aFeature)
498         aRefShape = aFeature->lastResult()->shape();
499     }
500   }
501   if (aRefShape) {
502     // Calculate end point as an intersection between circle and another shape
503     intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
504   } else {
505     // End point should be a projection on circle.
506     projectPointOnCircle(anEndPointAttr, aCircleForArc);
507   }
508   data()->blockSendAttributeUpdated(aWasBlocked, false);
509   myEnd = anEndPointAttr->pnt();
510
511   // update the REVERSED flag
512   recalculateReversedFlagByEnd(aCircleForArc);
513 }
514
515 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
516 {
517   double aParameterNew = 0.0;
518   if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
519     if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
520       boolean(REVERSED_ID())->setValue(true);
521     } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
522       boolean(REVERSED_ID())->setValue(false);
523     }
524   }
525   myParamBefore = aParameterNew;
526 }
527
528 void SketchPlugin_MacroArc::fillByThreePassedPoints()
529 {
530   AttributePoint2DPtr aStartPointAttr =
531       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
532   if (!aStartPointAttr->isInitialized())
533     return;
534
535   AttributePoint2DPtr anEndPointAttr =
536       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
537   if (!anEndPointAttr->isInitialized())
538     return;
539
540   myStart = aStartPointAttr->pnt();
541   myEnd = anEndPointAttr->pnt();
542
543   AttributePoint2DPtr aPassedPointAttr =
544       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
545   if (aPassedPointAttr->isInitialized()) {
546     std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
547     std::shared_ptr<GeomAPI_Shape> aTangentCurve;
548     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
549         refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
550
551     GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
552     aCircBuilder.addPassingPoint(myStart);
553     aCircBuilder.addPassingPoint(myEnd);
554     if (aTangentCurve) {
555       aCircBuilder.addTangentCurve(aTangentCurve);
556       aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
557     } else
558       aCircBuilder.addPassingPoint(aPassedPnt);
559
560     std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
561     if (!aCircle)
562       return;
563     myCenter = aCircle->center();
564
565     aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
566     recalculateReversedFlagByPassed(*aCircle);
567   } else
568     myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
569 }
570
571 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
572     const GeomAPI_Circ2d& theCurrentCircular)
573 {
574   AttributePoint2DPtr aPassedAttr =
575       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
576   std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
577
578   double aEndParam, aPassedParam;
579   theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
580   theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
581
582   if(aPassedParam > aEndParam)
583     boolean(REVERSED_ID())->setValue(true);
584   else
585     boolean(REVERSED_ID())->setValue(false);
586
587   myParamBefore = aEndParam;
588 }
589
590 void SketchPlugin_MacroArc::fillByTangentEdge()
591 {
592   AttributeRefAttrPtr aTangentAttr = refattr(TANGENT_POINT_ID());
593   if (!aTangentAttr->isInitialized())
594     return;
595
596   AttributePoint2DPtr aTangentPointAttr =
597       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
598   if (!aTangentPointAttr->isInitialized())
599     return;
600
601   AttributePoint2DPtr anEndPointAttr =
602       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_3_ID()));
603   if (!anEndPointAttr->isInitialized())
604     return;
605
606   myStart = aTangentPointAttr->pnt();
607   myEnd = anEndPointAttr->pnt();
608   if (myStart->isEqual(myEnd))
609     return;
610
611   // obtain a shape the tangent point belongs to
612   FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPointAttr->owner());
613   std::shared_ptr<GeomAPI_Shape> aTangentShape = aTangentFeature->lastResult()->shape();
614
615   GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
616   aCircBuilder.addPassingPoint(myStart);
617   aCircBuilder.addPassingPoint(myEnd);
618   aCircBuilder.addTangentCurve(aTangentShape);
619
620   std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
621   myCenter = aCircle->center();
622
623   // rebuild circle to set start point equal to zero parameter
624   aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
625   recalculateReversedFlagByEnd(*aCircle);
626 }