]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_MacroArc.cpp
Salome HOME
[bos #37570] [CEA] SHAPER hdf tests are failing. Fix Sphere feature attributes order.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroArc.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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   SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
306   return anAIS;
307 }
308
309 void SketchPlugin_MacroArc::execute()
310 {
311   FeaturePtr anArcFeature = createArcFeature();
312
313   myCenter.reset();
314   myStart.reset();
315   myEnd.reset();
316
317   // Create constraints.
318   std::string anArcType = string(ARC_TYPE())->value();
319   if(anArcType == ARC_TYPE_BY_CENTER_AND_POINTS()) {
320     SketchPlugin_Tools::createCoincidenceOrTangency(this,
321                                          CENTER_POINT_REF_ID(),
322                                          anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
323                                          ObjectPtr(),
324                                          false);
325     SketchPlugin_Tools::createCoincidenceOrTangency(this,
326                                          START_POINT_REF_ID(),
327                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
328                                          ObjectPtr(),
329                                          false);
330     SketchPlugin_Tools::createCoincidenceOrTangency(this,
331                                          END_POINT_REF_ID(),
332                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
333                                          ObjectPtr(),
334                                          false);
335   } else if(anArcType == ARC_TYPE_BY_THREE_POINTS()) {
336     SketchPlugin_Tools::createCoincidenceOrTangency(this,
337                                          START_POINT_REF_ID(),
338                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
339                                          ObjectPtr(),
340                                          false);
341     SketchPlugin_Tools::createCoincidenceOrTangency(this,
342                                          END_POINT_REF_ID(),
343                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
344                                          ObjectPtr(),
345                                          false);
346     SketchPlugin_Tools::createCoincidenceOrTangency(this,
347                                          PASSED_POINT_REF_ID(),
348                                          AttributePtr(),
349                                          anArcFeature->lastResult(),
350                                          true);
351   } else {
352     // coincident with connection point
353     const std::string& aPointAttr = anArcType == ARC_TYPE_BY_TANGENT_EDGE() ?
354                                                  TANGENT_POINT_ID() : TRANSVERSAL_POINT_ID();
355     SketchPlugin_Tools::createCoincidenceOrTangency(this,
356                                          aPointAttr,
357                                          anArcFeature->attribute(SketchPlugin_Arc::START_ID()),
358                                          ObjectPtr(),
359                                          false);
360     // tangent or perpendicular constraint
361     FeaturePtr aStartPointConstraint;
362     if (anArcType == ARC_TYPE_BY_TANGENT_EDGE())
363       aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
364     else
365       aStartPointConstraint = sketch()->addFeature(SketchPlugin_ConstraintPerpendicular::ID());
366     // setting attributes of the start point constraint
367     AttributeRefAttrPtr aRefAttrA =
368         aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
369     AttributeRefAttrPtr aTgPntRefAttr = refattr(aPointAttr);
370     FeaturePtr aTgFeature = ModelAPI_Feature::feature(aTgPntRefAttr->attr()->owner());
371     aRefAttrA->setObject(aTgFeature->lastResult());
372     AttributeRefAttrPtr aRefAttrB =
373         aStartPointConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
374     aRefAttrB->setObject(anArcFeature->lastResult());
375     // constraint for end point
376     SketchPlugin_Tools::createCoincidenceOrTangency(this,
377                                          END_POINT_REF_ID(),
378                                          anArcFeature->attribute(SketchPlugin_Arc::END_ID()),
379                                          ObjectPtr(),
380                                          false);
381   }
382
383   // message to init reentrant operation
384   static Events_ID anId = SketchPlugin_MacroArcReentrantMessage::eventId();
385   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aMessage = std::shared_ptr
386     <SketchPlugin_MacroArcReentrantMessage>(new SketchPlugin_MacroArcReentrantMessage(anId, this));
387
388   std::string anEditArcType = string(EDIT_ARC_TYPE_ID())->value();
389   aMessage->setTypeOfCreation(!anEditArcType.empty() ? anEditArcType : anArcType);
390   aMessage->setCreatedFeature(anArcFeature);
391   Events_Loop::loop()->send(aMessage);
392 }
393
394 // LCOV_EXCL_START
395 std::string SketchPlugin_MacroArc::processEvent(const std::shared_ptr<Events_Message>& theMessage)
396 {
397   std::string aFilledAttributeName;
398   std::shared_ptr<SketchPlugin_MacroArcReentrantMessage> aReentrantMessage =
399         std::dynamic_pointer_cast<SketchPlugin_MacroArcReentrantMessage>(theMessage);
400   if (aReentrantMessage.get()) {
401     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
402     std::string anArcType = aReentrantMessage->typeOfCreation();
403
404     string(ARC_TYPE())->setValue(anArcType);
405
406     aFilledAttributeName = ARC_TYPE();
407     if(anArcType == ARC_TYPE_BY_TANGENT_EDGE()) {
408       aFilledAttributeName = TANGENT_POINT_ID();
409       AttributeRefAttrPtr aRefAttr = refattr(aFilledAttributeName);
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                 if (aCreatedFeature.get()) {
448                   std::string anID = anAttribute->id();
449                   std::string anArcID;
450                   if (anID == END_POINT_1_ID() || anID == END_POINT_2_ID() ||
451                       anID == END_POINT_3_ID())
452                     anArcID = SketchPlugin_Arc::END_ID();
453                   else if (anID == START_POINT_1_ID() || anID ==START_POINT_2_ID())
454                     anArcID = SketchPlugin_Arc::START_ID();
455                   else if (anID == CENTER_POINT_ID())
456                     anArcID = SketchPlugin_Arc::CENTER_ID();
457                   anAttribute = aCreatedFeature->attribute(anArcID);
458                 }
459               }
460               aRefAttr->setAttr(anAttribute);
461             }
462             else if (anObject.get()) {
463               // if attribute is NULL, only object is defined, it should be processed outside
464               // the feature because it might be an external feature, that will be
465               // removed/created again after restart operation
466               // #2468 - Crash when sketching circles successively on a repetition
467               aFilledAttributeName = ARC_TYPE();
468             }
469           }
470         }
471       }
472     }
473     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
474   }
475   return aFilledAttributeName;
476 }
477 // LCOV_EXCL_STOP
478
479 FeaturePtr SketchPlugin_MacroArc::createArcFeature()
480 {
481   FeaturePtr anArcFeature = sketch()->addFeature(SketchPlugin_Arc::ID());
482   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
483       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenter);
484   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
485       anArcFeature->attribute(SketchPlugin_Arc::START_ID()))->setValue(myStart);
486   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
487       anArcFeature->attribute(SketchPlugin_Arc::END_ID()))->setValue(myEnd);
488   anArcFeature->boolean(SketchPlugin_Arc::REVERSED_ID())
489                 ->setValue(boolean(REVERSED_ID())->value());
490   anArcFeature->boolean(SketchPlugin_Arc::AUXILIARY_ID())
491                 ->setValue(boolean(AUXILIARY_ID())->value());
492   anArcFeature->execute();
493
494   return anArcFeature;
495 }
496
497 void SketchPlugin_MacroArc::fillByCenterAndTwoPassed()
498 {
499   AttributePoint2DPtr aCenterPointAttr =
500       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()));
501   if (!aCenterPointAttr->isInitialized())
502       return;
503
504   AttributePoint2DPtr aStartPointAttr =
505       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_1_ID()));
506   if (!aStartPointAttr->isInitialized())
507     return;
508
509   myCenter = aCenterPointAttr->pnt();
510   myStart = aStartPointAttr->pnt();
511   myEnd = myStart;
512
513   AttributePoint2DPtr anEndPointAttr =
514       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_1_ID()));
515   if (!anEndPointAttr->isInitialized())
516     return;
517
518   GeomAPI_Circ2d aCircleForArc(myCenter, myStart);
519   // avoid degerated arc, when the center and the start points are equal
520   if (!aCircleForArc.implPtr<void*>())
521     return;
522
523   bool aWasBlocked = data()->blockSendAttributeUpdated(true);
524   // check the end point is referred to another feature
525   GeomShapePtr aRefShape;
526   AttributeRefAttrPtr aEndPointRefAttr = refattr(END_POINT_REF_ID());
527   if (aEndPointRefAttr && aEndPointRefAttr->isInitialized()) {
528     if (aEndPointRefAttr->isObject()) {
529       FeaturePtr aFeature = ModelAPI_Feature::feature(aEndPointRefAttr->object());
530       if (aFeature)
531         aRefShape = aFeature->lastResult()->shape();
532     }
533   }
534   if (aRefShape) {
535     // Calculate end point as an intersection between circle and another shape
536     intersectShapeAndCircle(aRefShape, aCircleForArc, sketch(), anEndPointAttr);
537   } else {
538     // End point should be a projection on circle.
539     projectPointOnCircle(anEndPointAttr, aCircleForArc);
540   }
541   data()->blockSendAttributeUpdated(aWasBlocked, false);
542   myEnd = anEndPointAttr->pnt();
543
544   // update the REVERSED flag
545   recalculateReversedFlagByEnd(aCircleForArc);
546 }
547
548 void SketchPlugin_MacroArc::recalculateReversedFlagByEnd(const GeomAPI_Circ2d& theCurrentCircular)
549 {
550   double aParameterNew = 0.0;
551   if(theCurrentCircular.parameter(myEnd, paramTolerance, aParameterNew)) {
552     if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
553       boolean(REVERSED_ID())->setValue(true);
554     } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
555       boolean(REVERSED_ID())->setValue(false);
556     }
557   }
558   myParamBefore = aParameterNew;
559 }
560
561 void SketchPlugin_MacroArc::fillByThreePassedPoints()
562 {
563   AttributePoint2DPtr aStartPointAttr =
564       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_POINT_2_ID()));
565   if (!aStartPointAttr->isInitialized())
566     return;
567
568   AttributePoint2DPtr anEndPointAttr =
569       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_POINT_2_ID()));
570   if (!anEndPointAttr->isInitialized())
571     return;
572
573   myStart = aStartPointAttr->pnt();
574   myEnd = anEndPointAttr->pnt();
575
576   AttributePoint2DPtr aPassedPointAttr =
577       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
578   if (aPassedPointAttr->isInitialized()) {
579     std::shared_ptr<GeomAPI_Pnt2d> aPassedPnt;
580     std::shared_ptr<GeomAPI_Shape> aTangentCurve;
581     SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
582         refattr(PASSED_POINT_REF_ID()), aPassedPointAttr, aTangentCurve, aPassedPnt);
583
584     GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
585     aCircBuilder.addPassingPoint(myStart);
586     aCircBuilder.addPassingPoint(myEnd);
587     if (aTangentCurve) {
588       aCircBuilder.addTangentCurve(aTangentCurve);
589       aCircBuilder.setClosestPoint(aPassedPointAttr->pnt());
590     } else
591       aCircBuilder.addPassingPoint(aPassedPnt);
592
593     std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
594     if (!aCircle)
595       return;
596     myCenter = aCircle->center();
597
598     aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
599     recalculateReversedFlagByPassed(*aCircle);
600   } else
601     myCenter.reset(new GeomAPI_Pnt2d(myStart->xy()->added(myEnd->xy())->multiplied(0.5)));
602 }
603
604 void SketchPlugin_MacroArc::recalculateReversedFlagByPassed(
605     const GeomAPI_Circ2d& theCurrentCircular)
606 {
607   AttributePoint2DPtr aPassedAttr =
608       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
609   std::shared_ptr<GeomAPI_Pnt2d> aPassed = theCurrentCircular.project(aPassedAttr->pnt());
610
611   double aEndParam, aPassedParam;
612   theCurrentCircular.parameter(myEnd, paramTolerance, aEndParam);
613   theCurrentCircular.parameter(aPassed, paramTolerance, aPassedParam);
614
615   if(aPassedParam > aEndParam)
616     boolean(REVERSED_ID())->setValue(true);
617   else
618     boolean(REVERSED_ID())->setValue(false);
619
620   myParamBefore = aEndParam;
621 }
622
623 void SketchPlugin_MacroArc::fillByEdge(bool theTransversal)
624 {
625   const std::string& aStartPoint = theTransversal ? TRANSVERSAL_POINT_ID() : TANGENT_POINT_ID();
626   const std::string& aEndPoint = theTransversal ? END_POINT_4_ID() : END_POINT_3_ID();
627
628   AttributeRefAttrPtr aTangentAttr = refattr(aStartPoint);
629   if (!aTangentAttr->isInitialized())
630     return;
631
632   AttributePoint2DPtr aConnectionPointAttr =
633       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangentAttr->attr());
634   if (!aConnectionPointAttr->isInitialized())
635     return;
636
637   AttributePoint2DPtr anEndPointAttr =
638       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aEndPoint));
639   if (!anEndPointAttr->isInitialized())
640     return;
641
642   myStart = aConnectionPointAttr->pnt();
643   myEnd = anEndPointAttr->pnt();
644   if (myStart->isEqual(myEnd))
645     return;
646
647   // obtain a shape the tangent point belongs to
648   FeaturePtr aConnectedFeature = ModelAPI_Feature::feature(aConnectionPointAttr->owner());
649   std::shared_ptr<GeomAPI_Shape> aTangentShape = aConnectedFeature->lastResult()->shape();
650
651   GeomAlgoAPI_Circ2dBuilder aCircBuilder(SketchPlugin_Sketch::plane(sketch()));
652   aCircBuilder.addPassingPoint(myStart);
653   aCircBuilder.addPassingPoint(myEnd);
654   if (theTransversal)
655     aCircBuilder.setTransversalLine(aTangentShape);
656   else
657     aCircBuilder.addTangentCurve(aTangentShape);
658
659   std::shared_ptr<GeomAPI_Circ2d> aCircle = aCircBuilder.circle();
660   if (!aCircle)
661     return;
662   myCenter = aCircle->center();
663
664   // rebuild circle to set start point equal to zero parameter
665   aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(myCenter, myStart));
666   recalculateReversedFlagByEnd(*aCircle);
667 }