2
votes

I'm creating a dynamic VSD from a hierarchical set of data that represents a flowchart. I don't want/need to fuddle with absolute positioning of these elements - the automatic layout options will work just fine.

The problem is I can't figure out how to perform this command via code. In the UI (Visio 2010), the commands are on the ribbon here: Design (tab) -> Layout (group) -> Re-Layout (SplitButton).

Any of these will do. Looking through the Visio SDK documentation and Googling for a couple days have turned up nothing of very much use.

Any ideas? (using C#, but VB/VBA would do)

2

2 Answers

2
votes

The Page.Layout() method itself is not enough.

In the WBSTreeView.sln sample project (VB.Net) I found how to accomplish this, but couldn't post my answer until 8 hours later :-x

The other layout types are possible by looking through the enums used below. Compact -> DownRight just ended up being better for most of the flows we're creating.

Translated to C#:

        // auto-layout, Compact Tree -> Down then Right
        var layoutCell = this._page.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOPlaceStyle);
        layoutCell.set_Result(
            VisUnitCodes.visPageUnits,
            (short)VisCellVals.visPLOPlaceCompactDownRight);
        layoutCell = this._page.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLORouteStyle);
        layoutCell.set_Result(
            VisUnitCodes.visPageUnits,
            (short)VisCellVals.visLORouteFlowchartNS);

        //// to change page orientation
        //layoutCell = this._page.PageSheet.get_CellsSRC(
        //    (short)VisSectionIndices.visSectionObject,
        //    (short)VisRowIndices.visRowPrintProperties,
        //    (short)VisCellIndices.visPrintPropertiesPageOrientation);
        //layoutCell.set_Result(
        //    VisUnitCodes.visPageUnits,
        //    (short)VisCellVals.visPPOLandscape);

        // curved connector lines
        layoutCell = this._page.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOLineRouteExt); 
        layoutCell.set_Result(
            VisUnitCodes.visPageUnits, 
            (short)VisCellVals.visLORouteExtNURBS);


        // perform the layout
        this._page.Layout();
        // optionally resize the page to fit the space taken by its shapes
        this._page.ResizeToFitContents();
        // 

Changing Connector Line Colors

If you're unfamiliar with how formulas for colors work, this might also be very frustrating. By default you can give an int as a string to get pre-defined colors, but this isn't very helpful because there isn't an easy way to figure out what each of those colors are. (There is a Page.Colors collection, but you have to inspect each of their RGB values and figure out the color from them.)

Instead, you can use your own RGB values for the formula.

    private void SetConnectorLineColor(Shape connector, string colorFormula)
    {
        var cell = connector.get_Cells("LineColor");
        cell.Formula = colorFormula;
    }

    internal static class AnswerColorFormula
    {
        public static string Green = "RGB(0,200,0)";
        public static string Orange = "RGB(255,100,0)";
        public static string Yellow = "RGB(255,200,0)";
        public static string Red = "RGB(255,5,5)";
    }
1
votes

Call the Layout method on the Page object. If there are shapes selected on this page then this method will only operate on the current selection. You may want to call DeselectAll on the ActiveWindow first.