Identifying a Part Number Within VBA

Identifying a Part Number Within VBA

eric.frissell26WKQ
Advocate Advocate
1,663 Views
4 Replies
Message 1 of 5

Identifying a Part Number Within VBA

eric.frissell26WKQ
Advocate
Advocate

Hi, I've been struggling with some code recently that traverses through my design tree which I intend to have the script identify certain part numbers and run a private sub on that part number.  I can get it to traverse the tree just fine and run private subs based on whether it is a part or an assembly however, prior to where it's returning part or assembly and running a sub based on that criteria, I have it set to look for specific part numbers and run a private sub based on whether or not that part number matches a few pre-determined ones.  The code is as follows however it seems like it's having issues with the oOcc.name not matching the part number variable (trough) which is defined as a string.

 

If you were going to have VBA run a private sub based on a part number how would you do it?

 

 ' Iterate through assembly
    Dim oOcc As ComponentOccurrence
    For Each oOcc In Occurrences
            
        ' Print the name of the current occurrence.
        'Debug.Print Space(Level * 3) & oOcc.Name
        
        'Call appropriate part or assembly sub
        If oOcc.DefinitionDocumentType = kPartDocumentObject Then
            
            'compare part number strings
            If Trough = oOcc.Name Then   -->(Trough is previously defined as a global string outside of the public sub with a few other variables that get passed between various private subs)
                Call TroughLoop(oOcc)
            Else
                        
            Call PartParameter(oOcc)
            'End If
        
        ElseIf oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
        
            'compare assembly part number strings
        
            Call AssemblyParameter(oOcc)
        
        End If

 

 

0 Likes
Accepted solutions (1)
1,664 Views
4 Replies
Replies (4)
Message 2 of 5

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

If you only modify Properties and Parameters, you don't need to get every occurrence of a part or assembly. If a part is placed 500 times and your code modifys a parameter value, the code will 500 times modifiy this value. It would be better to use the referenced documents.

I don't know what's the value of Trough, so the alternative code may fail in this point. It's in general not a good idea to use the name of an occurrence or the display name of a part or assembly. They are just strings which can be changed by user.

Private Sub TraverseAssOccs()

Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument

Dim Occurrences As ComponentOccurrences
Set Occurrences = oAssDoc.ComponentDefinition.Occurrences

Dim Trough As String
Trough = ""

Dim aPartNumbers(5) As String
    aPartNumbers(0) = "12345"
    aPartNumbers(1) = "23456"
    aPartNumbers(2) = "34567"
    aPartNumbers(3) = "45678"
    aPartNumbers(4) = "56789"
    
    Dim PartNumber As Variant
    Dim oDoc As Document
    
' Iterate through assembly
    Dim oOcc As ComponentOccurrence
    For Each oOcc In Occurrences
        For Each PartNumber In aPartNumbers
            If PartNumber = oOcc.Definition.Document.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Part Number").Value Then
                'Code to process if part number matches
            Else
                'Code to process if not part number matches
            End If
        Next
        
        ' Print the name of the current occurrence.
        'Debug.Print Space(Level * 3) & oOcc.Name
        'Call appropriate part or assembly sub
        If oOcc.DefinitionDocumentType = kPartDocumentObject Then
            'compare part number strings
            If Trough = oOcc.Name Then   '-->(Trough is previously defined as a global string outside of the public sub with a few other variables that get passed between various private subs)
                Call TroughLoop(oOcc)
            Else
                Call PartParameter(oOcc)
            End If
        ElseIf oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
            'compare assembly part number strings
            Call AssemblyParameter(oOcc)
        End If
    Next
End Sub

 

Alternativ using referenced documents

Private Sub TraverseAssDocs()
    Dim Trough As String
    Trough = ""
    
    Dim aPartNumbers(5) As String
    aPartNumbers(0) = "12345"
    aPartNumbers(1) = "23456"
    aPartNumbers(2) = "34567"
    aPartNumbers(3) = "45678"
    aPartNumbers(4) = "56789"
    
    Dim PartNumber As Variant
    Dim oDoc As Document
    For Each oDoc In ThisApplication.ActiveDocument.AllReferencedDocuments
        For Each PartNumber In aPartNumbers
            If PartNumber = oDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Part Number").Value Then
                'Code to process if part number matches
            Else
                'Code to process if not part number matches
            End If
        Next
        If oDoc.DocumentType = kPartDocumentObject Then
            'compare part number strings
            If Trough = oDoc.DisplayName Then   '-->(Trough is previously defined as a global string outside of the public sub with a few other variables that get passed between various private subs)
                Call TroughLoop(oDoc)
            Else
                Call PartParameter(oDoc)
            End If
        ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
            'compare assembly part number strings
            Call AssemblyParameter(oDoc)
        End If
    
    
    Next
End Sub

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 5

eric.frissell26WKQ
Advocate
Advocate

I just learned so much from this one single post, thanks a ton!

 

I like the way it's broken out and I think I'll re-write the parameter assignment to follow this model.  In the previous example I was having trouble defining the part number as a string and having trouble matching it with occurrence.name even going so far as to put the occurrence number, trough:1, as the string but that failed too.  I assume you have better luck matching a part number to DisplayName?

0 Likes
Message 4 of 5

Ralf_Krieg
Advisor
Advisor

Hello

 

As I understand, you want to compare the value of the iProperty "Part Number" with the name of an occurrence?

The value of iProperty "Part Number" is always a string. No need to convert it.

The occurrence get's automatic numbered with ":1", ":2" and so on. That need's to be removed for compare. One possible way is to find the position of ":" in the name an get all chars left of it.

In case the occurrence is renamed in the model browser and the ":" is removed, the complete occurence name is compared to the part number property.

 

Private Sub Compare()

Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument

Dim oOcc As ComponentOccurrence
Dim sPartNumber As String
Dim sOccName As String
Dim iOccPos As Integer
For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
    sPartNumber = oOcc.Definition.Document.PropertySets.Item("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").Item("Part Number").Value
    If InStrRev(oOcc.Name, ":") = 0 Then
        sOccName = oOcc.Name
    Else
        sOccName = Left(oOcc.Name, InStrRev(oOcc.Name, ":") - 1)
    End If
    If sPartNumber = "" Then
        MsgBox "iProperty Part Number is empty!"
        Exit Sub
    End If
    If sOccName = sPartNumber Then
        MsgBox "Part Number equal Occurrence Name"
    End If
Next
End Sub

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 5

eric.frissell26WKQ
Advocate
Advocate

Thanks for the reply.  The occurrence value for the part would always be occurrence 1 so preferably something like document name which should be pretty easy to find.  Previously I was using occurrence name with trough equal to a combination of variables with :1 added to the end and defined as a string.  When matching that to the oOcc.Name it would never seem to run the private sub.

 

'compare part number strings
            'If Trough = oOcc.Name Then
            '    Call CompactorTroughLoop(oOcc)
            'Else

 

I'm thinking that the previous script you posted here would allow for a lot more flexibility when it comes to items you want to call.

0 Likes