04-06-2022
06:14 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-06-2022
06:14 PM
OK, Pending more testing I think I have found a solution.
Essentially I needed to rename all occurrences available from that BOM level to have a unique name (Sequentially numbered occurrence suffix)
Here is the currently working Code.
Sub Main
Dim oAdoc As AssemblyDocument
oAdoc = ThisApplication.ActiveDocument
'Check it is the active document that triggered this script
Dim activeDocName As String = oAdoc.FullFileName
Dim thisDocName As String = ThisDoc.Document.FullFileName
If (activeDocName <> thisDocName) Then
'the script was not triggered by the active document
Return
End If
'Check active document is an assembly
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("This rule " & iLogicVb.RuleName & " only works for Assembly Documents.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
'Get current Assembly TOTAL_QUANTITY value as Pack Quantity multiplier
Dim oPQ As String
oPQ = (iProperties.Value("Custom", "TOTAL_QUANTITY"))
Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAdoc.ComponentDefinition
Dim asmOccurrences = oCompDef.Occurrences
'Get BOM Definition
Dim oBOM As BOM
oBOM = oCompDef.BOM
'Set BOM to structured view
oBOM.StructuredViewEnabled = True
oBOM.StructuredViewFirstLevelOnly = True
Dim oBOMView As BOMView
oBOMView = oBOM.BOMViews.Item("Structured")
Dim oRow As BOMRow
'Lets rename all our occurrences to make them unique
OccCounter = 0
For Each oRow In oBOMView.BOMRows
'Message box for debugging
'MessageBox.Show(oRow.ItemNumber, "Debug-row")
For Each CompDef As ComponentDefinition In oRow.ComponentDefinitions
Dim occurrences = asmOccurrences.AllLeafOccurrences(CompDef)
For Each occ As ComponentOccurrence In occurrences
OccCounter = OccCounter + 1
'Removes the :x portion of name
NewName = (occ.Name.Split(":")(0))
'Trace.WriteLine(occ.Name & " - Original name - " & NewName)
'Append the new :x portion of name
NewName = NewName & ":" & OccCounter
If occ.Name = NewName Then
'Do nothing it's already that anyway
Else
Try
'Write new name
occ.Name = NewName
'Message box for debugging
'MessageBox.Show(occ.Name, "Debug-Name")
Catch
Try
'Write new name with an A suffix. - Assumes fail was due to existing name in assembly already
occ.Name = NewName & "A"
'Message box for debugging
'MessageBox.Show(occ.Name, "Debug-Name Retry")
Catch
MessageBox.Show(occ.Name & ": Count=" & OccCounter & ", Rename didn't work." & vbNewLine & "Please run again", "Debug - rename try")
Exit Sub
End Try
End Try
End If
Next
Next
Next
'Now lets write our instance Variable
For Each oRow In oBOMView.BOMRows
For Each CompDef As ComponentDefinition In oRow.ComponentDefinitions
Dim occurrences = asmOccurrences.AllLeafOccurrences(CompDef)
For Each occ As ComponentOccurrence In occurrences
Try
'Message box for debugging
'MessageBox.Show(occ.Name & " - " & oPQ, "Debug-Name")
'Write the new instance Property
iProperties.InstanceValue(occ.Name, "PACK_QTY") = oPQ
Catch
MessageBox.Show(occ.Name & ", Instance property write failed" & vbNewLine & "Please run again", "Debug - Instance property - Write")
Exit Sub
End Try
Next
Next
Next
End Sub