Automatic linking nodes

Could anyone please suggest if there is a way (property that I can set) to auto connect the nodes (ports) that I drop in design area, to other existing objects already available in design area?

Thanks in advance.

With regards.

Like the Flowgrammer sample does?

Somewhat like that. But in this case, I have t drop the object on the existing flow and it gets linked in between.

With reference to the flow grammer example, if i have effect 1 only in design area and i drop action 1 anywhere in design area and it gets linked to effect 1.

What I am looking for is, if I just have single object existing and I add the second one and that gets linked to the nearest object existing on the design area.

What node class are you using?

The class is GoGeneralNode.

OK… I did this code after I asked my question but before you replied. It uses
GoSimpleNode, which just has one input port and one output port.

void goViewDataflow_ExternalObjectsDropped(object sender, GoInputEventArgs e) {
  GoView view = sender as GoView;
  if (view == null) return;
  GoSimpleNode node = view.Selection.Primary as GoSimpleNode;
  if (node == null) return;

  if (node.InPort.LinksCount > 0) return; // only one in link.

  GoPort p = node.InPort;
  PointF toPoint = p.GetToLinkPoint(null);
  float bestDist = 9999999;
  GoPort bestPort = null;
  foreach (GoObject o in view.Document) {
    GoSimpleNode fromNode = o as GoSimpleNode;
    if (fromNode == null || fromNode == node) continue;
    GoPort fp = fromNode.OutPort;
    PointF fPt = fp.GetFromLinkPoint(null);
    float dx = fPt.X - toPoint.X;
    float dy = fPt.Y - toPoint.Y;
    float dist = dx * dx + dy * dy;  // don't bother taking sqrt
    if (dist < bestDist) {  // closest so far
      bestDist = dist;
      bestPort = fp;
    }
  }

  IGoLink l = view.CreateLink(bestPort, p);  // will check for null inputs 
}

GoGeneralNode is a little more complex as it can have any number of inputs and outputs,
so if you want to auto-wire the closest output to the input you just dropped, you have to pick
one of each on the node.

(or… if all the outputs match the “types” of inputs, you could wire them all up… although it’s up
to you to actually define a notion of a “port type”, whatever might make sense for your application.)