Code:
Option ExplicitPrivate Sub AssemblyCosts()
Dim oApp As Inventor.Application
Set oApp = ThisApplication
If Not oApp.ActiveDocumentType = kAssemblyDocumentObject Then Exit Sub
Dim oAssDoc As AssemblyDocument
Set oAssDoc = oApp.ActiveDocument
Dim oBom As BOM
Set oBom = oAssDoc.ComponentDefinition.BOM
oBom.StructuredViewEnabled = True
oBom.StructuredViewFirstLevelOnly = False
Dim oBomview As BOMView
Set oBomview = oBom.BOMViews(2)
Dim dCost As Currency
Dim iTotalQty As Integer
iTotalQty = 1
dCost = ProcessBomRows(oBomview.BOMRows, iTotalQty, dCost)
If MsgBox("Gesamtkosten: " & dCost & vbCrLf & "Speichern in iProperty geschätzte Kosten?", vbYesNo) = vbYes Then
oAssDoc.PropertySets(3).Item(21).Value = dCost
End If
End Sub
Private Function ProcessBomRows(ByVal oBomRows As BOMRowsEnumerator, ByVal iTotalQty As Integer, ByVal dCost As Double) As Currency
Dim oBomRow As BOMRow
For Each oBomRow In oBomRows
If Not oBomRow.ChildRows Is Nothing Then
dCost = ProcessBomRows(oBomRow.ChildRows, iTotalQty * oBomRow.ItemQuantity, dCost)
End If
Dim oCompDef As ComponentDefinition
For Each oCompDef In oBomRow.ComponentDefinitions
Dim oDoc As Document
Set oDoc = oCompDef.Document
Dim dItemCost As Currency
dItemCost = oDoc.PropertySets(3).Item(21).Value
If Not dItemCost = 0 Then
dCost = dCost + oBomRow.ItemQuantity * iTotalQty * dItemCost
End If
Next
Next
ProcessBomRows = dCost
End Function