Of Connectivity

Started by wapperdude, February 04, 2019, 11:12:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

Now that I'm into the modern era of Visio, I re-looked at finding connectivity.  After all, there are new features in Visio object model.  What I didn't realize is that there are two ways to go about this...well, three, using the old style.  One method uses GluedShapes method and the other uses ConnectedShapes method. 

Thanks to David Parker for his efforts and code for both of these:  https://blog.bvisual.net/2009/09/16/listing-connections-in-visio-2010/

I've modified both versions to be a little more general, and just serve as basic building block code.


Public Sub ListGluedConnections()
'Code lists shapes attached to connectors
'Original code developed by David Parker
'Code located @ https://blog.bvisual.net/2009/09/16/listing-connections-in-visio-2010/
'Minor code modification by Wapperdude, 2/4/2019
'
    Dim shp As Visio.Shape
    Dim connectorShape As Visio.Shape
    Dim sourceShape As Visio.Shape
    Dim targetShape As Visio.Shape
    Dim aryTargetIDs() As Long
    Dim arySourceIDs() As Long
    Dim targetID As Long
    Dim sourceID As Long
    Dim i As Integer
    For Each shp In Visio.ActivePage.Shapes
        If shp.OneD Then
            Debug.Print "Connector", shp.Name
            arySourceIDs = shp.GluedShapes(visGluedShapesIncoming2D, "")
            For i = 0 To UBound(arySourceIDs)
                Set sourceShape = Visio.ActivePage.Shapes.ItemFromID(arySourceIDs(i))
                Debug.Print "Src Shp: ", sourceShape.Name
            Next
            aryTargetIDs = shp.GluedShapes(visGluedShapesOutgoing2D, "")
            For i = 0 To UBound(aryTargetIDs)
            Set targetShape = Visio.ActivePage.Shapes.ItemFromID(aryTargetIDs(i))
            Debug.Print "Tgt Shp: ", targetShape.Name
            Next
        Debug.Print ""
        End If
    Next
End Sub
Public Sub ListShapeConnections()
'Code finds shapes with outgoing connections and lists their target shapes.
'Original code developed by David Parker
'Code located @ https://blog.bvisual.net/2009/09/16/listing-connections-in-visio-2010/
'Code adapted by Wapperdude, 2/4/2019
'
    Dim shp As Visio.Shape
    Dim connectorShape As Visio.Shape
    Dim sourceShape As Visio.Shape
    Dim targetShape As Visio.Shape
    Dim aryTargetIDs() As Long
    Dim arySourceIDs() As Long
    Dim targetID As Long
    Dim sourceID As Long
    Dim i As Integer
    For Each shp In Visio.ActivePage.Shapes
        If Not (shp.OneD) Then
            arySourceIDs = shp.ConnectedShapes(visConnectedShapesOutgoingNodes, "")
            For i = 0 To UBound(arySourceIDs)
                If i = 0 And UBound(arySourceIDs) >= 0 Then
                    Debug.Print ""
                    Debug.Print shp.Name
                End If
                    Set targetShape = Visio.ActivePage.Shapes.ItemFromID(arySourceIDs(i))
                    Debug.Print "Tgt Shp: ", targetShape.Name
            Next
        End If
    Next
End Sub


Output from 1st code:
Connector     Relationship
Src Shp:      Entity
Tgt Shp:      Entity.19

Connector     Relationship.60
Src Shp:      Entity.19
Tgt Shp:      Entity.37

Connector     Dynamic connector
Src Shp:      Entity
Tgt Shp:      Entity.65

Connector     Dynamic connector.106
Src Shp:      Entity.65
Tgt Shp:      Entity.83

Connector     Dynamic connector.116
Src Shp:      Entity.19
Tgt Shp:      Sheet.85

Output from 2nd code:
Entity
Tgt Shp:      Entity.19
Tgt Shp:      Entity.65

Entity.19
Tgt Shp:      Entity.37
Tgt Shp:      Sheet.85

Entity.65
Tgt Shp:      Entity.83
Visio 2019 Pro

wapperdude

Updated the 2nd code module to include Incoming connections.  The results are somewhat redundant except for the case when a shape has connections that are sourced from or targeted to another 1D shape.  Those are mutually exclusive.  The GluedShapes method may be more efficient / effective for such a scenario.

Anyway, here's the updated code:

Public Sub ListShapeConnections()
'Code finds shapes with outgoing connections and lists their target shapes.
'Original code developed by David Parker
'Code located @ https://blog.bvisual.net/2009/09/16/listing-connections-in-visio-2010/
'Code adapted by Wapperdude, 2/4/2019
'The code has two search sections:  OutgoingNodes and IncomingNodes
'These tend to have redundant info except for the unique case where shapes with connections
'that are derived via other 1D shapes.
'
    Dim shp As Visio.Shape
    Dim connectorShape As Visio.Shape
    Dim sourceShape As Visio.Shape
    Dim targetShape As Visio.Shape
    Dim aryTargetIDs() As Long
    Dim arySourceIDs() As Long
    Dim targetID As Long
    Dim sourceID As Long
    Dim i As Integer
    For Each shp In Visio.ActivePage.Shapes
        If Not (shp.OneD) Then
            aryTargetIDs = shp.ConnectedShapes(visConnectedShapesOutgoingNodes, "")
            For i = 0 To UBound(aryTargetIDs)
                If i = 0 And UBound(aryTargetIDs) >= 0 Then
                    Debug.Print ""
                    Debug.Print shp.Name
                End If
                    Set targetShape = Visio.ActivePage.Shapes.ItemFromID(aryTargetIDs(i))
                    Debug.Print "Tgt Shp: ", targetShape.Name
            Next
''This next section will catch shapes that have incoming connections and list their sources.
'            arySourceIDs = shp.ConnectedShapes(visConnectedShapesIncomingNodes, "")
'            For i = 0 To UBound(arySourceIDs)
'                If i = 0 And UBound(arySourceIDs) >= 0 Then
'                    Debug.Print ""
'                    Debug.Print shp.Name
'                End If
'                    Set sourceShape = Visio.ActivePage.Shapes.ItemFromID(arySourceIDs(i))
'                    Debug.Print "Src Shp: ", sourceShape.Name
'            Next
        End If
    Next
End Sub
Visio 2019 Pro