There’s a question on the Inventor Customization newsgroup that I thought might be of general interest so I’m posting my response here. The specific question is that they have a cabinet assembly and some of the parts in the assembly represent pieces of molding. They want to get a total length of all of the molding used in the assembly. I’ve written a small sample below that illustrates one approach to this.
The first thing before writing any program is to fully understand the conditions the program can expect. You might need to impose certain requirements on the model in order for the program to function correctly. A computer program isn’t very smart and only does exactly what you’ve programmed it to do. Each variation in the conditions of the model requires more code to check for and handle each case. Here are the assumptions that this program makes about the assembly.
- The program is executed when the cabinet assembly is active.
- Any parts that represent molding will have the word “molding” in the category iProperty. This is how the program determines which parts are molding parts.
- Each molding part has a parameter called “Length” that is the actual length of the molding part.
The program demonstrates several basic things that should be interesting for many of you. First, it traverses the entire assembly looking for molding parts. It uses the iProperty portion of the API and rule number 2 above to determine which parts in the assembly are molding parts. Once it finds a molding part it uses the parameters portion of the API to get the length of the molding. It again uses iProperties to create or update a custom iProperty in the assembly with the total length of the molding (plus 30% for waste).
Not everyone needs a program that totals the length of molding parts but hopefully you can see how this sample could be easily edited to perform other similar tasks.
Something to be aware of is that this is not automatic. It’s essentially a “snapshot” of the length of molding at the time you run the program. If you edit, add, or remove parts the iProperty will not automatically update but you’ll need to manually run the program again.
If you’re new to Inventor’s VBA, here’s a post that lists the steps of how to use a VBA macro within Inventor.
' Main program that computes the total length of all molding
' parts in the active assembly.
Public Sub ComputeMoldingLength()
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asmDoc.ComponentDefinition
' Call the recursive function to traverse the assembly.
Dim MoldingLength As Double
MoldingLength = 0
Call GetMoldingLength(asmDef.Occurrences, MoldingLength)
' Get the iProperties for this document.
Dim propSets As PropertySets
Set propSets = asmDoc.PropertySets
' Get the user defined property set.
Dim userProps As PropertySet
Set userProps = propSets.Item("Inventor User Defined Properties")
' Create a string that represents the length value,
' including an extra 30% for waste.
Dim totalLength As String
totalLength = asmDoc.UnitsOfMeasure.GetStringFromValue( _
MoldingLength * 1.3, kDefaultDisplayLengthUnits)
' Update or create the iProperty containing the length.
On Error Resume Next
Dim lengthProp As Inventor.Property
Set lengthProp = userProps.Item("MoldingLength")
If Err.Number <> 0 Then
' The property doesn't exist so create it.
Set lengthProp = userProps.Add(totalLength, "MoldingLength")
Else
lengthProp.value = totalLength
End If
MsgBox "Total molding length (including 30% waste): " _
& totalLength
End Sub
' assembly structure to find and total the lengths of all
' molding parts.
Private Sub GetMoldingLength( _
ByVal Occurrences As ComponentOccurrences, _
ByRef MoldingLength As Double)
' Iterate through the occurrences in the current assembly.
Dim occ As ComponentOccurrence
For Each occ In Occurrences
' Check if this is a part or assembly.
If occ.DefinitionDocumentType = kPartDocumentObject Then
' Get the iProperties for this document.
Dim propSets As PropertySets
Set propSets = occ.Definition.Document.PropertySets
' Get the Document Summary Info property set.
Dim docSummaryInfo As PropertySet
Set docSummaryInfo = propSets.Item( _
"Inventor Document Summary Information")
' Check to see if the term "molding" is part of
' the category property.
If InStr("MOLDING", _
UCase(docSummaryInfo.Item("Category").value)) > 0 Then
' Get the user defined property set.
Dim userDefinedProps As PropertySet
Set userDefinedProps = propSets.Item( _
"Inventor User Defined Properties")
' Get the component definition of the part.
Dim partCompDef As PartComponentDefinition
Set partCompDef = occ.Definition
' Get the parameter for the length.
' It will be in centimeters.
On Error Resume Next
Dim length As Double
length = partCompDef.Parameters.Item("Length").value
If Err.Number = 0 Then
MoldingLength = MoldingLength + length
Else
MsgBox "Error getting the Length property from " & _
occ.Definition.Document.DisplayName
End If
On Error GoTo 0
End If
Else
' It's an assembly so call this sub again.
Call GetMoldingLength(occ.SubOccurrences, MoldingLength)
End If
Next
End Sub

Subscribe
A drawing with 20 different seizes metal sheets. I was wondering if I would put a BOM table with dimensions (like length X wide x thickness) rather than dimensioning each one. If is possible to get a macro code for that?
Best Regards
Wojciech
Posted by: Wojciech | September 20, 2010 at 04:15 AM