Item Number to iProperty 2

Item Number to iProperty 2

marcin_bargiel
Advocate Advocate
895 Views
6 Replies
Message 1 of 7

Item Number to iProperty 2

marcin_bargiel
Advocate
Advocate

Hi there was similar topic, but i have different issue.

I'm also trying to find a way to catch the Item number (structured BOM) and create an iProperty "BOM Number" in every assembly's parts.

 

Problem is that,  rule doesn't add BOM Number to parts that have the same filename (i have many parts with the same filenames - and i'm not going to change it! they always are in different folder) . It adds only to the first occurance as i understand.

When using this :

 

For i = 1 To oBOMRow.ComponentDefinitions.Count
iProperties.Value(oBOMRow.ComponentDefinitions(i).Document.DisplayName, "Custom", "BOM Number") = oBOMItemNumber
Next

 

When using this instead of above code :

oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber

Problem is when i have many parts with the same Partname- it adds BOM Number to the first of it.

 

Can anyone help me ?

 

Whole rule here, and also attached with my TEST FILES.

 

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
'Logger.Info(oBOMItemNumber & " BOM Number") 'just to show what's going on
Dim oComponentDefinitionPropertySet As PropertySet
oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item("Inventor User Defined Properties")


'Try
'If already exists Then Set it
On Error Resume Next
For i = 1 To oBOMRow.ComponentDefinitions.Count
iProperties.Value(oBOMRow.ComponentDefinitions(i).Document.DisplayName, "Custom", "BOM Number") = oBOMItemNumber

'oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber

Logger.Info(oBOMRow.ComponentDefinitions(i).Document.DisplayName & " - " & oBOMItemNumber)
Next


'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

Vote for REPLACE VARIES !
REPLACE VARIES
0 Likes
Accepted solutions (2)
896 Views
6 Replies
Replies (6)
Message 2 of 7

tonythm
Advocate
Advocate

Hi @marcin_bargiel 

 

You can refer code below:

I use excel vba.

Sub SetItemBOM()
    Dim oWkbk As Workbook
    Set oWkbk = ThisWorkbook
    Dim oSheet As Worksheet
    Set oSheet = oWkbk.ActiveSheet
    Dim oInv As Inventor.Application
    Set oInv = GetObject(, "Inventor.Application")
    Dim oDoc As Document
    Set oDoc = oInv.ActiveDocument
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oAssy As AssemblyDocument
    Set oAssy = oInv.ActiveDocument
    Dim sParamRange As String
        sParamRange = "A44:A200"
    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    oBOM.StructuredViewEnabled = True
    oBOM.StructuredViewFirstLevelOnly = True ' Display First level
    oBOM.StructuredViewMinimumDigits = 3
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Structured")
    Dim i As Integer
    For i = 1 To oBOMView.BOMRows.Count
        Dim oRow As BOMRow
        Set oRow = oBOMView.BOMRows.Item(i)
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)
        Dim oPartNumProperty As Property
        Set oPartNumProperty = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number")
        
        Dim oCell As Range
        For Each oCell In oSheet.Range(sParamRange)
            If oCell.Value = oPartNumProperty.Value Then
            oRow.ItemNumber = oCell.Offset(0, 1).Value
            End If
            'If oCell.Value = oPartNumProperty.Value Then
                'If oCell.Offset(0, 2).Value = oRow.ItemQuantity Then
                'oCell.Offset(0, 2).Interior.Color = RGB(0, 176, 240)
                'Else
                'oCell.Offset(0, 2).Interior.Color = RGB(255, 0, 0)
                'End If
            'End If
        Next oCell
    Next
    Range("A40").Select
    MsgBox ("Done!")
    oWkbk.Save
End Sub

 

0 Likes
Message 3 of 7

marcin_bargiel
Advocate
Advocate

thanks, but that's not very helpful to my problem

Vote for REPLACE VARIES !
REPLACE VARIES
0 Likes
Message 4 of 7

marcin_bargiel
Advocate
Advocate

can anyone help me on the topic?

Vote for REPLACE VARIES !
REPLACE VARIES
0 Likes
Message 5 of 7

marcin_otręba
Advisor
Advisor
Accepted solution

try:

 

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

For i = 1 To oBOMRow.ComponentDefinitions.Count
	Dim oComponentDefinitionPropertySet As PropertySet
oComponentDefinitionPropertySet = oBOMRow.ComponentDefinitions(i).Document.PropertySets.Item(4)
Try

oComponentDefinitionPropertySet.Add(oBOMItemNumber, "BOM Number")

Catch
	oComponentDefinitionPropertySet.Item("BOM Number").Value=oBOMItemNumber
End Try
Logger.Info(oBOMRow.ComponentDefinitions(i).Document.DisplayName & " - " & oBOMItemNumber)
Next
If Not oBOMRow.ChildRows Is Nothing Then
Call RecursiveCheckAndSetProps(oBOMRow.ChildRows)
End If
Next
End Sub

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 6 of 7

j.pavlicek
Collaborator
Collaborator
Accepted solution

@marcin_bargiel Hello, I see problem on line (24)

oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)

Where the first Component Definition is stored in variable oComponentDefinition.

 

Following for goes trough all of the definitions, but oComponentDefinition* is still targeting the first definition.

For i = 1 To oBOMRow.ComponentDefinitions.Count
iProperties.Value(oBOMRow.ComponentDefinitions(i).Document.DisplayName, "Custom", "BOM Number") = oBOMItemNumber

'oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber

Logger.Info(oBOMRow.ComponentDefinitions(i).Document.DisplayName & " - " & oBOMItemNumber)
Next

 You need something like

oComponentDefinition = oBOMRow.ComponentDefinitions.Item(i)
oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item("Inventor User Defined Properties")

inside the for cycle.



Inventor 2022, Windows 10 Pro
Sorry for bad English.
Message 7 of 7

marcin_bargiel
Advocate
Advocate

MANY THANK'S !!!

Vote for REPLACE VARIES !
REPLACE VARIES
0 Likes