diff --git a/InitGui.py b/InitGui.py index 456ff4a..6d3e303 100644 --- a/InitGui.py +++ b/InitGui.py @@ -5,23 +5,43 @@ class Assembly3Workbench(FreeCADGui.Workbench): MenuText = 'Assembly 3' Icon = asm3.utils.addIconToFCAD('AssemblyWorkbench.svg') + def __init__(self): + self.observer = None + def Activated(self): - import asm3 - asm3.constraint.Observer.attach() + self.observer.attach() def Deactivated(self): - import asm3 - asm3.constraint.Observer.detach() + self.observer.detach() def Initialize(self): import asm3 - cmds = asm3.gui.AsmCmdType.getInfo().TypeNames + cmdInfo = asm3.gui.AsmCmdType.getInfo() + cmds = cmdInfo.TypeNames asm3.utils.logger.debug(cmds) self.appendToolbar('asm3',cmds) self.appendMenu('&Assembly3', cmds) self.appendToolbar('asm3 Constraint', - asm3.constraint.Constraint.CommandList) + asm3.constraint.Constraint.CommandList) + self.observer = asm3.gui.SelectionObserver( + cmdInfo.Types + asm3.constraint.Constraint.Commands) # FreeCADGui.addPreferencePage( # ':/assembly3/ui/assembly3_prefs.ui','Assembly3') + def ContextMenu(self, _recipient): + import asm3 + cmds = [] + for cmd in asm3.gui.AsmCmdType.getInfo().Types: + if cmd.IsActive: + cmds.append(cmd.getName()) + if cmds: + self.appendContextMenu('Assembly',cmds) + + cmds.clear() + for cmd in asm3.constraint.Constraint.Commands: + if cmd.IsActive: + cmds.append(cmd.getName()) + if cmds: + self.appendContextMenu('Constraint',cmds) + FreeCADGui.addWorkbench(Assembly3Workbench) diff --git a/constraint.py b/constraint.py index 09f60a5..98be56f 100644 --- a/constraint.py +++ b/constraint.py @@ -149,6 +149,9 @@ class ConstraintCommand: def __init__(self,tp): self.tp = tp + def getName(self): + return 'asm3Add'+self.tp.getName() + def GetResources(self): return self.tp.GetResources() @@ -159,49 +162,19 @@ class ConstraintCommand: def IsActive(self): return FreeCADGui.ActiveDocument and self.tp._active -class SelectionObserver: - def __init__(self): - self._attached = False - - def onChanged(self): + def checkActive(self): from asm3.assembly import AsmConstraint - for cls in Constraint._cmdTypes: - try: - AsmConstraint.getSelection() - except Exception as e: - logger.trace('selection "{}" exception: {}'.format( - cls.getName(),e.message),frame=1) - cls._active = False - else: - cls._active = True + try: + AsmConstraint.getSelection(self.tp._id) + except Exception as e: + logger.trace('selection "{}" exception: {}'.format( + self.tp.getName(),e.message),frame=1) + self.tp._active = False + else: + self.tp._active = True - def addSelection(self,*_args): - self.onChanged() - - def removeSelection(self,*_args): - self.onChanged() - - def setSelection(self,*_args): - self.onChanged() - - def clearSelection(self,*_args): - logger.trace('selection cleared') - for cls in Constraint._cmdTypes: - cls._active = False - - def attach(self): - if not self._attached: - FreeCADGui.Selection.addObserver(self) - self._attached = True - self.onChanged() - - def detach(self): - if self._attached: - FreeCADGui.Selection.removeObserver(self) - self._attached = False - self.clearSelection('') - -Observer = SelectionObserver() + def deactive(self): + self.tp._active = False class Constraint(ProxyType): 'constraint meta class' @@ -211,16 +184,17 @@ class Constraint(ProxyType): _disabled = 'Disabled' CommandList = [] - _cmdTypes = [] + Commands = [] def register(cls): super(Constraint,cls).register() if cls._menuItem: - name = 'asm3Add'+cls.getName() mcs = cls.__class__ + cmd = ConstraintCommand(cls) + name = cmd.getName() mcs.CommandList.append(name) - mcs._cmdTypes.append(cls) - FreeCADGui.addCommand(name,ConstraintCommand(cls)) + mcs.Commands.append(cmd) + FreeCADGui.addCommand(name,cmd) @classmethod def attach(mcs,obj,checkType=True): diff --git a/gui.py b/gui.py index 532cde4..0162d4e 100644 --- a/gui.py +++ b/gui.py @@ -4,6 +4,41 @@ from asm3.utils import logger,objName,addIconToFCAD from asm3.assembly import Assembly,AsmConstraint from asm3.proxy import ProxyType +class SelectionObserver: + def __init__(self, cmds): + self._attached = False + self.cmds = cmds + + def onChanged(self): + for cmd in self.cmds: + cmd.checkActive() + + def addSelection(self,*_args): + self.onChanged() + + def removeSelection(self,*_args): + self.onChanged() + + def setSelection(self,*_args): + self.onChanged() + + def clearSelection(self,*_args): + for cmd in self.cmds: + cmd.deactive() + + def attach(self): + if not self._attached: + FreeCADGui.Selection.addObserver(self) + self._attached = True + self.onChanged() + + def detach(self): + if self._attached: + FreeCADGui.Selection.removeObserver(self) + self._attached = False + self.clearSelection('') + + class AsmCmdType(ProxyType): def register(cls): super(AsmCmdType,cls).register() @@ -36,12 +71,20 @@ class AsmCmdBase(with_metaclass(AsmCmdType,object)): @classmethod def IsActive(cls): - if FreeCAD.ActiveDocument and cls._active: + if cls._active and cls._id>=0 and FreeCAD.ActiveDocument: return True + @classmethod + def checkActive(cls): + pass + + @classmethod + def deactive(cls): + pass + class AsmCmdNew(AsmCmdBase): _id = 0 - _menuText = 'Create a new assembly' + _menuText = 'Create assembly' _iconName = 'Assembly_New_Assembly.svg' def Activated(self): @@ -49,17 +92,16 @@ class AsmCmdNew(AsmCmdBase): class AsmCmdSolve(AsmCmdBase): _id = 1 - _menuText = 'Solve the constraints of assembly(s)' + _menuText = 'Solve constraints' _iconName = 'AssemblyWorkbench.svg' def Activated(self): import asm3.solver as solver solver.solve() - class AsmCmdMove(AsmCmdBase): _id = 2 - _menuText = 'Move assembly' + _menuText = 'Move part' _iconName = 'Assembly_Move.svg' def Activated(self):