Sketcher: Handle PyException in createGeneratedAutoConstraints

Command::doCommand may throw an exception that isn't handled by the active sketch handler. This may leave it in an inconsistent state and can cause a crash later.

See #14945
This commit is contained in:
wmayer
2024-06-26 11:30:10 +02:00
committed by Chris Hennes
parent 6640815c4d
commit 1eaf89b485

View File

@@ -859,11 +859,29 @@ protected:
* all the constraints stored in the AutoConstraints vector. */
void createGeneratedAutoConstraints(bool owncommand)
{
// add auto-constraints
if (owncommand) {
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add auto constraints"));
}
try {
// add auto-constraints
if (owncommand) {
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Add auto constraints"));
}
tryAddAutoConstraints();
if (owncommand) {
Gui::Command::commitCommand();
}
}
catch (const Base::PyException&) {
if (owncommand) {
Gui::Command::abortCommand();
}
}
}
/** @brief Convenience function to automatically add to the SketchObjects
* all the constraints stored in the AutoConstraints vector. */
void tryAddAutoConstraints()
{
auto autoConstraints = toPointerVector(AutoConstraints);
Gui::Command::doCommand(
@@ -871,10 +889,6 @@ protected:
Sketcher::PythonConverter::convert(Gui::Command::getObjectCmd(sketchgui->getObject()),
autoConstraints)
.c_str());
if (owncommand) {
Gui::Command::commitCommand();
}
}
/** @brief Convenience function to remove redundant autoconstraints from the AutoConstraints