Search code examples
c#vbavisio

How to reroute the connector in visio programmatically?


I'm developing VSTO add-in for visio 2013. I need to reroute the connector, for the diagram is complex and consists lots of steps. The automatically routed connector doesn't have a clear path. Is there any way available to reroute the connector programmatically? Just like draging the connector points in visio application manually.

I read some vba documents https://learn.microsoft.com/en-us/office/vba/api/visio.connect but none gives me insight.


Solution

  • You inspired me to take a crack at this, since I need to do something similar. Here's a way that I found to make it work. This subroutine takes a reference to a connector shape, hides the automatically-routed line, adds a new geometry section, and manually adjusts several points within that section (based on the percentage height/width of the originally-visible line). You can obviously adjust quantities / locations programatically.

    Sub CustomRoutedConnector(shpConnectorShape As Shape)
    
        With shpConnectorShape
            'Hide the automatically routed connector line (still there, just hidden)
            .CellsSRC(visSectionFirstComponent, 0, 2).FormulaU = "TRUE"
    
            'Make a custom geometry section
            .AddSection visSectionFirstComponent + 1
            .AddRow visSectionFirstComponent + 1, visRowComponent, visTagComponent
            .AddRow visSectionFirstComponent + 1, visRowVertex, visTagLineTo
            .AddRow visSectionFirstComponent + 1, visRowVertex, visTagMoveTo
    
            'Set up the new geometry section rows (11,1 and 11,2 are the start and end rows)
            .CellsSRC(11, 0, 0).FormulaForceU = "TRUE"
            .CellsSRC(11, 0, 1).FormulaForceU = "FALSE"
            .CellsSRC(11, 0, 2).FormulaForceU = "FALSE"
            .CellsSRC(11, 0, 3).FormulaForceU = "FALSE"
            .CellsSRC(11, 0, 5).FormulaForceU = "FALSE"
            .CellsSRC(11, 1, 0).FormulaU = "0"
            .CellsSRC(11, 1, 1).FormulaU = "0"
            .CellsSRC(11, 2, 0).FormulaU = "0"
            .CellsSRC(11, 2, 1).FormulaU = "0"
    
            'Add two additional rows for demonstration (could be programatically 
            'adjusted to however many points you need)
    
            .AddRow visSectionFirstComponent + 1, 2, visTagLineTo
            .CellsSRC(11, 2, 0).FormulaU = "0"
            .CellsSRC(11, 2, 1).FormulaU = "0"
    
            .AddRow visSectionFirstComponent + 1, 3, visTagLineTo
            .CellsSRC(11, 3, 0).FormulaU = "0"
            .CellsSRC(11, 3, 1).FormulaU = "0"
    
            'Adjust the geometry of the rows (the doubles given are percentages of the height/width of the original connector)
            'I recommend stepping through this to see how it moves the points:
            .CellsSRC(visSectionFirstComponent + 1, 2, 0).FormulaU = ".5"
            .CellsSRC(visSectionFirstComponent + 1, 3, 0).FormulaU = ".5"
            .CellsSRC(visSectionFirstComponent + 1, 3, 1).FormulaU = ".5"
            .CellsSRC(visSectionFirstComponent + 1, 4, 0).FormulaU = "1"
            .CellsSRC(visSectionFirstComponent + 1, 4, 1).FormulaU = "1"
    
        End With
    
    End Sub
    

    The end points of this custom route are still tied to the start and end shapes. To completely kill all automatic routing, use the .LayoutRoutePassive property of the page. (This might give you what you're looking for, but it would take some tinkering).