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