Skip to main content

Change port position

Change port position

To change the port position, use the SetLocationRatio method of the IPort object. The port position is set as a double value ratio between 0 and 1.

public void SetLocationRatio(ICommandContext c, ICommandParams p) 
{
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null) return;

//Get the selected node
var nodes = diagram.GetSelectedShapes().OfType<INode>();

//Process each node
foreach (INode node in nodes)
{
IPortCollection ports = node.Ports;
int count = 0;
int max = ports.Count();

//Process each port that the node has
foreach (IPort port in ports)
{
double ratio = (double)count/(double)max;
port.SetLocationRatio(ratio);
c.App.Output.WriteLine("sample", $"Port location ratio : {port.LocationRatio}");
count++;
}
}
}

Aligning ports

To align ports, use the RelocatePorts of the INode object. You can specify the side to align with the PortOrientation enumeration. The enumeration values ​​that can be specified are as follows.

Enumeration valueAlignment target
TopTop side
LeftLeft side
BottomBottom side
RightRight side
AllAll four sides
public void RelocatePorts(ICommandContext c, ICommandParams p) 
{
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null) return;

var nodes = diagram.GetSelectedShapes().OfType<INode>();

foreach (INode node in nodes)
{
//Align the ports of the node
node.RelocatePorts(PortOrientation.All);
}
}