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