Skip to main content

Connect a connector

This is a sample of how to create a connector by associating models. When using this sample to connect a connector on a diagram,

  • A reference association class must exist between the models of the two nodes you select
  • A connector shape that models the above association must already be defined in the diagram.
public void CreateConnectors(ICommandContext c, ICommandParams p) 
{
//Get the currently displayed diagram
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null)
{
//If no diagram is displayed, terminate the process
c.App.Output.WriteLine("sample", "Diagram not displayed.");
return;
}

//Get the nodes selected on the diagram
IEnumerable<INode> nodes = diagram.GetSelectedShapes().OfType<INode>();
if (nodes.Count() != 2)
{
c.App.Output.WriteLine("sample", "Please select only two nodes.");
return;
}

//Attach all possible reference fields between the selected nodes
INode node = nodes.ElementAt(0);
INode other = nodes.ElementAt(1);
node.Model.RelateAll(other.Model);
}

To remove a connector connection, delete the association between the models that correspond to the shape. For more information on adding and deleting associations, see Relating Models.

public void CreateConnectors(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", "Diagram not 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;
}

//Disassociate the connectors in the relationships that correspond to the connectors obtained
foreach(IConnector connector in connectors)
{
IRelationship relationship = connector.Model as IRelationship;
c.App.Output.WriteLine("sample", $"UnRelate Relation: {relationship.Metaclass.Name}");
c.App.Output.WriteLine("sample", $" Connector SourceField: {relationship.SourceField.Name}");
c.App.Output.WriteLine("sample", $" Connector TargetField: {relationship.TargetField.Name}");
relationship.UnRelate();
}
}