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
| Key | Description | Range | Required |
|---|---|---|---|
| id | An ID that uniquely identifies the command. A unique string across all deployed extensions | A string representing the ID | Required |
| title | Command title 1 | Any string | - |
| description | Command description 1 | Any string | - |
| group | Group to which the command belongs 1 | A string representing the group name | - |
| execFunc | The command handler name implemented at the entry point. Specify the method name for C# scripts, or the function name for Python scripts | A string representing the method or function name | Required |
| canExecWhen | The condition under which the command is valid. See the sub-property for details | Object | - |
| canExecWhen.uiState | One 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.profiles | Profile name of the target project | String representing the profile name | - |
| canExecWhen.metamodels | Class 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.viewDefinitions | Name 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. | - |
| canExecFunc | The 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. 3 | String 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
- C#
- Python
public void GenerateCode(ICommandContext commandContext, ICommandParams commandParams)
{
//Command handler implementation code
}
from nd.desktop import *
def generate_code(context: ICommandContext, commandParams: ICommandParams):
# Command handler implementation code
pass
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
-
In the current version, this does not affect the extension's operation and is not displayed.
↩ ↩2 ↩3 ↩4 -
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 -
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