From 2843bbefc76d992236ba902f128399a46ca41504 Mon Sep 17 00:00:00 2001 From: paddle Date: Thu, 24 Jul 2025 14:13:47 +0200 Subject: [PATCH] Assembly: Add 'Activate assembly' command. --- src/Mod/Assembly/CommandCreateAssembly.py | 79 +++- src/Mod/Assembly/Gui/Resources/Assembly.qrc | 1 + .../icons/Assembly_ActivateAssembly.svg | 388 ++++++++++++++++++ 3 files changed, 467 insertions(+), 1 deletion(-) create mode 100644 src/Mod/Assembly/Gui/Resources/icons/Assembly_ActivateAssembly.svg diff --git a/src/Mod/Assembly/CommandCreateAssembly.py b/src/Mod/Assembly/CommandCreateAssembly.py index cb1f3ed143..6a9af33bc7 100644 --- a/src/Mod/Assembly/CommandCreateAssembly.py +++ b/src/Mod/Assembly/CommandCreateAssembly.py @@ -27,11 +27,12 @@ from PySide.QtCore import QT_TRANSLATE_NOOP if App.GuiUp: import FreeCADGui as Gui + from PySide import QtCore, QtGui, QtWidgets import UtilsAssembly import Preferences -# translate = App.Qt.translate +translate = App.Qt.translate __title__ = "Assembly Command Create Assembly" __author__ = "Ondsel" @@ -91,5 +92,81 @@ class CommandCreateAssembly: App.closeActiveTransaction() +class ActivateAssemblyTaskPanel: + """A basic TaskPanel to select an assembly to activate.""" + + def __init__(self, assemblies): + self.assemblies = assemblies + self.form = QtWidgets.QWidget() + self.form.setWindowTitle(translate("Assembly_ActivateAssembly", "Activate Assembly")) + + layout = QtWidgets.QVBoxLayout(self.form) + label = QtWidgets.QLabel( + translate("Assembly_ActivateAssembly", "Select an assembly to activate:") + ) + self.combo = QtWidgets.QComboBox() + + for asm in self.assemblies: + # Store the user-friendly Label for display, and the internal Name for activation + self.combo.addItem(asm.Label, asm.Name) + + layout.addWidget(label) + layout.addWidget(self.combo) + + def accept(self): + """Called when the user clicks OK.""" + selected_name = self.combo.currentData() + if selected_name: + Gui.doCommand(f"Gui.ActiveDocument.setEdit('{selected_name}')") + return True + + def reject(self): + """Called when the user clicks Cancel or closes the panel.""" + return True + + +class CommandActivateAssembly: + def __init__(self): + self.task_panel = None + + def GetResources(self): + return { + "Pixmap": "Assembly_ActivateAssembly", + "MenuText": QT_TRANSLATE_NOOP("Assembly_ActivateAssembly", "Activate Assembly"), + "ToolTip": QT_TRANSLATE_NOOP( + "Assembly_ActivateAssembly", "Sets an assembly as the active one for editing." + ), + "CmdType": "ForEdit", + } + + def IsActive(self): + if Gui.Control.activeDialog() or App.ActiveDocument is None: + return False + + # Command is only active if no assembly is currently active + if UtilsAssembly.activeAssembly() is not None: + return False + + # And if there is at least one assembly in the document to activate + for obj in App.ActiveDocument.Objects: + if obj.isDerivedFrom("Assembly::AssemblyObject"): + return True + + return False + + def Activated(self): + doc = App.ActiveDocument + assemblies = [o for o in doc.Objects if o.isDerivedFrom("Assembly::AssemblyObject")] + + if len(assemblies) == 1: + # If there's only one, activate it directly without showing a dialog + Gui.doCommand(f"Gui.ActiveDocument.setEdit('{assemblies[0].Name}')") + elif len(assemblies) > 1: + # If there are multiple, show a task panel to let the user choose + self.task_panel = ActivateAssemblyTaskPanel(assemblies) + Gui.Control.showDialog(self.task_panel) + + if App.GuiUp: Gui.addCommand("Assembly_CreateAssembly", CommandCreateAssembly()) + Gui.addCommand("Assembly_ActivateAssembly", CommandActivateAssembly()) diff --git a/src/Mod/Assembly/Gui/Resources/Assembly.qrc b/src/Mod/Assembly/Gui/Resources/Assembly.qrc index 3cbbbf896f..8455b59f51 100644 --- a/src/Mod/Assembly/Gui/Resources/Assembly.qrc +++ b/src/Mod/Assembly/Gui/Resources/Assembly.qrc @@ -1,5 +1,6 @@ + icons/Assembly_ActivateAssembly.svg icons/Assembly_AssemblyLink.svg icons/Assembly_AssemblyLinkRigid.svg icons/Assembly_InsertLink.svg diff --git a/src/Mod/Assembly/Gui/Resources/icons/Assembly_ActivateAssembly.svg b/src/Mod/Assembly/Gui/Resources/icons/Assembly_ActivateAssembly.svg new file mode 100644 index 0000000000..ad832f7a3d --- /dev/null +++ b/src/Mod/Assembly/Gui/Resources/icons/Assembly_ActivateAssembly.svg @@ -0,0 +1,388 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + Path-Stock + 2015-07-04 + https://www.freecad.org/wiki/index.php?title=Artwork + + + FreeCAD + + + FreeCAD/src/Mod/Path/Gui/Resources/icons/Path-Stock.svg + + + FreeCAD LGPL2+ + + + https://www.gnu.org/copyleft/lesser.html + + + [agryson] Alexander Gryson + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +