Issues with Connecting Shapes; when in Debug code works. When in Run it breaks

Started by Scott Morrison, June 16, 2021, 04:21:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Scott Morrison

Hello Folks,
I have not posted in a long time so this might not be clear.
I am using Visio 2019 to create Data Model diagrams.
Where I am using Visio to generate Relationships (To and From) for an object.
The code uses Template shapes, "Relationship Name", "Object Referenced" to be used to generate new shapes.

The problem I'm experiencing revolves around the generation of the Connections between generated shapes.
If I step through the code with a breakpoint and F8 I get the expected results.
If I run it in the normal mode it does not generate the connector shapes properly.

I've attach an explanation of the process and the suspect code.
I've tried "Just one more thing" for a day now and am getting no where.
I appreciate any help or Observation.
Thanks Scott

============
*Code that determines how to generate the connections and place them
              If cnt = 2 Then
                If GenerationTypeCboBox.Value = cGenUnderShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "1"
                    If Not (gShapeToConnectTo Is Nothing) Then
                      'The Connection point in the Visio Shape Sheet starts at row 1 BUT
                      '   The VBA Visio API starts at row 0. So in VBA reference the vso
                      '   connectionpoint row with Shape Sheet value minus 1
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "2", 3
                    End If
                End If
               
                If GenerationTypeCboBox.Value = cGenRightOfShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "1"
                    'If Not (gShapeToConnectTo Is Nothing) Then
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "2", 1
                    'End If
                End If
                 
                If GenerationTypeCboBox.Value = cGenLeftOfShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "2"
                    'If Not (gShapeToConnectTo Is Nothing) Then
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "1", 4
                    'End If
                End If

===========
* Code that generates the connectors with 2 PinX references
Private Sub connectShpsWithStdPinX(fromShape As Visio.Shape, toShape As Visio.Shape, _
                             lineWeight As String, linePattern As String)

    Dim cellBeginX As Visio.Cell
    Dim cellEndX As Visio.Cell
    Dim connectorShp As Visio.Shape

    Set vsoConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic connector")
    Set connectorShp = ActiveWindow.Page.Drop(vsoConnectorShape, 0, 0)
   
    connectorShp.Cells("LinePattern").formula = "= " & linePattern
    connectorShp.Cells("LineWeight").formula = "= " & lineWeight & " pt"
   
    connectorShp.Cells("BeginX").GlueTo fromShape.Cells("PinX")
    connectorShp.Cells("EndX").GlueTo toShape.Cells("PinX")

End Sub

===============
*Code that generates the connectors with a Connection Point and a PinX references
Private Sub connectShpsWithStdPinXCpt(fromShape As Visio.Shape, toShape As Visio.Shape, _
                             lineWeight As String, linePattern As String, connectPt As Integer)

    Dim cellBeginX As Visio.Cell
    Dim cellEndX As Visio.Cell
    Dim connectorShp As Visio.Shape

    Set vsoConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic connector")
    Set connectorShp = ActiveWindow.Page.Drop(vsoConnectorShape, 0, 0)
   
    connectorShp.Cells("LinePattern").formula = "= " & linePattern
    connectorShp.Cells("LineWeight").formula = "= " & lineWeight & " pt"
   
    connectorShp.Cells("BeginX").GlueTo fromShape.CellsSRC(visSectionConnectionPts, connectPt, 0)
    connectorShp.Cells("EndX").GlueTo toShape.Cells("PinX")

End Sub


Scott Morrison

Ok Folks so I have changed some of the code and have attached it below.
The issue seems to be that the connectors between the "Relationship" shape and the "To" shape. These connectors do not present them selves until the shapes are updated or moved after my code generates the connectors. If I attached the stacked dash connectors the connectors between the "Relationship" shape and the "To" shape appear (automagically).
So .... Is there a way to refresh the Active Page so it reflects all the changes to the page?
Thanks
Scott

============
*Code that determines how to generate the connections and place them
the cnt = 2 means that the shapes are in place to connect

              If cnt = 2 Then
                If GenerationTypeCboBox.Value = cGenUnderShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "1"
                    If Not (gShapeToConnectTo Is Nothing) Then
                      'The Connection point in the Visio Shape Sheet starts at row 1 BUT
                      '   The VBA Visio API starts at row 0. So in VBA reference the vso
                      '   connectionpoint row with Shape Sheet value minus 1
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "2", 3
                    End If
                End If
               
                If GenerationTypeCboBox.Value = cGenRightOfShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "1"
                    'If Not (gShapeToConnectTo Is Nothing) Then
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "2", 1
                    'End If
                End If
                 
                If GenerationTypeCboBox.Value = cGenLeftOfShape Then
                    connectShpsWithStdPinX fromVisioShapeObj, dupShape, "1.500", "2"
                    'If Not (gShapeToConnectTo Is Nothing) Then
                      connectShpsWithStdPinXCpt gShapeToConnectTo, fromVisioShapeObj, "1.500", "1", 4
                    'End If
                End If
             
              End If
            End If


