Links, Nodes, Ports

So when have a node and want to add a link to it I know you need to:

  1. Set the link as to/from on the port.
  2. Add the port as Source/Destination on the link.
  3. Add the port to the node.

But do you have to add the link to the node as well? If not, then how does the Links property work on nodes?

The section titled “Diagrams” in the User Guide (p. 16) discusses the relationship between nodes, ports, and links.

Nodes normally contain ports. One port per node is sufficient for most of the simpler node classes. By default there’s no limit on how many links can be connected to a port. Various properties on GoPort control where and how a link will actually be routed at the port.

Links do not normally contain ports. However, if you want to connect a link to another link, you can add a port (or more than one) to a GoLabeledLink, typically as the MidLabel.

Nodes do not normally contain nodes or links, unless they are GoSubGraphBases, such as GoSubGraph or the BoxArea example class of Demo1, in which case they may very well contain a bunch of nodes and links.

The section on “Links” in the User Guide (p. 69) talks about how to create a link programmatically and how to control their appearance and routing. Typically all you need to do is:

GoLink link = new GoLink();
// … other initialization of the link …
link.FromPort = aNode.Port;
link.ToPort = bNode.Port;
goView1.Document.Add(link);

Yes I saw that and I wrote that sequence down in my original reply. Thank you for restating…

The question I was getting at is this… If you do that sequence, does aNode.Links provide the valid links? Does it ask all of its ports for their links behind the scenes?

Or is aNode.Links invalid unless you directly add a link to a node?

In the above code, aNode.Links would enumerate a single link, the one you just created, if there weren’t any links already connected to aNode.

So, yes, it “does ask all of its ports for their links”.

All of the graph traversal support (and for that matter, all of the group-children traversal) is purely structural. “Validity” of links is very much an application-specific concept. GoDiagram tries to provide mechanisms for doing all kinds of things, but your application will need to impose policies for making sure users only draw “valid” links. You can do that by overriding GoToolLinking.IsValidLink and/or GoPort.IsValidLink.