Author Topic: splitting connector event  (Read 568 times)

0 Members and 1 Guest are viewing this topic.

perry59

  • Sr. Member
  • ****
  • Posts: 279
splitting connector event
« on: June 07, 2022, 10:15:29 AM »
Is there an event which fires when a connector is split?
Reason:
I have connectors on a page which represent wires, there are several pieces of data associated with them. When I drag a shape which represents a splice (it has two connection points on it) it will split the connector as expected. When this happens one of the connectors still has it's original data but the newly created one does not. I know I can trap the shape added event when the new connector is added but I need to also know, or get the ID, of the original connector so I can copy it's data to the new one.
Is there some way to do this?
Thanks

Yacine

  • Hero Member
  • *****
  • Posts: 2948
Re: splitting connector event
« Reply #1 on: June 07, 2022, 11:59:02 PM »
Try the code bellow in a modeless form.


Code
Option Explicit


Private WithEvents vPages As Visio.Pages


Private startTime As Date
Private bNewShape As Boolean
Private bConnsDel As Boolean
Private bConnsAdded As Boolean
Private oldConn As Shape
Private newConn As Shape
Private newShp As Shape


Private Sub UserForm_Initialize()
    Set vPages = ActiveDocument.Pages
End Sub


Private Sub vPages_ConnectionsAdded(ByVal Connects As IVConnects)
    Dim conn As Connect
   
    If Now - startTime < 2 And bConnsDel Then
        Debug.Print "Now we can do stuff with"
        Debug.Print "New shape: ", newShp.Name
        Debug.Print "New connector: ", newConn.Name
        Debug.Print "Old connector: ", oldConn.Name
    End If
   
    'Reset'
    bConnsAdded = False
    bNewShape = False
    bConnsDel = False
    startTime = 0
    Set oldConn = Nothing
    Set newConn = Nothing
    Set newShp = Nothing
End Sub


Private Sub vPages_ConnectionsDeleted(ByVal Connects As IVConnects)
    Dim conn As Connect
   
    If Now - startTime < 1 And bNewShape Then
        bConnsDel = True
        Debug.Print "Conns deleted"
        For Each conn In Connects
            Debug.Print "conn deleted from sheet"; conn.FromSheet.Name
            Debug.Print "conn deleted to sheet"; conn.ToSheet.Name
            Set oldConn = conn.FromSheet
        Next conn
    Else
        'Reset'
        bNewShape = False
        bConnsDel = False
        startTime = 0
        Set oldConn = Nothing
        Set newConn = Nothing
        Set newShp = Nothing
    End If
   
End Sub


Private Sub vPages_ShapeAdded(ByVal Shape As IVShape)
    startTime = Now
    bNewShape = True
    If Shape.OneD Then
        Set newConn = Shape
    Else
        Set newShp = Shape
    End If
End Sub


I thought that it would be important to differentiate between a real split and manual operations.
So I catch the different events shapeAdded (twice - one for the shape the second for the connector), connections deleted and connections added.
Plus I set a timer so as to make sure this happens in a time shorter than done manually.
Only if all the flags are set to true and the time is less than a hard coded value (here 2), then do stuff.
Otherwise do nothing. Reset the variables in both cases.


Really cumbersome and complicated, MS should have given a dedicated event.
« Last Edit: June 08, 2022, 02:14:34 AM by Yacine »
Yacine

perry59

  • Sr. Member
  • ****
  • Posts: 279
Re: splitting connector event
« Reply #2 on: June 08, 2022, 12:33:46 PM »
Thanks Yacine, I'll play with that.
I also thought about catching the shape added event to see if a splice was added to the drawing, but often it is just dropped near the area to be used then moved into place so the shape added wouldn't be much use and I'd have to monitor cell change events (position) for the splice. VERY cumbersome. Then I thought about catching the connections added event which may get me what I want.
Perry

perry59

  • Sr. Member
  • ****
  • Posts: 279
Re: splitting connector event
« Reply #3 on: June 09, 2022, 02:43:23 PM »
The line in your code that was the key for me was:
"Set oldConn = conn.FromSheet"
I already had event handlers for connections added/deleted so I just integrated that and it gave me the ID of the connector that was split (I was typically looking at the "fromCell" objects). Once I had the "old" connector I just saved it's data and applied it to the "new" connector in my NoPendingEvents handler, worked like a charm.
Thanks Yacine!