We will look briefly at raw document references in this article, and then take a bit of time stepping into the Assembly Document container hierarchy in a follow up.

For the scope of this discussion, I will limit that object hierarchy as follows:

Document -> AssemblyDocument -> AssemblyComponentDefinition -> ComponentOccurences -> ComponentOccurence

and

ComponentOccurancesEnumerator

Assembly Components

This is what cleaned up my code tremendously. I am dividing into parts so that it is (hopefully) easier to digest.

Assembly Component Occurrences are all components in an assembly, which contain Occurrences, or each component. This section will discuss using these, and the functionality associated with extracting each portion.

Assembly Component Definition

The complete component container; similar to the CAD Bill of Materials (BOM), except that the actual BOM definition data is contained separately inside this structure.

Base Object Class: AssemblyDocument

Type: Standard Object

Object Definition: AssemblyComponentDefinition

Returned By: AssemblyDocument.ComponentDefinition Function

Access to the Assembly component structure is performed through this object. We need to declare an ‘AssemblyComponentDefinition’ object, and assign it with data from a call to the Assembly Document’s ‘ComponentDefinition’ function.

Example:

' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
' Get the assembly component definition.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAsmDoc.ComponentDefinition

Enter

Now that the Assembly Component Definition is filled, it is time to do some digging.

Component Occurrences

As you might expect, these are the containers that each component document is present in.

If a part definition occurs 10 times in an assembly, there are 10 Component Occurrences, and all 10 will show up in this container. The benefit here is that skeleton and reference files are not part of the ‘Assembly Component Definition’ structure, so they don’t get in the way here.

Base Object Class: AssemblyComponentDefinition

Type: Collection Object

Object Definition: ComponentOccurrence

Referenced at: AssemblyComponentDefinition.Occurences

The Occurrences collection is the whole enchilada of Component Occurrences, which you can access directly.

Now we can declare a ‘ComponentOccurence’ object, and use it to inspect each ‘ComponentOccurrence’ that exists in our ‘AssemblyComponentDefinition’.

In this example, I used a For Each function to pull each component.

Example:

' Iterate through all of the Part Occurrences

Dim oOccurrence As ComponentOccurrence

For Each oOccurrence In oAsmCompDef.Occurrences

 

' Set Reference to Occurrence Name

Dim oOccName As String

oOccName = oOccurrence.Name

 

‘ Show each name in a dialog

MessageBox.Show(oOccName, "Document Name")

Next

This is a great way to quickly run through the Assembly and get every component. What follows is more functionality when you want to be specific about what you are getting.

Occurrence Enumeration

This object goes hand in hand with the next function. While its complete functionality is still not quite understood by me, this object acts as a container to catch a collection of Component Occurrences, and disseminate them by type into an intelligent object with appropriate functions.

Base Object Class: ComponentOccurences

Type: Collection Object

Object Definition: ComponentOccurrencesEnumerator

Referenced At: itself as dimensioned object

In the last example, we bypassed the need for this and iterated through our Assembly Component Definition’s collection of Components directly, like a book, one page at a time. However when we want the Assembly Component Definition to hand us a big list of components, we need somewhere to put them.  The Inventor API has provided this container for that purpose.

We need to define the ‘ComponentOccurrenceEnumerator’ object for the next section.

Example:

‘ Define the Component Occurrence Enumerator 
Dim oLeafOccs As ComponentOccurrencesEnumerator

All Leaf Occurrences

Leaf Occurrences are part files in an assembly, the end of any branching in the structure.

Base Object Class: AssemblyComponentDefinition

Type: Function

Function Call: Occurences.AllLeafOccurrences

Returns: ComponentOccurrence

This function returns the very same Component Occurrences as we iterated through previously, however this function only returns a collection of those components that represent the end of assembly branches, Part or ‘Leaf’ objects.

While we were able to peruse the Assembly Component Definition’s components like reading a book, in this function, the Assembly Component Definition object will dump an appendix in our lap.

Here, we will fill our Component Occurrence Enumerator by a call to the Component Definition’s ‘AllLeafOccurrences’ function. Then define another Component Occurrence to represent and investigate each Leaf Occurrence that is in our filled Enumerator.

Example :

' Create the Enumerator to catch all the leaf occurrences of the assembly. 
Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oAsmCompDef.Occurrences.AllLeafOccurrences

' Iterate through the occurrences and print the name.
Dim oOcc As ComponentOccurrence
For Each oOcc In oLeafOccs
MessageBox.Show(oOcc.Name, "Occurance Name")

All Referenced Occurrences

The Inventor API will also permit users to extract all occurrences of specific Inventor documents.

Base Object Class: AssemblyComponentDefinition

Type: Function

Function Call: AssemblyComponentDefinition.Occurences.

AllReferencedOccurrences(Document)

Returns: ComponentOccurrences

This function will return all instances of the specified document, at any level within a Component Definition.

This example takes a file name, and returns all occurrences of it that exist. It uses a call to opened documents expecting that if it exists, Inventor has it opened. Some good bounds checking could be applied to catch any error associated with an unopened file.

Example:

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

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

' Get the document to find occurrences for.
Dim sDocName as String

sDocName = “C:\designandmotion.ipt”

Dim oDoc As Document
oDoc = ThisApplication.Documents.ItemByName(sDocName)

' Get the occurrences that represent this document.
Dim oOccs As ComponentOccurrencesEnumerator
oOccs = oAsmCompDef.Occurrences.AllReferencedOccurrences(oDoc)

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

‘ Do Something here with the Occurrence

‘ --------------------------------
Next

Closing Thoughts

I used this code to automate the production of drawings, running out as many as 20 at a time, right out of an assembly drawing file. I added substantial enhancements so that each drawing contained views, parts lists, as well as automating the population of various key iProperty values as well.

Help Button

I hope this helps you get a jump start on iLogic Assembly code as much as it did for me. If you’d like more information on Inventor, iLogic, or even how we built the drawing generator, stop by Design & Motion.

 

REFERENCES:

Mod The Machine article “Accessing Assembly Components” from 2009

The Autodesk Inventor 2014 API chart

Mod the Machine Article “Understanding File References” from 2008