Yeah, I agree with your comments about untidy work...bad habit I'm working on breaking. Took it to heart though and tried working this through in what I think is the right way by using ModTheMachine's example you posted, and then breaking that down into a few private subs. This one runs but it appears to be missing a lot of the components and I really don't understand why that's the case yet. The intention is that I will be iterating through all parts and assemblies to find a user parameter called 'FTF' and change that parameter. Currently I have one macro that is somewhat successful but it uses the 'AllReferencedDocuments' and isn't getting the assemblies (I use an if/else if for if document type is kpart or kassembly) and it's also not set up the way I'd like it to be, hence this updated one.
Currently this macro is set up to print the activedocument name, FTF and length, then go into the traverseassembly. As it's going through traverse assembly it prints out the name, determines whether it's a part or assembly document, goes to that sub, then prints FTF changed. As I said it's missing a lot of components and I have a object (class?) issue. oOcc defined as ComponentOccurrence and is passed from private sub TraverseAssembly to private sub AssemblyParameter or PartParameter. Because it's passed as a ComponentOccurrence I'm not sure how to use that to get to the ComponentDefinition which is needed to get Parameters and UserParameters. I don't think I can redefine ActiveDocument in the private sub as it would be looking at the open assembly, rather than the part or subassembly?
I thought the private sub might clean this up a bit if I could figure out how to define the part I'm working on to pass it
This is the code I'd like to run because I think it's simpler but it's missing components.
Public length As Double
Public Sub CompactorBuilder()
' Get compactor assembly.
Dim CompAsm As AssemblyDocument
Set CompAsm = ThisApplication.ActiveDocument
' Get length
'length = InputBox("Enter the length for compactor. I.e. 35.88", LengAssign)
length = 24.55
Debug.Print CompAsm.FullDocumentName
Debug.Print "FTF Compactor length is "
' Call recursion function
Call TraverseAssembly(CompAsm.ComponentDefinition.Occurrences, 1)
End Sub
Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)
' Iterate through assembly
Dim oOcc As ComponentOccurrence
For Each oOcc In Occurrences
' Print the name of the current occurrence.
Debug.Print Space(Level * 3) & oOcc.Name
'Verify part or assembly document
'Call appropriate part or assembly sub
If oOcc.DefinitionDocumentType = kPartDocumentObject Then
Call PartParameter(oOcc)
ElseIf oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
Call AssemblyParameter(oOcc)
End If
' Get/verify material
' Check to see if this occurrence represents a subassembly
' and recursively call this function to traverse through it.
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
End If
Next
End Sub
Private Sub AssemblyParameter(oOcc As ComponentOccurrence)
Debug.Print oOcc.Name & "FTF changed"
End Sub
Private Sub PartParameter(oOcc As ComponentOccurrence)
Debug.Print oOcc.Name & "FTF changed"
End Sub