Change the position of the shape
To change the position of a shape, use the SetLocationAt
method of the INode
object.
public void SetLocationAt(ICommandContext c, ICommandParams p)
{
//Get the currently displayed diagram
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
if (diagram == null)
{
//If the diagram is not displayed, end the process
c.App.Output.WriteLine("sample", "Diagram is not displayed.");
return;
}
//Get the node displayed on the diagram
INode node = diagram.Nodes.FirstOrDefault();
if (node == null)
{
//If the node is not displayed, end the process
c.App.Output.WriteLine("sample", "Node is not displayed.");
return;
}
//Specify the coordinates for the node and change the node position
node.SetLocationAt(100, 100);
}
You can also specify coordinates using System.Drawing.Point
.
In that case, use the SetLocation
method of the INode
object.
public void SetLocation(ICommandContext c, ICommandParams p)
{
//Omitted
//Specify coordinates for the node and change the node's position
System.Drawing.Point point = new System.Drawing.Point(100, 100);
node.SetLocation(point);
}
This is a sample that aligns the currently displayed shapes to the left.
public void SetLocation(ICommandContext c, ICommandParams p)
{
IDiagram diagram = c.App.Workspace.CurrentEditor as IDiagram;
INodeCollection nodes = diagram.Nodes;
//Get the X coordinate of the leftmost node in the editor
double leftmostX = nodes.Select(n => n.LocationX).Min();
//Set the left coordinate of the node in the editor
foreach(INode node in nodes)
{
node.SetLocationAt(leftmostX, node.LocationY);
}
}