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