Skip to main content

Change connector label

An example of changing the label of a connector displayed on a diagram.
The connector label is assigned a connector field, and changing the field value changes the label display.

public void ModifyConnectorLabel(ICommandContext c, ICommandParams p) 
{
//Get the currently displayed diagram
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null)
{
//End processing if no diagram is displayed
c.App.Output.WriteLine("sample", "No diagram displayed");
return;
}

//Get the connectors selected on the diagram
IEnumerable<IConnector> connectors = diagram.GetSelectedShapes().OfType<IConnector>().ToList();
if (!connectors.Any())
{
c.App.Output.WriteLine("sample", "No connector selected");
return;
}

//Update the labels of the obtained connectors
foreach(IConnector connector in connectors)
{
//Get the associations
IRelationship relationship = connector.Model as IRelationship;

//Update the field assigned to the label.
string labelFieldName = "ModifyLabelName";
relationship.SetField(labelFieldName, "SampleLabel");
}
}

:::note
You cannot assign new connector fields to labels from an extension.

:::