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