With one trick that makes things easier - to enforce incoming points to be at the beginning of the collection - the code looks like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
AddElemWnd dlg = new AddElemWnd();
if ((bool)dlg.ShowDialog())
{
int nInpNum = int.Parse(dlg.textBoxInps.Text);
int nOutNum = int.Parse(dlg.textBoxOuts.Text);
ShapeNode node = m_Diagram.Factory.CreateShapeNode(40, 40, 8, 180, Shapes.Rectangle);
node.AnchorPattern = CreateAnchorPattern(nInpNum, nOutNum);
node.EnabledHandles = AdjustmentHandles.ResizeTopCenter |
AdjustmentHandles.ResizeBottomCenter | AdjustmentHandles.Move;
node.HandlesStyle = HandlesStyle.EasyMove;
node.Brush = Brushes.Black;
NodeProperties props = new NodeProperties(nInpNum, nOutNum);
node.Tag = props;
}
}
AnchorPattern CreateAnchorPattern(int nInpNum, int nOutNum)
{
AnchorPointCollection anchorPnts = new AnchorPointCollection();
double nOffset = 100.0 / (nInpNum + 1);
for (int i = 0; i < nInpNum; ++i)
{
AnchorPoint point = new AnchorPoint(0,
nOffset * (i + 1), true, false, MarkStyle.Rectangle);
anchorPnts.Add(point);
}
nOffset = 100.0 / (nOutNum + 1);
for (int i = 0; i < nOutNum; ++i)
{
AnchorPoint point = new AnchorPoint(100,
nOffset * (i + 1), false, true, MarkStyle.Rectangle);
anchorPnts.Add(point);
}
return new AnchorPattern(anchorPnts);
}
void ChangeAnchorPnts(int a_nInps, int a_nOuts)
{
var node = m_Diagram.ActiveItem as ShapeNode;
if (node == null || !(node.Tag is NodeProperties))
return;
NodeProperties props = (NodeProperties)node.Tag;
node.AnchorPattern = CreateAnchorPattern(a_nInps, a_nOuts);
var linksToDelete = new List<DiagramLink>();
foreach (DiagramLink link in node.IncomingLinks)
{
if (link.DestinationAnchor >= a_nInps)
linksToDelete.Add(link);
else
ReassignInLink(link, link.DestinationAnchor);
}
foreach (DiagramLink link in node.OutgoingLinks)
{
if (link.OriginAnchor - props.IncommingPointCount >= a_nOuts)
linksToDelete.Add(link);
else
ReassignOutLink(link, link.OriginAnchor - (props.IncommingPointCount - a_nInps));
}
foreach (DiagramLink link in linksToDelete)
m_Diagram.Links.Remove(link);
props.OutgoingPointCount = a_nOuts;
props.IncommingPointCount = a_nInps;
}
private void ReassignInLink(DiagramLink link, int newIndex)
{
link.DestinationAnchor = -1;
link.DestinationAnchor = newIndex;
link.Route();
}
private void ReassignOutLink(DiagramLink link, int newIndex)
{
link.OriginAnchor = -1;
link.OriginAnchor = newIndex;
link.Route();
}
We've also changed the NodeProperties attributes from uint to int to avoid some typecasts.
I hope that helps,
Stoyan