Comando

From FreeCAD Documentation
Revision as of 13:41, 25 February 2018 by Renatorivo (talk | contribs) (Created page with "Dato che la maggior parte richiede l'interazione dell'utente, i comandi di FreeCAD sono disponibili solo in modalità GUI e non in modalità console. Tuttavia, per praticità,...")

Un comando di FreeCAD è ciò che viene eseguito quando si preme un pulsante della barra degli strumenti o si digita una scorciatoia da tastiera. Può essere un'azione molto semplice, come cambiare il livello di zoom della vista 3D o ruotare il punto di vista oppure un sistema complesso che apre le finestre di dialogo e attende che l'utente esegua delle specifiche attività.

Ogni comando di FreeCAD ha un nome univoco, che appare nella pagina Category:Command_Reference se originale inglese, e nella pagina Category:Command_Reference/it per le traduzioni . I comandi possono essere lanciati da un pulsante della barra degli strumenti, da una voce di menu o da uno script python o dalla console python, eseguendo:

FreeCADGui.runCommand("my_Command_Name")

I comandi di FreeCAD sono definiti per ambienti (workbench). Normalmente gli ambienti aggiungono le loro definizioni di comando all'avvio di FreeCAD, quindi il comando esiste ed è disponibile non appena viene avviato FreeCAD, non importa se l'ambiente corrispondente è stato attivato o meno. In alcuni casi, però, l'autore dell'ambiente può aver deciso, per non sovraccaricare troppo il processo di avvio di FreeCAD, di caricare le definizioni dei comandi solo all'avvio dell'ambiente. In questi casi, il comando è disponibile solo dopo che l'ambiente è stato attivato (si è passati ad esso almeno una volta usando il selettore dei workbench).

Dato che la maggior parte richiede l'interazione dell'utente, i comandi di FreeCAD sono disponibili solo in modalità GUI e non in modalità console. Tuttavia, per praticità, la maggior parte dei comandi di FreeCAD ha una corrispondente funzione Python (come Part.makeBox o Draft.makeLine), o si può eseguire il codice che è molto facile da replicare in uno script Python.

Commands can be defined either in C++ or in Python.

Example of a C++ command definition (usually defined in /Mod/ModuleName/Gui/Command.cpp):

DEF_STD_CMD_A(StdCmdMyCommand);

StdCmdMyCommand::StdCmdMyCommand()
  : Command("Std_My_Command")
{
    sGroup        = QT_TR_NOOP("File");
    sMenuText     = QT_TR_NOOP("My Command");
    sToolTipText  = QT_TR_NOOP("Runs my command in the active document");
    sWhatsThis    = "Std_MyCommand";
    sStatusTip    = QT_TR_NOOP("Runs my command in the active document");
    sPixmap       = "MyCommand.svg";
    sAccel        = "Ctrl+A";
}

void StdCmdExport::activated(int iMsg)
{
    // place here the code to be executed when the command is ran
}

bool StdCmdMyCommand::isActive(void)
{
    // here you have a chance to return true or false depending if your command must be shown as active or inactive (greyed).
}

// the command must be "registered" in FreeCAD's command system
CommandManager &rcCmdMgr = Application::Instance->commandManager();
rcCmdMgr.addCommand(new StdCmdMyCommand());

and a similar command in python (no rule for where it must be done, each python workbench does as it sees fit...)

class MyCommand:

    def __init__(self):
        # you can add things here like defining some variables that must exist at all times

    def GetResources(self):
        return {'Pixmap'  : 'MyCommand.svg',
                    'Accel' : "Ctrl+A",
                    'MenuText': QtCore.QT_TRANSLATE_NOOP("My_Command", "My Command"),
                   'ToolTip': QtCore.QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}

    def Activated(self):
        # place here the code to be executed when the command is ran

    def isActive(self):
        # here you have a chance to return True or False depending if your command must be shown as active or inactive (greyed).

# the command must be "registered" in FreeCAD's command system
FreeCADGui.addCommand('My_Command',MyCommand())