Improved FCAD command management
This commit is contained in:
parent
f681cc2035
commit
65abe89d1d
30
InitGui.py
30
InitGui.py
|
@ -5,23 +5,43 @@ class Assembly3Workbench(FreeCADGui.Workbench):
|
||||||
MenuText = 'Assembly 3'
|
MenuText = 'Assembly 3'
|
||||||
Icon = asm3.utils.addIconToFCAD('AssemblyWorkbench.svg')
|
Icon = asm3.utils.addIconToFCAD('AssemblyWorkbench.svg')
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.observer = None
|
||||||
|
|
||||||
def Activated(self):
|
def Activated(self):
|
||||||
import asm3
|
self.observer.attach()
|
||||||
asm3.constraint.Observer.attach()
|
|
||||||
|
|
||||||
def Deactivated(self):
|
def Deactivated(self):
|
||||||
import asm3
|
self.observer.detach()
|
||||||
asm3.constraint.Observer.detach()
|
|
||||||
|
|
||||||
def Initialize(self):
|
def Initialize(self):
|
||||||
import asm3
|
import asm3
|
||||||
cmds = asm3.gui.AsmCmdType.getInfo().TypeNames
|
cmdInfo = asm3.gui.AsmCmdType.getInfo()
|
||||||
|
cmds = cmdInfo.TypeNames
|
||||||
asm3.utils.logger.debug(cmds)
|
asm3.utils.logger.debug(cmds)
|
||||||
self.appendToolbar('asm3',cmds)
|
self.appendToolbar('asm3',cmds)
|
||||||
self.appendMenu('&Assembly3', cmds)
|
self.appendMenu('&Assembly3', cmds)
|
||||||
self.appendToolbar('asm3 Constraint',
|
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(
|
# FreeCADGui.addPreferencePage(
|
||||||
# ':/assembly3/ui/assembly3_prefs.ui','Assembly3')
|
# ':/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)
|
FreeCADGui.addWorkbench(Assembly3Workbench)
|
||||||
|
|
|
@ -149,6 +149,9 @@ class ConstraintCommand:
|
||||||
def __init__(self,tp):
|
def __init__(self,tp):
|
||||||
self.tp = tp
|
self.tp = tp
|
||||||
|
|
||||||
|
def getName(self):
|
||||||
|
return 'asm3Add'+self.tp.getName()
|
||||||
|
|
||||||
def GetResources(self):
|
def GetResources(self):
|
||||||
return self.tp.GetResources()
|
return self.tp.GetResources()
|
||||||
|
|
||||||
|
@ -159,49 +162,19 @@ class ConstraintCommand:
|
||||||
def IsActive(self):
|
def IsActive(self):
|
||||||
return FreeCADGui.ActiveDocument and self.tp._active
|
return FreeCADGui.ActiveDocument and self.tp._active
|
||||||
|
|
||||||
class SelectionObserver:
|
def checkActive(self):
|
||||||
def __init__(self):
|
|
||||||
self._attached = False
|
|
||||||
|
|
||||||
def onChanged(self):
|
|
||||||
from asm3.assembly import AsmConstraint
|
from asm3.assembly import AsmConstraint
|
||||||
for cls in Constraint._cmdTypes:
|
|
||||||
try:
|
try:
|
||||||
AsmConstraint.getSelection()
|
AsmConstraint.getSelection(self.tp._id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.trace('selection "{}" exception: {}'.format(
|
logger.trace('selection "{}" exception: {}'.format(
|
||||||
cls.getName(),e.message),frame=1)
|
self.tp.getName(),e.message),frame=1)
|
||||||
cls._active = False
|
self.tp._active = False
|
||||||
else:
|
else:
|
||||||
cls._active = True
|
self.tp._active = True
|
||||||
|
|
||||||
def addSelection(self,*_args):
|
def deactive(self):
|
||||||
self.onChanged()
|
self.tp._active = False
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
class Constraint(ProxyType):
|
class Constraint(ProxyType):
|
||||||
'constraint meta class'
|
'constraint meta class'
|
||||||
|
@ -211,16 +184,17 @@ class Constraint(ProxyType):
|
||||||
_disabled = 'Disabled'
|
_disabled = 'Disabled'
|
||||||
|
|
||||||
CommandList = []
|
CommandList = []
|
||||||
_cmdTypes = []
|
Commands = []
|
||||||
|
|
||||||
def register(cls):
|
def register(cls):
|
||||||
super(Constraint,cls).register()
|
super(Constraint,cls).register()
|
||||||
if cls._menuItem:
|
if cls._menuItem:
|
||||||
name = 'asm3Add'+cls.getName()
|
|
||||||
mcs = cls.__class__
|
mcs = cls.__class__
|
||||||
|
cmd = ConstraintCommand(cls)
|
||||||
|
name = cmd.getName()
|
||||||
mcs.CommandList.append(name)
|
mcs.CommandList.append(name)
|
||||||
mcs._cmdTypes.append(cls)
|
mcs.Commands.append(cmd)
|
||||||
FreeCADGui.addCommand(name,ConstraintCommand(cls))
|
FreeCADGui.addCommand(name,cmd)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def attach(mcs,obj,checkType=True):
|
def attach(mcs,obj,checkType=True):
|
||||||
|
|
52
gui.py
52
gui.py
|
@ -4,6 +4,41 @@ from asm3.utils import logger,objName,addIconToFCAD
|
||||||
from asm3.assembly import Assembly,AsmConstraint
|
from asm3.assembly import Assembly,AsmConstraint
|
||||||
from asm3.proxy import ProxyType
|
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):
|
class AsmCmdType(ProxyType):
|
||||||
def register(cls):
|
def register(cls):
|
||||||
super(AsmCmdType,cls).register()
|
super(AsmCmdType,cls).register()
|
||||||
|
@ -36,12 +71,20 @@ class AsmCmdBase(with_metaclass(AsmCmdType,object)):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def IsActive(cls):
|
def IsActive(cls):
|
||||||
if FreeCAD.ActiveDocument and cls._active:
|
if cls._active and cls._id>=0 and FreeCAD.ActiveDocument:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def checkActive(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def deactive(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
class AsmCmdNew(AsmCmdBase):
|
class AsmCmdNew(AsmCmdBase):
|
||||||
_id = 0
|
_id = 0
|
||||||
_menuText = 'Create a new assembly'
|
_menuText = 'Create assembly'
|
||||||
_iconName = 'Assembly_New_Assembly.svg'
|
_iconName = 'Assembly_New_Assembly.svg'
|
||||||
|
|
||||||
def Activated(self):
|
def Activated(self):
|
||||||
|
@ -49,17 +92,16 @@ class AsmCmdNew(AsmCmdBase):
|
||||||
|
|
||||||
class AsmCmdSolve(AsmCmdBase):
|
class AsmCmdSolve(AsmCmdBase):
|
||||||
_id = 1
|
_id = 1
|
||||||
_menuText = 'Solve the constraints of assembly(s)'
|
_menuText = 'Solve constraints'
|
||||||
_iconName = 'AssemblyWorkbench.svg'
|
_iconName = 'AssemblyWorkbench.svg'
|
||||||
|
|
||||||
def Activated(self):
|
def Activated(self):
|
||||||
import asm3.solver as solver
|
import asm3.solver as solver
|
||||||
solver.solve()
|
solver.solve()
|
||||||
|
|
||||||
|
|
||||||
class AsmCmdMove(AsmCmdBase):
|
class AsmCmdMove(AsmCmdBase):
|
||||||
_id = 2
|
_id = 2
|
||||||
_menuText = 'Move assembly'
|
_menuText = 'Move part'
|
||||||
_iconName = 'Assembly_Move.svg'
|
_iconName = 'Assembly_Move.svg'
|
||||||
|
|
||||||
def Activated(self):
|
def Activated(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user