[GELÖST] Makro beim starten von FC automatisch ausführen

In diesem Forum Fragen und Diskussionen in Deutsch
Forum rules
Foren-Regeln und hilfreiche Informationen

WICHTIG: Bitte zuerst lesen, bevor Sie posten
Post Reply
ROGA
Posts: 8
Joined: Sat Apr 24, 2021 8:11 am
Location: Zürich

[GELÖST] Makro beim starten von FC automatisch ausführen

Post by ROGA »

Hallo,

ich habe versucht, nach dieser Anleitung ein Makro automatisch ausführen zu lassen. Leider gelingt mir dies nicht und FC zeigt mir beim starten im Ausgabefenster folgenden Fehler:

Code: Select all

12:53:30  During initialization the error "name 'runMacro' is not defined" occurred in /home/rga/.FreeCAD/Mod/MacroAutoStart/InitGui.py
12:53:30  Please look into the log file for further information
Das Makro, welches ich automatisch beim Start von FC ausführen lassen möchte liegt wie in der Anleitung beschrieben in einem Unterordner unterhalb von Mod, hat die Erweiterung .py und sieht wie folgt aus:

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui

class DocObserver():
	def slotCreatedDocument(self,doc):
		print ("Dokument erstellt")
	def slotDeletedDocument(self,doc):
		self.cancelObservation(obs)
		print ("Dokument gelöscht")
	def slotChangedObject(self,doc,obj):
		print("Du hast ein Objekt verändert!")
	def slotRecomputedObject(self,doc):
		print("Aktualisierung ausgelöst")
	def slotCreatedObject(self,doc):
		print("Ein neues Objekt hinzugefügt")
	def cancelObservation(self,obs):
		App.removeDocumentObserver(obs)		
		print("Überwachung abgebrochen")

# function to detect work bench change
def wbChange(name):
    print ("Du hast folgende Workbench aktiviert: {}".format(name))

# set up document observer

def run():
    obs=DocObserver()
    App.addDocumentObserver(obs)

##Ensure main instructions are still called in case of manal run
if __name__ == '__main__':
    run()
    
# initiate a new doc
#doc = FreeCAD.newDocument()
#mw=Gui.getMainWindow()
#mw.workbenchActivated.connect(wbChange)

# Do something:
#Volume = doc.addObject("Part::Sphere", "TreatmentVolume")
# recompute:
#doc.recompute()
#Gui.SendMsgToActiveView("ViewFit")
#Gui.activateWorkbench("PartWorkbench")
# now play with the object size etc, and note the resultant messages in the Report View
Bei der InitGui.py bin ich dann vermutlich irgendwie gescheitert. Sie sieht bei mir wie folgt aus:

Code: Select all

def runMacroStartup(name):
    #Do not run when NoneWorkbench is activated because UI isn't yet completely there
    if name != "NoneWorkbench":
        #Run macro only once by disconnecting the signal at first call
        FreeCADGui.getMainWindow().workbenchActivated.disconnect(runMacroStartup)
        ##Following 2 lines shall be duplicated for each macro to run
        import DocObserver
        DocObserver.run()
        ##Eg. if a second macro shall be launched at startup
        #import MyWonderfulMacro
        #MyWonderfulMacro.run()

##The following 2 lines are important because InitGui.py files are passed to the exec() function...
##...and the runMacro wouldn't be visible outside. So explicitly add it to __main__
import __main__
__main__.runMacro = runMacro

##Connect the function that runs the macro to the appropriate signal
FreeCADGui.getMainWindow().workbenchActivated.connect(runMacro)
Da ich von Python noch wenig bis gar nix verstehe, weiss ich nicht, wo hier genau der Fehler liegt. Könnte mir jemand behilflich sein? Vielen Dank schon Mal im voraus.
Roli
Last edited by ROGA on Sun May 16, 2021 1:37 pm, edited 2 times in total.
chrisb
Veteran
Posts: 53919
Joined: Tue Mar 17, 2015 9:14 am

Re: Makro beim starten von FC automatisch ausführen

Post by chrisb »

Vielleicht hilft Dir das Super User Makro oder wenigstens die Diskussion dazu weiter.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
ROGA
Posts: 8
Joined: Sat Apr 24, 2021 8:11 am
Location: Zürich

Re: Makro beim starten von FC automatisch ausführen

Post by ROGA »

Danke chrisb!

Jetzt läuft's :-) Die Diskussion hat mich auf die Idee gebracht, dass die Funktion runMacro in der Datei InitGui.py nicht richtig definiert wurde. Ich habe es wie folgt abgeändert:

Code: Select all

def runMacroStartup(name):
    #Do not run when NoneWorkbench is activated because UI isn't yet completely there
    if name != "NoneWorkbench":
        #Run macro only once by disconnecting the signal at first call
        FreeCADGui.getMainWindow().workbenchActivated.disconnect(runMacroStartup)
        ##Following 2 lines shall be duplicated for each macro to run
        import DocObserver
        DocObserver.run()
        ##Eg. if a second macro shall be launched at startup
        #import MyWonderfulMacro
        #MyWonderfulMacro.run()

##The following 2 lines are important because InitGui.py files are passed to the exec() function...
##...and the runMacro wouldn't be visible outside. So explicitly add it to __main__
import __main__
__main__.runMacroStartup = runMacroStartup
#                /\/\/\/\          /\/\/\/\

##Connect the function that runs the macro to the appropriate signal
FreeCADGui.getMainWindow().workbenchActivated.connect(runMacroStartup)
#                                                             /\/\/\/\
Vielen Dank!
Russ4262
Posts: 941
Joined: Sat Jun 30, 2018 3:22 pm
Location: Oklahoma
Contact:

Re: Makro beim starten von FC automatisch ausführen

Post by Russ4262 »

ROGA wrote: Sun May 16, 2021 1:35 pm ... dass die Funktion runMacro in der Datei InitGui.py nicht richtig definiert wurde....
Vielen Dank.
Ich habe das im Wiki korrigiert.

Thank you.
I corrected this in the Wiki, Macro_at_Startup.

Ich benutze einen Übersetzer.
I use a translator.

Russell
Post Reply