vsoShape.connectedShapes () возвращает Sheet.xxx вместо Shape Name для некоторых фигур - PullRequest
1 голос
/ 24 сентября 2019

Я работал над кодом для идентификации кабелей в чертеже Visio на основе форм разъемов, из которых состоит кабель.

Мой код в основном реализован на этом этапе.В настоящее время он:

  1. Идентифицирует фигуры "соединителя" на чертеже
  2. Проверяет, с какими другими фигурами они связаны, используя vsoShape.connectedShapes (visConnectedShapesAllNodes, "")
  3. Печатает найденные соединители (исходная форма соединителя и форма соединенного соединителя)
  4. Выполняет серию операторов if / elseif для определения кабеля на основе найденных соединителей и дополнительного пользовательского ввода длины (некоторые кабели используют одни и те же соединители))
  5. Печатает число, идентифицирующее кабель

Успешный вывод выглядит следующим образом:

Found Connector: USB A - top.6
Shapes connected:
USB Micro B.113

CABLE INFO
Connector 1: USB A - top.6
Connector 2: USB Micro B.113
Connector 3: None
Cable Length: 1 m
Cable #: 812943-0010

Это работает для более 95% моего рисунка.Однако у меня возникла проблема, из-за которой иногда имя фигуры будет возвращаться как «sheet.xxx», как показано ниже:

Found Connector: USB A - top.199
Shapes connected:
Sheet.211

CABLE INFO
Connector 1: USB A - top.199
Connector 2: Sheet.211
Connector 3: None
Cable Length: 0
Cable #: 



Found Connector: USB A Female
Shapes connected:

CABLE INFO
Connector 1: USB A Female
Connector 2: 
Connector 3: None
Cable Length: 0
Cable #: 

Если указано «Sheet.211», возвращаемое имя должно были "USB A Female".Затем для фигуры «USB A Female» подключенная фигура должна была быть «USB A - top.199», а не возвращаться пустой.

Я попытался повторно добавить фигуры в свой чертеж и повторно-соединение их с динамическим разъемом, но проблема не устранена.Кто-нибудь еще сталкивался с этим?Может быть что-то не так с формами, поскольку они являются частью пользовательского трафарета?

Ниже приведен код из моего модуля:

Option Explicit 

Public LengthAndNum As Variant ' Retrieved from UserForm Selection
Dim foundShapes As New Collection ' The 'new' initialised the Collection
Dim foundCables() As Variant

Sub Main()
    Dim thePage As Page
    Dim connectedShapes As Collection
    Dim cableList As Variant

    Set thePage = ActiveDocument.Pages("Page-1") ' Will need to account for multi page schematics
    cableList = FindCables(thePage)

End Sub

