From 6c494623d4f34aee8ce9663da74517bc2d055b3a Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Thu, 3 Oct 2024 16:49:09 +0200 Subject: [PATCH] QuickMeasure: Prevent crash by limiting selection to Part::Features (#16921) * QuickMeasure: Prevent crash by limiting selection to Part::Features * chennes' fix Co-authored-by: Chris Hennes --------- Co-authored-by: Chris Hennes --- src/Mod/Measure/Gui/QuickMeasure.cpp | 30 ++++++++++++++-------------- src/Mod/Measure/Gui/QuickMeasure.h | 3 ++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Mod/Measure/Gui/QuickMeasure.cpp b/src/Mod/Measure/Gui/QuickMeasure.cpp index 4762f18c77..a2ba43d5c2 100644 --- a/src/Mod/Measure/Gui/QuickMeasure.cpp +++ b/src/Mod/Measure/Gui/QuickMeasure.cpp @@ -67,7 +67,7 @@ QuickMeasure::~QuickMeasure() void QuickMeasure::onSelectionChanged(const Gui::SelectionChanges& msg) { - if (canMeasureSelection(msg)) { + if (shouldMeasure(msg)) { if (!pendingProcessing) { selectionTimer->start(100); } @@ -109,7 +109,7 @@ void QuickMeasure::tryMeasureSelection() printResult(); } -bool QuickMeasure::canMeasureSelection(const Gui::SelectionChanges& msg) const +bool QuickMeasure::shouldMeasure(const Gui::SelectionChanges& msg) const { if (msg.Type == Gui::SelectionChanges::SetPreselect || msg.Type == Gui::SelectionChanges::RmvPreselect) { @@ -120,22 +120,22 @@ bool QuickMeasure::canMeasureSelection(const Gui::SelectionChanges& msg) const return doc != nullptr; } +bool QuickMeasure::isObjAcceptable(App::DocumentObject* obj) +{ + if (!obj || !obj->isDerivedFrom(Part::Feature::getClassTypeId())) { + return false; + } + + std::string vpType = obj->getViewProviderName(); + auto* vp = Gui::Application::Instance->getViewProvider(obj); + return !(vpType == "SketcherGui::ViewProviderSketch" && vp && vp->isEditing()); +} + void QuickMeasure::addSelectionToMeasurement() { int count = 0; int limit = 100; - // Lambda function to check whether to continue - auto shouldSkip = [](App::DocumentObject* obj) { - std::string vpType = obj->getViewProviderName(); - auto* vp = Gui::Application::Instance->getViewProvider(obj); - return (vpType == "SketcherGui::ViewProviderSketch" && vp->isEditing()) - || vpType.find("Gui::ViewProviderOrigin") != std::string::npos - || vpType.find("Gui::ViewProviderPart") != std::string::npos - || vpType.find("SpreadsheetGui") != std::string::npos - || vpType.find("TechDrawGui") != std::string::npos; - }; - auto selObjs = Gui::Selection().getSelectionEx(nullptr, App::DocumentObject::getClassTypeId(), Gui::ResolveMode::NoResolve); @@ -152,7 +152,7 @@ void QuickMeasure::addSelectionToMeasurement() } if (subNames.empty()) { - if (!shouldSkip(rootObj)) { + if (isObjAcceptable(rootObj)) { measurement->addReference3D(rootObj, ""); } continue; @@ -161,7 +161,7 @@ void QuickMeasure::addSelectionToMeasurement() for (auto& subName : subNames) { App::DocumentObject* obj = rootObj->getSubObject(subName.c_str()); - if (shouldSkip(obj)) { + if (!isObjAcceptable(obj)) { continue; } measurement->addReference3D(rootObj, subName); diff --git a/src/Mod/Measure/Gui/QuickMeasure.h b/src/Mod/Measure/Gui/QuickMeasure.h index 66c5ea360b..41ce72d5b7 100644 --- a/src/Mod/Measure/Gui/QuickMeasure.h +++ b/src/Mod/Measure/Gui/QuickMeasure.h @@ -52,8 +52,9 @@ private: void onSelectionChanged(const Gui::SelectionChanges& msg) override; void tryMeasureSelection(); - bool canMeasureSelection(const Gui::SelectionChanges& msg) const; + bool shouldMeasure(const Gui::SelectionChanges& msg) const; void addSelectionToMeasurement(); + bool isObjAcceptable(App::DocumentObject* obj); void printResult(); void print(const QString& message);