visual-studio-2015dsl-tools

How to add context menu option for laying out the diagram in DSL Tools?


I would like to add an option to the DSL extension that I am creating in the Visual Studio DSL Tools (Visualization and Modeling SDK), to auto-arrange the layout via the context menu that appears when right-clicking on the diagram. Is this possible?


Solution

  • This can be done by first of all declaring a new command that appears in the context menu when right-clicking on the diagram, and by then writing the handler code for this to layout the diagram.

    There's a very good guide to declaring and registering a new command on MSDN: How to: Add a Command to the Shortcut Menu

    The method needed layout the diagram is AutoLayoutShapeElements on the Diagram class.

    The following code will work for laying out the diagram (assuming you registered the method called OnArrangeDiagramClick as the event handler when overriding the GetMenuCommands method):

    private void OnArrangeDiagramClick(object sender, EventArgs e)
    {
        foreach (var selectedObject in CurrentSelection)
        {
            if (selectedObject is YourDslDiagram)
            {
                var diagram = (selectedObject as YourDslDiagram);
                using (var tx = diagram.Store.TransactionManager.BeginTransaction("ModelAutoLayout"))
                {
                    diagram.AutoLayoutShapeElements(diagram.NestedChildShapes, Microsoft.VisualStudio.Modeling.Diagrams.GraphObject.VGRoutingStyle.VGRouteStraight, Microsoft.VisualStudio.Modeling.Diagrams.GraphObject.PlacementValueStyle.VGPlaceSN, false);
                    tx.Commit();
                }
            }
        }
    }