命令

From FreeCAD Documentation
Revision as of 03:26, 9 July 2019 by Wconly (talk | contribs) (Created page with "大多命令都需要与用户进行交互,FreeCAD命令仅存在于GUI模式下,而不存在于控制台模式下。但是,为了方便,大多FreeCAD命令也都有...")

一条FreeCAD命令是指:当您按下一个工具栏按钮或输入一个键盘快捷键时所执行的命令。它可能是一个非常简单的动作,如改变3D视图的缩放级别,或旋转视角;也可能是一个打开对话框并等待用户执行特定任务的复杂系统。

每个FreeCAD命令都有自己独一无二的名称,可参见Category:Command_Reference页面。加载命令的方式很多,如通过工具栏按钮、菜单项,也可以在python脚本或python控制台中执行:

FreeCADGui.runCommand("my_Command_Name")

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命令仅存在于GUI模式下,而不存在于控制台模式下。但是,为了方便,大多FreeCAD命令也都有与之对应的python函数(如Part.makeBox 或 Draft.makeLine),或者执行非常便于从python脚本中复制的代码。

可以通过C++或Python来定义命令。

以下为一个C++的命令定义示例(通常定义于/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());

以及用python实现相似的命令(并没有具体规定python文件一定放于何处,只要令每个python工作台都能如预期那样工作即可……)

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())