Macro at Startup/it

= Prefazione =

Questa documentazione spiega come impostare una macro in modo che venga eseguita automaticamente all'avvio di FreeCAD.

Prima di iniziare, bisogna considerare le seguenti cose :
 * L'esecuzione automatica di macro all'avvio può essere considerata un rischio per la sicurezza. Si devono eseguire solo delle macro fidate e testate in precedenza.
 * Per seguire questa procedura bisogna avere alcune nozioni di Python e di codifica.
 * Quando si fa riferimento a delle cartelle utente ('Mod', 'Macro', ...), esse si trovano nella cartella FreeCAD dell'utente. È possibile trovarle in Avvio e configurazione → Informazioni relative all'utente
 * Questo comportamento non dovrebbe essere utilizzato per le macro che si occupano della modellazione delle parti. Questo è più appropriato per le macro che aggiungono funzionalità, migliorano l'interfaccia utente, ...

= Come fare =

Preparare la macro
In genere, una macro non è pensata e fatta per essere eseguita all'avvio, quindi deve essere ottimizzata.

Consider the below macro that you downloaded from somewhere and is stored in your 'Macro' folder with name 'MySuperMacro.FCMacro' : from PySide import QtGui
 * 1) Import section ##

class MyMsgBox(QtGui.QMessageBox):
 * 1) Definition section (classes, functions, ...)

def __init__(self): super(MyMsgBox, self).information(None, "MyTitle", "MyText")

MyMsgBox All macros will generally present a similar structure with first import section, then definition section and finally main instruction section. We will focus on this latter because main instructions (they are quite easy to spot because they start at the full beginning of the line) are actually the ones that 'execute' the macro. For later step, we'll need to programmatically import the macro then execute it. This can't be done with the actual structure of the macro. To be able to do so, we need to enclose the main instructions in a function --eg. run-- then ensure this function is still called when the macro is manually run by the user. If you're not totally sure of what you're doing, it is advised to work on a copy of the macro (or you may just want to keep the original macro as is). The original file shall be modified as follow : from PySide import QtGui import FreeCAD as App import FreeCADGui as Gui
 * 1) Main instruction section
 * 1) The 2 below lines shall be added if not already present to ensure FreeCAD modules are imported

class MyMsgBox(QtGui.QMessageBox):

def __init__(self): super(MyMsgBox, self).information(None, "MyTitle", "MyText")

def run: MyMsgBox
 * 1) Enclose the main instructions in a function

if __name__ == '__main__': run Of course if the function 'run' already exists in the macro, you can choose any other convenient name Now the macro is ready to be integrated in FreeCAD startup.
 * 1) Ensure main instructions are still called in case of manal run

Integrate into FreeCAD startup
First create a new folder in your user 'Mod' folder, let's say called 'MacroStartup'. Copy the modified macro into this newly created folder. Finally create in the same folder a file called 'InitGui.py' which contains the following code : 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 MySuperMacro MySuperMacro.run ##Eg. if a second macro shall be launched at startup #import MyWonderfulMacro #MyWonderfulMacro.run

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

FreeCADGui.getMainWindow.workbenchActivated.connect(runMacro) Notice that it shall be done only once. If you want to run more than one macro, you can just add the others in the same file (look at the comments on the above code).
 * 1) Connect the function that runs the macro to the appropriate signal

We are over. Your macro should automatically run at next FreeCAD launch.

Notice that if the original macro was downloaded through the Addon Manager, it will be overwritten on update and thus you have to follow again the steps here.