Basics of object access
This explains the objects and properties that are often used in extension development and how to access them.
Object structure
This is a diagram showing the representative objects and their relationships in six functional areas: global, user interface, editor, workspace/project, model, and profile. For details, see API specification.
Access in commands and event handlers
In commands and events, you can access application information using the App
property of the ICommandContext
object or IEventContext
object.
protected void SomeCommand(ICommandContext c, ICommandParams p)
{
//Get the application
var app = c.App;
//Get the workspace (project file operations)
var workspace = c.App.Workspace;
//Get the current project
var currentProject = workspace.CurrentProject;
//Get the current model
var currentModel = workspace.CurrentModel;
//Get the metamodels
var metamodels = currentProject.Profile.Metamodels;
//Get the standard UI, such as the file open dialog
var ui = app.Window.UI;
}
It can be implemented as follows:
public void SomeCommand(ICommandContext c, ICommandParams p)
{
//Get the project file path
var projectFilePath = c.App.Workspace.CurrentProject.ModelUnit.AbsolutePath;
c.App.Output.WriteLine("sample", projectFilePath);
}
Access in extension (IExtension)
In your extension's Activate/Deactivate implementation, you can access the application object through the App
property of the IContext
object.
public class MyExtension : IExtension
{
public void Activate(IContext context)
{
//Get the application
IApplication app = context.App;
//Get the workspace (project file operations)
var workspace = context.App.Workspace;
//...
}
public void Deactivate(IContext context)
{
}
}
Differences in access methods between DLL and script
The following global variables are predefined in the script extension.
App
CurrentProject
CurrentModel
UI
Output
Workspace
- etc.
So if you implement it in your DLL as follows,
//Command handler
public void SayHello(ICommandContext c, ICommandParams p)
{
//You can access "App" directly
c.App.Window.UI.ShowInformationDialog("Hello !", "Hello World");
//Get the current project
var project = c.App.Workspace.CurrentProject;
}
You can access it in scripts without using the ICommandContext
object.
//Command handler
public void SayHello(ICommandContext c, ICommandParams p)
{
//You can access "App" directly
App.Window.UI.ShowInformationDialog("Hello !", "Hello World");
//Get the current project with "CurrentProject"
var project = CurrentProject;
}
For more information, see Developing with Scripts > Global Objects.
Access from the ExtensionPoints library
If you use the ExtensionPoints library, you can access properties such as App
and Output
in the command or event handler as shown below, making the implementation simpler and closer to scripting. There will be almost no need to use the ICommandContext
or IEventContext
properties.
public class HelloCommand : CommandHandlerBase
{
///<summary>
///Execute command
///</summary>
///<param name="c"></param>
///<param name="p"></param>
protected override void OnExecute(ICommandContext c, ICommandParams p)
{
//You can access it directly from the "CurrentProject" property instead of using "c.App.Workspace.CurrentProject"
if ( CurrentProject != null)
{
Output.WriteLine(ExtensionName, $"ProjectName : {CurrentProject.Name}");
} else
{
Output.WriteLine(ExtensionName, $"Project is not opened");
}
//You can access IApplication from the "App" property
//Activate
App.Window.IsInformationPaneVisible = true;
App.Window.CurrentOutputCategory = ExtensionName;
App.Window.ActiveInfoWindow = "Output";
}
}
Examples of accessible properties
App
CurrentProject
CurrentModel
CurrentEditor
UI
Output
Workspace
Window
- etc.
For more information, see ExtensionPoints > Implementing a Command Handler.