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