Function FindCables(chosenPage As Page) As Variant
    Dim shapeLoopIterator As Shape
    Dim arrayLoopIterator As Long
    Dim validShapes As Variant

    Dim allShapes As Visio.Shapes
    Dim vsoShape As Visio.Shape
    Dim lngShapeIDs() As Long
    Dim intCount As Integer
    Dim intCount2 As Integer

    Dim cable As CCable


    ' validShapes is a list of all possible connector shapes for a schematic
    validShapes = Array("USB A - top", "USB A BLK", "USB A RED", "USB A - side", "USB A Female", "USB Mini B", "USB Micro B", _
    "USB C Male", "USB C Female", "Frames charger", "Lightning", "USB Surface Mount", "Sidecar", _
    "3.5mm stereo M", "3.5mm stereo F", "3.5mm stereo BLK", "3.5mm stereo RED", "3.5mm 5-pin", "3.5mm stereo right angle", _
    "3.5mm Aux", "HDMI", "VGA", "DVI", "SNF cable", "Optical plug", "Optical mini", "Ethernet", "Ethernet Female", _
    "4pin bare wire adapter", "Small 6pin power", "120/520 6pin plug", "120/520 4pin plug", "120/520 10pin plug", _
    "Maxwell 2pin plug", "AC2 plug", "2pin bare wire block", "4pin bare wire block", "SMB plug", "Coax", _
    "C13 plug - top", "C13 plug - side", "C14 'Ears' plug - top", "C7 plug", "C14/C18 plug - side", _
    "Liberator plug", "Liberator", "C14 Universal Plug Adapter", "Generic wall plug", "3pin plug male", "2pin plug male", _
    "2pin plug female", "2pin top", "4pin molded plug - side", "4pin molded plug - top", "4pin plug - female", "4pin plug - top", _
    "Barrel plug male", "Barrel plug female", "Mini barrel plug")


    For Each shapeLoopIterator In chosenPage.Shapes ' Loop through object collection
        For arrayLoopIterator = LBound(validShapes) To UBound(validShapes) ' Loop through the array
            If InStr(1, shapeLoopIterator, validShapes(arrayLoopIterator)) Then

                Set cable = New CCable ' Empty property values
                cable.Connector3() = "None" ' In case there isn't a 3rd connector - May be able to remove
                Set vsoShape = shapeLoopIterator

                Debug.Print vbCrLf + vbCrLf + vbCrLf + "Found Connector: " + CStr(vsoShape)  ' Sanity Print
                lngShapeIDs = vsoShape.connectedShapes(visConnectedShapesAllNodes, "")
                cable.Connector1() = CStr(vsoShape)
                cable.Length() = "0" ' Will need user input

                Debug.Print "Shapes connected:"
                Set allShapes = ActiveDocument.Pages.Item(1).Shapes

                intCount2 = 0
                For intCount = 0 To UBound(lngShapeIDs)
                    Debug.Print allShapes.ItemFromID(lngShapeIDs(intCount)).Name ' Sanity Print
                    If intCount2 = 0 Then
                        cable.Connector2() = allShapes.ItemFromID(lngShapeIDs(intCount)).Name
                    Else
                        cable.Connector3() = allShapes.ItemFromID(lngShapeIDs(intCount)).Name
                    End If
                    intCount2 = intCount2 + 1
                Next

                Debug.Print vbCrLf + "CABLE INFO" + vbCrLf + "Connector 1: " + cable.Connector1
                Debug.Print "Connector 2: " + cable.Connector2
                Debug.Print "Connector 3: " + cable.Connector3

                ' If there are TWO connectors
                If cable.Connector3 = "None" Then
                    ' If one connector is USB A MALE
                    If InStr(1, cable.Connector1, "USB A -") = 1 Then
                        If InStr(1, cable.Connector2, "USB A Female") = 1 Then
                            cable.Num = "831835-0010"
                            cable.Length = "3 m"

                        ElseIf InStr(1, cable.Connector2, "USB Mini B") = 1 Then
                            cable.Num = "720031-0010"
                            cable.Length = "4 ft"

                        ElseIf InStr(1, cable.Connector2, "USB Micro B") = 1 Then

                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm1.Show

                            If InStr(1, LengthAndNum, "807815-0522102") Then
                                cable.Num = "807815-0522102"
                                cable.Length = "0.5 m"

                            ElseIf InStr(1, LengthAndNum, "785495-0010") Then
                                cable.Num = "785495-0010"
                                cable.Length = ".5 m"

                            ElseIf InStr(1, LengthAndNum, "785495-0020") Then
                                cable.Num = "785495-0020"
                                cable.Length = "1 mm"

                            ElseIf InStr(1, LengthAndNum, "785495-0030") Then
                                cable.Num = "785495-0030"
                                cable.Length = "1.5 m"

                            ElseIf InStr(1, LengthAndNum, "785495-0040") Then
                                cable.Num = "785495-0040"
                                cable.Length = "2 m"

                            ElseIf InStr(1, LengthAndNum, "812943-0010") Then
                                cable.Num = "812943-0010"
                                cable.Length = "1 m"

                            ElseIf InStr(1, LengthAndNum, "812943-0020") Then
                                cable.Num = "812943-0020"
                                cable.Length = "2 m"

                            ElseIf InStr(1, LengthAndNum, "812943-0030") Then
                                cable.Num = "812943-0030"
                                cable.Length = "0.5 m"

                            ElseIf InStr(1, LengthAndNum, "795696-0010") Then
                                cable.Num = "795696-0010"
                                cable.Length = "2 m"
                            End If


                        ElseIf InStr(1, cable.Connector2, "USB C Male") = 1 Then
                            cable.Num = "806256-0010"
                            cable.Length = "1 m"

                        ElseIf InStr(1, cable.Connector2, "USB C Female") = 1 Then
                            cable.Num = "845443-0010"
                            cable.Length = "1 ft"

                        ElseIf InStr(1, cable.Connector2, "3.5mm stereo M") = 1 Then
                            cable.Num = "844283-0010"
                            cable.Length = "40 in"

                        End If

                    ElseIf InStr(1, cable.Connector1, "USB A Female") = 1 Then
                        If InStr(1, cable.Connector2, "USB C Male") = 1 Then
                            cable.Num = "835613-0010"
                            cable.Length = "4 in"
                        End If

                    ElseIf InStr(1, cable.Connector1, "USB Mini B") = 1 Then
                        If InStr(1, cable.Connector2, "USB C Male") = 1 Then
                            cable.Num = "806930-0010"
                            cable.Length = "4 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "HDMI") = 1 Then
                        If InStr(1, cable.Connector2, "HDMI") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm2.Show

                            If InStr(1, LengthAndNum, "326853-0110") Then
                                cable.Num = "326853-0110"
                                cable.Length = "2 m"
                            ElseIf InStr(1, LengthAndNum, "625673-0010") Then
                                cable.Num = "625673-0010"
                                cable.Length = "3 ft"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "3.5mm stereo M") = 1 Then
                        If InStr(1, cable.Connector2, "3.5mm stereo M") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm6.Show

                            If InStr(1, LengthAndNum, "777201-0020") Then
                                cable.Num = "777201-0020"
                                cable.Length = "3 ft"
                            ElseIf InStr(1, LengthAndNum, "777201-0010") Then
                                cable.Num = "777201-0010"
                                cable.Length = "6 ft"
                            ElseIf InStr(1, LengthAndNum, "777201-0030") Then
                                cable.Num = "777201-0030"
                                cable.Length = "15 ft"
                            End If

                        ElseIf InStr(1, cable.Connector2, "3.5mm stereo F") = 1 Then
                            cable.Num = "844319-0010"
                            cable.Length = "1 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "Ethernet") = 1 Then
                        If InStr(1, cable.Connector2, "Ethernet Female") = 1 Then
                            cable.Num = "776598-0010"
                            cable.Length = "3 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "Optical plug") = 1 Then
                        If InStr(1, cable.Connector2, "Optical plug") = 1 Then
                            cable.Num = "629769-0010"
                            cable.Length = "2 m"
                        End If

                    ElseIf InStr(1, cable.Connector1, "Optical plug") = 1 Then
                        If InStr(1, cable.Connector2, "Optical mini") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm7.Show

                            If InStr(1, LengthAndNum, "806259-0010") Then
                                cable.Num = "806259-0010"
                                cable.Length = "1.5 m"
                            ElseIf InStr(1, LengthAndNum, "806259-0020") Then
                                cable.Num = "806259-0020"
                                cable.Length = "2 m"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "3pin plug male") = 1 Then
                        If InStr(1, cable.Connector2, "Mini barrel plug") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm8.Show

                            If InStr(1, LengthAndNum, "727228-0010") Then
                                cable.Num = "727228-0010"
                                cable.Length = "1 ft"
                            ElseIf InStr(1, LengthAndNum, "727228-0020") Then
                                cable.Num = "727228-0020"
                                cable.Length = "42 in"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "Mini barrel plug") = 1 Then
                        If InStr(1, cable.Connector2, "Barrel plug male") = 1 Then
                            cable.Num = "774988-0010"
                            cable.Length = "2 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "2pin plug male") = 1 Or InStr(1, cable.Connector1, "2pin top") = 1 Then
                        If InStr(1, cable.Connector2, "2pin plug female") = 1 Then
                            cable.Num = "716730-0010"
                            cable.Length = "4 m"
                        End If

                    ElseIf InStr(1, cable.Connector1, "2pin plug female") = 1 Then
                        If InStr(1, cable.Connector2, "4pin plug - female") = 1 Then
                            cable.Num = "727252-0010"
                            cable.Length = "4 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "3pin plug male") = 1 Then
                        If InStr(1, cable.Connector2, "4pin plug - top") = 1 Then
                            cable.Num = "727254-0010"
                            cable.Length = "3 ft"
                        End If

                    ElseIf InStr(1, cable.Connector1, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "2pin top") = 1 Then
                        If InStr(1, cable.Connector2, "Barrel plug male") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm9.Show

                            If InStr(1, LengthAndNum, "718531-0010") Then
                                cable.Num = "718531-0010"
                                cable.Length = "3 ft"
                            ElseIf InStr(1, LengthAndNum, "773374-0010") Then
                                cable.Num = "773374-0010"
                                cable.Length = "1.5 m"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "4pin plug - top") = 1 Then
                        If InStr(1, cable.Connector2, "Mini barrel plug") = 1 Then
                            cable.Num = "778056-0010"
                            cable.Length = "1 ft"
                        ElseIf InStr(1, cable.Connector2, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "2pin top") = 1 Then
                            cable.Num = "815212-0010"
                            cable.Length = "18 in"
                        End If

                    ElseIf InStr(1, cable.Connector1, "2pin plug male") = 1 Or InStr(1, cable.Connector1, "2pin top") = 1 Then
                        If InStr(1, cable.Connector2, "4pin plug - female") = 1 Then
                            cable.Num = "806251-0010"
                            cable.Length = "18 in"
                        ElseIf InStr(1, cable.Connector2, "3pin plug male") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm10.Show

                            If InStr(1, LengthAndNum, "806253-0010") Then
                                cable.Num = "806253-0010"
                                cable.Length = "1 ft"
                            ElseIf InStr(1, LengthAndNum, "806253-0020") Then
                                cable.Num = "806253-0020"
                                cable.Length = "3 ft"
                            End If

                        ElseIf InStr(1, cable.Connector2, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "2pin top") = 1 Then
                            ''''''''''''''''''''''''''''''''
                            ' Ask user for length of cable '
                            ''''''''''''''''''''''''''''''''
                            UserForm11.Show

                            If InStr(1, LengthAndNum, "806254-0010") Then
                                cable.Num = "806254-0010"
                                cable.Length = "1 ft"
                            ElseIf InStr(1, LengthAndNum, "806254-0020") Then
                                cable.Num = "806254-0020"
                                cable.Length = "3 ft"
                            End If

                        ElseIf InStr(1, cable.Connector1, "C7 plug") = 1 Then
                            If InStr(1, cable.Connector2, "C14/C18 plug - side") = 1 Then
                                ''''''''''''''''''''''''''''''''
                                ' Ask user for length of cable '
                                ''''''''''''''''''''''''''''''''
                                UserForm12.Show

                                If InStr(1, LengthAndNum, "843987-0010") Then
                                    cable.Num = "843987-0010"
                                    cable.Length = "1 ft"
                                ElseIf InStr(1, LengthAndNum, "843987-0020") Then
                                    cable.Num = "843987-0020"
                                    cable.Length = "18 in"
                                ElseIf InStr(1, LengthAndNum, "843987-0030") Then
                                    cable.Num = "843987-0030"
                                    cable.Length = "2 ft"
                                ElseIf InStr(1, LengthAndNum, "843987-0040") Then
                                    cable.Num = "843987-0040"
                                    cable.Length = "3 ft"
                                ElseIf InStr(1, LengthAndNum, "843987-0050") Then
                                    cable.Num = "843987-0050"
                                    cable.Length = "54 in"
                                End If
                            End If

                        ElseIf InStr(1, cable.Connector1, "C13 plug - top") = 1 Or InStr(1, cable.Connector1, "C13 plug - side") = 1 Then
                            If InStr(1, cable.Connector2, "C14/C18 plug - side") = 1 Then
                                ''''''''''''''''''''''''''''''''
                                ' Ask user for length of cable '
                                ''''''''''''''''''''''''''''''''
                                UserForm13.Show

                                If InStr(1, LengthAndNum, "844019-0010") Then
                                    cable.Num = "844019-0010"
                                    cable.Length = "1 ft"
                                ElseIf InStr(1, LengthAndNum, "844019-0020") Then
                                    cable.Num = "844019-0020"
                                    cable.Length = "18 in"
                                ElseIf InStr(1, LengthAndNum, "844019-0030") Then
                                    cable.Num = "844019-0030"
                                    cable.Length = "2 ft"
                                ElseIf InStr(1, LengthAndNum, "844019-0040") Then
                                    cable.Num = "844019-0040"
                                    cable.Length = "3 ft"
                                ElseIf InStr(1, LengthAndNum, "844019-0050") Then
                                    cable.Num = "844019-0050"
                                    cable.Length = "79 in"
                                End If
                            End If

                        ElseIf InStr(1, cable.Connector1, "Liberator") = 1 Then
                            If InStr(1, cable.Connector2, "C14/C18 plug - side") = 1 Then ' BUT this fieldwas showing as blank
                                ''''''''''''''''''''''''''''''''
                                ' Ask user for length of cable '
                                ''''''''''''''''''''''''''''''''
                                UserForm14.Show

                                If InStr(1, LengthAndNum, "7 IN") Then
                                    Debug.Print "7 inch CABLE - picklist"
                                    cable.Num = "7INLIBERATOR"
                                    cable.Length = "7 in"
                                ElseIf InStr(1, LengthAndNum, "3 FT") Then
                                    Debug.Print "3 foot CABLE - picklist"
                                    cable.Num = "3FTLIBERATOR"
                                    cable.Length = "3 ft"
                                End If
                            End If

                        ElseIf InStr(1, cable.Connector1, "C13 plug - top") = 1 Then
                            If InStr(1, cable.Connector2, "Generic wall plug") = 1 Then ' BUT this fieldwas showing as blank
                                cable.Num = "INLET"
                                cable.Length = "2.5 m or 4.5 m"
                            End If
                        End If
                    End If

                Else ' If there are THREE CONNECTORS
                    If InStr(1, cable.Connector1, "USB C Male") = 1 Then
                        If InStr(1, cable.Connector2, "USB A Female") = 1 Or InStr(1, cable.Connector2, "3.5mm stereo F") = 1 Then
                            If InStr(1, cable.Connector3, "USB A Female") = 1 Or InStr(1, cable.Connector3, "3.5mm stereo F") = 1 Then
                                cable.Num = "809528-0010"
                                cable.Length = "6 in"
                            End If
                        ElseIf InStr(1, cable.Connector2, "USB A RED") = 1 Or InStr(1, cable.Connector2, "USB A BLK") = 1 Then
                            If InStr(1, cable.Connector3, "USB A RED") = 1 Or InStr(1, cable.Connector3, "USB A BLK") = 1 Then
                                cable.Num = "831834-0010"
                                cable.Length = "2 m"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "3.5mm 5-pin") = 1 Then
                        If InStr(1, cable.Connector2, "3.5mm stereo BLK") = 1 Or InStr(1, cable.Connector2, "3.5mm stereo RED") = 1 Then
                            If InStr(1, cable.Connector3, "3.5mm stereo RED") = 1 Or InStr(1, cable.Connector3, "3.5mm stereo BLK") = 1 Then
                                ''''''''''''''''''''''''''''''''
                                ' Ask user for length of cable '
                                ''''''''''''''''''''''''''''''''
                                UserForm3.Show

                                If InStr(1, LengthAndNum, "371949-0010") Then
                                    cable.Num = "371949-0010"
                                    cable.Length = "4 ft"
                                ElseIf InStr(1, LengthAndNum, "775397-0010") Then
                                    cable.Num = "775397-0010"
                                    cable.Length = "8 ft"
                                ElseIf InStr(1, LengthAndNum, "797051-0010") Then
                                    cable.Num = "797051-0010"
                                    cable.Length = "8 ft"
                                End If
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "4pin plug - top") = 1 Then
                        If InStr(1, cable.Connector2, "2pin top") = 1 Or InStr(1, cable.Connector2, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "Mini barrel plug") = 1 Then
                            If InStr(1, cable.Connector3, "Mini barrel plug") = 1 Or InStr(1, cable.Connector3, "2pin top") = 1 Or InStr(1, cable.Connector3, "2pin plug male") = 1 Then
                                cable.Num = "739259-0010"
                                cable.Length = "1 ft"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "2pin top") = 1 Or InStr(1, cable.Connector2, "2pin plug male") = 1 Then
                        If InStr(1, cable.Connector2, "2pin plug female") = 1 Then
                            If InStr(1, cable.Connector3, "2pin plug female") = 1 Then
                                cable.Num = "774410-0010"
                                cable.Length = "4 ft"
                            End If
                        End If

                    ElseIf InStr(1, cable.Connector1, "4pin plug - female") = 1 Then
                        If InStr(1, cable.Connector2, "2pin plug female") = 1 Or InStr(1, cable.Connector2, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "2pin top") = 1 Then
                            If InStr(1, cable.Connector3, "2pin plug male") = 1 Or InStr(1, cable.Connector3, "2pin top") = 1 Or InStr(1, cable.Connector3, "2pin plug female") = 1 Then
                                cable.Num = "806255-0010"
                                cable.Length = "18 in"
                            End If
                        ElseIf InStr(1, cable.Connector2, "2pin plug male") = 1 Or InStr(1, cable.Connector2, "2pin top") = 1 Or InStr(1, cable.Connector2, "4pin plug - top") = 1 Then
                            If InStr(1, cable.Connector3, "4pin plug - top") = 1 Or InStr(1, cable.Connector3, "2pin plug male") = 1 Or InStr(1, cable.Connector3, "2pin top") = 1 Then
                                cable.Num = "809521-0010"
                                cable.Length = "18 in"
                            End If
                        End If
                    End If
                End If

                Debug.Print "Cable Length: " + cable.Length
                Debug.Print "Cable #: " + cable.Num
            End If
        Next arrayLoopIterator
    Next shapeLoopIterator


    FindCables = foundCables() ' Return found cables array
End Function
...