From f43339ad235e003f9cc0ae47de9fa100a4f73f4b Mon Sep 17 00:00:00 2001 From: wwmayer Date: Mon, 13 Jan 2025 17:42:40 +0100 Subject: [PATCH] Fix issues in ellipse creation (#18800) * Sketcher: Handle exceptions in onViewValueChanged Because the method onViewValueChanged is used as a Qt slot it must handle all possibly raised exceptions as otherwise they will slip through GUIApplication::notify() that usually results into a crash on some platforms like macOS * Sketch: Handle undefined values in calculateThroughPointMinorAxisParameters * The argument of acos() must be in the range [-1, +1], otherwise it returns nan (not a number) * The value of sin(0) = 0. So, it cannot be used in the denominator of a fraction --- src/Mod/Sketcher/Gui/DrawSketchController.h | 16 ++++++++++++++++ src/Mod/Sketcher/Gui/DrawSketchHandlerEllipse.h | 15 ++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Mod/Sketcher/Gui/DrawSketchController.h b/src/Mod/Sketcher/Gui/DrawSketchController.h index 819e8ebf0e..7f0347953d 100644 --- a/src/Mod/Sketcher/Gui/DrawSketchController.h +++ b/src/Mod/Sketcher/Gui/DrawSketchController.h @@ -23,6 +23,7 @@ #ifndef SKETCHERGUI_DrawSketchController_H #define SKETCHERGUI_DrawSketchController_H +#include #include #include @@ -350,6 +351,21 @@ public: * It is intended to remote control the DrawSketchDefaultWidgetHandler */ void onViewValueChanged(int onviewparameterindex, double value) + { + // Since this method is used as a Qt slot we must handle + // all exceptions here + try { + tryViewValueChanged(onviewparameterindex, value); + } + catch (const Base::Exception& e) { + e.ReportException(); + } + catch (const std::exception& e) { + Base::Console().Error("C++ exception in onViewValueChanged: %s\n", e.what()); + } + } + + void tryViewValueChanged(int onviewparameterindex, double value) { int nextindex = onviewparameterindex + 1; if (isOnViewParameterOfCurrentMode(nextindex)) { diff --git a/src/Mod/Sketcher/Gui/DrawSketchHandlerEllipse.h b/src/Mod/Sketcher/Gui/DrawSketchHandlerEllipse.h index 5c319ba18a..1996013ef9 100644 --- a/src/Mod/Sketcher/Gui/DrawSketchHandlerEllipse.h +++ b/src/Mod/Sketcher/Gui/DrawSketchHandlerEllipse.h @@ -325,9 +325,18 @@ private: auto lprojx = projx.Length(); // Px = a cos t auto lprojy = projy.Length(); // Py = b sin t - double t = std::acos(lprojx / firstRadius); - - secondRadius = lprojy / std::sin(t); // b = Py / sin t + if (lprojx > firstRadius) { + secondRadius = 0.0; + } + else { + double t = std::acos(lprojx / firstRadius); + if (t == 0.0) { + secondRadius = 0.0; + } + else { + secondRadius = lprojy / std::sin(t); // b = Py / sin t + } + } secondAxis = projy.Normalize() * secondRadius; }