Clear Diagram / Model


What is the simplest way to clear/remove all nodes and links within a diagram? (Other than iterating over the node collection and removing one node at a time)


You could replace the Diagram.Model with an empty model.

Or if you want to keep the model that you are using, you could replace the DiagramModel.NodesSource with an empty collection (and if you are using a GraphLinksModel, its LinksSource too).

Thanks Walter

Hi Walter

I tried your suggestions but I’m an experiencing some odd behaviours.

I first tried replacing the Model with a new, empty one. I then add some nodes to this new, empty model but nothing plots at all.

I then tried your suggestion of replacing the NodeSource and LinksSource collections. Nodes are now plotting but they’re plotting incorrectly. They’re supposed to be plotting like this:

But instead they’re plotting like this:

With nodes right on top of each other.

Note, I’m clearing the model and adding the nodes within the same method call. Is this another example of the layout system not having sufficient time to ‘adjust’ to the changes?


Either way ought to work – I know we have lots of test cases that do those two things and several others too.

When you modify the model is your code executing within a transaction?

I’m using two transactions. One to clear/replace the Model and then one to add the nodes afterwards.

Here’s the code that is being called:


<span ="Apple-tab-span" style="white-space:pre">	</span>  //Clear the diagram<span ="Apple-tab-span" style="white-space: pre; ">	</span> 

        var mtDiagram = measureTreeDiagram.Diagram;

        //First, get the relevant measure node
        List<MeasureNode> measureMapCenterNodes = mtDiagram.Model.NodesSource.OfType<MeasureNode>().Where(n => n.MeasureID == measureID).ToList();

        if(measureMapCenterNodes.Count == 0)
            throw new InvalidOperationException("Measure is not contained in diagram");            


<span =“Apple-tab-span” style="white-space: pre; "> //Plot the relevant measure at the center
var measure = measureMapCenterNodes.First();
//Reset the location
measure.Location = new Point();
measure.LayoutID = “All”;
<span =“Apple-tab-span” style="white-space: pre; "> measure.Category = “MeasureMapNode”;

        //Add the center node

        int downstreamMeasures = 0, upstreamMeasures = 0;

        foreach(var measureMapCenterNode in measureMapCenterNodes) {
            downstreamMeasures += PlotDownstreamMeasures(measureMapCenterNode, measureTreeDiagram);
            upstreamMeasures += PlotUpstreamMeasures(measureMapCenterNode, measureTreeDiagram);

        measure["UpstreamMeasures"] = upstreamMeasures;
        measure["DownstreamMeasures"] = downstreamMeasures;


<span =“Apple-tab-span” style=“white-space:pre”> [/code]

and here’s the code of the ClearModel() method:

<span ="Apple-tab-span" style="white-space:pre">	</span>Diagram.StartTransaction("Clear");
<span ="Apple-tab-span" style="white-space:pre">	</span>Diagram.Model.NodesSource = new ObservableCollection<TNodeType>(); 
<span ="Apple-tab-span" style="white-space:pre">	</span>Diagram.Model.LinksSource = new ObservableCollection<TLinkType>();
<span ="Apple-tab-span" style="white-space:pre">	</span>Diagram.CommitTransaction("Clear");

<span ="Apple-tab-span" style="white-space:pre">	</span>

It’s hard for me to tell, but that looks OK. Have you temporarily disabled the automatic layout, perhaps by changing the Conditions? If you explicitly call Diagram.LayoutDiagram(), is everything positioned OK?

No, I’m not disabling the automatic layout. Explicitly calling Diagram.LayoutDiagram() does resolve the issue in the case where I replace the NodesSource and LinksSource collection. If I replace the entire Model, nothing get’s plotted.

This isn’t a major issue for me, I’ll just use whatever works. Just thought you guys should know of the issues I was experiencing.