Position a Group Node via code

Hi Walter

I’d would like to know what is the most appropriate way to position a group node via code?

Consider the following method:

private void BuildDiagram()
{
var diagram = ctlDiagram;

            diagram.StartTransaction("Test");

            var group = new GraphLinksModelNodeData<string>();
            group.IsSubGraph = true;
            group.Key = "TheGroup";
            group.Text = "Grouper";
            diagram.Model.AddNode(group);

            var n1 = new GraphLinksModelNodeData<string> { Key = "32", Location = new Point(250, 80), Text = "Accounts Payable", SubGraphKey = "TheGroup" };
            var n2 = new GraphLinksModelNodeData<string> { Key = "10", Location = new Point(160, 210), Text = "Administration Expenses", SubGraphKey = "TheGroup" };
            var n3 = new GraphLinksModelNodeData<string> { Key = "22", Location = new Point(330, 210), Text = "Cash", SubGraphKey = "TheGroup" };
            var n4 = new GraphLinksModelNodeData<string> { Key = "16", Location = new Point(183, 329), Text = "Deprecitation", SubGraphKey = "TheGroup" };

          

            diagram.Model.AddNode(n1);
            diagram.Model.AddNode(n2);
            diagram.Model.AddNode(n3);
            diagram.Model.AddNode(n4);                

            diagram.Model.AddLink(n1, null, n2, "T");
            diagram.Model.AddLink(n1, null, n3, "T");
            diagram.Model.AddLink(n2, null, n4, "T");

<span =“Apple-tab-span” style=“white-space:pre”> //Option1
<span =“Apple-tab-span” style=“white-space:pre”> //group.Location = new Point(500, 300);

<span =“Apple-tab-span” style=“white-space:pre”> //Option2
//var part = diagram.PartManager.FindNodeForData(group, diagram.Model);
//part.Move(new Point(500, 300), false);

            diagram.CommitTransaction("Test");                                           
        
    }

Option1
---------
group.Location = new Point(500, 300);
This seems to have no effect. The group node’s initial location is determined by it’s children.

Option 2
----------

var part = diagram.PartManager.FindNodeForData(group, diagram.Model);
part.Move(new Point(500, 300), false);
This sort of works but it produces the following strange behaviour:

The child links of the root node are no longer visible.

Here’s a breakdown of what I’m trying to achieve:

  1. I’ll be provided a collection of nodes from an external source, say a database for example. The nodes contain existing location data.
  2. I then want to add these nodes to a group node. The nodes should be laid out according to their associated location data.
  3. I then want to move the group to a specific location

Thanks
Justin

Yes, you should call Part.Move when you want to (also) move all of the Parts that are owned by that Part.

However, you need to be careful about finding that Part (a Group in your case) – it might not exist yet. That would cause a null reference exception.

Basically that’s a complication of trying to make model changes via the Diagram rather than directly in the model. I think it would make more sense that if you are positioning all of the regular nodes beforehand, you could just as easily shift all of those node’s locations in the node data.

But if your moving of the Group is dependent on the size of the group (and that would include the size of the individual member nodes), the move ought to be done later, after all of the nodes and links have been created and have had time to be measured and arranged and automatically laid out by the LayoutManager. You could do that either in a custom DiagramLayout or in a Diagram.InitialLayoutCompleted event handler.

Regarding the invisible links, that will require further investigation.

Thanks Walter