Get iProperties of Component Occurrences

Get iProperties of Component Occurrences

Kyle.Arnold
Enthusiast Enthusiast
11,084 Views
17 Replies
Message 1 of 18

Get iProperties of Component Occurrences

Kyle.Arnold
Enthusiast
Enthusiast

Hi Folks.

 

I'm working on a program to get into the iProperties of each Component within an Assembly.

 

The attached code can run through each occurrence in an assembly and return the "Name" displayed in the browser window, printed out as a list in the Immediate window of the VBA Editor.

 

I would like to return the value of the "Category" iProperty of each occurrence.  I would assume that I need to access the part associated with each occurrence in order to get to the iProperties of that part. 

 

After fighting it most of the day yesterday, I'm fed up and need some help.

 

How do I access the iProperties of an OCCURRENCE? I can get the iProperties from a PART, ASSEMBLY, or DRAWING easily, but OCCURRENCES are killing me... Should be a cakewalk for some of you more experienced people.

 

The attached code was inspired by/imitates/borrowed from a Brian Ekins post on Mod the Machine. 

 

Sub Test()

' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument

' Get the definition of the assembly.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = oAsmDoc.ComponentDefinition

' Get the occurrences that represent this document.
Dim oOccs As ComponentOccurrencesEnumerator
Set oOccs = oAsmCompDef.Occurrences.AllLeafOccurrences

' Iterate through the Occurrences
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs

    Debug.Print oOcc.Name
    
Next

End Sub

 

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Accepted solutions (1)
11,085 Views
17 Replies
Replies (17)
Message 2 of 18

MechMachineMan
Advisor
Advisor

You jsut need to access the Component Definition of the Occurrence, and from there you can access the Document from where you grab the iProperties.

 

iProps are not stored on occurrences, but only within the part, so pulling an iProp from an occurrence doesn't make sense; instead you need to pull it from the document the occurrence references.

 

Also, depending on what you are trying to accomplish, it may just be easier to iterate through all of the referenced documents for the active file, rather than going through occurrences. You still will end up accessing the same files in the end, so the only real benefit to going through occurrences is for order/suppression.

 

See resource below for API regarding component definition.

"C:\Program Files\Autodesk\Inventor 2016\Local Help\admapi_20_0.chm"

 

See below for "proper" calls to properties:

http://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html

 

ie; 

 

'in vb.net, not vba, so use as a rule/external rule vs as a macro

Sub Main()

 

Dim oDoc as Document

 

For Each oDoc in ThisApplication.ActiveDocument.AllReferencedDocuments

      oStr = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value & " <<<<<< " & oStr

Next

 

MsgBox(oStr)

 

End Sub


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 18

Kyle.Arnold
Enthusiast
Enthusiast

AllReferencedDocuments won't work for me in the way you show above, becuase I need a list of the Category iProperty of each Occurrence or Leaf.  I need the list to tell me what each leaf's Category is regardless of how many time that particular refereced part shows up in an assembly. For example:

 

Assembly A's browser tree looks like this-

 

Part 1:1

Part 1:2

Part 1:3

Part 2:1

Part 3:1

Part 1:4

 

I need the program to return:

 

Category X

Category X

Category X

Category Y

Category Z

Category X

 

Do you know if there is a way to get this using AllLeafOccurrences or something similar?

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Message 4 of 18

Kyle.Arnold
Enthusiast
Enthusiast
Sub Main()

Dim oDoc As Document

For Each oDoc In ThisApplication.ActiveDocument.AllReferencedDocuments

      oStr = oDoc.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value & vbNewLine & oStr
Next

Debug.Print oStr

End Sub

A slight modification of your proposed code gives me what I'm looking for EXCEPT it's giving me the category of the referenced documents only. On an assembly with 9 parts (4 unique) it only returns the category of each unique part, so 4 lines returned rather than the desired 9.  

 

I don't know how to adapt it to give me the category of every occurrence in the assembly.

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Message 5 of 18

MechMachineMan
Advisor
Advisor
http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 6 of 18