===========
* Code that generates the connectors with 2 PinX references Updated

Private Sub connectShpsWithStdPinX(ByRef fromShape As Visio.Shape, ByRef toShape As Visio.Shape, _
                             ByVal lineWeight As String, ByVal linePattern As String)

    Dim cellBeginX As Visio.Cell
    Dim cellEndX As Visio.Cell
    Dim vsoMasterConnectorShape As Visio.Master
    Dim connectorShp As Visio.Shape

   
    Set vsoMasterConnectorShape = ActiveDocument.Masters.Item(cVsoConnectorShapeName)
    'Set vsoMasterConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic connector")
    Set connectorShp = ActiveWindow.Page.Drop(vsoMasterConnectorShape, 0, 0)
    'If connectorMaster Is Nothing Then
    '   ActiveDocument.Application.Documents.Add cVsoConnectorStencilName
    '   Set vsoConnectorMasterShape = visioStencil.Masters(cVsoConnectorShape)
    '   'Set vsoConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic Connector")
    'End If
   
   
    connectorShp.Cells("LinePattern").formula = "= " & linePattern
    connectorShp.Cells("LineWeight").formula = "= " & lineWeight & " pt"
   
    connectorShp.Cells("BeginX").GlueTo fromShape.Cells("PinX")
    connectorShp.Cells("EndX").GlueTo toShape.Cells("PinX")

End Sub

===============
*Code that generates the connectors with a Connection Point and a PinX references

Private Sub connectShpsWithStdPinXCpt(ByRef fromShape As Visio.Shape, ByRef toShape As Visio.Shape, _
                             ByVal lineWeight As String, ByVal linePattern As String, ByVal connectPt As Integer)

    Dim cellBeginX As Visio.Cell
    Dim cellEndX As Visio.Cell
    Dim cptCell As Visio.Cell
    Dim vsoMasterConnectorShape As Visio.Master
    Dim connectorShp As Visio.Shape

    Set vsoMasterConnectorShape = ActiveDocument.Masters.Item(cVsoConnectorShapeName)
    'Set vsoMasterConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic connector")
    Set connectorShp = ActiveWindow.Page.Drop(vsoMasterConnectorShape, 0, 0)
    'If connectorMaster Is Nothing Then
    '   ActiveDocument.Application.Documents.Add cVsoConnectorStencilName
    '   Set vsoConnectorMasterShape = visioStencil.Masters(cVsoConnectorShape)
    '   'Set vsoConnectorShape = Visio.ActivePage.Shapes.Item("Dynamic Connector")
    'End If
   
    connectorShp.Cells("LinePattern").formula = "= " & linePattern
    connectorShp.Cells("LineWeight").formula = "= " & lineWeight & " pt"
   
    Set cptCell = fromShape.CellsSRC(visSectionConnectionPts, connectPt, 0)
    If cptCell = 0 Then
        connectorShp.Cells("BeginX").GlueTo fromShape.Cells("PinX")
      Else
        connectorShp.Cells("BeginX").GlueTo fromShape.CellsSRC(visSectionConnectionPts, connectPt, 0)
    End If
    connectorShp.Cells("EndX").GlueTo toShape.Cells("PinX")

End Sub

Yacine

My 2 cents - without having checked your code - whenever I get results of the kind "it runs in step-by-step mode but not in continuous", I will add some "doevents" in the loops at appropriate places (possibly at the end of the loops) so the system (Visio) gets time to process my code and gets ready to process the next steps.
Yacine

vojo

FWIW, I have seen problems around trying use formulas to adjust pinx piny when NOT in a group.
Might be something there.   Not really seen anybody try to use beginx et al to a Pinx et al.

Scott Morrison

Thank you both for your comments.
I will try the doevents today and let you know the outcome.
Take care
Scott

Scott Morrison

Yacine,
You were right on.
I implemented the "DoEvents" and that solved the issue.
Works beautifully.
Thank you!
Scott