Iterate through whole assembly - trying to build mark numbers

Iterate through whole assembly - trying to build mark numbers

mslosar
Advisor Advisor
414 Views
2 Replies
Message 1 of 3

Iterate through whole assembly - trying to build mark numbers

mslosar
Advisor
Advisor

OK, what I'm trying to do is iterate through an entire assembly and generate mark numbers for parts that are library and/or content center parts. Parts that are read only.

 

For example - a whole shipping unit with have, say, 4 assemblies (ZR1-300, ZR1-330, ZR5-800, and ZR6-900). In our case, the mark number for each of those is: 

 

ZR1-300-1

ZR1-330-1

ZR5-800-1

ZR6-900-1

 

Those are no problem. The custom iproperty MARK_NO exists in them and is editable on a per project basis.

 

Now, there are assorted nuts/bolts associated with each of the 4 sub assemblies. These, being content center part can't have different MARK_NO's per use. Therefore, presently we've been just manually assigned them MARK_NO's but manually editing the part's list. If the bolt is on line 5, for the ZR-300, it's MARK_NO is ZR1-300-5. In the nut was on line 8, ZR1-300-8. Basically, the line of the parts list/BOM equals the mark number.

 

I used the sample code from the help file below to make the original code:

 

Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set 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
    Set 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
    Set oBOMView = oBOM.BOMViews.Item("Structured")
        
    Debug.Print "Item"; Tab(15); "Quantity"; Tab(30); "Part Number"; Tab(70); "Description"
    Debug.Print "----------------------------------------------------------------------------------"

    '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
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

        Dim oPartNumProperty As Property
        Dim oDescripProperty As 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
            Set oPartNumProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
        Else
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Set oPartNumProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
            
            '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

My theoretical solution to being able to add our mark numbers directly to exports was to see if the object had a tag number. If it did, use it. Otherwise it was going to be get the first 8 digits of the parents MARK_NO (ZR1-300-) and then append the BOM's line number to it (ZR1-300-5).

 

Problem is, i cannot figure out how get the parent from within this code properly. Best I can figure is at the "

Recursively iterate child rows if present

 

line at the end, pull the mark number from the assembly, but this isn't tracking the assembly, just the BOM rows, so that's where I get lost.

 

Thanks for reading, hope it makes sense and is an easy fix 🙂 

0 Likes
415 Views
2 Replies
Replies (2)
Message 2 of 3

woodstylee3
Advocate
Advocate

You may find the below code useful, I use it to process a structured bom which is all levels. Rather than resolve the parent from the child, I use an approach of nested function calls where data from the parent is passed as a second parameter to the nested call. If doesn't make sense I can talk you through more..

 

Hope it helps..

 

Private Sub export_materials_rough_list() Handles ss_roughcutmatl.OnExecute

Dim ref_doc As AssemblyDocument

 

ref_doc = GlobalVar.m_inventorApplication.ActiveDocument

If IsNothing(ref_doc) Then Return

Dim excel As Microsoft.Office.Interop.Excel.Application

Dim wkbk As Microsoft.Office.Interop.Excel.Workbook

Try

excel = New Microsoft.Office.Interop.Excel.Application

wkbk = excel.Workbooks.Add

Dim excelwks = wkbk.Worksheets.Add

Dim n() As String = New String() {"Parent Assembly", "PartNumber", "Qty", "Material Spec", "Thickness", "Mass", "Total Mass"}

For x = 0 To 6

If Not (IsNothing(n(x))) Then excelwks.Cells(1, x + 1).value = n(x)

Next

export_materials_rough_list_assy(ref_doc.ComponentDefinition, excelwks, 2, 1, "")

' excelwks.SaveAs()

wkbk.SaveAs(ref_doc.FullFileName & "_Materialisation.xlsx")

excel.Visible = True

Catch ex As Exception

Dim a As Integer = 1

If IsNothing(wkbk) Then

Else

wkbk.close(False)

End If

If IsNothing(excel) Then

Else

excel.Quit()

End If

End Try

End Sub

Private Sub export_materials_rough_list_assy(componentdef As ComponentDefinition, excelwks As Microsoft.Office.Interop.Excel.Worksheet, ByRef ctr As Integer, ByVal baseqty As Integer, ByVal parenttext As String)

Dim bom As Inventor.BOMStructureEnum

Dim m_parent As String

Try

If (CInt(componentdef.Type) = CInt(ObjectTypeEnum.kAssemblyComponentDefinitionObject)) Or (CInt(componentdef.Type) = CInt(ObjectTypeEnum.kWeldmentComponentDefinitionObject)) Then

bom = componentdef.BOMStructure

For Each bomv As Inventor.BOMView In componentdef.BOM.BOMViews

If bomv.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then

For Each bomrow As Inventor.BOMRow In bomv.BOMRows

Dim process As Boolean

Select Case bomrow.BOMStructure

Case Is = BOMStructureEnum.kVariesBOMStructure

process = True

m_parent = GlobalVar.m_project_file.iprop_mappings.itemfromkey(GlobalVar.m_project_file.Partslist_Settings.PartNumber.PropertyGuid).Read(componentdef.Document)

Case Is = BOMStructureEnum.kInseparableBOMStructure

process = True

m_parent = GlobalVar.m_project_file.iprop_mappings.itemfromkey(GlobalVar.m_project_file.Partslist_Settings.PartNumber.PropertyGuid).Read(componentdef.Document)

Case Is = BOMStructureEnum.kNormalBOMStructure

process = True

m_parent = GlobalVar.m_project_file.iprop_mappings.itemfromkey(GlobalVar.m_project_file.Partslist_Settings.PartNumber.PropertyGuid).Read(componentdef.Document)

Case Is = BOMStructureEnum.kPhantomBOMStructure

process = True

m_parent = parenttext

Case Is = BOMStructureEnum.kPurchasedBOMStructure

process = False

Case Is = BOMStructureEnum.kReferenceBOMStructure

process = False

End Select

If process Then

If bomrow.ComponentDefinitions.Count = 1 Then

export_materials_rough_list_assy(bomrow.ComponentDefinitions(1), excelwks, ctr, baseqty * bomrow.ItemQuantity, m_parent)

End If

End If

Next

End If

Next

ElseIf (CInt(componentdef.Type) = CInt(ObjectTypeEnum.kPartComponentDefinitionObject)) Or (CInt(componentdef.Type) = CInt(ObjectTypeEnum.kSheetMetalComponentDefinitionObject)) Then

bom = componentdef.BOMStructure

Dim process As Boolean

Select Case bom

Case Is = BOMStructureEnum.kInseparableBOMStructure

process = True

Case Is = BOMStructureEnum.kNormalBOMStructure

process = True

Case Is = BOMStructureEnum.kPhantomBOMStructure

process = True

Case Is = BOMStructureEnum.kPurchasedBOMStructure

process = False

Case Is = BOMStructureEnum.kReferenceBOMStructure

process = False

End Select

If process Then

Dim n As String()

Try

'''your code here...

Catch ex As Exception

n = Nothing

End Try

 

End If

End If

Catch ex As Exception

Dim a As Integer

End Try

End Sub

0 Likes
Message 3 of 3

mslosar
Advisor
Advisor
Thanks for the suggestion. I'll dig into it and see what happens 🙂
0 Likes