Salome HOME
Fix failed unit tests
[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   data()->blockSendAttributeUpdated(true);
403
404   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
405       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
406   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
407       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
408   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
409       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
410   std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
411       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
412   AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
413       data()->attribute(RADIUS_ID()));
414   AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
415       data()->attribute(ANGLE_ID()));
416
417   calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
418                        isReversed(), aPassedPoint);
419   std::shared_ptr<GeomAPI_Circ2d> aCircle(
420       new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
421   calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
422                           anAngleAttr, aRadiusAttr);
423   data()->blockSendAttributeUpdated(false);
424 }
425
426
427 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
428 {
429   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
430       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
431   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
432       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
433   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
434       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
435   // the second condition for unability to move external segments anywhere
436   if (theID == EXTERNAL_ID() || isFixed()) {
437     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
438     // update arguments due to the selection value
439     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
440       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
441       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
442       if (aCirc.get()) {
443         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
444         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
445         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
446       }
447     }
448     return;
449   }
450
451   AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
452       data()->attribute(RADIUS_ID()));
453   AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
454       data()->attribute(ANGLE_ID()));
455
456   if (theID == RADIUS_ID()) {
457     if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
458       return;
459     // move center and passed point
460     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
461     std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = anEndAttr->pnt();
462     double aDist = aStartPnt->distance(aEndPnt);
463     if (fabs(aDist) < tolerance)
464       return;
465     std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy());
466     std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
467     std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->xy()->added(aEndPnt->xy())->multiplied(0.5);
468
469     double anAngle = anAngleAttr->value() * PI / 180.0;
470     adjustPeriod(anAngle);
471     if (anAngle > PI)
472       aMidPerpDir->reverse();
473
474     double aRadius = aRadiusAttr->value();
475     aDist = sqrt(aRadius * aRadius - aDist * aDist / 4.0);
476
477     std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
478
479     data()->blockSendAttributeUpdated(true);
480     aCenterAttr->setValue(aCenter->x(), aCenter->y());
481     updateDependentAttributes();
482     data()->blockSendAttributeUpdated(false);
483     return;
484   }
485   if (theID == ANGLE_ID()) {
486     if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
487       return;
488     data()->blockSendAttributeUpdated(true);
489     // move end point and passed point
490     std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
491     double anAngle = anAngleAttr->value() * PI / 180.0;
492     double sinA = sin(anAngle);
493     double cosA = cos(anAngle);
494     std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
495     std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
496         aStartDir->x() * cosA - aStartDir->y() * sinA,
497         aStartDir->x() * sinA + aStartDir->y() * cosA));
498     anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
499
500     anAngle /= 2.0;
501     sinA = sin(anAngle);
502     cosA = cos(anAngle);
503     aDir = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(
504         aStartDir->x() * cosA - aStartDir->y() * sinA,
505         aStartDir->x() * sinA + aStartDir->y() * cosA));
506     std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
507         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
508     aPassedPoint->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
509
510     std::shared_ptr<GeomAPI_Circ2d> aCircle(
511         new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
512     calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
513                             anAngleAttr, aRadiusAttr);
514     data()->blockSendAttributeUpdated(false);
515     return;
516   }
517
518   if (theID == CENTER_ID()) {
519     if (!isFeatureValid())
520       return;
521     if (aCenterAttr->pnt()->distance(aStartAttr->pnt()) < tolerance)
522       return;
523     data()->blockSendAttributeUpdated(true);
524     // compute and change the arc end point
525     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
526         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
527     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
528     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
529       anEndAttr->setValue(aProjection);
530     updateDependentAttributes();
531     data()->blockSendAttributeUpdated(false);
532     return;
533   }
534
535   AttributeStringPtr aTypeAttr =
536       std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
537   if (!aTypeAttr)
538     return;
539   std::string anArcType = aTypeAttr->value();
540
541   // update the points in accordance to the changed point changes
542   if (anArcType == ARC_TYPE_CENTER_START_END()) {
543     if (!isFeatureValid())
544       return;
545     updateDependentAttributes();
546   }
547   else if (anArcType == ARC_TYPE_THREE_POINTS() &&
548           (theID == START_ID() || theID == END_ID() || theID == PASSED_POINT_ID())) {
549     data()->blockSendAttributeUpdated(true);
550
551     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
552     int aNbInitialized = 0;
553     for (int i = 1; i <= 3; ++i) {
554       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
555           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
556       if (aCurPnt->isInitialized())
557         aPoints[aNbInitialized++] = aCurPnt->pnt();
558     }
559
560     if (aNbInitialized == 1)
561       aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
562     else if (aNbInitialized == 2) {
563       // calculate center point, which gives a quarter of circle for the given start and end points
564       std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aPoints[0];
565       std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aPoints[1];
566       std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy())->multiplied(0.5);
567       double x = aDir->x();
568       double y = aDir->y();
569       aDir->setX(x - y);
570       aDir->setY(y + x);
571       std::shared_ptr<GeomAPI_XY> aCenter = aStartPnt->xy()->added(aDir);
572       double aRadius = sqrt(aDir->dot(aDir));
573
574       aCenterAttr->setValue(aCenter->x(), aCenter->y());
575       aRadiusAttr->setValue(aRadius);
576       anAngleAttr->setValue(90.0);
577     }
578     else {
579       std::shared_ptr<GeomAPI_Circ2d> aCircle(
580           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
581
582       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
583       if (aCenter) {
584         aCenterAttr->setValue(aCenter);
585         if (theID == START_ID() || theID == END_ID())
586           updateDependentAttributes();
587         else
588           calculateArcAngleRadius(aCircle, aPoints[0], aPoints[1], aPoints[2],
589                                   anAngleAttr, aRadiusAttr);
590       }
591     }
592
593     data()->blockSendAttributeUpdated(false);
594   }
595   else if (anArcType == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
596     SketchPlugin_Sketch* aSketch = sketch();
597     AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
598         data()->attribute(TANGENT_POINT_ID()));
599
600     if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
601       data()->blockSendAttributeUpdated(true);
602       // compute orthogonal direction
603       std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
604       std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
605           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
606       std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
607       FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
608       std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
609           aTangFeature->lastResult()->shape());
610       if (aTangEdge->isLine()) {
611         std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
612         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
613         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
614         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
615       }
616       else if (aTangEdge->isArc()) {
617         std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
618         std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
619         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
620             new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
621       }
622
623       // compute parameters of the middle perpendicular
624       std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
625       std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
626       std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
627       std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
628           new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
629
630       // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
631       std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
632       std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
633       std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
634       if (aCenter) {
635         aCenterAttr->setValue(aCenter);
636         aStartAttr->setValue(aTangPnt2d);
637         updateDependentAttributes();
638       }
639
640       data()->blockSendAttributeUpdated(false);
641
642       if (theID == TANGENT_POINT_ID())
643         tangencyArcConstraints();
644     }
645   }
646 }
647
648 void SketchPlugin_Arc::setReversed(bool isReversed)
649 {
650   std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
651   myParamBefore = 0.0;
652 }
653
654 bool SketchPlugin_Arc::isReversed()
655 {
656   return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
657 }
658
659 void SketchPlugin_Arc::tangencyArcConstraints()
660 {
661   if (!lastResult())
662     return;
663
664   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
665       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
666   AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
667       attribute(TANGENT_POINT_ID()));
668   if (!aTangPtAttr->attr())
669     return;
670
671   FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
672   ObjectPtr aThisArc = aFeature->lastResult();
673   aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
674   ObjectPtr aTangFeature = aFeature->lastResult();
675
676   // trying to find constraints to fix the tangency of the arc
677   std::set<FeaturePtr> aCoincidence;
678   std::set<FeaturePtr> aTangency;
679
680   AttributeRefAttrPtr aRefAttrA, aRefAttrB;
681   std::set<AttributePtr> aRefs = data()->refsToMe();
682   const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
683   aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
684   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
685   for (; aRefIt != aRefs.end(); ++aRefIt) {
686     FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
687     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
688       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
689           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
690       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
691           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
692       if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
693           (aRefAttrB && aRefAttrB->attr() == aStartAttr))
694         aCoincidence.insert(aConstrFeature);
695     }
696     else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
697       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
698           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
699       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
700           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
701       if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
702           (aRefAttrB && aRefAttrB->object() == aThisArc))
703         aTangency.insert(aConstrFeature);
704     }
705   }
706   // search applicable pair of constraints
707   bool isFound = false;
708   FeaturePtr aPrevCoincidence, aPrevTangency;
709   std::set<FeaturePtr>::const_iterator aCIt, aTIt;
710   for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
711     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
712         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
713     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
714         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
715     AttributePtr anOtherPoint =
716         aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
717     for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
718       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
719           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
720       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
721           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
722       ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
723           aRefAttrB->object() : aRefAttrA->object();
724       if (anOtherPoint->owner() == anOtherObject) {
725         isFound = true;
726         aPrevCoincidence = *aCIt;
727         aPrevTangency = *aTIt;
728       }
729     }
730   }
731
732   if (isFound) {
733     // update previous constraints
734     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
735         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
736     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
737         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
738     if (aRefAttrA->attr() == aStartAttr)
739       aRefAttrB->setAttr(aTangPtAttr->attr());
740     else
741       aRefAttrA->setAttr(aTangPtAttr->attr());
742
743     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
744         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
745     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
746         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
747     if (aRefAttrA->object() == aThisArc)
748       aRefAttrB->setObject(aTangFeature);
749     else
750       aRefAttrA->setObject(aTangFeature);
751   } else {
752     // Wait all constraints being removed, then send update events
753     static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
754     bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
755     if (isDeleteFlushed)
756       Events_Loop::loop()->setFlushed(aDeleteEvent, false);
757     // Remove all obtained constraints which use current arc, because
758     // there is no information which of them were used to build tangency arc.
759     DocumentPtr aDoc = sketch()->document();
760     for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
761       aDoc->removeFeature(*aCIt);
762     for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
763       aDoc->removeFeature(*aTIt);
764     // Send events to update the sub-features by the solver.
765     if (isDeleteFlushed)
766       Events_Loop::loop()->setFlushed(aDeleteEvent, true);
767     else
768       Events_Loop::loop()->flush(aDeleteEvent);
769
770     // Wait all constraints being created, then send update events
771     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
772     bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
773     if (isUpdateFlushed)
774       Events_Loop::loop()->setFlushed(anUpdateEvent, false);
775
776     // Create new constraints
777     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
778     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
779         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
780     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
781         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
782     aRefAttrA->setAttr(aStartAttr);
783     aRefAttrB->setAttr(aTangPtAttr->attr());
784     aConstraint->execute();
785     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
786
787     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
788     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
789         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
790     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
791         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
792     aRefAttrA->setObject(aThisArc);
793     aRefAttrB->setObject(aTangFeature);
794     aConstraint->execute();
795     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
796
797     // Send events to update the sub-features by the solver.
798     if(isUpdateFlushed)
799       Events_Loop::loop()->setFlushed(anUpdateEvent, true);
800   }
801 }