I have taken the task to traverse through the assembly.
It works fine, but, when I traverse the assembly structure I cannot maintain the LOD that I set throughout the structure as it changes when the code reaches the next level, see attached code and picture.
The assembly that I tested on consists of:
- Top level assembly
- Two sub assemblies where one of the sub assemblies contains multiple level of subassemblies
The code does the following: (The numbering relates to the pictures in the attached file)
- Looks at the top level assembly LOD
- See if the first subassembly contains this LOD; if yes set it as active, if not create it and set it as active
- See if the next subassembly contains this LOD; if yes set it as active, if not create it and set it as active
- As the next subassembly contains multiple subassemblies the code traverses through the assembly structure... But here the code failes
- The code goes to the next level again but with the same result...
How can I maintain the LOD of all the assemblies and subassemblies?
Or should I try another approach?
Any input would be helpful.
CODE start
PrivateSub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
' Get the active assembly.
Dim oAsmDoc AsAssemblyDocument
oAsmDoc = _invApp.ActiveDocument 'ThisApplication.ActiveDocument
'Debug.Print(oAsmDoc.DisplayName)
Dim oAsmDocDef AsAssemblyComponentDefinition
oAsmDocDef = oAsmDoc.ComponentDefinition
Dim ActiveLODRepName AsString
Try
ActiveLODRepName = oAsmDocDef.RepresentationsManager.ActiveLevelOfDetailRepresentation.Name
Catch ex1 AsException
ActiveLODRepName = "Test"
EndTry
' Call the function that does the recursion.
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, ActiveLODRepName)
EndSub
PrivateSub TraverseAssembly(Occurrences AsComponentOccurrences, _
Level AsInteger, ActiveLODRepName AsString)
' Iterate through all of the occurrence in this collection. This
' represents the occurrences at the top level of an assembly.
Dim oOcc AsComponentOccurrence
Dim oCompDef AsAssemblyComponentDefinition
ForEach oOcc In Occurrences
' Check to see if this occurrence represents a subassembly
' and recursively call this function to traverse through it.
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
If oOcc.Suppressed ThenContinue For
Try
'Activate a writeable LOD (master LOD is not writeable)
oOcc.SetLevelOfDetailRepresentation(ActiveLODRepName)
Catch
'Assume error means this LOD does not exist, so create it
oCompDef = oOcc.Definition
oCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add(ActiveLODRepName)
oOcc.SetLevelOfDetailRepresentation(ActiveLODRepName)
EndTry
Call TraverseAssembly(oOcc.SubOccurrences, Level + 1, ActiveLODRepName)
EndIf
Next
EndSub
CODE end