Command/it: Difference between revisions

From FreeCAD Documentation
(Created page with "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 s...")
No edit summary
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
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à.
==Introduzione==


{{TOCright}}
Each FreeCAD command has a unique name, that appears in the [[:Category:Command_Reference]] page. Commands can be launched by a toolbar button, a menu item, or from a python script or the python console, by running:


Un '''Comando''' è 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à.
FreeCADGui.runCommand("my_Command_Name")


Ogni comando di FreeCAD ha un nome univoco, che appare nella pagina [[:Category:Command_Reference|: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/it|Python]] o dalla [[Python_console/it|console Python]], eseguendo:
FreeCAD commands are defined per workbench. Workbenches will normally add their command definitions at FreeCAD init time, so the command exists and is available as soon as FreeCAD is started, no matter if the corresponding workbench has been activated yet or not. In some cases, however, the workbench author might have decided, to not overload too much the FreeCAD startup process, to load the command definitions only at workbench init. In those cases, the command will only be available after the workbench has been activated (you have switched to it at least once with the workbench selector).


{{Code|code=
As most of them require user interaction, FreeCAD commands are only available in GUI-mode, and not in console mode. However, for convenience, most FreeCAD commands will either have a corresponding python function (like Part.makeBox or Draft.makeLine), or will execute code that is very easy to replicate in a python script.
FreeCADGui.runCommand("my_Command_Name")
}}


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


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).
Example of a C++ command definition (usually defined in /Mod/ModuleName/Gui/Command.cpp):

{{Code|code=
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 {{incode|Part.makeBox}} o {{incode|Draft.makeLine}}), o si può eseguire il codice che è molto facile da replicare in uno script Python o in una [[macros/it|macro]].

I comandi possono essere definiti sia in C++ che in Python.

== Comandi definiti in C++ ==

Esempio di definizione di un comando C++, di solito definito seguendo la struttura {{FileName|Mod/ModuleName/Gui/Command.cpp}}.

{{Code|lang=cpp|code=
DEF_STD_CMD_A(StdCmdMyCommand);
DEF_STD_CMD_A(StdCmdMyCommand);


Line 42: Line 54:
}}
}}


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

Esempio di definizione di un comando Python, esso può essere posizionato in una directory come {{FileName|Mod/ModuleName/tools/commands.py}}.
{{Code|code=
{{Code|code=
from PySide.QtCore import QT_TRANSLATE_NOOP


class MyCommand:
class MyCommand:
"""Explanation of the command."""


def __init__(self):
def __init__(self):
# you can add things here like defining some variables that must exist at all times
"""Initialize variables for the command that must exist at all times."""
pass


def GetResources(self):
def GetResources(self):
"""Return a dictionary with data that will be used by the button or menu item."""
return {'Pixmap' : 'MyCommand.svg',
'Accel' : "Ctrl+A",
return {'Pixmap': 'MyCommand.svg',
'MenuText': QtCore.QT_TRANSLATE_NOOP("My_Command", "My Command"),
'Accel': "Ctrl+A",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}
'MenuText': QT_TRANSLATE_NOOP("My_Command", "My Command"),
'ToolTip': QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}


def Activated(self):
def Activated(self):
# place here the code to be executed when the command is ran
"""Run the following code when the command is activated (button press)."""
print("Activated")


def isActive(self):
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).
"""Return True when the command should be active or False when it should be disabled (greyed)."""
return True


# the command must be "registered" in FreeCAD's command system
# The command must be "registered" with a unique name by calling its class.
FreeCADGui.addCommand('My_Command',MyCommand())
FreeCADGui.addCommand('My_Command', MyCommand())
}}
}}


== Esempi ==
<languages/>

Vedere [[Line_drawing_function/it|Funzione per disegnare delle linee]].

<!-- Anything below this line must come after a closing translate tag -->
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
[[Category:Glossary{{#translation:}}]]

Latest revision as of 21:56, 15 September 2022

Introduzione

Un Comando è 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")

Introduzione

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 o in una macro.

I comandi possono essere definiti sia in C++ che in Python.

Comandi definiti in C++

Esempio di definizione di un comando C++, di solito definito seguendo la struttura 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());

Comandi definiti in Python

Esempio di definizione di un comando Python, esso può essere posizionato in una directory come Mod/ModuleName/tools/commands.py.

from PySide.QtCore import QT_TRANSLATE_NOOP


class MyCommand:
    """Explanation of the command."""

    def __init__(self):
        """Initialize variables for the command that must exist at all times."""
        pass

    def GetResources(self):
        """Return a dictionary with data that will be used by the button or menu item."""
        return {'Pixmap': 'MyCommand.svg',
                'Accel': "Ctrl+A",
                'MenuText': QT_TRANSLATE_NOOP("My_Command", "My Command"),
                'ToolTip': QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}

    def Activated(self):
        """Run the following code when the command is activated (button press)."""
        print("Activated")

    def IsActive(self):
        """Return True when the command should be active or False when it should be disabled (greyed)."""
        return True

# The command must be "registered" with a unique name by calling its class.
FreeCADGui.addCommand('My_Command', MyCommand())

Esempi

Vedere Funzione per disegnare delle linee.