From 37caf53dfdebe0a0bf38b97e33a8286f45fe8a42 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Sun, 8 Dec 2019 08:50:39 +0800 Subject: [PATCH] Part: fix TaskAttacher::visibilityAutomation() Rename Python variable 'tv' to avoid potential conflict with others (PS. I can't reproduce error caused by this. But there is no harm doing it either). Handle editing attachment through an App::Link. --- src/Mod/Part/Gui/TaskAttacher.cpp | 46 +++++++++++++------ src/Mod/Part/Gui/TaskAttacher.h | 4 +- .../Gui/TaskPrimitiveParameters.cpp | 20 ++++---- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/Mod/Part/Gui/TaskAttacher.cpp b/src/Mod/Part/Gui/TaskAttacher.cpp index 796ac121c6..7dc2b33d44 100644 --- a/src/Mod/Part/Gui/TaskAttacher.cpp +++ b/src/Mod/Part/Gui/TaskAttacher.cpp @@ -937,33 +937,38 @@ void TaskAttacher::changeEvent(QEvent *e) void TaskAttacher::visibilityAutomation(bool opening_not_closing) { auto defvisfunc = [] (bool opening_not_closing, + const std::string &postfix, Gui::ViewProviderDocumentObject* vp, App::DocumentObject *editObj, const std::string& editSubName) { if (opening_not_closing) { QString code = QString::fromLatin1( "import Show\n" - "tv = Show.TempoVis(App.ActiveDocument, tag= 'PartGui::TaskAttacher')\n" + "_tv_%4 = Show.TempoVis(App.ActiveDocument, tag= 'PartGui::TaskAttacher')\n" "tvObj = %1\n" - "dep_features = tv.get_all_dependent(%2, '%3')\n" + "dep_features = _tv_%4.get_all_dependent(%2, '%3')\n" "if tvObj.isDerivedFrom('PartDesign::CoordinateSystem'):\n" "\tvisible_features = [feat for feat in tvObj.InList if feat.isDerivedFrom('PartDesign::FeaturePrimitive')]\n" "\tdep_features = [feat for feat in dep_features if feat not in visible_features]\n" "\tdel(visible_features)\n" - "tv.hide(dep_features)\n" + "_tv_%4.hide(dep_features)\n" "del(dep_features)\n" "if not tvObj.isDerivedFrom('PartDesign::CoordinateSystem'):\n" "\t\tif len(tvObj.Support) > 0:\n" - "\t\t\ttv.show([lnk[0] for lnk in tvObj.Support])\n" + "\t\t\t_tv_%4.show([lnk[0] for lnk in tvObj.Support])\n" "del(tvObj)" ).arg( QString::fromLatin1(Gui::Command::getObjectCmd(vp->getObject()).c_str()), QString::fromLatin1(Gui::Command::getObjectCmd(editObj).c_str()), - QString::fromLatin1(editSubName.c_str())); + QString::fromLatin1(editSubName.c_str()), + QString::fromLatin1(postfix.c_str())); + Gui::Command::runCommand(Gui::Command::Gui,code.toLatin1().constData()); + } else if(postfix.size()) { + QString code = QString::fromLatin1( + "_tv_%1.restore()\n" + "del(_tv_%1)" + ).arg(QString::fromLatin1(postfix.c_str())); Gui::Command::runCommand(Gui::Command::Gui,code.toLatin1().constData()); - } - else { - Base::Interpreter().runString("del(tv)"); } }; @@ -981,14 +986,23 @@ void TaskAttacher::visibilityAutomation(bool opening_not_closing) auto editDoc = Gui::Application::Instance->editDocument(); App::DocumentObject *editObj = ViewProvider->getObject(); std::string editSubName; - ViewProviderDocumentObject *editVp = 0; - if (editDoc) { - editDoc->getInEdit(&editVp,&editSubName); - if (editVp) - editObj = editVp->getObject(); + auto sels = Gui::Selection().getSelection(0,0,true); + if(sels.size() && sels[0].pResolvedObject + && sels[0].pResolvedObject->getLinkedObject()==editObj) + { + editObj = sels[0].pObject; + editSubName = sels[0].SubName; + } else { + ViewProviderDocumentObject *editVp = 0; + if (editDoc) { + editDoc->getInEdit(&editVp,&editSubName); + if (editVp) + editObj = editVp->getObject(); + } } + ObjectName = ViewProvider->getObject()->getNameInDocument(); try { - visAutoFunc(opening_not_closing, ViewProvider, editObj, editSubName); + visAutoFunc(opening_not_closing, ObjectName, ViewProvider, editObj, editSubName); } catch (const Base::Exception &e){ e.ReportException(); @@ -1000,7 +1014,9 @@ void TaskAttacher::visibilityAutomation(bool opening_not_closing) } else { try { - visAutoFunc(opening_not_closing, nullptr, nullptr, std::string()); + std::string objName; + objName.swap(ObjectName); + visAutoFunc(opening_not_closing, objName, nullptr, nullptr, std::string()); } catch (Base::Exception &e) { e.ReportException(); diff --git a/src/Mod/Part/Gui/TaskAttacher.h b/src/Mod/Part/Gui/TaskAttacher.h index 206e90a372..ee1a4a3abc 100644 --- a/src/Mod/Part/Gui/TaskAttacher.h +++ b/src/Mod/Part/Gui/TaskAttacher.h @@ -53,7 +53,8 @@ class PartGuiExport TaskAttacher : public Gui::TaskView::TaskBox, public Gui::Se Q_OBJECT public: - typedef boost::function VisibilityFunction; + typedef boost::function VisibilityFunction; TaskAttacher(Gui::ViewProviderDocumentObject *ViewProvider, QWidget *parent = 0, QString picture = QString(), @@ -125,6 +126,7 @@ private: protected: Gui::ViewProviderDocumentObject *ViewProvider; + std::string ObjectName; private: QWidget* proxy; diff --git a/src/Mod/PartDesign/Gui/TaskPrimitiveParameters.cpp b/src/Mod/PartDesign/Gui/TaskPrimitiveParameters.cpp index 671d0a76c5..3437b7e406 100644 --- a/src/Mod/PartDesign/Gui/TaskPrimitiveParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskPrimitiveParameters.cpp @@ -748,33 +748,37 @@ TaskPrimitiveParameters::TaskPrimitiveParameters(ViewProviderPrimitive* Primitiv // handle visibility automation differently to the default method auto customvisfunc = [] (bool opening_not_closing, + const std::string &postfix, Gui::ViewProviderDocumentObject* vp, App::DocumentObject *editObj, const std::string& editSubName) { if (opening_not_closing) { QString code = QString::fromLatin1( "import Show\n" - "tv = Show.TempoVis(App.ActiveDocument, tag= 'PartGui::TaskAttacher')\n" + "_tv_%4 = Show.TempoVis(App.ActiveDocument, tag= 'PartGui::TaskAttacher')\n" "tvObj = %1\n" - "dep_features = tv.get_all_dependent(%2, '%3')\n" + "dep_features = _tv_%4.get_all_dependent(%2, '%3')\n" "if tvObj.isDerivedFrom('PartDesign::CoordinateSystem'):\n" "\tvisible_features = [feat for feat in tvObj.InList if feat.isDerivedFrom('PartDesign::FeaturePrimitive')]\n" "\tdep_features = [feat for feat in dep_features if feat not in visible_features]\n" "\tdel(visible_features)\n" - "tv.hide(dep_features)\n" + "_tv_%4.hide(dep_features)\n" "del(dep_features)\n" "del(tvObj)" ).arg( QString::fromLatin1(Gui::Command::getObjectCmd(vp->getObject()).c_str()), QString::fromLatin1(Gui::Command::getObjectCmd(editObj).c_str()), - QString::fromLatin1(editSubName.c_str())); + QString::fromLatin1(editSubName.c_str()), + QString::fromLatin1(postfix.c_str())); + Gui::Command::runCommand(Gui::Command::Gui,code.toLatin1().constData()); + } else if(postfix.size()) { + QString code = QString::fromLatin1( + "_tv_%1.restore()\n" + "del(_tv_%1)" + ).arg(QString::fromLatin1(postfix.c_str())); Gui::Command::runCommand(Gui::Command::Gui,code.toLatin1().constData()); } - else { - Base::Interpreter().runString("del(tv)"); - } }; - parameter = new PartGui::TaskAttacher(PrimitiveView, nullptr, QString(), tr("Attachment"), customvisfunc); Content.push_back(parameter); }