From 244282f855767755d0d3064658656d1e3d830226 Mon Sep 17 00:00:00 2001 From: marbocub Date: Tue, 23 Dec 2025 02:18:00 +0900 Subject: [PATCH] Fix rotation expression errors in handleLegacyTangentPlaneOrientation() (#26058) * Fix rotation expression handling - Make added rotation angle unit the same as the original expression unit - Keep rotated angle expressions within the accepted [-180, +180] range --- src/Mod/Part/App/AttachExtension.cpp | 41 +++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Mod/Part/App/AttachExtension.cpp b/src/Mod/Part/App/AttachExtension.cpp index cd812a61d3..f6b29b2ca4 100644 --- a/src/Mod/Part/App/AttachExtension.cpp +++ b/src/Mod/Part/App/AttachExtension.cpp @@ -607,6 +607,31 @@ void AttachExtension::handleLegacyTangentPlaneOrientation() } // convert placement and expressions according to the dominant axis + auto makeRotatedExpression = + [owner](const App::Expression* expr, double angle) -> App::Expression* { + if (!expr) { + return nullptr; + } + + std::string unitSafeExprStr = "(" + expr->toString() + ")"; + if (angle >= 0) { + unitSafeExprStr += " + " + std::to_string(angle); + } + else { + unitSafeExprStr += " - " + std::to_string(-angle); + } + + if (const App::Expression* simple = expr->eval()) { + if (auto ue = dynamic_cast(simple)) { + const auto& q = ue->getQuantity(); + if (q.getUnit() == Base::Unit::Angle) { + unitSafeExprStr += " deg"; + } + } + } + + return App::ExpressionParser::parse(owner, unitSafeExprStr.c_str()); + }; App::Expression* newExprX = nullptr; App::Expression* newExprY = nullptr; App::Expression* newExprYaw = nullptr; @@ -625,8 +650,12 @@ void AttachExtension::handleLegacyTangentPlaneOrientation() newExprX = App::ExpressionParser::parse(owner, expr.c_str()); } if (exprYaw) { - std::string expr = "(" + exprYaw->toString() + ") + 90 deg"; - newExprYaw = App::ExpressionParser::parse(owner, expr.c_str()); + if (yaw > 90) { + newExprYaw = makeRotatedExpression(exprYaw, -270); + } + else { + newExprYaw = makeRotatedExpression(exprYaw, 90); + } } } else if (axis == 1) { // normal mostly Y @@ -644,8 +673,12 @@ void AttachExtension::handleLegacyTangentPlaneOrientation() newExprX = App::ExpressionParser::parse(owner, exprY->toString().c_str()); } if (exprYaw) { - std::string expr = "(" + exprYaw->toString() + ") - 90 deg"; - newExprYaw = App::ExpressionParser::parse(owner, expr.c_str()); + if (yaw < -90) { + newExprYaw = makeRotatedExpression(exprYaw, 270); + } + else { + newExprYaw = makeRotatedExpression(exprYaw, -90); + } } } else {