From 1b7fbeb622f6a9231b50451f2db216ab1defd2a1 Mon Sep 17 00:00:00 2001 From: jrheinlaender Date: Thu, 18 Apr 2013 17:24:33 +0430 Subject: [PATCH] Update ActivePartObject etc. on switching documents, creating new documents, and loading documents in the PartDesign workbench --- src/Mod/PartDesign/Gui/Command.cpp | 2 +- src/Mod/PartDesign/Gui/Workbench.cpp | 74 ++++++++++++++++++--------- src/Mod/PartDesign/Gui/Workbench.h | 5 ++ src/Mod/Start/StartPage/PartDesign.py | 1 - 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/Mod/PartDesign/Gui/Command.cpp b/src/Mod/PartDesign/Gui/Command.cpp index ca74c343c..8430d0fed 100644 --- a/src/Mod/PartDesign/Gui/Command.cpp +++ b/src/Mod/PartDesign/Gui/Command.cpp @@ -749,7 +749,6 @@ void finishSketchBased(const Gui::Command* cmd, const Part::Part2DObject* sketch ,grp->getNameInDocument(),sketch->getNameInDocument()); } - cmd->updateActive(); PartDesign::Body *pcActiveBody = PartDesignGui::getBody(); if (cmd->isActiveObjectValid()) { cmd->doCommand(cmd->Gui,"Gui.activeDocument().hide(\"%s\")", sketch->getNameInDocument()); @@ -759,6 +758,7 @@ void finishSketchBased(const Gui::Command* cmd, const Part::Part2DObject* sketch cmd->doCommand(cmd->Gui,"Gui.activeDocument().hide(\"%s\")", prevSolidFeature->getNameInDocument()); } } + cmd->updateActive(); // #0001721: use '0' as edit value to avoid switching off selection in // ViewProviderGeometryObject::setEditViewer cmd->doCommand(cmd->Gui,"Gui.activeDocument().setEdit('%s', 0)", FeatName.c_str()); diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index b4a0538f8..eb4bead40 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -25,17 +25,20 @@ #ifndef _PreComp_ # include +# include #endif #include "Workbench.h" -#include +#include #include #include +#include #include #include #include #include +#include #include #include #include @@ -75,12 +78,57 @@ TYPESYSTEM_SOURCE(PartDesignGui::Workbench, Gui::StdWorkbench) Workbench::Workbench() { + // Let us be notified when a document is activated, so that we can update the ActivePartObject + Gui::Application::Instance->signalActiveDocument.connect(boost::bind(&Workbench::slotActiveDocument, this, _1)); + App::GetApplication().signalNewDocument.connect(boost::bind(&Workbench::slotNewDocument, this, _1)); + App::GetApplication().signalFinishRestoreDocument.connect(boost::bind(&Workbench::slotFinishRestoreDocument, this, _1)); } Workbench::~Workbench() { } +void switchToDocument(const App::Document* doc) +{ + if (doc == NULL) return; + + PartDesign::Body* activeBody = NULL; + std::vector bodies = doc->getObjectsOfType(PartDesign::Body::getClassTypeId()); + + for (std::vector::const_iterator b = bodies.begin(); b != bodies.end(); b++) { + PartDesign::Body* body = static_cast(*b); + if (body->IsActive.getValue()) { + activeBody = body; + break; + } + } + + // If there is only one body, make it active + if ((activeBody == NULL) && (bodies.size() == 1)) + activeBody = static_cast(bodies.front()); + + if (activeBody != NULL) { + Gui::Command::doCommand(Gui::Command::Doc,"import PartDesignGui"); + Gui::Command::doCommand(Gui::Command::Gui,"PartDesignGui.setActivePart(App.activeDocument().%s)", activeBody->getNameInDocument()); + } +} + +void Workbench::slotActiveDocument(const Gui::Document& Doc) +{ + switchToDocument(Doc.getDocument()); +} + +void Workbench::slotNewDocument(const App::Document& Doc) +{ + Gui::Command::runCommand(Gui::Command::Doc, "FreeCADGui.runCommand('PartDesign_Body')"); + switchToDocument(&Doc); +} + +void Workbench::slotFinishRestoreDocument(const App::Document& Doc) +{ + switchToDocument(&Doc); +} + void Workbench::setupContextMenu(const char* recipient, Gui::MenuItem* item) const { if (strcmp(recipient,"Tree") == 0) @@ -249,28 +297,7 @@ void Workbench::activated() )); // make the previously used active Body active again - PartDesign::Body* activeBody = NULL; - App::Document* activeDocument = App::GetApplication().getActiveDocument(); - if (activeDocument != NULL) { - std::vector bodies = activeDocument->getObjectsOfType(PartDesign::Body::getClassTypeId()); - for (std::vector::const_iterator b = bodies.begin(); b != bodies.end(); b++) { - PartDesign::Body* body = static_cast(*b); - if (body->IsActive.getValue()) { - activeBody = body; - break; - } - } - - // If there is only one body, make it active - if ((activeBody == NULL) && (bodies.size() == 1)) - activeBody = static_cast(bodies.front()); - } - - - if (activeBody != NULL) { - Gui::Command::doCommand(Gui::Command::Doc,"import PartDesignGui"); - Gui::Command::doCommand(Gui::Command::Gui,"PartDesignGui.setActivePart(App.activeDocument().%s)", activeBody->getNameInDocument()); - } + switchToDocument(App::GetApplication().getActiveDocument()); addTaskWatcher(Watcher); Gui::Control().showTaskView(); @@ -287,7 +314,6 @@ void Workbench::deactivated() Gui::Workbench::deactivated(); - } Gui::MenuItem* Workbench::setupMenuBar() const diff --git a/src/Mod/PartDesign/Gui/Workbench.h b/src/Mod/PartDesign/Gui/Workbench.h index c2e3d0f28..51d1c5b3d 100644 --- a/src/Mod/PartDesign/Gui/Workbench.h +++ b/src/Mod/PartDesign/Gui/Workbench.h @@ -74,6 +74,11 @@ protected: Gui::MenuItem* setupMenuBar() const; Gui::ToolBarItem* setupToolBars() const; Gui::ToolBarItem* setupCommandBars() const; + +private: + void slotActiveDocument(const Gui::Document&); + void slotFinishRestoreDocument(const App::Document&); + void slotNewDocument(const App::Document&); }; } // namespace PartDesignGui diff --git a/src/Mod/Start/StartPage/PartDesign.py b/src/Mod/Start/StartPage/PartDesign.py index bf59c27db..fc77976f6 100644 --- a/src/Mod/Start/StartPage/PartDesign.py +++ b/src/Mod/Start/StartPage/PartDesign.py @@ -24,7 +24,6 @@ import FreeCADGui FreeCADGui.activateWorkbench("PartDesignWorkbench") App.newDocument() -FreeCADGui.runCommand('PartDesign_Body') # Make the planes properly visible FreeCADGui.activeDocument().activeView().viewAxometric() FreeCADGui.SendMsgToActiveView("ViewFit")