- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I usually give Part Numbers to Parts through the BOM. The CC parts always rename "Part Number" by their defaults if there are any changes. For this reason I create an additional iProperty "Number" which stores the "Part Number" value so that it doesn't dissapear after every change. Then I have to again set Part Number = <Number> in BOM to fix the issue.
I wrote this little code to:
- unmerge BOM
- set Part Number =<Number> (yes I even tried giving it a copied valued instead of formula)
- re-merge BOM
But it still doesn't work. The part's which are of same stock size and same length still cause issues.
So I manually un-merge and I have this (none of the parts are named the same):
Next, I retype the Number values - same values as before (essentially 'dirtying' the part) and everything works well.
How can this be fixed? I tried "dirtying" the part through the code with: oRefDoc.Dirty = True, oRefDoc.Update() but without any success...
Here is the code. Many thanks!
Sub Main()
Dim oAsmDoc As Document = ThisApplication.ActiveDocument
If oAsmDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("Run this rule on an ASSEMBLY document. Exiting...", "ERROR",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
' Disable merge before updating Part Numbers
Bom_DisableMerge(oAsmDoc)
' Update Part Numbers from iProperty "Number"
UpdatePartNumbersFromNumber(oAsmDoc)
' Re-enable merge
Bom_EnableMerge(oAsmDoc)
End Sub
Public Sub Bom_DisableMerge(oAsmDoc As AssemblyDocument)
Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True
' Disable "Merge Rows on Part Number Match"
oBOM.SetPartNumberMergeSettings2(False, Nothing, Nothing)
End Sub
Public Sub Bom_EnableMerge(oAsmDoc As AssemblyDocument)
Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
oBOM.PartsOnlyViewEnabled = True
' Enable "Merge Rows on Part Number Match"
oBOM.SetPartNumberMergeSettings2(True, Nothing, Nothing)
End Sub
Public Sub UpdatePartNumbersFromNumber(oAsmDoc As AssemblyDocument)
For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments
' Skip non‑modifiable and non‑real BOM parts
If Not oRefDoc.IsModifiable Then Continue For
If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPhantomBOMStructure Then Continue For
If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Continue For
' Get custom property set
Dim oCustom As PropertySet
Try
oCustom = oRefDoc.PropertySets("Inventor User Defined Properties")
Catch
' No custom props → skip
Continue For
End Try
' Try to get "Number"
Dim numberValue As String
Try
numberValue = CStr(oCustom("Number").Value)
Catch
'MessageBox.Show("Didn't work for: " & oRefDoc.DisplayName & " (no 'Number' iProperty)")
Continue For
End Try
If Trim(numberValue) = "" Then Continue For
' *** FORCE REFRESH OF NUMBER ***
oCustom("Number").Value = numberValue
' *** Assign formula-style reference to Part Number ***
Dim oPNProp As Object
Try
oPNProp = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
oPNProp.Value = "=<Number>"
Catch
'MessageBox.Show("Didn't work for: " & oRefDoc.DisplayName & " (no 'Part Number' iProperty)")
Continue For
End Try
Next
End Sub
Solved! Go to Solution.