From 71192a91295369639a2b43cbbd99892ae6d9764c Mon Sep 17 00:00:00 2001 From: wandererfan Date: Wed, 18 Mar 2020 13:20:41 -0400 Subject: [PATCH] [TD]improve selection filtering for ArchSection --- src/Mod/TechDraw/Gui/Command.cpp | 72 ++++++++++------------------ src/Mod/TechDraw/Gui/DrawGuiUtil.cpp | 67 +++++++++++++++++++++++++- src/Mod/TechDraw/Gui/DrawGuiUtil.h | 4 ++ 3 files changed, 95 insertions(+), 48 deletions(-) diff --git a/src/Mod/TechDraw/Gui/Command.cpp b/src/Mod/TechDraw/Gui/Command.cpp index 36d6c9c35e..e28271917e 100644 --- a/src/Mod/TechDraw/Gui/Command.cpp +++ b/src/Mod/TechDraw/Gui/Command.cpp @@ -1157,56 +1157,34 @@ void CmdTechDrawArchView::activated(int iMsg) TechDraw::DrawPage* page = DrawGuiUtil::findPage(this); if (!page) { return; - } - - const std::vector objects = getSelection().getObjectsOfType(App::DocumentObject::getClassTypeId()); - if (objects.size() != 1) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Select exactly one object.")); - return; - } - //if the docObj doesn't have a Proxy property, it definitely isn't an ArchSection - App::DocumentObject* frontObj = objects.front(); - App::Property* proxy = frontObj->getPropertyByName("Proxy"); - if (proxy == nullptr) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Selected object is not ArchSection.")); - return; - } - App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); - Py::Object proxyObj = proxyPy->getValue(); - std::stringstream ss; - bool proceed = false; - if (proxyPy != nullptr) { - Base::PyGILStateLocker lock; - try { - if (proxyObj.hasAttr("__module__")) { - Py::String mod(proxyObj.getAttr("__module__")); - ss << (std::string)mod; - } - if (ss.str() == "ArchSectionPlane") { - proceed = true; - } - } - catch (Py::Exception&) { - Base::PyException e; // extract the Python error text - e.ReportException(); - proceed = false; - } - } else { - proceed = false; - } - - if (!proceed) { - QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), - QObject::tr("Selected object is not ArchSection.")); - return; - } - + } std::string PageName = page->getNameInDocument(); + + const std::vector objects = getSelection(). + getObjectsOfType(App::DocumentObject::getClassTypeId()); + App::DocumentObject* archObject = nullptr; + int archCount = 0; + for (auto& obj : objects) { + if (DrawGuiUtil::isArchSection(obj) ) { + archCount++; + archObject = obj; + } + } + if ( archCount > 1 ) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("Please select only 1 Arch Section.")); + return; + } + + if (archObject == nullptr) { + QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"), + QObject::tr("No Arch Sections in selection.")); + return; + } + std::string FeatName = getUniqueObjectName("ArchView"); - std::string SourceName = objects.front()->getNameInDocument(); + std::string SourceName = archObject->getNameInDocument(); openCommand("Create ArchView"); doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawViewArch','%s')",FeatName.c_str()); doCommand(Doc,"App.activeDocument().%s.Source = App.activeDocument().%s",FeatName.c_str(),SourceName.c_str()); diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp index e14b4a6954..91dda1ed68 100644 --- a/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.cpp @@ -96,7 +96,6 @@ void DrawGuiUtil::loadArrowBox(QComboBox* qcb) } } - //=========================================================================== // validate helper routines //=========================================================================== @@ -182,6 +181,72 @@ bool DrawGuiUtil::isDraftObject(App::DocumentObject* obj) return result; } +bool DrawGuiUtil::isArchObject(App::DocumentObject* obj) +{ + bool result = false; + App::Property* proxy = obj->getPropertyByName("Proxy"); + + if (proxy != nullptr) { + //if no proxy, can not be Arch obj + //if has proxy, might be Arch obj + App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); + Py::Object proxyObj = proxyPy->getValue(); + std::stringstream ss; + if (proxyPy != nullptr) { + Base::PyGILStateLocker lock; + try { + if (proxyObj.hasAttr("__module__")) { + Py::String mod(proxyObj.getAttr("__module__")); + ss << (std::string)mod; + //does this have to be an ArchSection, or can it be any Arch object? + if (ss.str().find("Arch") != std::string::npos) { + result = true; + } + } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + result = false; + } + } + } + return result; +} + +bool DrawGuiUtil::isArchSection(App::DocumentObject* obj) +{ + bool result = false; + App::Property* proxy = obj->getPropertyByName("Proxy"); + + if (proxy != nullptr) { + //if no proxy, can not be Arch obj + //if has proxy, might be Arch obj + App::PropertyPythonObject* proxyPy = dynamic_cast(proxy); + Py::Object proxyObj = proxyPy->getValue(); + std::stringstream ss; + if (proxyPy != nullptr) { + Base::PyGILStateLocker lock; + try { + if (proxyObj.hasAttr("__module__")) { + Py::String mod(proxyObj.getAttr("__module__")); + ss << (std::string)mod; + //does this have to be an ArchSection, or can it be other Arch objects? + if (ss.str().find("ArchSectionPlane") != std::string::npos) { + result = true; + } + } + } + catch (Py::Exception&) { + Base::PyException e; // extract the Python error text + e.ReportException(); + result = false; + } + } + } + return result; +} + bool DrawGuiUtil::needPage(Gui::Command* cmd) { //need a Document and a Page diff --git a/src/Mod/TechDraw/Gui/DrawGuiUtil.h b/src/Mod/TechDraw/Gui/DrawGuiUtil.h index 1829ae1bb7..94150c8f04 100644 --- a/src/Mod/TechDraw/Gui/DrawGuiUtil.h +++ b/src/Mod/TechDraw/Gui/DrawGuiUtil.h @@ -53,7 +53,11 @@ class TechDrawGuiExport DrawGuiUtil { Q_DECLARE_TR_FUNCTIONS(TechDrawGui::DrawGuiUtil) public: static TechDraw::DrawPage* findPage(Gui::Command* cmd); + static bool isDraftObject(App::DocumentObject* obj); + static bool isArchObject(App::DocumentObject* obj); + static bool isArchSection(App::DocumentObject* obj); + static bool needPage(Gui::Command* cmd); static bool needView(Gui::Command* cmd, bool partOnly = true); static void dumpRectF(const char* text, const QRectF& r);