Salome HOME
Correct processing of the fixed arc in PlaneGCS (issue #1280)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Arc.cpp
4 // Created:     26 Apr 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_Arc.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintTangent.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Session.h>
22
23 #include <GeomAPI_Ax2.h>
24 #include <GeomAPI_Circ2d.h>
25 #include <GeomAPI_Circ.h>
26 #include <GeomAPI_Dir2d.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Lin2d.h>
29 #include <GeomAPI_Lin.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <GeomDataAPI_Dir.h>
34 #include <GeomAlgoAPI_PointBuilder.h>
35 #include <GeomAlgoAPI_EdgeBuilder.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
37 // for sqrt on Linux
38 #include <math.h>
39
40 const double tolerance = 1e-7;
41 const double paramTolerance = 1.e-4;
42 const double PI =3.141592653589793238463;
43
44 namespace {
45   static const std::string& ARC_TYPE()
46   {
47     static const std::string TYPE("ArcType");
48     return TYPE;
49   }
50   static const std::string& ARC_TYPE_CENTER_START_END()
51   {
52     static const std::string TYPE("CenterStartEnd");
53     return TYPE;
54   }
55   static const std::string& ARC_TYPE_THREE_POINTS()
56   {
57     static const std::string TYPE("ThreePoints");
58     return TYPE;
59   }
60   static const std::string& ARC_TYPE_TANGENT()
61   {
62     static const std::string TYPE("Tangent");
63     return TYPE;
64   }
65
66   static const std::string& PASSED_POINT_ID()
67   {
68     static const std::string PASSED_PNT("ArcPassedPoint");
69     return PASSED_PNT;
70   }
71   static const std::string& TANGENT_POINT_ID()
72   {
73     static const std::string TANGENT_PNT("ArcTangentPoint");
74     return TANGENT_PNT;
75   }
76   static const std::string& RADIUS_ID()
77   {
78     static const std::string RADIUS("ArcRadius");
79     return RADIUS;
80   }
81   static const std::string& ANGLE_ID()
82   {
83     static const std::string ANGLE("ArcAngle");
84     return ANGLE;
85   }
86
87   static const std::string& POINT_ID(int theIndex)
88   {
89     switch (theIndex) {
90     case 1: return SketchPlugin_Arc::START_ID();
91     case 2: return SketchPlugin_Arc::END_ID();
92     case 3: return PASSED_POINT_ID();
93     }
94
95     static const std::string DUMMY;
96     return DUMMY;
97   }
98 }
99
100
101
102 SketchPlugin_Arc::SketchPlugin_Arc()
103     : SketchPlugin_SketchEntity()
104 {
105   myStartUpdate = false;
106   myEndUpdate = false;
107   // default values
108   myXEndBefore = 0;
109   myYEndBefore = 0;
110
111   myParamBefore = 0;
112 }
113
114 void SketchPlugin_Arc::initDerivedClassAttributes()
115 {
116   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
117   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
118   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
119     GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
120   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
121   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
122
123   data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
124   AttributeBooleanPtr isInversed =
125       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
126   if (!isInversed->isInitialized())
127     isInversed->setValue(false);
128
129   // get the initial values
130   if (anEndAttr->isInitialized()) {
131     myXEndBefore = anEndAttr->x();
132     myYEndBefore = anEndAttr->y();
133   }
134
135   data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
136   std::dynamic_pointer_cast<ModelAPI_AttributeString>(
137       data()->attribute(ARC_TYPE()))->setValue(ARC_TYPE_CENTER_START_END());
138
139   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
140   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
141   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
142   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
143 }
144
145 void SketchPlugin_Arc::execute()
146 {
147   SketchPlugin_Sketch* aSketch = sketch();
148   // result for the arc is set only when all obligatory attributes are initialized,
149   // otherwise AIS object is used to visualize the arc's preview
150   if (aSketch && isFeatureValid()) {
151     ResultPtr aLastResult = lastResult();
152     bool hasResult = aLastResult && aLastResult.get();
153
154     // compute a circle point in 3D view
155     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
156         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
157     // compute the arc start point
158     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
159         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
160
161     std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
162     // make a visible point
163     std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
164     std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
165         data(), 0);
166     aConstr1->setShape(aCenterPointShape);
167     aConstr1->setIsInHistory(false);
168     setResult(aConstr1, 0);
169
170     // make a visible circle
171     std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
172         aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
173     std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
174     std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
175
176     // compute and change the arc end point
177     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
178         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
179     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
180         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
181     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
182     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
183       anEndAttr->setValue(aProjection);
184     std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
185     AttributeBooleanPtr isInversed =
186         std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
187
188     std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
189     std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
190     std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
191     double aParameterNew = 0.0;
192     if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
193       if(0 <= myParamBefore && myParamBefore <= PI / 2.0
194         && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
195           isInversed->setValue(true);
196       } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
197         && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
198           isInversed->setValue(false);
199       }
200     }
201     myParamBefore = aParameterNew;
202
203     std::shared_ptr<GeomAPI_Shape> aCircleShape;
204     if(!isInversed->value()) {
205       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
206     } else {
207       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
208     }
209
210     if (aCircleShape) {
211       std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
212           data(), 1);
213       aConstr2->setShape(aCircleShape);
214       aConstr2->setIsInHistory(false);
215       setResult(aConstr2, 1);
216     }
217
218     AttributeRefAttrPtr aTangentPoint = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
219         attribute(TANGENT_POINT_ID()));
220     if (!hasResult && aTangentPoint->attr())
221       tangencyArcConstraints();
222   }
223 }
224
225 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
226 {
227   SketchPlugin_Sketch* aSketch = sketch();
228   if (aSketch) {
229     // if the feature is valid, the execute() method should be performed, AIS object is empty
230     if (!isFeatureValid()) {
231       // compute a circle point in 3D view
232       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
233           GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
234       if (aCenterAttr->isInitialized()) {
235         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
236
237         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
238             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
239         std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<
240             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
241         AttributeStringPtr aTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
242             data()->attribute(ARC_TYPE()));
243
244         if (aStartAttr->isInitialized()) {
245           // make a visible circle
246           std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
247               aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
248           bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
249           if (aHasPlane) {
250             std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
251             std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
252             std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
253             if (aTypeAttr && aTypeAttr->isInitialized() &&
254                 aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized())
255               aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
256             std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
257                                                             aCenter, aStartPoint, aEndPoint, aNormal);
258             if (aCircleShape) {
259               std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
260               // make a visible point
261               std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
262               aShapes.push_back(aCenterPointShape);
263
264               aShapes.push_back(aCircleShape);
265               if (!aShapes.empty())
266               {
267                 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
268                 AISObjectPtr anAIS = thePrevious;
269                 if (!anAIS)
270                   anAIS = AISObjectPtr(new GeomAPI_AISObject);
271                 anAIS->createShape(aCompound);
272                 anAIS->setWidth(3);
273                 return anAIS;
274               }
275             }
276           }
277         }
278       }
279     }
280   }
281   return AISObjectPtr();
282 }
283
284 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
285 {
286   std::shared_ptr<ModelAPI_Data> aData = data();
287   if (!aData->isValid())
288     return;
289
290   aData->blockSendAttributeUpdated(true);
291
292   myStartUpdate = true;
293   myEndUpdate = true;
294   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
295       aData->attribute(SketchPlugin_Arc::START_ID()));
296   aPoint2->move(theDeltaX, theDeltaY);
297
298   std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
299       aData->attribute(SketchPlugin_Arc::END_ID()));
300   aPoint3->move(theDeltaX, theDeltaY);
301   myStartUpdate = false;
302   myEndUpdate = false;
303
304   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
305       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
306   aPoint1->move(theDeltaX, theDeltaY);
307
308   std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
309       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
310   aPassedPoint->move(theDeltaX, theDeltaY);
311   aData->blockSendAttributeUpdated(false);
312 }
313
314 bool SketchPlugin_Arc::isFixed() {
315   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
316 }
317
318 bool SketchPlugin_Arc::isFeatureValid()
319 {
320   AttributeStringPtr anArcTypeAttr =
321       std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
322   if (!anArcTypeAttr)
323     return false;
324   std::string anArcType = anArcTypeAttr->value();
325
326   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
327       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
328   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
329       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
330   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
331       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
332   std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
333       GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
334
335   bool isValid = false;
336   if (anArcType == ARC_TYPE_THREE_POINTS())
337     isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
338   else
339     isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
340
341   return isValid;
342 }
343
344 static inline void adjustPeriod(double& theParam)
345 {
346   static const double PERIOD = 2.0 * PI;
347   while (theParam < 0.0) theParam += PERIOD;
348   while (theParam >= PERIOD) theParam -= PERIOD;
349 }
350
351 static inline void calculateArcAngleRadius(
352     const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
353     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
354     const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
355     const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
356     AttributeDoublePtr theAngleAttr,
357     AttributeDoublePtr theRadiusAttr)
358 {
359   double aStartParam, aEndParam, aPassedParam;
360   theCircle->parameter(theStartPoint, paramTolerance, aStartParam);
361   theCircle->parameter(theEndPoint, paramTolerance, aEndParam);
362   theCircle->parameter(thePassedPoint, paramTolerance, aPassedParam);
363   adjustPeriod(aStartParam);
364   adjustPeriod(aEndParam);
365   adjustPeriod(aPassedParam);
366
367   if (aPassedParam >= aStartParam && aPassedParam <= aEndParam)
368     theAngleAttr->setValue((aEndParam - aStartParam) * 180.0 / PI);
369   else
370     theAngleAttr->setValue((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
371   theRadiusAttr->setValue(theCircle->radius());
372 }
373
374 static inline void calculatePassedPoint(
375     const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
376     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
377     const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
378     bool theArcReversed,
379     std::shared_ptr<GeomDataAPI_Point2D> thePassedPoint)
380 {
381   std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
382       theStartPoint->xy()->decreased(theCenter->xy())));
383   std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
384       theEndPoint->xy()->decreased(theCenter->xy())));
385   std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
386   if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
387     // start and end directions are opposite, so middle direction will be orthogonal
388     aMidDirXY->setX(-aStartDir->y());
389     aMidDirXY->setY(aStartDir->x());
390   }
391   std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
392   if ((aStartDir->cross(aMidDir) > 0) ^ !theArcReversed)
393     aMidDir->reverse();
394
395   double aRadius = theCenter->distance(theStartPoint);
396   std::shared_ptr<GeomAPI_XY> aPassedPnt = theCenter->xy()->added( aMidDir->xy()->multiplied(aRadius) );
397   thePassedPoint->setValue(aPassedPnt->x(), aPassedPnt->y());
398 }
399
400 void SketchPlugin_Arc::updateDependentAttributes()
401 {
402   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
403       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
404   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
405       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
406   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
407       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
408   std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
409       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
410   AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
411       data()->attribute(RADIUS_ID()));
412   AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
413       data()->attribute(ANGLE_ID()));
414
415   if (!aPassedPoint)
416     return;
417
418   data()->blockSendAttributeUpdated(true);
419
420   calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
421                        isReversed(), aPassedPoint);
422   if (aRadiusAttr && anAngleAttr) {
423     std::shared_ptr<GeomAPI_Circ2d> aCircle(
424         new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
425     calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
426                             anAngleAttr, aRadiusAttr);
427   }
428   data()->blockSendAttributeUpdated(false);
429 }
430
431
432 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
433 {
434   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
435       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
436   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
437       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
438   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
439       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
440   // the second condition for unability to move external segments anywhere
441   if (theID == EXTERNAL_ID() || isFixed()) {
442     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
443     // update arguments due to the selection value
444     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
445       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
446       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
447       if (aCirc.get()) {
448         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
449         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
450         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
451       }
452     }
453     return;
454   }
455
456   AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
457       data()->attribute(RADIUS_ID()));
458   AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
459       data()->attribute(ANGLE_ID()));
460
461   if (theID == RADIUS_ID()) {
462     if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
463       return;
464     // move center and passed point
465     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
466     std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = anEndAttr->pnt();
467     double aDist = aStartPnt->distance(aEndPnt);
468     if (fabs(aDist) < tolerance)
469       return;
470     std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy());
471     std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
472     std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->xy()->added(aEndPnt->xy())->multiplied(0.5);
473
474     double anAngle = anAngleAttr->value() * PI / 180.0;
475     adjustPeriod(anAngle);
476     if (anAngle > PI)
477       aMidPerpDir->reverse();
478
479     double aRadius = aRadiusAttr->value();
480     aDist = sqrt(aRadius * aRadius - aDist * aDist / 4.0);
481
482     std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
483
484     data()->blockSendAttributeUpdated(true);
485     aCenterAttr->setValue(aCenter->x(), aCenter->y());
486     updateDependentAttributes();
487     data()->blockSendAttributeUpdated(false);
488     return;
489   }
490   if (theID == ANGLE_ID()) {
491     if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
492       return;
493     data()->blockSendAttributeUpdated(true);
494     // move end point and passed point
495     std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
496     double anAngle = anAngleAttr->value() * PI / 180.0;
497     double sinA = sin(anAngle);
498     double cosA = cos(anAngle);
499     std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
500     std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
501         aStartDir->x() * cosA - aStartDir->y() * sinA,
502         aStartDir->x() * sinA + aStartDir->y() * cosA));
503     anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
504
505     anAngle /= 2.0;
506     sinA = sin(anAngle);
507     cosA = cos(anAngle);
508     aDir = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(
509         aStartDir->x() * cosA - aStartDir->y() * sinA,
510         aStartDir->x() * sinA + aStartDir->y() * cosA));
511     std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
512         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
513     aPassedPoint->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
514
515     std::shared_ptr<GeomAPI_Circ2d> aCircle(
516         new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
517     calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
518                             anAngleAttr, aRadiusAttr);
519     data()->blockSendAttributeUpdated(false);
520     return;
521   }
522
523   if (theID == CENTER_ID()) {
524     if (!isFeatureValid())
525       return;
526     if (aCenterAttr->pnt()->distance(aStartAttr->pnt()) < tolerance)
527       return;
528     data()->blockSendAttributeUpdated(true);
529     // compute and change the arc end point
530     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
531         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
532     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
533     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
534       anEndAttr->setValue(aProjection);
535     updateDependentAttributes();
536     data()->blockSendAttributeUpdated(false);
537     return;
538   }
539
540   AttributeStringPtr aTypeAttr =
541       std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
542   if (!aTypeAttr)
543     return;
544   std::string anArcType = aTypeAttr->value();
545
546   // update the points in accordance to the changed point changes
547   if (anArcType == ARC_TYPE_CENTER_START_END()) {
548     if (!isFeatureValid())
549       return;
550     updateDependentAttributes();
551   }
552   else if (anArcType == ARC_TYPE_THREE_POINTS() &&
553           (theID == START_ID() || theID == END_ID() || theID == PASSED_POINT_ID())) {
554     data()->blockSendAttributeUpdated(true);
555
556     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
557     int aNbInitialized = 0;
558     for (int i = 1; i <= 3; ++i) {
559       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
560           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
561       if (aCurPnt->isInitialized())
562         aPoints[aNbInitialized++] = aCurPnt->pnt();
563     }
564
565     if (aNbInitialized == 1)
566       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
567     else if (aNbInitialized == 2) {
568       // calculate center point, which gives a quarter of circle for the given start and end points
569       std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aPoints[0];
570       std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aPoints[1];
571       std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy())->multiplied(0.5);
572       double x = aDir->x();
573       double y = aDir->y();
574       aDir->setX(x - y);
575       aDir->setY(y + x);
576       std::shared_ptr<GeomAPI_XY> aCenter = aStartPnt->xy()->added(aDir);
577       double aRadius = sqrt(aDir->dot(aDir));
578
579       aCenterAttr->setValue(aCenter->x(), aCenter->y());
580       aRadiusAttr->setValue(aRadius);
581       anAngleAttr->setValue(90.0);
582     }
583     else {
584       std::shared_ptr<GeomAPI_Circ2d> aCircle(
585           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
586
587       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
588       if (aCenter) {
589         aCenterAttr->setValue(aCenter);
590         if (theID == START_ID() || theID == END_ID())
591           updateDependentAttributes();
592         else
593           calculateArcAngleRadius(aCircle, aPoints[0], aPoints[1], aPoints[2],
594                                   anAngleAttr, aRadiusAttr);
595       }
596     }
597
598     data()->blockSendAttributeUpdated(false);
599   }
600   else if (anArcType == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
601     SketchPlugin_Sketch* aSketch = sketch();
602     AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
603         data()->attribute(TANGENT_POINT_ID()));
604
605     if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
606       data()->blockSendAttributeUpdated(true);
607       // compute orthogonal direction
608       std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
609       std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
610           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
611       std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
612       FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
613       std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
614           aTangFeature->lastResult()->shape());
615       if (aTangEdge->isLine()) {
616         std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
617         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
618         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
619         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
620       }
621       else if (aTangEdge->isArc()) {
622         std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
623         std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
624         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
625             new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
626       }
627
628       // compute parameters of the middle perpendicular
629       std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
630       std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
631       std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
632       std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
633           new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
634
635       // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
636       std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
637       std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
638       std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
639       if (aCenter) {
640         aCenterAttr->setValue(aCenter);
641         aStartAttr->setValue(aTangPnt2d);
642         updateDependentAttributes();
643       }
644
645       data()->blockSendAttributeUpdated(false);
646
647       if (theID == TANGENT_POINT_ID())
648         tangencyArcConstraints();
649     }
650   }
651 }
652
653 void SketchPlugin_Arc::setReversed(bool isReversed)
654 {
655   std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
656   myParamBefore = 0.0;
657 }
658
659 bool SketchPlugin_Arc::isReversed()
660 {
661   return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
662 }
663
664 void SketchPlugin_Arc::tangencyArcConstraints()
665 {
666   if (!lastResult())
667     return;
668
669   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
670       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
671   AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
672       attribute(TANGENT_POINT_ID()));
673   if (!aTangPtAttr->attr())
674     return;
675
676   FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
677   ObjectPtr aThisArc = aFeature->lastResult();
678   aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
679   ObjectPtr aTangFeature = aFeature->lastResult();
680
681   // trying to find constraints to fix the tangency of the arc
682   std::set<FeaturePtr> aCoincidence;
683   std::set<FeaturePtr> aTangency;
684
685   AttributeRefAttrPtr aRefAttrA, aRefAttrB;
686   std::set<AttributePtr> aRefs = data()->refsToMe();
687   const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
688   aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
689   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
690   for (; aRefIt != aRefs.end(); ++aRefIt) {
691     FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
692     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
693       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
694           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
695       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
696           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
697       if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
698           (aRefAttrB && aRefAttrB->attr() == aStartAttr))
699         aCoincidence.insert(aConstrFeature);
700     }
701     else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
702       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
703           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
704       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
705           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
706       if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
707           (aRefAttrB && aRefAttrB->object() == aThisArc))
708         aTangency.insert(aConstrFeature);
709     }
710   }
711   // search applicable pair of constraints
712   bool isFound = false;
713   FeaturePtr aPrevCoincidence, aPrevTangency;
714   std::set<FeaturePtr>::const_iterator aCIt, aTIt;
715   for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
716     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
717         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
718     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
719         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
720     AttributePtr anOtherPoint =
721         aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
722     for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
723       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
724           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
725       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
726           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
727       ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
728           aRefAttrB->object() : aRefAttrA->object();
729       if (anOtherPoint->owner() == anOtherObject) {
730         isFound = true;
731         aPrevCoincidence = *aCIt;
732         aPrevTangency = *aTIt;
733       }
734     }
735   }
736
737   if (isFound) {
738     // update previous constraints
739     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
740         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
741     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
742         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
743     if (aRefAttrA->attr() == aStartAttr)
744       aRefAttrB->setAttr(aTangPtAttr->attr());
745     else
746       aRefAttrA->setAttr(aTangPtAttr->attr());
747
748     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
749         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
750     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
751         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
752     if (aRefAttrA->object() == aThisArc)
753       aRefAttrB->setObject(aTangFeature);
754     else
755       aRefAttrA->setObject(aTangFeature);
756   } else {
757     // Wait all constraints being removed, then send update events
758     static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
759     bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
760     if (isDeleteFlushed)
761       Events_Loop::loop()->setFlushed(aDeleteEvent, false);
762     // Remove all obtained constraints which use current arc, because
763     // there is no information which of them were used to build tangency arc.
764     DocumentPtr aDoc = sketch()->document();
765     for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
766       aDoc->removeFeature(*aCIt);
767     for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
768       aDoc->removeFeature(*aTIt);
769     // Send events to update the sub-features by the solver.
770     if (isDeleteFlushed)
771       Events_Loop::loop()->setFlushed(aDeleteEvent, true);
772     else
773       Events_Loop::loop()->flush(aDeleteEvent);
774
775     // Wait all constraints being created, then send update events
776     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
777     bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
778     if (isUpdateFlushed)
779       Events_Loop::loop()->setFlushed(anUpdateEvent, false);
780
781     // Create new constraints
782     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
783     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
784         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
785     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
786         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
787     aRefAttrA->setAttr(aStartAttr);
788     aRefAttrB->setAttr(aTangPtAttr->attr());
789     aConstraint->execute();
790     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
791
792     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
793     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
794         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
795     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
796         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
797     aRefAttrA->setObject(aThisArc);
798     aRefAttrB->setObject(aTangFeature);
799     aConstraint->execute();
800     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
801
802     // Send events to update the sub-features by the solver.
803     if(isUpdateFlushed)
804       Events_Loop::loop()->setFlushed(anUpdateEvent, true);
805   }
806 }