Inventor iLogic Notes for Assembly Drawings
It has been some time since I was regularly programming. My background was C++ and I was moving to C# when, well, I started writing. It appears as though there is only enough time to either research engineering software or research software API.
I have been on a project that demanded a lot of drawings, and after I got through revising about 20 drawings, I decided that digging into iLogic would be a sound investment. What follows is a collection of my notes and references relating to the iLogic Assembly document functions that I used to automate drawing creation.
Introduction
It is very important to understand the Autodesk Inventor® assembly document structure and hierarchy. Each component in an assembly is a document. Each document is simply a reference to a file that is, or will be upon saving, stored on a file server or local drive.
However, documents can be interpreted in numerous ways. In our case, documents represent parts, assemblies, spreadsheets, drawings, etc. Inventor has class objects that have intelligence built in to deal with how an assembly document, for example, will behave.
We will look briefly at raw document references, and then take a bit of time stepping into the assembly document container hierarchy.
For the scope of this discussion, I will limit that object hierarchy as follows:
Document -> AssemblyDocument -> AssemblyComponentDefinition -> ComponentOccurances -> ComponentOccurance
and
ComponentOccurancesEnumerator
Just Give Me Everything
First, we can get all file references by calling for ‘AllReferencedDocuments’. This function is basically a document dump.
Base Object Class: Document
Type: Function Call
Object Function: AllReferencedDocuments
Returns: Document object
This function does a jam-up job of returning all FILE instances within any document object. Only one instance per file is returned, regardless of how many times a part is used in an assembly.
This function is not reserved to assembly files and will work on any document class-based object. This makes it a great, universal front-end build for code that needs to work in a broad range of Inventor documents.
Caution should be used to provide significant bounds checking as to what type of document is being returned, as AllReferencedDocuments will return anything at all—spreadsheets, part files, you name it.
Example (working with only the part files in a document):
‘ Get the active assembly document.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
‘ Iterate through all of the documents referenced by the assembly.
Dim oDoc As Document
For Each oDoc In oAsmDoc.AllReferencedDocuments
' Verify that the document is a part.
If oDoc.DocumentType = kPartDocumentObject Then
Dim oPartDoc As PartDocument
oPartDoc = oDoc
‘------------------
‘Do something with the part document here
‘-------------------
End If
Next
…
This is great when you want everything, but I need to see some structure, and having some refinement would be nice.
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 the use of these, and the functionality associated with extracting each portion.
Assembly Component Definition
The complete component container is similar to the CAD bills of material (BOM), except that the actual BOM definition data is contained separately inside this structure.
Base Object Class: AssemblyDocument
Type: Standard Object
Object Definition: AssemblyComponentDefinition
Retrurned By: AssemblyDocument.ComponentDefinition Function
Access to the assembly component structure is performed through this object. We need to define the ‘AssemblyComponentDefinition’ object and load 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
...
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 in which each component document is present.
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 define 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 to be used 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: ComponentOccurances
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 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 we define another component occurrence to represent and investigate each leaf occurrences 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 various key iProperties values.
I hope this helps you get a jump start on iLogic assembly code 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 (http://designandmotion.net).
References:
Special thanks to the Mod The Machine team and their article “Accessing Assembly Components” from 2009:-
http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html
The Autodesk Inventor 2014 API chart:
http://images.autodesk.com/adsk/files/Inventor2014Model.pdf
Mod the Machine Article “Understanding File References” from 2008:
http://modthemachine.typepad.com/my_weblog/2008/11/understanding-file-references.html