Skip to main content

Command

Overview

  • By defining commands and associating them with command handlers implemented in extensions, you can call custom processing from ribbon extension points, etc.
  • You can specify the model selection state, model class, and target view definition as conditions for enabling/disabling commands.

Command Details

  • Properties
KeyDescriptionRangeRequired
idAn ID that uniquely identifies the command. A unique string across all deployed extensionsA string representing the IDRequired
titleCommand title 1Any string-
descriptionCommand description 1Any string-
groupGroup to which the command belongs 1A string representing the group name-
execFuncThe command handler name implemented at the entry point. Specify the method name for C# scripts, or the function name for Python scriptsA string representing the method or function nameRequired
canExecWhenThe condition under which the command is valid. See the sub-property for detailsObject-
canExecWhen.uiStateOne of the following values ​​representing the UI state.
Always: Always valid (default value when the property is omitted).
ProjectOpened: Valid when the project is open.
ModelSelected: Valid when a model is selected.
ModelSelectedInEditor: Valid when a model is selected in the editor.
"Always", "ProjectOpened", "ModelSelected", "ModelSelectedInEditor"-
canExecWhen.profilesProfile name of the target projectString representing the profile name-
canExecWhen.metamodelsClass name of the selected model, or the name of its inherited class. Specifying * as the value, or omitting the property, is effective for all models. Ignored if the uiState property is anything other than ModelSelected or ModelSelectedInEditor."*", or a string representing the class name. If multiple, specify separated by commas.-
canExecWhen.viewDefinitionsName of the target view definition. Specifying * as the value, or omitting the property, is effective for all view definitions. Ignored if the uiState property is anything other than ModelSelectedInEditor."*", or a string representing the view definition name. If multiple, specify separated by commas.-
canExecFuncThe name of the method that evaluates the command's validity condition. 2 If set simultaneously with canExecWhen, this takes precedence. However, only functions set in .NET DLL extensions are evaluated. 3String representing the method name-

Command definition example

{
"extensionPoints": {

"commandGroups" :
[
{
"name": "codegen",
"title": "Generate Source Code"
}
],

"commands":
[
{
"id": "myExtension.generateCode",
"title": "Generate Code",
"description": "generate code description",
"group": "codegen",
"execFunc": "GenerateCode",
"canExecWhen":
{
"uiState": "ModelSelected",
"metamodels": "FunctionalComponent,Component",
"viewDefinitions": "*"
}
},
{
"id": "myExtension.checkError",
"execFunc": "CheckError"
}
]
}
}

Command Handler Implementation Example

public void GenerateCode(ICommandContext commandContext, ICommandParams commandParams)
{
//Command handler implementation code
}

Example of Command Parameter Usage

When executing a command defined in the manifest's extension point from within an extension, you can specify parameters.

void ExecSomeCommands()
{
/ Create parameters
var execParams = App.CreateCommandParams();
execParams.AddParam("value1"); //Value can be accessed via execParams[0]
execParams.AddParam("value2"); //Value can be accessed via execParams[1]
/ //execParams.AddParamWithName("param1", "value1"); //Value can be accessed via execParams["param1"]
/ //execParams.AddParamWithName("param2", "value2"); //Value can be accessed via execParams["param2"]
/ //Execute a command with specified parameters
App.ExecuteCommand("otherExtension.someCommand", execParams);
}

Footnotes

  1. In the current version, this does not affect the extension's operation and is not displayed.
    2 3 4

  2. The signature of the function that can be set in canExecFunc is as follows: bool function-name(ICommandContext context, ICommandParams parameters). If the set function is not found, it evaluates to false.
    2

  3. If canExecFunc is set in a script-based (C#/Python) extension, it always evaluates to true. For script-based extensions, only canExecWhen should be set. 2