Sketcher: Fix circle-line negative distance

This commit is contained in:
Florian Foinant-Willig
2025-06-16 22:25:13 +02:00
committed by Chris Hennes
parent 4e30eb21c6
commit f6f75a6a0d
3 changed files with 17 additions and 8 deletions

View File

@@ -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;

View File

@@ -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))",

View File

@@ -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".