Denying InValid Links

I am trying to figure out a way to accept or reject a new link at my input port. I need to be able to make this decision based on the port that will be linking to my input port. My best guess was to override IsValidLink in my input port class since this function passes the other port but it doesn’t get called automatically. Is there something I can override or otherwise do that would allow me to accomplish the thing I described? Thanks.

Yes, that’s right, you can override GoPort.IsValidLink(IGoPort), or override GoToolLinking.IsValidLink(IGoPort, IGoPort).

I must be missing something. My overriden CanLinkTo gets called just fine but my IsValidLink does not get called when the link is made. What do I not understand about this?

Sorry, we must have different assumptions of what the circumstances are. I was assuming you wanted to limit which links the user could draw, either new or when relinking (those being two different tools that use GoToolLinking.IsValidLink, which calls GoPort.IsValidLink).
If you are talking about programmatic checks, then you can certainly call GoPort.IsValidLink yourself when you want to, or you can implement your own checking mechanism.

Right, I want to limit which links the user could draw, either new or when relinking. You say that my GoPort.IsValidLink should be called in this case, but it doesn’t get called. My port is derived from GoGeneralNodePort. I put a breakpoint in my IsValidLink function but it never goes there even when I’m drawing links left and right. Do I need to do something with GoToolLinking to make it work?

Are you sure that all the ports are actually instances of your derived port class? Remember that GoPort.IsValidLink(IGoPort) is not symmetrical, whereas GoToolLinking.IsValidLink(IGoPort, IGoPort) is.

Yes I am certain. In fact, the CanLinkTo method gets called in the same class for the same port that I would have expected the IsValidLink to be called but isn’t.

Do I have to make a class to override GoTool in order to get my Port’s IsValidLink to be called?

Maybe GoToolLinking.IsValidFromPort(IGoPort) is returning false, so that it doesn’t get a chance to consider GoToolLinking.IsValidLink from that port, because it thinks that a link cannot come out from that port.
GoToolLinking.IsValidFromPort is defined as:
public virtual bool IsValidFromPort(IGoPort fromPort) {
return fromPort.CanLinkFrom();
}

So if GoPort.CanLinkFrom() is returning false, the drawing-a-new-link operation won’t be starting from that port, but might consider going to it. Thus GoPort.IsValidLink wouldn’t be called on that port, but instead on some other port with your port as the argument.
GoPort.CanLinkFrom is defined as:
public virtual bool CanLinkFrom() {
if (!this.IsValidFrom) return false;
if (!CanView()) return false;
if (this.Layer != null &&
!this.Layer.CanLinkObjects())
return false;
return true;
}

That’s the trick. I didn’t consider that the IsValidLink would only fire on the output port and not on the input port. When I moved the code to my output port class, it worked fine.