Try a .rebuild. Also try re-assigning to BOM object AFTER you perform a function on the BOM, instead of using the object you had used previously.
*verbatim code below*
Class BomCouplers
' Doc Parameters
Shared Dim modelDoc As AssemblyDocument
Shared Dim MainAsmFilename As String
Shared Dim asmCompDef As AssemblyComponentDefinition
Shared Dim UoM As UnitsOfMeasure
Shared Dim BedCompOccS As ComponentOccurrences
Shared Dim oBOM As BOM
Shared Dim oBOMRowS As BOMRowsEnumerator
Shared Dim keyVirtCoupler As String = "VirtCoupler"
Shared Dim CoupCounts As New Dictionary(Of String, Integer)
Shared Dim keyVirtEndCap As String = "Virtual End Cap"
Sub Main()
'[ SETUP
modelDoc = ThisDoc.Document
MainAsmFilename = IO.Path.GetFileName(modelDoc.FullFileName) ''Assembly Filename
''Assembly references
UoM = modelDoc.UnitsOfMeasure
asmCompDef = modelDoc.ComponentDefinition
BedCompOccS = asmCompDef.Occurrences
']
'[ BOM SETUP
oBOM = asmCompDef.BOM
If Not oBOM.StructuredViewEnabled Then
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = False
oBOM.StructuredViewDelimiter = "-"
End If
Dim oBOMViews As BOMViews = oBOM.BOMViews
Dim oBOMStruct As BOMView = oBOMViews.Item("Structured")
oBOMRowS = oBOMStruct.BOMRows
']
CoupCounts.Clear() ''reset dictionary
DeleteVirtCouplers() '' delete all virtual pipe couplers
RuleParametersOutput()
InventorVb.DocumentUpdate()
CountPrefabEndCaps()
''iterate through Coupler counts dictionary
Dim pair As KeyValuePair(Of String, Integer)
For Each pair In CoupCounts
Dim size As String = pair.Key
Dim PlainEnds As Integer = pair.Value
'MessageBox.Show("size: " & vbTab & size & vbCrLf & "PlainEnds: " & vbTab & PlainEnds & vbCrLf & "zzzzzzzzz: " & vbTab & zzzzzzz)
''number of plain ends divided by 2 = couplers
Dim qty As Integer = Ceil(PlainEnds/2)
'MessageBox.Show("size: " & vbTab & size & vbCrLf & "PlainEnd: " & vbTab & PlainEnds & vbCrLf & "qty: " & vbTab & qty)
VirtCoupler(qty, size) ''Create or or update
Next
']
End Sub
Sub CountPrefabEndCaps()
Dim PF_Size As String
Dim PropSetPrj As PropertySet
Dim BOM_PN As String
For Each oBOMRow In oBOMRowS
''query part number and quantity
oCompDef1 = oBOMRow.ComponentDefinitions.Item(1)
If TypeOf oCompDef1 Is VirtualComponentDefinition Then
PropSetPrj = oCompDef1.PropertySets.Item("Design Tracking Properties")
Else
''line below will return the assembly file name for Virtual Components
PropSetPrj = oCompDef1.Document.PropertySets.Item("Design Tracking Properties")
End If
BOM_PN = PropSetPrj.Item("Part Number").Value
''query part number for size
PF_Size = PrefabStubSize(BOM_PN)
If PF_Size <> "" Then ''PN is a End Cap with a Prefab Stub
Dim TotQTY As String = oBOMRow.TotalQuantity
Dim PF_Qty As Integer = CInt(TotQTY)
''Each prefab Stub would have a plain end
If CoupCounts.TryGetValue(PF_Size, currentCount) Then
''add value to existing key
CoupCounts(PF_Size) = currentCount + PF_Qty
Else
''create key and store value
CoupCounts.Add(PF_Size, PF_Qty)
End If
End If
Next
End Sub
Function PrefabStubSize(strPN As String) As String
Dim size As String
Select Case strPN
Case "SC310EPE10B", "SC310EPE10T"
size = "10"
Case "SC310EPE12B"
size = "12"
Case "SC740EPE18", "SC740EPE18B", "SC740EPE18T"
size = "18"
Case "SC740EPE24B", "SC740EPE24"
size = "24"
Case Else
''if part number is not above then it's not a prefab stub
size = ""
End Select
Return size
End Function
'[ VIRTUAL
Sub DeleteVirtCouplers()
''Deletes all Virtual Split Couplers
For Each occ In BedCompOccS
If iProperties.Value(occ.Name, "Summary", "Keywords") = keyVirtCoupler Then
occ.Delete
End If
Next
End Sub
Sub VirtCoupler(qty As Integer, size As String)
Dim identity As Matrix
identity = ThisServer.TransientGeometry.CreateMatrix
Dim virtOcc As ComponentOccurrence
Dim strPN As String = Coupler_PN(size)
Dim strDesc As String = Coupler_Desc(size)
Dim strCompName As String = "Coupler " & size & " inch"
''create first instance Of the virtual part
virtOcc = BedCompOccS.AddVirtual(strCompName, identity)
virtOcc.Name = strCompName
''iProperties and quantity
iProperties.Value(strCompName, "Project", "Part Number") = strPN
iProperties.Value(strCompName, "Project", "Description") = strDesc
iProperties.Value(strCompName, "Summary", "Keywords") = keyVirtCoupler
iProperties.Value(strCompName, "Custom", "STAN") = "STAN"
iProperties.Value(strCompName, "Project", "Vendor") = "ADS"
iProperties.Value(strCompName, "Custom", "NOTE") = "NOT SHOWN"
iProperties.Value(strCompName, "Custom", "ItemPriority") = 15.0
ThisBOM.OverrideQuantity("Model Data", strPN, qty)
End Sub
']
'[ COUPLER
Function Coupler_PN(size As String) As String
Dim strPN As String
Select Case size
Case "4"
strPN = "0412AA"
Case "6"
strPN = "0613AA"
Case "8"
strPN = "0813AA"
Case "10"
strPN = "1011AA"
''future option "1013AA" "10"".DWALL SNAP COUPLER.(3/BG)"
Case "12"
strPN = "1265AA"
Case "15"
strPN = "1565AA"
Case "18"
strPN = "1865AA"
Case "24"
strPN = "2465AA"
Case "30"
strPN = "3065AA"
Case "36"
strPN = "3661AA"
Case "42"
strPN = "4265AA"
Case "48"
strPN = "4865AA"
Case "60"
strPN = "6065AA"
Case Else
strPN = "??????????"
End Select
Return strPN
End Function
Function Coupler_Desc(size As String) As String
Select Case size
Case "4"
Desc = "4"".SNAP COUPLER.(24/BG)"
Case "6"
Desc = "6"".DWALL SNAP COUPLER.(8/BG)"
Case "8"
Desc = "8"".DWALL SNAP COUPLER.(4/BG)"
Case "10"
Desc = "10"".SPLIT COUPLER.(H - 12/BG)"
''future option "1013AA" "10"".DWALL SNAP COUPLER.(3/BG)"
Case "12"
Desc = "12"".SPLIT COUPLER.(200/PALLET)"
Case "15"
Desc = "15"".SPLIT COUPLER.(150/PALLET)"
Case "18"
Desc = "18"".SPLIT COUPLER.(150/PALLET)"
Case "24"
Desc = "24"".SPLIT COUPLER.(100/PALLET)"
Case "30"
Desc = "30"".SPLIT COUPLER.(50/PALLET)"
Case "36"
Desc = "36"".SPLIT COUPLER.(50/PALLET)"
Case "42"
Desc = "42"".SPLIT COUPLER.(50/PALLET)"
Case "48"
Desc = "48"".SPLIT COUPLER.(25/PALLET)"
Case "60"
Desc = "60"".SPLIT COUPLER.(25/PALLET)"
Case Else
PN = "??????????"
End Select
Return Desc
End Function
']
End Class
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.