Have you written some code to do some type of operation on a part, (i.e. create/update a parameter, create/update an iProperty, etc.), and now would like to perform that update on every part that’s used within a particular assembly? I have good news, if you’ve already figured out how to do the work for a single part, doing it for all of the parts in an assembly is a simple step away. Let’s look at updating a single part first and the progression of that program to updating a complete assembly.
Below is a typical VBA macro that creates or updates a parameter in a part.
Public Sub UpdateParameter() ' Get the active part. Dim part As PartDocument Set part = ThisApplication.ActiveDocument ' Get the user parameters collection. Dim userParams As UserParameters Set userParams = _ part.ComponentDefinition.Parameters.UserParameters ' Check to see if the parameter already exists. Dim param As Parameter On Error Resume Next Set param = userParams.Item("SomeValue") On Error GoTo 0 If param Is Nothing Then ' The parameter doesn't exist so add it. Set param = userParams.AddByExpression("SomeValue", _ "2 inch", kDefaultDisplayLengthUnits) End If End Sub
Here’s a variation of the macro above converted into a Sub that gets the part document through an argument. The additional code for the argument is highlighted below and the first portion of the code where it got the active document in the sample above has been removed since the document is now passed to it.
Public Sub UpdateParameter(part As PartDocument) ' Get the user parameters collection. Dim userParams As UserParameters Set userParams = _ part.ComponentDefinition.Parameters.UserParameters ' Check to see if the parameter already exists. Dim param As Parameter On Error Resume Next Set param = userParams.Item("SomeValue") On Error GoTo 0 If param Is Nothing Then ' The parameter doesn't exist so add it. Set param = userParams.AddByExpression("SomeValue", _ "2 inch", kDefaultDisplayLengthUnits) End If End Sub
Here’s the macro that calls the sub above. It gets the active part document and calls the Sub, passing it the part document.
Public Sub UpdatePart() ' Get the active part. Dim partDoc As PartDocument Set partDoc = ThisApplication.ActiveDocument ' Call the function to update the part. Call AddParameter(partDoc) End Sub
Now we want to perform this same update to every part in the active assembly. The code to do that is below. It gets the active document, which in this case is an assembly and then uses the AllReferencedDocuments property of the assembly document to get the documents referenced by the assembly. It then looks for the parts, to skip any referenced subassemblies, and calls the AddParameter sub just the same as before, passing in the part document. This is the same PartDocument object you would get if you opened the part by itself and used the Application.ActiveDocument property. The only difference is how you got the PartDocument object which in this case is through the assembly.
Public Sub UpdateAssembly() Dim asmDoc As AssemblyDocument Set asmDoc = ThisApplication.ActiveDocument ' Iterate through all of the referenced documents at ' all levels of the assembly. Dim doc As Document For Each doc In asmDoc.AllReferencedDocuments ' Check for part documents. If doc.DocumentType = kPartDocumentObject Then ' Call the function to update the part. Call AddParameter(doc) End If Next End Sub
The function getting the part document can do anything to the part because it is a full PartDocument object. It doesn’t even know how the reference was obtained. There are no limitations.
-Brian

Leave a Reply to ioannis_gkiokas@hotmail.comCancel reply