Command: Difference between revisions

From FreeCAD Documentation
m (correct isActive to IsActive in python example)
 
(22 intermediate revisions by 6 users not shown)
Line 1: Line 1:
<languages/>
<translate>
<translate>
==Introduction== <!--T:9-->

</translate>
{{TOCright}}
<translate>

<!--T:1-->
<!--T:1-->
A FreeCAD command is what is being executed when you press a toolbar button or type a keyboard shortcut. It can be a very simple action, like changing the zoom level of the 3D view or rotating the point of view, or a complex system that will open dialog boxes and wait for the user to perform specific tasks.
A [[Command|command]] is what is being executed when you press a toolbar button or type a keyboard shortcut. It can be a very simple action, like changing the zoom level of the [[3D view]] or rotating the point of view, or a complex system that will open dialog boxes and wait for the user to perform specific tasks.


<!--T:2-->
<!--T:2-->
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:
Each FreeCAD command has a unique name, that appears in the [[:Category:Command_Reference|:Category:Command Reference]] page. Commands can be launched by a toolbar button, a menu item, or from a [[Python|python]] script or the [[Python_console|Python console]], by running:


</translate>
</translate>
{{Code|code=
FreeCADGui.runCommand("my_Command_Name")
FreeCADGui.runCommand("my_Command_Name")
}}
<translate>
<translate>

== Background == <!--T:10-->


<!--T:3-->
<!--T:3-->
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).
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/burden the FreeCAD startup process and therefore loaded 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).


<!--T:4-->
<!--T:4-->
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.
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 {{incode|Part.makeBox}} or {{incode|Draft.makeLine}}), or will execute code that is very easy to replicate in a python script and/or [[macros|macro]].


<!--T:5-->
<!--T:5-->
Commands can be defined either in C++ or in Python.
Commands can be defined either in C++ or in Python.

== Commands defined in C++ == <!--T:11-->


<!--T:6-->
<!--T:6-->
Example of a C++ command definition (usually defined in /Mod/ModuleName/Gui/Command.cpp):
Example of a C++ command definition, usually defined following the structure {{FileName|Mod/ModuleName/Gui/Command.cpp}}.

</translate>
</translate>
{{Code|code=
{{Code|lang=cpp|code=
DEF_STD_CMD_A(StdCmdMyCommand);
DEF_STD_CMD_A(StdCmdMyCommand);


Line 52: Line 66:
}}
}}
<translate>
<translate>

== Commands defined in Python == <!--T:12-->


<!--T:7-->
<!--T:7-->
Example of a Python command definition, it can be placed in a directory like {{FileName|Mod/ModuleName/tools/commands.py}}.
and a similar command in python (no rule for where it must be done, each python workbench does as it sees fit...)
</translate>
</translate>
{{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())
}}
}}

<translate>
<translate>

<!--T:8-->
== Examples == <!--T:13-->
[[Category:User Documentation]]

[[Category:Developer Documentation]]
<!--T:14-->
[[Category:Command Reference]]
See [[Line_drawing_function|Line drawing function]].

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

Latest revision as of 21:29, 17 April 2022

Introduction

A command is what is being executed when you press a toolbar button or type a keyboard shortcut. It can be a very simple action, like changing the zoom level of the 3D view or rotating the point of view, or a complex system that will open dialog boxes and wait for the user to perform specific tasks.

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:

FreeCADGui.runCommand("my_Command_Name")

Background

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/burden the FreeCAD startup process and therefore loaded 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).

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 and/or macro.

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

Commands defined in C++

Example of a C++ command definition, usually defined following the structure 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());

Commands defined in Python

Example of a Python command definition, it can be placed in a directory like 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())

Examples

See Line drawing function.