Assembly: Add 'Activate assembly' command.

This commit is contained in:
paddle
2025-07-24 14:13:47 +02:00
committed by Kacper Donat
parent 013bbf1937
commit 2843bbefc7
3 changed files with 467 additions and 1 deletions

View File

@@ -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())

View File

@@ -1,5 +1,6 @@
<RCC>
<qresource prefix="/">
<file>icons/Assembly_ActivateAssembly.svg</file>
<file>icons/Assembly_AssemblyLink.svg</file>
<file>icons/Assembly_AssemblyLinkRigid.svg</file>
<file>icons/Assembly_InsertLink.svg</file>

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 13 KiB