trigger macro when workbench changed

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
antimaterie99
Posts: 10
Joined: Wed Oct 20, 2021 1:52 pm
Contact:

trigger macro when workbench changed

Post by antimaterie99 »

Hi,
is it possible to trigger a python script/macro when the workbench is changed?

thx
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: trigger macro when workbench changed

Post by TheMarkster »

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:

Code: Select all

class myworkbench(Workbench):

    def Deactivated(self):
        FreeCAD.Console.PrintMessage("Workbench deactivated.\n")
User avatar
antimaterie99
Posts: 10
Joined: Wed Oct 20, 2021 1:52 pm
Contact:

Re: trigger macro when workbench changed

Post by antimaterie99 »

currently I played around and this is my current "solution"

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")
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?
Syres
Veteran
Posts: 2899
Joined: Thu Aug 09, 2018 11:14 am

Re: trigger macro when workbench changed

Post by Syres »

antimaterie99 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?
It's in the Wiki https://wiki.freecadweb.org/Macro_at_Startup
Post Reply