Get models from a project
You can get models in a project in various ways by using the methods of the IProject
object.
Get models directly under a project
To get models directly under a project, call the GetRootChildren
method on the IProject
object. The following sample gets all models directly under a project that are displayed in the model navigator.
public void GetRootModels(ICommandContext c, ICommandParams p)
{
IProject project = c.App.Workspace.CurrentProject;
//Get the design model root (root model of the model navigator)
IModel designModel = project.DesignModel;
//Get the models directly under the project
IModelCollection rootModels = project.GetRootChildren();
//...
}
You can use the IsDesignModel
method of the IModel
object to determine whether a model is a design model root (root model of the model navigator).
- The design model root (root model of the model navigator) refers to the element enclosed in the rectangle in the image below.
- If you want to create a model directly under the project, see here.
Get all models in a project
To get all models in a project, call the GetAllChildren
method on the IProject
object.
public void GetAllChildren(ICommandContext c, ICommandParams p)
{
//Get all descendant elements of the project
IModelCollection children = c.App.Workspace.CurrentProject.GetAllChildren();
//...
}
Get from model path
Gets a model based on the model path displayed in the editor. Returns null if no model exists for the specified model hierarchy path. If there are multiple matching model hierarchy paths, returns the first model found.
public void GetModelByPath(ICommandContext c, ICommandParams p)
{
IProject project = c.App.Workspace.CurrentProject;
//Get model from path
//Model is a slash-separated string from the root of the project
IModel model = project.GetModelByPath("path1/path2");
if ( model == null) {
c.App.Output.WriteLine("sample", "error: No model found");
return;
}
c.App.Output.WriteLine("sample", model.Name);
}
Get model from model Id
You can get all models in a project by Id.
public void GetModelById(ICommandContext c, ICommandParams p)
{
IProject project = c.App.Workspace.CurrentProject;
//Id of the target model
string modelId = "f514fa53-3720-4f93-9aa9-6cd38d850aa3";
//Get the model
IModel model = project.GetModelById(modelId);
//...
}
For relationships, use the GetRelationshipById
method of the IProject
object.
public void GetRelationshipById(ICommandContext c, ICommandParams p)
{
//Target model Id
string relId = "f514fa53-3720-4f93-9aa9-6cd38d850aa3";
//Get the model
IRelationship relation = c.App.Workspace.CurrentProject.GetRelationshipById(relId);
}