I'm try for read item number like a picture below and wanna get the data structure as per Structured View.
But I can't. Because the program read the ItemNumber as per Model data or Hierarchy in object history.
I wanna the data like this:
1
1.1
1.2
1.3
1.4
1.5
2
2.1
2.2
2.3
3
3.1
3.2
4
5
6
But I get actual data like this :
1
1.1
1.2
1.3
1.4
1.5
4
2
2.1
2.2
2.3
5
6
3
3.1
3.2
Here's the code :
Sub ListBomItems() Dim asm As AssemblyDocument asm = invApp.ActiveDocument Dim b As BOM b = asm.ComponentDefinition.BOM b.StructuredViewEnabled = True Dim bv As BOMView bv = b.BOMViews("Structured") Call ListItems(bv.BOMRows, 0) End Sub Sub ListItems(rows As BOMRowsEnumerator, indent As Integer) Dim row As BOMRow For Each row In rows Debug.Print(row.ItemNumber.ToString) If Not row.ChildRows Is Nothing Then Call ListItems(row.ChildRows, indent + 2) End If Next End Sub
Is there way for rearrange data structure in Model Data / Hierarchy model ?
Solved! Go to Solution.
Solved by chandra.shekar.g. Go to Solution.
Solved by chandra.shekar.g. Go to Solution.
Hi,
Output looks very interesting.
Could you please provide dataset for investigation?
Thanks,
Martin
It has been like this forever.
Inventor doesn't internally resort the data based on the item numbering scheme you have set.
The internal order is based on the order in which the documents were added to the assembly (I think).
If you want to get a list by the bom structure, you should just be able to re-sort it by item number afterwords using a custom comparison. Or you could evaluate if you really need the program to do the sorting, and can just get by with some other sort method (ie; excel) afterwards.
@MechMachineMan : Thanks for your reply. Because it's impossible for Numbering component direct from Inventor. So I decide using Excel Spreadsheet in this case.
Regards,
Afri
I tested provided sample and source code in Inventor 2019 and Visual studio 2017. The results are sorted as shown below. Can you please specify Inventor version that is currently using?
Thanks and regards,
Hi, this my version. and work for me.
use strcomp argument to find item no. and sort it.
Public Sub OlahDataExport()
Dim oAPp As Application
Dim oAD As AssemblyDocument
Dim oACD As AssemblyComponentDefinition
Dim oBOM As BOM
Dim oBOMStructure As BOMView
Dim oBomRow As BOMRow
Dim oGetBomRow As BOMRow
Set oAPp = ThisApplication
Set oAD = oAPp.ActiveDocument
Set oACD = oAD.ComponentDefinition
Set oBOM = oACD.BOM
On Error Resume Next
Set oBOMStructure = oBOM.BOMViews.Item("Structured")
If Err.Number <> 0 Then
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
Set oBOMStructure = oBOM.BOMViews.Item("Structured")
End If
oBOMStructure.Sort ("Item")
For i = 1 To oBOMStructure.BOMRows.Count
For Each oBomRow In oBOMStructure.BOMRows
If StrComp(oBomRow.ItemNumber, i, vbTextCompare) = 0 Then
Set oGetBomRow = oBomRow
Debug.Print oGetBomRow.ItemNumber
Call EachChildRowCall(oGetBomRow)
End If
Next
Next
End Sub
Public Sub EachChildRowCall(ByVal oBomRow As BOMRow)
Dim oChildROw As BOMRow
Dim oGetChildRow As BOMRow
For i = 1 To oBomRow.ChildRows.Count
For Each oChildROw In oBomRow.ChildRows
If StrComp(Right(oChildROw.ItemNumber, 1), i, vbTextCompare) = 0 Then
Set oGetChildRow = oChildROw
Debug.Print oGetChildRow.ItemNumber
If oGetChildRow.ChildRows Is Nothing Then
Else
Call EachChildRowCall(oGetChildRow)
End If
End If
Next
Next
End Sub
Many thanks for your information. But we don't have planning for upgrade to 2019. We still use 2015. Because our head quarter in Canada still use 2015.
Thanks.
It's not working in Inventor 2015 .
Here's the result.
1
1.1
1.2
1.3
1.4
1.5
6
Here's the code
Public Sub OlahDataExport() Dim oAD As AssemblyDocument Dim oACD As AssemblyComponentDefinition Dim oBOM As BOM Dim oBOMStructure As BOMView Dim oBomRow As BOMRow Dim oGetBomRow As BOMRow oAD = invApp.ActiveDocument oACD = oAD.ComponentDefinition oBOM = oACD.BOM On Error Resume Next oBOMStructure = oBOM.BOMViews.Item("Structured") If Err.Number <> 0 Then oBOM.StructuredViewEnabled = True oBOM.StructuredViewFirstLevelOnly = False oBOMStructure = oBOM.BOMViews.Item("Structured") End If oBOMStructure.Sort("Item") For i = 1 To oBOMStructure.BOMRows.Count For Each oBomRow In oBOMStructure.BOMRows If StrComp(oBomRow.ItemNumber, i, vbTextCompare) = 0 Then oGetBomRow = oBomRow Debug.Print(oGetBomRow.ItemNumber) Call EachChildRowCall(oGetBomRow) End If Next Next End Sub Public Sub EachChildRowCall(ByVal oBomRow As BOMRow) Dim oChildROw As BOMRow Dim oGetChildRow As BOMRow For i = 1 To oBomRow.ChildRows.Count For Each oChildROw In oBomRow.ChildRows If StrComp(Right(oChildROw.ItemNumber, 1), i, vbTextCompare) = 0 Then oGetChildRow = oChildROw Debug.Print(oGetChildRow.ItemNumber) If oGetChildRow.ChildRows Is Nothing Then Else Call EachChildRowCall(oGetChildRow) End If End If Next Next End Sub
I have not an Inventor 2015 build at hand, but tested with Inventor 2016 and it prints the right result. Can you debug the code to check why it ignored the ItemNumber from 2 to 5? Also please check that in your data the ItemNumber is not overridden.
Hoping that below sorting code would help to sort items of Structured view.
Sub ListBomItems() Dim asm As AssemblyDocument asm = invApp.ActiveDocument Dim b As BOM b = asm.ComponentDefinition.BOM b.StructuredViewEnabled = True Dim bv As BOMView bv = b.BOMViews("Structured") Dim oSort As Dictionary(Of Integer, Integer) oSort = BOM_Sort(bv.BOMRows) Call ListItems(bv.BOMRows, 0, oSort) End Sub Function BOM_Sort(rows As BOMRowsEnumerator) Dim oSort As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer) Dim oRow As BOMRow For i As Integer = 1 To rows.Count Step 1 oRow = rows.Item(i) oSort.Add(oRow.ItemNumber, i) Next BOM_Sort = oSort End Function Sub ListItems(rows As BOMRowsEnumerator, indent As Integer, oSort As Dictionary(Of Integer, Integer)) Dim row As BOMRow Dim i As Integer For i = 1 To rows.Count Step 1 Dim key As Integer = oSort(i) row = rows.Item(key) Debug.Print(row.ItemNumber.ToString) If Not row.ChildRows Is Nothing Then Call ListItems(row.ChildRows, indent + 2) End If Next End Sub Sub ListItems(rows As BOMRowsEnumerator, indent As Integer) Dim row As BOMRow For Each row In rows Debug.Print(row.ItemNumber.ToString) If Not row.ChildRows Is Nothing Then Call ListItems(row.ChildRows, indent + 2) End If Next End Sub
Thanks and regards,
Below highlighted (red in color) was missing in previous code.
Sub ListBomItems() Dim asm As AssemblyDocument asm = invApp.ActiveDocument Dim b As BOM b = asm.ComponentDefinition.BOM b.StructuredViewEnabled = True Dim bv As BOMView bv = b.BOMViews("Structured") Dim oSort As Dictionary(Of Integer, Integer) oSort = BOM_Sort(bv.BOMRows) Call ListItems(bv.BOMRows, 0, oSort) End Sub Function BOM_Sort(rows As BOMRowsEnumerator) Dim oSort As Dictionary(Of Integer, Integer) = New Dictionary(Of Integer, Integer) Dim oRow As BOMRow For i As Integer = 1 To rows.Count Step 1 oRow = rows.Item(i) oSort.Add(oRow.ItemNumber, i) Next BOM_Sort = oSort End Function Sub ListItems(rows As BOMRowsEnumerator, indent As Integer, oSort As Dictionary(Of Integer, Integer)) Dim row As BOMRow Dim i As Integer For i = 1 To rows.Count Step 1 Dim key As Integer = oSort(i) row = rows.Item(key) Dim oCompDef As ComponentDefinition oCompDef = row.ComponentDefinitions.Item(1) Debug.Print(row.ItemNumber.ToString & " " & oCompDef.Type.ToString) If Not row.ChildRows Is Nothing Then Call ListItems(row.ChildRows, indent + 2) End If Next End Sub Sub ListItems(rows As BOMRowsEnumerator, indent As Integer) Dim row As BOMRow For Each row In rows Dim oCompDef As ComponentDefinition oCompDef = row.ComponentDefinitions.Item(1) Debug.Print(row.ItemNumber.ToString & " " & oCompDef.Type.ToString) If Not row.ChildRows Is Nothing Then Call ListItems(row.ChildRows, indent + 2) End If Next End Sub
Thanks and regards,
Can't find what you're looking for? Ask the community or share your knowledge.