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