Wasif Ali
Wasif Ali

Reputation: 227

auto arrange visio shapes through C#

I am using Microsoft Visio as a COM object in my C# application. I want to auto arrange shapes on a Visio page. What should I code for this task? The shapes are database entities.

userView.Shapes.SomeMethod();

userView is name of COM object but what should SomeMethod be?

Upvotes: 2

Views: 2032

Answers (3)

Todd
Todd

Reputation: 139

I know this is an 'older' question, but I'm working on something quite similar, and have managed to 'Auto-Layout' a flow chart with the following code:

public enum GraphStyles { TopDown, LeftRight };
public void ArrangeGraph(GraphStyles Style)
{
    if (Style == GraphStyles.TopDown)
    {
        // set 'PlaceStyle'
        var placeStyleCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOPlaceStyle).ResultIU = 1;
        // set 'RouteStyle'
        var routeStyleCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLORouteStyle).ResultIU = 5;
        // set 'PageShapeSplit'
        var pageShapeSplitCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOSplit).ResultIU = 1;
    }
    else if (Style == GraphStyles.LeftRight)
    {
        // set 'PlaceStyle'
        var placeStyleCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOPlaceStyle).ResultIU = 2;
        // set 'RouteStyle'
        var routeStyleCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLORouteStyle).ResultIU = 6;
        // set 'PageShapeSplit'
        var pageShapeSplitCell = VisApp.ActivePage.PageSheet.get_CellsSRC(
            (short)VisSectionIndices.visSectionObject,
            (short)VisRowIndices.visRowPageLayout,
            (short)VisCellIndices.visPLOSplit).ResultIU = 1;
    }
    else { throw new NotImplementedException("GraphStyle " + Style.ToString() + " is not supported"); }
    VisApp.ActivePage.Layout();
}

Hopefully this saves someone some time. It took me awhile to figure it out.

I'm using visio 2010 and visual studio 2010

Upvotes: 3

JensB
JensB

Reputation: 6880

I needed to do something similar a while ago..

I used Microsofts Glee library for the layout. There are very good samples included with the download which show you how to add nodes and relations and make them "auto arrange". However do note that Glee is not free for commercial use.

And then I used this example for converting the calculated positions from Glee to a Visio drawing.

Basically what I do is add all my nodes and relations too Glee and then get a list of nodes and their positions and add them to Visio using the second link.

Here is a graph example of what Glee can do:

Glee image example

Upvotes: 0

noonand
noonand

Reputation: 2865

This might help

Pertinent quote

To lay out a subset of the shapes of a page, master, or group, establish a Selection object in which the shapes to be laid out are selected, and then call the Layout method. If the Layout method is performed on a Selection object and the object has no shapes selected, all shapes in the page, master, or group of the selection are laid out.

EDIT: noonand 2012-09-21 Added information about the LayoutIncremental method

Just had another look at the object model and it appears the method you want is the LayoutIncremental method

Excerpt from the relevant help topic says:

Page.LayoutIncremental(AlignOrSpace, AlignHorizontal, AlignVertical, SpaceHorizontal, SpaceVertical, UnitsNameOrCode)

Upvotes: 0

Related Questions