Modify Assembly Components with Automation

August 12, 2021 Randall Mabery

Quite often we find ourselves with the need to modify all components inside an assembly. 

This happens to me when building configurators.  I may need to push the Project Number or Sales Order Number iProperty to all parts.  I may need to export all Sheet Metal Flat Patterns as a DXF file.  I may need to change the Material based upon design requirements.

The route we take to accomplish this depends on what components we need to modify or work with.

Parts Only

If we only need to only modify the lowest level parts, meaning no sub-assemblies, then we can look at the ComponentOccurrencesEnumerator.  This is a collection of all lowest level parts in the current assembly.  Think of setting the Selection Filter to Select Part Priority in an assembly, you can only select parts regardless if they are at the top level or in a lower level sub-assembly.

The AllLeafOccurrences property of the ComponentOccurrences object returns the ComponentOccurrencesEnumerator.  With this, we can loop thru all occurrences in that collection with a For / Next loop.

Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument

Dim oAssyCompDef As AssemblyComponentDefinition
oAssyCompDef = oAssyDoc.ComponentDefinition

Dim oOccurrences As ComponentOccurrences
oOccurrences = oAssyCompDef.Occurrences

Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oOccurrences.AllLeafOccurrences

Dim oLeafOcc As ComponentOccurrence

For Each oLeafOcc in oLeafOccs

 ' DO YOUR WORK HERE

Next

Part And Sub-Assemblies

If we need to modify parts as well as sub-assemblies, then we need to take a different approach since the LeafOccurrences approach above would skip sub-assemblies.  The approach we need to take here is to traverse the assembly.  This means we will look at each occurrence in the assembly and do something to it i.e. write an iProperty.  If that occurrence is a sub-assembly then we'll look at all of its children and do the same work to it.  We then move on to the next occurrence.

Sub Main
    ' Get the active assembly. 
	Dim oAssyDoc As AssemblyDocument
	oAssyDoc = ThisApplication.ActiveDocument 
	
    ' Call the function
    Call TraverseAssembly(oAssyDoc.ComponentDefinition.Occurrences)
End Sub 
 
Sub TraverseAssembly(oOccs As ComponentOccurrences)
    ' Iterate through all of the occurrence in this collection
	Dim oOcc As ComponentOccurrence
	
	For Each oOcc In oOccs 
		
		' DO YOUR WORK HERE

		' Recursively call sub if needed
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
			Call TraverseAssembly(oOcc.SubOccurrences)
		End If
	Next
End Sub

In both approaches above, I've marked the area where you're "doing the work".  You may need to get additional information about the occurrence, depending on what you're trying to accomplish.  For example, if I were wanting to modify the custom iProperty called Sales Order Number in all the sub-assemblies and parts in the current assembly (or create and modify if it doesn't already exist) then I'd replace the ' DO YOUR WORK HERE section above with something like this:

oCustomPropertySet = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties")

Dim iProp As String = "Sales Order Number"
Try
	oiPropToSearchFor = oCustomPropertySet.Item(iProp)
Catch ex As Exception
	oCustomPropertySet.Add("", iProp)
	oiPropToSearchFor = oCustomPropertySet.Item(iProp)
End Try

oiPropToSearchFor.Value = sSalesOrderNumber

 

About the Author

Randall Mabery

I love the technology, thoroughly enjoy helping people find solutions and believe in the software. I train individuals in the use of Autodesk design software such as Inventor and AutoCAD. The most satisfying part of my job is working on automation solutions for clients, assisting them in streamlining their design process and automating tasks.

Follow on Linkedin More Content by Randall Mabery
Previous Article
Setup a 4th Axis by Modifying Fusion 360 and Inventor CAM Post Processors
Setup a 4th Axis by Modifying Fusion 360 and Inventor CAM Post Processors

How to customize your CAM post processor to include a 4th Axis.

Next Article
Automating Port-Cavity Programming with FeatureCAM
Automating Port-Cavity Programming with FeatureCAM

With FeatureCAM’S Port-Cavity feature add-in CNC programmers can store the geometric data associated with t...