From a836e0d6560336cd8f5474d30e5720bd30f6eade Mon Sep 17 00:00:00 2001 From: mwganson Date: Mon, 30 Sep 2024 18:34:08 -0500 Subject: [PATCH] [Part Workbench] Fix issue with Sketcher_NewSketch icon moving on changing back to workbench --- src/Mod/Part/Gui/Workbench.cpp | 35 ++++++++++++++++++++++++++++------ src/Mod/Part/Gui/Workbench.h | 3 +++ src/Mod/Part/InitGui.py | 17 ----------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/Mod/Part/Gui/Workbench.cpp b/src/Mod/Part/Gui/Workbench.cpp index 20dc2b0738..a43de94bd5 100644 --- a/src/Mod/Part/Gui/Workbench.cpp +++ b/src/Mod/Part/Gui/Workbench.cpp @@ -24,6 +24,7 @@ #include "PreCompiled.h" #include "Workbench.h" +#include #include #include @@ -46,7 +47,25 @@ using namespace PartGui; /// @namespace PartGui @class Workbench TYPESYSTEM_SOURCE(PartGui::Workbench, Gui::StdWorkbench) -Workbench::Workbench() = default; +Workbench::Workbench() { + /** If we are to have Sketcher_NewSketch as command in toolbar and menu, + then we must assure SketcherGui has already been loaded. + By putting this in a try/except block we avoid creating a dependency + on sketcher workbench as the import will silently fail if sketcher wb is not built. + Note that BUILD_SKETCHER is a cmake-gui option. + **/ + + const char* code = + "try:\n" + " import SketcherGui\n" + " success = 'True'\n" + "except ImportError:\n" + " success = 'False'"; + + const std::string result = Base::Interpreter().runStringWithKey(code, "success", "False"); + hasSketcher = (result == "True"); + +} Workbench::~Workbench() = default; @@ -121,9 +140,11 @@ Gui::MenuItem* Workbench::setupMenuBar() const << "Materials_InspectMaterial" << "Separator" << bop << join << split << compound - << "Separator" - << "Sketcher_NewSketch" - << "Part_Extrude" + << "Separator"; + if (hasSketcher) { + *part << "Sketcher_NewSketch"; + } + *part << "Part_Extrude" << "Part_Revolve" << "Part_Mirror" << "Part_Scale" @@ -171,8 +192,10 @@ Gui::ToolBarItem* Workbench::setupToolBars() const Gui::ToolBarItem* tool = new Gui::ToolBarItem(root); tool->setCommand("Part tools"); - *tool << "Sketcher_NewSketch" - << "Part_Extrude" + if (hasSketcher) { + *tool << "Sketcher_NewSketch"; + } + *tool << "Part_Extrude" << "Part_Revolve" << "Part_Mirror" << "Part_Scale" diff --git a/src/Mod/Part/Gui/Workbench.h b/src/Mod/Part/Gui/Workbench.h index 7c2134ab48..d05512877c 100644 --- a/src/Mod/Part/Gui/Workbench.h +++ b/src/Mod/Part/Gui/Workbench.h @@ -40,6 +40,9 @@ public: Workbench(); ~Workbench() override; +private: + bool hasSketcher = false; + protected: Gui::MenuItem* setupMenuBar() const override; Gui::ToolBarItem* setupToolBars() const override; diff --git a/src/Mod/Part/InitGui.py b/src/Mod/Part/InitGui.py index b3557cfe78..5e3fecaea6 100644 --- a/src/Mod/Part/InitGui.py +++ b/src/Mod/Part/InitGui.py @@ -38,21 +38,6 @@ class PartWorkbench(Gui.Workbench): self.__class__.MenuText = "Part" self.__class__.ToolTip = "Part workbench" - def tryAddManipulator(self): - try: - import SketcherGui - - class Manipulator: - def modifyToolBars(self): - return [{"insert" : "Sketcher_NewSketch", "toolItem" : "Part_Extrude"}] - def modifyMenuBar(self): - return [{"insert" : "Sketcher_NewSketch", "menuItem" : "Part_Extrude"}] - - manip = Manipulator() - Gui.addWorkbenchManipulator(manip) - except ImportError as err: - pass - def Initialize(self): # load the module import PartGui @@ -79,8 +64,6 @@ class PartWorkbench(Gui.Workbench): App.Console.PrintError("'BOPTools' package cannot be loaded. " "{err}\n".format(err=str(err))) - self.tryAddManipulator() - def GetClassName(self): return "PartGui::Workbench"