Kyle.Arnold
Enthusiast
Enthusiast

Yep, that's the page I got the idea and the first draft of code from. In the last paragraph of the "Assembly Structure" section, the author is talking about replacing the "Debug.Print" line and says:

 

"This line is executed for every occurrence in the assembly.  You can replace this with code that performs whatever functionality you need to accomplish.  For example, if you need to create a custom BOM your code might access the document that’s referenced by this occurrence and extract some iProperty values and then print this out as part of a structured BOM." 

 

The underlined section is what I can't figure out.  How do I extract iProperties of the Parts referenced by the Occurrences?

 

All should have to do is replace the "Debug.Print" line of that code with the code neccessary to extract the iProperties.  I don't know enough about VBA and using the Inventor API to figure out the answer on my own.

 

There is definitely something that I'm missing.

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Message 7 of 18

MechMachineMan
Advisor
Advisor

This is close, but still not quite what you need.

 

Why exactly do you need each browser node to be printed/accessed though? They change quite often so it seems to be a weird thing to track.

 

SyntaxEditor Code Snippet

 

Class PVars
    Dim oPStr As String

Sub Main()
    Dim PVar As New PVars
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 

    TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
    MsgBox(PVar.oPStr)
End Sub 

Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) 
       Dim PVar As New PVars
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In Occurrences
        PVar.oPStr = PVar.oPStr & " - " & oOcc.Definition.Document.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value
        MsgBox(PVar.oPStr)
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
        End If 
    Next
End Sub

End Class

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 8 of 18

MechMachineMan
Advisor
Advisor

Here are 2 working methods you could possibly adapt:

Note: Both are written as external rules, rather than macros.

 

Outputs to msgbox:

 

Sub Main()
    Dim oStr As String
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 

    oStr = TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oStr)
    MsgBox(oStr)
End Sub 

Function TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, oStr As String) 

   	Dim oOcc As ComponentOccurrence 
   	For Each oOcc In Occurrences
        oStr = oStr & " - " & oOcc.Definition.Document.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value
		'oStr = oStr & " - " & oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            oStr = TraverseAssembly(oOcc.SubOccurrences, Level + 1, oStr) 
        End If
    Next
	Return oStr
End Function

Outputs to text file:

 

SyntaxEditor Code Snippet

Public Class PVars
    Shared oTextSave As String = "C:\Windows\Temp\iLogicBuffer.txt"

Sub Main()
    Dim PVar As New PVars
    oWrite = System.IO.File.CreateText(PVar.oTextSave)
    oWrite.WriteLine("CATEGORY")
    oWrite.Close()
    
    Dim oStr As String
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 

    TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
    
    Process.Start ("Notepad.exe",PVar.oTextSave)
End Sub 

Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer) 
    Dim PVar As New PVars
       Dim oOcc As ComponentOccurrence 
       For Each oOcc In Occurrences
        oWrite = System.IO.File.AppendText(PVar.oTextSave)
        'oWrite.WriteLine(oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value)
        oWrite.WriteLine(oOcc.Definition.Document.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value)
        oWrite.Flush()
        oWrite.Close()

        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
        End If
    Next
End Sub

End Class

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 9 of 18

Kyle.Arnold
Enthusiast
Enthusiast

Ok, I'm back after a month-long hiatus...

 

So, trying those two options, I can get it to work exactly how I want except that it works in an assembly file. Which, judging by the first few lines of code, this program was intended to do.

 

However, I'm trying to do exactly what the first of the two above programs does, except I want to do it from a drawing file.

 

Assuming that I'm only worried about doing this for drawings of assembly files, how do I "step into" the Assembly files represented in each view?

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Message 10 of 18

MechMachineMan
Advisor
Advisor

If you want it only for the active sheet when the rule is ran, you need to access the drawing view's referenced document.

 

ie;

 

ThisApplication.ActiveDocument.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 11 of 18

MechMachineMan
Advisor
Advisor
Accepted solution

So full code to check one sheet with an assembly on it in the active document would be:

 

Sub Main()
    Dim oStr As String
