Deleting a node should not delete link connected to it


#1

Hi,

Consider a I have 2 nodes and a link connecting the nodes. Default behavior is that when I delete anyone of the nodes the link is also deleted along with the node.

I want to have a feature where in if I delete a node the link should not be deleted rather it should be a valid unconnected link.

Can you suggest any ways to do it?


#2

If you set CommandHandler.DeletingInclusions to None, do you get the behavior that you want?


#3

Hi Walter

I tried the above code but its not what I expected. Is it because I am using GraphLinkModel ?

Consider following:

I have 2 connected nodes as follows
image

If I delete the node which is on right, this is what I get i.e. the right node is deleted along with link
image

but what I want is
image


#4

Ah, yes – it’s the model that is enforcing that behavior.

OK, try this instead, which first disconnects the links from the nodes:

      myDiagram.SelectionDeleting += (object sender, DiagramEventArgs e) => {
        var m = myDiagram.Model as ILinksModel;
        foreach (var n in myDiagram.SelectedParts.OfType<Node>()) {
          foreach (var l in n.LinksConnected) {
            if (l.FromNode == n) m.SetLinkFromPort(l.Data, null, null);
            if (l.ToNode == n) m.SetLinkToPort(l.Data, null, null);
          }
        }
      };

#5

Hi,

I wanted to know if I can do the above code using GraphLinksModel instead of diagram object?

Thanks


#6

Not easily. If GraphLinksModel.RemoveNode were virtual, one could override that to just call DeleteNode. (Although if the node were a group, it should first remove all of the member parts.) Sorry.


#7

Actually I want to find the links connected to a node using nodeData in the deleting event we are finding it using selectedPart which is straight forward. But rather I would have to it in some way using nodedata and Model.


#8

Node.LinksConnected is implemented by asking the model to get the collection of connected links. So are all of the other properties and methods of Nodes and Links that involve relationships. So I don’t understand what you want.


#9

Well I found the solution for it. Here it is:

var connections = GoXamModel.GetLinksForNode(node).ToList();
foreach (var link in connections)
{
      if (link.From == node.Key)
      {
                 GoXamModel.SetLinkFromPort(link, null, null);
      }
     if (link.To == node.Key)
     {
                 GoXamModel.SetLinkToPort(link, null, null);
     }
}

where GoXamModel is my Graph link model.