Skip to main content

Retrieving Models from a Project

By using the methods of the IProject object, you can retrieve models within a project in various ways.

Retrieving Models Directly Under the Project​

To retrieve models directly under the project, call the GetRootChildren method on the IProject object. The following sample retrieves all models directly under the 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(the root model in the Model Navigator)
IModel designModel = project.DesignModel;

/ Get the models directly under the project
IModelCollection rootModels = project.GetRootChildren();

/ ...
}
INFO

The IsDesignModel method of the IModel object can be used to determine if a model is the design model root (the root model in the Model Navigator).

Supplement
  • The design model root (the root model in the Model Navigator) refers to the element enclosed in the rectangle in the image below.

Design Model

  • If you want to create a model directly under the project, please refer to here.

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

//...
}

Getting Models from a Model Path​

Get models based on the model path as displayed in the editor. Returns null if the model at the specified model hierarchy path does not exist. If there are multiple matching model hierarchy paths, the first model found is returned.

public void GetModelByPath(ICommandContext c, ICommandParams p)
{
IProject project = c.App.Workspace.CurrentProject;

// Gets the model from the path
// The model is a slash - separated string from the project root
IModel model = project.GetModelByPath("path1/path2");

if (model == null)
{
c.App.Output.WriteLine("sample", "error: Model not found.");
return;
}

c.App.Output.WriteLine("sample", model.Name);
}

Getting the model from the model ID​

You can get all models in the project using their IDs.

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

//...

}
note

When retrieving an unloaded model file with manual loading settings by Id, even if it is not referenced by a loaded model, a proxy model (IsProxy is true) is returned instead of null (in V5.0, null was returned). For details, please refer to Changes in V5.1.

For relationships, use the GetRelationshipById method of the IProject object.

public void GetRelationshipById(ICommandContext c, ICommandParams p)
{
//Id of the target model
string relId = "f514fa53-3720-4f93-9aa9-6cd38d850aa3";

//Get the model
IRelationship relation = c.App.Workspace.CurrentProject.GetRelationshipById(relId);
}