From f6f75a6a0d9e3532cf05a2c20e3a00150d5df796 Mon Sep 17 00:00:00 2001 From: Florian Foinant-Willig Date: Mon, 16 Jun 2025 22:25:13 +0200 Subject: [PATCH] Sketcher: Fix circle-line negative distance --- src/Mod/Sketcher/App/planegcs/Constraints.cpp | 14 ++++++++++++-- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 8 ++++---- .../Sketcher/SketcherTests/TestSketcherSolver.py | 3 +-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Mod/Sketcher/App/planegcs/Constraints.cpp b/src/Mod/Sketcher/App/planegcs/Constraints.cpp index d6efa6ec5c..b6f564ca2b 100644 --- a/src/Mod/Sketcher/App/planegcs/Constraints.cpp +++ b/src/Mod/Sketcher/App/planegcs/Constraints.cpp @@ -2990,11 +2990,21 @@ void ConstraintC2LDistance::errorgrad(double* err, double* grad, double* param) double dh = (darea - h * dlength) / length; if (err) { - *err = *distance() + *circle.rad - h; + if (h < *circle.rad) { + *err = *circle.rad - *distance() - h; + } + else { + *err = *circle.rad + *distance() - h; + } } else if (grad) { if (param == distance() || param == circle.rad) { - *grad = 1.0; + if (h < *circle.rad) { + *grad = -1.0; + } + else { + *grad = 1.0; + } } else { *grad = -dh; diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 3619111a05..894cd063c3 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -2259,10 +2259,10 @@ protected: Base::Vector3d pnt1 = lineSeg->getStartPoint(); Base::Vector3d pnt2 = lineSeg->getEndPoint(); Base::Vector3d d = pnt2 - pnt1; - double ActDist = - std::abs(-center1.x * d.y + center1.y * d.x + pnt1.x * pnt2.y - pnt2.x * pnt1.y) - / d.Length() - - radius1; + double ActDist = std::abs( + std::abs(-center1.x * d.y + center1.y * d.x + pnt1.x * pnt2.y - pnt2.x * pnt1.y) + / d.Length() + - radius1); Gui::cmdAppObjectArgs(Obj, "addConstraint(Sketcher.Constraint('Distance',%d,%d,%f))", diff --git a/src/Mod/Sketcher/SketcherTests/TestSketcherSolver.py b/src/Mod/Sketcher/SketcherTests/TestSketcherSolver.py index 00c9676ac0..e88c6dc6e7 100644 --- a/src/Mod/Sketcher/SketcherTests/TestSketcherSolver.py +++ b/src/Mod/Sketcher/SketcherTests/TestSketcherSolver.py @@ -464,8 +464,7 @@ class TestSketcherSolver(unittest.TestCase): sketch.addConstraint( [Sketcher.Constraint("Block", c_idx), Sketcher.Constraint("Block", l_idx)] ) - # use a negative distance to tell "line is within the circle" - expected_distance = -radius / 2 # note that we don't set this in the constraint below! + expected_distance = radius / 2 # note that we don't set this in the constraint below! # TODO: addConstraint(constraint) triggers a solve (for godd reasons) however, this way # one cannot add non-driving constraints. In contrast, addConstraint(list(constraint)) # does not solve automatically, thus we use this "overload".