Public Sub Main()
' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim FirstLevelOnly As Boolean
If MsgBox("First level only?", vbYesNo) = vbYes Then
FirstLevelOnly = True
Else
FirstLevelOnly = False
End If
' Set a reference to the BOM
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
' Set whether first level only or all levels.
If FirstLevelOnly Then
oBOM.StructuredViewFirstLevelOnly = True
Else
oBOM.StructuredViewFirstLevelOnly = False
End If
' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True
'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")
'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3
Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub
Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
ItemTab = ItemTab + 3
' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
oRow = oBOMRows.Item(i)
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As Inventor.ComponentDefinition
Try
oCompDef = oRow.ComponentDefinitions.Item(1)
Catch
MsgBox (oRow.ItemNumber)
End Try
Dim oPartNumProperty As Inventor.Property
Dim oDescripProperty As Inventor.Property
If TypeOf oCompDef Is VirtualComponentDefinition Then
'Get the file property that contains the "Part Number"
'The file property is obtained from the virtual component definition
oPartNumProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")
'Get the file property that contains the "Description"
oDescripProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Description")
Else
'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
oPartNumProperty = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number")
'Get the file property that contains the "Description"
oDescripProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")
'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
End If
End If
Next
ItemTab = ItemTab - 3
End Sub
This code from the help gives an error on the line with:
Try
oCompDef = oRow.ComponentDefinitions.Item(1)
Catch
MsgBox (oRow.ItemNumber)
msgbox (oRow.ComponentDefinitions.Count)
gives somtimes "0" for an assembly
End Try
Why is that the case
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Solved! Go to Solution.
Public Sub Main()
' Set a reference to the assembly document.
' This assumes an assembly document is active.
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim FirstLevelOnly As Boolean
If MsgBox("First level only?", vbYesNo) = vbYes Then
FirstLevelOnly = True
Else
FirstLevelOnly = False
End If
' Set a reference to the BOM
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
' Set whether first level only or all levels.
If FirstLevelOnly Then
oBOM.StructuredViewFirstLevelOnly = True
Else
oBOM.StructuredViewFirstLevelOnly = False
End If
' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True
'Set a reference to the "Structured" BOMView
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")
'Initialize the tab for ItemNumber
Dim ItemTab As Long
ItemTab = -3
Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub
Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
ItemTab = ItemTab + 3
' Iterate through the contents of the BOM Rows.
Dim i As Long
For i = 1 To oBOMRows.Count
' Get the current row.
Dim oRow As BOMRow
oRow = oBOMRows.Item(i)
'Set a reference to the primary ComponentDefinition of the row
Dim oCompDef As Inventor.ComponentDefinition
Try
oCompDef = oRow.ComponentDefinitions.Item(1)
Catch
MsgBox (oRow.ItemNumber)
End Try
Dim oPartNumProperty As Inventor.Property
Dim oDescripProperty As Inventor.Property
If TypeOf oCompDef Is VirtualComponentDefinition Then
'Get the file property that contains the "Part Number"
'The file property is obtained from the virtual component definition
oPartNumProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Part Number")
'Get the file property that contains the "Description"
oDescripProperty = oCompDef.PropertySets _
.Item("Design Tracking Properties").Item("Description")
Else
'Get the file property that contains the "Part Number"
'The file property is obtained from the parent
'document of the associated ComponentDefinition.
oPartNumProperty = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number")
'Get the file property that contains the "Description"
oDescripProperty = oCompDef.Document.PropertySets _
.Item("Design Tracking Properties").Item("Description")
'Recursively iterate child rows if present.
If Not oRow.ChildRows Is Nothing Then
Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
End If
End If
Next
ItemTab = ItemTab - 3
End Sub
This code from the help gives an error on the line with:
Try
oCompDef = oRow.ComponentDefinitions.Item(1)
Catch
MsgBox (oRow.ItemNumber)
msgbox (oRow.ComponentDefinitions.Count)
gives somtimes "0" for an assembly
End Try
Why is that the case
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Solved! Go to Solution.
Solved by bradeneuropeArthur. Go to Solution.
Found out why.
Since the implementation of Model States, suppressed components are no longer removed from the Bill Of Material.
They are only indicated with quantity "0".
These return a "oRow.ComponentDefinitions.Count" of "0" ofcourse.
This is very confusing because you don't see this directly during programming.
Still good to learn from this case and maybe it helps others.
Regards,
Arthur
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Found out why.
Since the implementation of Model States, suppressed components are no longer removed from the Bill Of Material.
They are only indicated with quantity "0".
These return a "oRow.ComponentDefinitions.Count" of "0" ofcourse.
This is very confusing because you don't see this directly during programming.
Still good to learn from this case and maybe it helps others.
Regards,
Arthur
Regards,
Arthur Knoors
Autodesk Affiliations:
Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!
! For administrative reasons, please mark a "Solution as solved" when the issue is solved !
Can't find what you're looking for? Ask the community or share your knowledge.