+
+void SketchPlugin_Arc::attributeChanged(const std::string& theID)
+{
+ std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+ std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
+ GeomDataAPI_Point2D>(data()->attribute(END_ID()));
+ // the second condition for unability to move external segments anywhere
+ if (theID == EXTERNAL_ID() || isFixed()) {
+ std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+ // update arguments due to the selection value
+ if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
+ std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+ std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
+ if (aCirc.get()) {
+ aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
+ anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
+ aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
+ }
+ }
+ return;
+ }
+ if (!aCenterAttr->isInitialized())
+ return;
+ if (!aStartAttr->isInitialized())
+ return;
+ if (!anEndAttr->isInitialized())
+ return;
+
+ // update the points in accordance to the changed point changes
+ if (theID == END_ID() && !myEndUpdate) {
+ myEndUpdate = true;
+ // compute and change the arc end point
+ std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
+ new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
+ std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
+ if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance) {
+ // issue #855: trying to update only not-updated coordinate if it is possible
+ if (abs(myXEndBefore - anEndAttr->x()) < 1.e-10) { // keep Y unchanged
+ double aVy = aCenterAttr->y() - anEndAttr->y();
+ double aVy2 = aVy * aVy;
+ double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
+ if (aVy2 <= aR2) {
+ double aDX = sqrt(aR2 - aVy * aVy);
+ if (anEndAttr->x() > aCenterAttr->x())
+ aProjection->setX(aCenterAttr->x() + aDX);
+ else
+ aProjection->setX(aCenterAttr->x() - aDX);
+ aProjection->setY(anEndAttr->y());
+ }
+ } else if (abs(myYEndBefore - anEndAttr->y()) < 1.e-10) { // keep X unchanged
+ double aVx = aCenterAttr->x() - anEndAttr->x();
+ double aVx2 = aVx * aVx;
+ double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
+ if (aVx2 <= aR2) {
+ double aDY = sqrt(aR2 - aVx * aVx);
+ if (anEndAttr->y() > aCenterAttr->y())
+ aProjection->setY(aCenterAttr->y() + aDY);
+ else
+ aProjection->setY(aCenterAttr->y() - aDY);
+ aProjection->setX(anEndAttr->x());
+ }
+ }
+
+ anEndAttr->setValue(aProjection);
+ }
+ myXEndBefore = anEndAttr->x();
+ myYEndBefore = anEndAttr->y();
+ myEndUpdate = false;
+ } else if (theID == START_ID() && !myStartUpdate) {
+ myStartUpdate = true;
+ // compute and change the arc start point
+ std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
+ new GeomAPI_Circ2d(aCenterAttr->pnt(), anEndAttr->pnt()));
+ std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aStartAttr->pnt());
+ if (aProjection && aStartAttr->pnt()->distance(aProjection) > tolerance)
+ aStartAttr->setValue(aProjection);
+ myStartUpdate = false;
+ } else if (theID == CENTER_ID() && !myEndUpdate) {
+ myEndUpdate = true;
+ // compute and change the arc end point
+ std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
+ new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
+ std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
+ if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
+ anEndAttr->setValue(aProjection);
+ myEndUpdate = false;
+ }
+}