Hi,
is it possible to trigger a python script/macro when the workbench is changed?
thx
trigger macro when workbench changed
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
- antimaterie99
- Posts: 10
- Joined: Wed Oct 20, 2021 1:52 pm
- Contact:
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: trigger macro when workbench changed
Edit: I think I misunderstood your question. You do not have a workbench probably. I'm not sure how you would be notified in a macro if the workbench has been changed. Possibly with an observer of some kind. This gives you the current workbench:
wb = Gui.activeWorkbench()
wb.GetClassName()
**********
Yes. In your class where you inherit from Workbench (probably in InitGui.py) define Deactivated:
wb = Gui.activeWorkbench()
wb.GetClassName()
**********
Yes. In your class where you inherit from Workbench (probably in InitGui.py) define Deactivated:
Code: Select all
class myworkbench(Workbench):
def Deactivated(self):
FreeCAD.Console.PrintMessage("Workbench deactivated.\n")
- antimaterie99
- Posts: 10
- Joined: Wed Oct 20, 2021 1:52 pm
- Contact:
Re: trigger macro when workbench changed
currently I played around and this is my current "solution"
in the while loop I can detect workbench change.
Then I want to contact my external button gear that wb has changed and it should change the hotkey layout to new workbench.
I hope There is a posibility to hook onto the workbench selector combobox ... If I modify the freecad sources ... of course this would not be so hard, but thats not what I want ....
Also a posibility to run the macro on startup would be nice ... is there any solution for that?
Code: Select all
import threading
import time
import FreeCAD
import FreeCADGui
from PySide import QtGui,QtCore
mw = Gui.getMainWindow()
statusBar = mw.statusBar()
windowFlags = QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint
doc = App.ActiveDocument
userCanceled=False
def on_clicked():
global userCanceled
userCanceled = True
statusBar.removeWidget(btn)
FreeCAD.Console.PrintMessage("Canceled.\n")
def worker():
global userCanceled
print("start")
while not userCanceled:
time.sleep(2)
current_wb = Gui.activeWorkbench().name()
print(current_wb)
print("ende")
FreeCADGui.updateGui()
btn = QtGui.QPushButton("Stop WSO")
btn.clicked.connect(on_clicked)
statusBar.addWidget(btn)
btn.show()
FreeCADGui.updateGui()
t = threading.Thread(target=worker)
t.start()
FreeCADGui.updateGui()
print("fin")
Then I want to contact my external button gear that wb has changed and it should change the hotkey layout to new workbench.
I hope There is a posibility to hook onto the workbench selector combobox ... If I modify the freecad sources ... of course this would not be so hard, but thats not what I want ....
Also a posibility to run the macro on startup would be nice ... is there any solution for that?
Re: trigger macro when workbench changed
It's in the Wiki https://wiki.freecadweb.org/Macro_at_Startupantimaterie99 wrote: ↑Thu Oct 21, 2021 1:59 pm Also a posibility to run the macro on startup would be nice ... is there any solution for that?