read item in udp of a part from assy using vba

read item in udp of a part from assy using vba

Anonymous
Not applicable
746 Views
7 Replies
Message 1 of 8

read item in udp of a part from assy using vba

Anonymous
Not applicable

im looking for an item "customer code" in the custom prop of a part

normally, i have 10+ parts in an assy.

 can vba access the udp of a part from assy?

and show which part doesnt have that item?

or vise versa or both? Smiley Frustrated

 

 

 

0 Likes
Accepted solutions (1)
747 Views
7 Replies
Replies (7)
Message 2 of 8

ekinsb
Alumni
Alumni

What you want to do is certainly possible using the API but to be able to give you as good of a sample as possible some more information would be useful.

 

When looking for this particular property in parts, do you want to look at all levels of the assembly or only the top-level. Do you want to look for this only in parts or also sub assemblies?


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 8

Anonymous
Not applicable
i only need the top level.
ex.
1.assy
1.1 part<--- i need this
1.2 sub assy <---and this
1.2.1 part

thanks in advance .
0 Likes
Message 4 of 8

ekinsb
Alumni
Alumni
Accepted solution

Here's one way to do it.

 

Public Sub GetiPropertiesOfTopLevel()
    ' Get the active assembly.
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    ' Iterate through the files referenced by the assembly.
    ' These are only direct references so parts and assemblies
    ' in subassemblies will be ignored.
    Dim doc As Document
    For Each doc In asmDoc.ReferencedFiles
        ' Get the "Customer Code" iProperty
        Dim customPropSet As PropertySet
        Set customPropSet = doc.PropertySets.item( _
                            "Inventor User Defined Properties")
        
        Dim prop As Inventor.Property
        On Error Resume Next
        Set prop = customPropSet.item("Customer Code")
        If Err.Number = 0 Then
            Debug.Print "The file """ & doc.FullFileName & """ has the value: " & prop.Value
        Else
            Debug.Print "The file """ & doc.FullFileName & _
                                          """ does not have the ""Customer Code"" iProperty"
        End If
        On Error GoTo 0
    Next
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 8

Anonymous
Not applicable

thank you so much ...works perfectlySmiley Happy

0 Likes
Message 6 of 8

Anonymous
Not applicable

can i print the whole output to a text file or messagebox?

0 Likes
Message 7 of 8

ekinsb
Alumni
Alumni

Here's a modified version of the previous program that writes the results to a file. I'm formatting with commas and using a .csv extension so it will open in Excel but you can easily modify it to output it any way you want.

Public Sub GetiPropertiesOfTopLevel()
    ' Get the active assembly.
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    ' Open the file to write the results.
    Dim filename As String
    filename = "C:\Temp\PropertyResults.csv"
    Open filename For Output As #1
    
    Print #1, "Filename,Has Customer Code,Value"
    
    ' Iterate through the files referenced by the assembly.
    ' These are only direct references so parts and assemblies
    ' in subassemblies will be ignored.
    Dim doc As Document
    For Each doc In asmDoc.ReferencedFiles
        ' Get the "Customer Code" iProperty
        Dim customPropSet As PropertySet
        Set customPropSet = doc.PropertySets.item( _
                            "Inventor User Defined Properties")
        
        Dim prop As Inventor.Property
        On Error Resume Next
        Set prop = customPropSet.item("Customer Code")
        If Err.Number = 0 Then
            Print #1, doc.FullFileName & ",True," & prop.Value
        Else
            Print #1, doc.FullFileName & ",False,"
        End If
        On Error GoTo 0
    Next
    
    Close #1
    MsgBox "Finished processing." & vbCrLf & _
           "Results written to """ & filename & """"
End Sub

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 8 of 8

Anonymous
Not applicable

this is so perfect !!!

 

thank you so much...

 

ive change the message to give user option to choose to open the csv file

but i get error when it reaches to the point where it about to open the file.

i think my code for opening a csv is wrong.

 

can you help me pls Smiley Sad

 

ive been googling and cant find some example...

0 Likes