Node Links

Hi,

How do I prevent the same two nodes from being linked together more than once? Port can accept multiple connections but only one from any given node.

Regards,

Martin

If there’s only one port on each node, then that is the default behavior, controlled by GoPort.IsValidDuplicateLinks on both ports.
But since that’s the default behavior, you must have more than one port on each node, or else you wouldn’t be asking. What you need to do is override GoPort.IsValidLink or GoToolLinking.IsValidLink (on both of the view’s linking tools).
I would just call the base method. If it returned false, return false. If it returned true, return true iff there is no link already connecting the two nodes.
There isn’t any predicate for checking whether there is a link between two nodes, but you can easily see if one node is in the other node’s collection of Sources or Destinations, depending on the direction you are checking.

Thanks Walter,

For anyone who’s interested, here’s how I implemeted it.

public class UniqueLinkNewTool : GoToolLinkingNew
{
    public UniqueLinkNewTool(GoView view)
          : base(view)
    {
    }

    public override bool IsValidLink(IGoPort fromPort, IGoPort toPort)
    {
          if (base.IsValidLink(fromPort, toPort) == false)
          {
              return false;
          }

          foreach (IGoNode node in fromPort.Node.Destinations)
          {
              if (node == toPort.Node)
                  return false;
          }

          foreach (IGoNode node in toPort.Node.Destinations)
          {
              if (node == fromPort.Node)
                  return false;
          }

          return true;
    }
}

Iterating over toPort.Node.Destinations doesn’t make sense to me.
Here’s what I would do:
public class UniqueLinkNewTool : GoToolLinkingNew {
public UniqueLinkNewTool(GoView view) : base(view) {}
public override bool IsValidLink(IGoPort fromPort, IGoPort toPort) {
if (!base.IsValidLink(fromPort, toPort)) return false;
IGoNode fromNode = fromPort.Node;
foreach (IGoNode src in toPort.Node.Sources) {
if (src == fromNode) return false;
}
return true;
}
}
Reminder: if you want relinking to obey the same validity checks, you need to define a subclass of GoToolRelinking to override IsValidLink the same way.