Skip to main content

Access related fields

A collection of related objects (IRelationship) can be obtained using the GetRelations method of the IModel object. IRelationship inherits from IModel and allows access to fields.

public void GetRelationFields(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Relations
foreach ( IRelationship transition in model.GetRelations("Transitions") )
{
//Get related fields
var trigger = transition.GetFieldString("Trigger");

//Write to output
c.App.Output.WriteLine("sample", $"Trigger: {trigger}");
}
}

The related model (IModel) can be accessed through the Source and Target properties of the IRelationship object.

public void GetRelationEnds(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Relationship
foreach ( IRelationship transition in model.GetRelations("Transitions") )
{
//Source and destination models
IModel source = transition.Source;
IModel target = transition.Target;

//Write to output
c.App.Output.WriteLine("sample", $"Source: {source.Name}");
c.App.Output.WriteLine("sample", $"Target: {target.Name}");
}
}

Field definition information (IField) on the source and destination metamodels can be accessed using the SourceField and TargetField properties of the IRelationship object.

public void GetRelationEndFields(ICommandContext c, ICommandParams p) 
{
IModel model = c.App.Workspace.CurrentModel;

//Relationship
foreach ( IRelationship transition in model.GetRelations("Transitions") )
{
//Source and destination fields
IField sourceField = transition.SourceField;
IField targetField = transition.TargetField;

//Write to output
c.App.Output.WriteLine("sample", $"SourceField: {sourceField.Name}");
c.App.Output.WriteLine("sample", $"TargetField: {targetField.Name}");
}
//...
}