From aaeea1784f58de0d79e3dd7c55bca447d93e4813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9as=20Livet?= Date: Thu, 8 Dec 2022 09:41:37 +0100 Subject: [PATCH] Button are activated only if view is selected --- ahb_cmd_new_step.py | 3 ++- ahb_cmd_view_annotate.py | 4 +++- ahb_cmd_view_edit_source_parts.py | 6 +++--- ahb_cmd_view_set_direction.py | 12 +++--------- ahb_utils.py | 8 ++++++++ 5 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 ahb_utils.py diff --git a/ahb_cmd_new_step.py b/ahb_cmd_new_step.py index 4b29f4b..074ea8e 100644 --- a/ahb_cmd_new_step.py +++ b/ahb_cmd_new_step.py @@ -1,6 +1,7 @@ import FreeCADGui as Gui import FreeCAD as App +import ahb_utils class AHB_New_Step: def GetResources(self): return {"MenuText": "New Step", @@ -9,7 +10,7 @@ class AHB_New_Step: } def IsActive(self): - return True + return ahb_utils.getCurrentView() is not None def Activated(self): import re diff --git a/ahb_cmd_view_annotate.py b/ahb_cmd_view_annotate.py index bc63c3d..a206935 100644 --- a/ahb_cmd_view_annotate.py +++ b/ahb_cmd_view_annotate.py @@ -1,6 +1,8 @@ import FreeCADGui as Gui import FreeCAD as App +import ahb_utils + class AHB_View_Annotate: def GetResources(self): return {"MenuText": "Annotate view", @@ -9,7 +11,7 @@ class AHB_View_Annotate: } def IsActive(self): - return True + return ahb_utils.getCurrentView() is not None def Activated(self): workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench diff --git a/ahb_cmd_view_edit_source_parts.py b/ahb_cmd_view_edit_source_parts.py index e0747aa..b03c318 100644 --- a/ahb_cmd_view_edit_source_parts.py +++ b/ahb_cmd_view_edit_source_parts.py @@ -1,6 +1,8 @@ import FreeCADGui as Gui import FreeCAD as App +import ahb_utils + class AHB_EditViewSourceParts: def GetResources(self): return {"MenuText": "Edit view source parts", @@ -19,9 +21,7 @@ class AHB_EditViewSourceParts: def Activated(self): workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench - view = None - if len(Gui.Selection.getSelection()) == 1 and Gui.Selection.getSelection()[0].TypeId == 'TechDraw::DrawViewPart': - view = Gui.Selection.getSelection()[0] + view = ahb_utils.getCurrentView() workbench.techDrawExtensions.toggleEditViewSourceParts(view) class AHB_AddSourcePartsToView: diff --git a/ahb_cmd_view_set_direction.py b/ahb_cmd_view_set_direction.py index d657491..c839e54 100644 --- a/ahb_cmd_view_set_direction.py +++ b/ahb_cmd_view_set_direction.py @@ -1,6 +1,7 @@ import FreeCADGui as Gui import FreeCAD as App +import ahb_utils class AHB_SetViewDirection: def GetResources(self): return {"MenuText": "Set view direction", @@ -9,22 +10,15 @@ class AHB_SetViewDirection: } def IsActive(self): - view = self._get_view() - return view is not None + return ahb_utils.getCurrentView() is not None def Activated(self): workbench = Gui.getWorkbench("AssemblyHandbookWorkbench") #: :type workbench: AssemblyHandbookWorkbench - view = self._get_view() + view = ahb_utils.getCurrentView() if view is None: raise Exception("Please select a TechDraw view") workbench.techDrawExtensions.setCurrentViewDirection(view) - def _get_view(self): - view = None if len(Gui.Selection.getSelection()) == 0 else Gui.Selection.getSelection()[0] - if view is not None and view.TypeId != 'TechDraw::DrawViewPart': - view = None - return view - from ahb_command import AHB_CommandWrapper AHB_CommandWrapper.addGuiCommand('AHB_view_set_direction', AHB_SetViewDirection()) diff --git a/ahb_utils.py b/ahb_utils.py new file mode 100644 index 0000000..6d66ff1 --- /dev/null +++ b/ahb_utils.py @@ -0,0 +1,8 @@ +import FreeCADGui as Gui + +def getCurrentView(): + currentSel = Gui.Selection.getSelection() + if len(currentSel) == 1 and currentSel[0].TypeId == 'TechDraw::DrawViewPart': + return currentSel[0] + else: + return None \ No newline at end of file