Command
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 or a complex system that will open dialog boxes and wait for the user to perform specific tasks.
Each LabRPS 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:
LabRPSGui.runCommand("my_Command_Name")
Background
LabRPS commands are defined per workbench. Workbenches will normally add their command definitions at LabRPS init time, so the command exists and is available as soon as LabRPS 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 LabRPS 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, LabRPS commands are only available in GUI-mode, and not in console mode. However, for convenience, most LabRPS commands will either have a corresponding python function, 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 LabRPS'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. LabRPSGui.addCommand('My_Command', MyCommand())
- LabRPS scripting: Python, Introduction to Python, Python scripting tutorial, LabRPS Scripting Basics
- Modules: Units, Quantity
- Workbenches: Gui Commands, Commands
- Parametric objects: Scripted objects, Viewproviders
- Graphical interface: Interface creation, Interface creation completely in Python, PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Other: Expressions
- Hubs: User hub, Power users hub, Developer hub