Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
fulvio81
in reply to: fulvio81

 

Final revised version including both option (custom iProperty and project iProperty)

there was an error 

PropertySets.Item(3)'correct  instead of (2)'wrong

 

    Sub Main()
        Dim oAssemblyDocument As AssemblyDocument
        oAssemblyDocument = ThisDoc.Document

        Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
        oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

        Dim oBOM As BOM
        oBOM = oAssemblyComponentDefinition.BOM
        oBom.StructuredViewEnabled = True
        Dim oBOMView As BOMView
        oBOMView = oBom.BOMViews(2) 'Structured view
        oBom.StructuredViewFirstLevelOnly = False
        oBom.StructuredViewDelimiter = "."

        Call RecursiveCheckAndSetProps(oBOMView.BOMRows)

    End Sub

    Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator)

        For Each oBOMRow As BOMRow In oRowsElements
            Dim oComponentDefinition As ComponentDefinition
            oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)

            Dim oBOMItemNumber As String
            oBOMItemNumber = oBOMRow.ItemNumber() 'this is item number in the BOM
            ' MessageBox.Show(oBOMItemNumber, "BOM Number") 'just to show what's going on
			
'			'if you want standard iproperty (e.g. "project")        
            Dim oComponentDefinitionPropertySet As PropertySet
            oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item(3) 'design tracking
            oComponentDefinitionPropertySet.ItemByPropId(7).Value = oBOMItemNumber  '7 is project property
			
'			'if you want custom iproperty then
'			Dim oComponentDefinitionPropertySet As PropertySet
'            oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item("Inventor User Defined Properties")
'            'custom property tab
' 			Try
'                'if already exists then set it 
'                oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber
'            Catch ex As Exception
'                'else add it
'                oComponentDefinitionPropertySet.Add(oBOMItemNumber, "BOM Number")
'            End Try
'            'creates the custom property and inputs the value

            If Not oBOMRow.ChildRows Is Nothing Then
                Call RecursiveCheckAndSetProps(oBOMRow.ChildRows)
            End If
        Next
    End Sub