Dim oDrawingDoc As DrawingDocument
Try oDwgDoc = ThisApplication.ActiveDocument
Catch
MsgBox("For use in drawing documents only!")
Exit Sub
End Try
Dim oAsmDoc As AssemblyDocument
Try
oAsmDoc = oDwgDoc.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
Catch
MsgBox("This rule only works on drawing sheets with an assembly as the first view!")
Exit Sub
End Try
oStr = TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oStr) MsgBox(oStr) End Sub Function TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, oStr As String) Dim oOcc As ComponentOccurrence For Each oOcc In Occurrences oStr = oStr & vblf & oOcc.Definition.Document.DisplayName & " - " & oOcc.Definition.Document.PropertySets.Item("Inventor Document Summary Information").Item("Category").Value 'oStr = oStr & " - " & oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Stock Number").Value If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then oStr = TraverseAssembly(oOcc.SubOccurrences, Level + 1, oStr) End If Next Return oStr End Function

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 12 of 18

Kyle.Arnold
Enthusiast
Enthusiast

Fantastic! This is exactly what I was trying to accomplish!!!  Made my day!

Thanks!

Kyle Arnold
Inventor Certified Professional

"Try not. Do or do not, there is no try." -Yoda
0 Likes
Message 13 of 18

Anonymous
Not applicable

Hi, can you please explain where exactly do I have to get in and type the definition code please?

Thanks,

Gabriel

0 Likes
Message 14 of 18

dschulteHR4D5
Advocate
Advocate
Hello, i am using a modified version of this code, to display the materials i have set in an assembly, basically a quick reference, i use material library to automate drawings.. So this is the code i am using, which works perfectly, but a couple questions...

Sub main
'Make sure ASM document is active'
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If

Dim oADoc As AssemblyDocument = ThisDoc.Document


oStr = TraverseAssembly(oADoc.ComponentDefinition.Occurrences, 1, oStr)
MsgBox(oStr)
End Sub

Function TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, oStr As String)

Dim oOcc As ComponentOccurrence
For Each oOcc In Occurrences
oStr = oStr & vbCrLf & oOcc.Definition.Document.DisplayName & " = " & oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Material").Value
'oStr = oStr & " - " & oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
oStr = TraverseAssembly(oOcc.SubOccurrences, Level + 1, oStr)
End If
Next
Return oStr
End Function



1) when running this rule, the MsgBox title comes up as random characters and letters, is there any way to change this to say something like, IPT Material List?

2) The List shows the file extension, like
Part1.ipt =
Part2.ipt =
Is there a different command to pull the displayname without the .ipt?

thanks for any help!!
0 Likes
Message 15 of 18

dudde.giridhar
Enthusiast
Enthusiast

Hello,

In this reply,

you have given the method to get the properties from referenced document but not from the occurrence.

 

There might be new cutouts made in assembly level, so both are not same,

Please let us know if there is any chance to get the updated iproperties from component occurrence of assembly doc.

 

Thanks in advance

0 Likes
Message 16 of 18

nmunro
Collaborator
Collaborator

If you are on Inventor 2022 or later, instance properties enable you to assign unique property values to each assembly component occurrence. Search for Print instance properties  in the API help to find an example VBA macro that should get you started.

        


https://c3mcad.com

0 Likes
Message 17 of 18

scott_ainsworth
Enthusiast
Enthusiast

I'm working in Inventor 2024 and need to do something similar.  My attempts to access property sets via: 

oCustomProps = oOccurrence.Definition.Document.PropertySets.Item("Custom Properties")

 results in an error message: 

"Error on line 32 in rule: AW Next Assy and Qty, in document: 40066295-A01.iam

Class not registered"

 

When I am typing this into the ilogic editor, I never get a pick for anything other than ToString after "Document."  It seems like something is amiss.  

scott_ainsworth_0-1723060022565.png

 

Any help would be welcome.

 

0 Likes
Message 18 of 18

MechMachineMan
Advisor
Advisor
Have you actually declared and assigned 'oOccurrence' anywhere in your
document?

Have you declared the 'oCustomProp' as well?

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes