when we drag the node(B) from the palette and drop it nearest to the other node(A). The automatic link should be created(A → B).
A and B are linked already. When we try to drop node C on the link. The output should be A → C ->B
is there any example for both the scenarios and using the location I need to get the nearest part.
Of course since your users are not dragging from a Palette but from your own component, you’ll have to implement your own drag-and-drop. Just make sure you compute the correct Point in document coordinates by calling Diagram.transformViewToDoc.
#2 There are several examples where dropping a Node onto a Link splices that node between the link’s connected nodes.
That’s right. As I said, if you aren’t using a Diagram such as Palette as the source of the drag-and-drop, you have to implement it yourself. So you cannot use the “ExternalObjectsDropped” DiagramEvent.
I assume you saw what that sample is doing – it calls Diagram.findPartsNear to find Nodes that are near a Point in document coordinates.
That Diagram method is just a convenience method that calls the more basic method Diagram.findObjectsNear, which is what you could use to look for individual objects that are ports, e.g. objects have GraphObject.portId set to some string.
As you can guess, the LinkingTool and RelinkingTool also search for nearby ports. If you want to look for valid ports that are nearby, consider this code:
LinkingBaseTool.findValidLinkablePort is an undocumented method that checks GraphObject.fromLinkable or toLinkable and calls either LinkingBaseTool.isValidFrom or isValidTo depending on the value of the “toEnd” argument. Whether you want to pass false or true there depends on which way you want to consider drawing a new link.