- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I found this code by E_Jeph and altered it to traverse through an assembly, ignore parts that are reference, phantom or material 'copper' and add up the cost of all to create a total. It only does this for the top assembly despite the recursive to traverse through all sub assemblies. I have tackled this every which way. I have a leaf occurrence rule that I could adapt but I need to get values from some assemblies as well as parts, so it needs to look at all. Can anyone see why this won't look at sub assemblies and parts? The blue text may need altering. Really stuck here so any help would be appreciated. Thank you.
Sub Main TraverseAssemblySample()
' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
Dim oCost As Decimal
oCost = 0
' Call the function that does the recursion.
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, oCost)
MsgBox(oCost)
End Sub
Private Sub TraverseAssembly(ByVal Occurrences As ComponentOccurrences, _
ByVal Level As Integer, _
ByRef oCost As Double)
' Iterate through all of the occurrence in this collection. This
' represents the occurrences at the top level of an assembly.
For Each oOcc As ComponentOccurrence In Occurrences
' Skip Phantom and reference parts.
If oOcc.BOMStructure = BOMStructureEnum.kPhantomBOMStructure _
Or oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
' Do Nothing
Else
' The occurrence is valid count the cost.
' Get the document file for this occurrence
Dim oDoc As Document
oDoc = oOcc.Definition.Document
' Get the iPropertySet that constains the estimated cost property
Dim oPropSet As PropertySet
oPropSet = oDoc.PropertySets.Item("Design Tracking Properties")
' Get the cost of this occurrence
oCost += oPropSet.Item("Cost").Value
End If
' 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, oCount)
End If
Next
End Sub
Solved! Go to Solution.