Part Numbers of Content Center Parts

Part Numbers of Content Center Parts

emanuel.c
Collaborator Collaborator
132 Views
3 Replies
Message 1 of 4

Part Numbers of Content Center Parts

emanuel.c
Collaborator
Collaborator

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.

emanuelc_0-1784556881520.png

 

So I manually un-merge and I have this (none of the parts are named the same):

emanuelc_1-1784556913362.png

 

Next, I retype the Number values - same values as before (essentially 'dirtying' the part) and everything works well.

emanuelc_2-1784556999766.png

 

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
Accepted solutions (2)
133 Views
3 Replies
Replies (3)
Message 2 of 4

dalton_northwind
Enthusiast
Enthusiast
Accepted solution

Maybe try updating the assembly file along with the parts.

We had a similar problem and from what I remember we had to uncheck a frame generator setting. attached

Message 3 of 4

emanuel.c
Collaborator
Collaborator

@dalton_northwind Thank you so much!! I can't believe I've been using Inventor for so many years and never knew of those settings! That auto renaming was always a frustration. I'll call this solved.

0 Likes
Message 4 of 4

emanuel.c
Collaborator
Collaborator
Accepted solution

Meanwhile I was able to adapt the code using Rebuild and Refresh on the BOM. If it's useful to anyone, here it is:

Sub Main()

    Dim oAsmDoc As AssemblyDocument = 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
	
    ' 1. Disable merge before updating Part Numbers
    Bom_DisableMerge(oAsmDoc)

    ' 2. Update Part Numbers from iProperty "Number"
    UpdatePartNumbersFromNumber(oAsmDoc)

    ' 3. Force assembly update AFTER parts update
    Try : oAsmDoc.Update2(True) : Catch : End Try

    ' 4. Full BOM rebuild sequence
    RebuildBOM(oAsmDoc)

    ' 5. Re-enable merge
    Bom_EnableMerge(oAsmDoc)

    ' 6. Final assembly update
    Try : oAsmDoc.Update2(True) : Catch : End Try

End Sub


' DISABLE MERGE
Public Sub Bom_DisableMerge(oAsmDoc As AssemblyDocument)

    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
    oBOM.PartsOnlyViewEnabled = True

    oBOM.SetPartNumberMergeSettings2(False, Nothing, Nothing)

End Sub


' ENABLE MERGE
Public Sub Bom_EnableMerge(oAsmDoc As AssemblyDocument)

    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM
    oBOM.PartsOnlyViewEnabled = True

    oBOM.SetPartNumberMergeSettings2(True, Nothing, Nothing)

End Sub


' UPDATE PART NUMBERS FROM "Number"
Public Sub UpdatePartNumbersFromNumber(oAsmDoc As AssemblyDocument)

    For Each oRefDoc As Document In oAsmDoc.AllReferencedDocuments

        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

        Dim oCustom As PropertySet
        Try
            oCustom = oRefDoc.PropertySets("Inventor User Defined Properties")
        Catch
            Continue For
        End Try

        Dim numberValue As String
        Try
            numberValue = CStr(oCustom("Number").Value)
        Catch
            Continue For
        End Try

        If Trim(numberValue) = "" Then Continue For

        ' Refresh Number
        oCustom("Number").Value = numberValue

        ' Assign formula Part Number
        Dim oPNProp As Object
        Try
            oPNProp = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
            oPNProp.Value = "=<Number>"
        Catch
            Continue For
        End Try

        ' Dirty the document
        oRefDoc.Dirty = True

        ' Force iProperty evaluator
        Try : oRefDoc.PropertySets.Flush() : Catch : End Try

        ' Rebuild geometry
        Try : oRefDoc.ComponentDefinition.Update() : Catch : End Try

        ' Update document
        Try : oRefDoc.Update2(True) : Catch : End Try

        ' ⭐ CRITICAL ⭐ Save forces formula evaluation
        Try : oRefDoc.Save() : Catch : End Try

    Next

End Sub


' FULL BOM REBUILD SEQUENCE
Public Sub RebuildBOM(oAsmDoc As AssemblyDocument)

    Dim oBOM As BOM = oAsmDoc.ComponentDefinition.BOM

    ' Ensure BOM views are enabled
    oBOM.PartsOnlyViewEnabled = True
    oBOM.StructuredViewEnabled = True

    Dim oStructured As BOMView
    Dim oPartsOnly As BOMView

    Try
        oStructured = oBOM.BOMViews.Item("Structured")
        oPartsOnly = oBOM.BOMViews.Item("Parts Only")
    Catch
        Exit Sub
    End Try

    ' Rebuild both views
    Try : oStructured.Rebuild() : Catch : End Try
    Try : oPartsOnly.Rebuild() : Catch : End Try

    ' Refresh both views
    Try : oStructured.Refresh() : Catch : End Try
    Try : oPartsOnly.Refresh() : Catch : End Try

    ' Update assembly
    Try : oAsmDoc.Update2(True) : Catch : End Try

End Sub