Hi All,
I've recently started using a script that iterates through a top level assembly BOM and conducts a few processes on each line of the BOM depending on what each item is. The loose format of the script is:
Sub Main
Dim Doc As AssemblyDocument = ThisApplication.ActiveDocument
Dim BOM As BOM = Doc.ComponentDefinition.BOM
Dim BomView As BOMView = BOM.BOMViews.Item(1)
SetPartNumbers (BOMView.BOMRows)
End Sub
Which then calls the subroutine:
Private Sub SetPartNumbers(BOMRows As BOMRowsEnumerator, A_No As Integer)
For i As Integer = 1 To BOMRows.Count
'Get the current row
Dim Row As BOMRow = BOMRows.Item(i)
'Reference to the primary component definition of the Row
Dim CompDef As ComponentDefinition = Row.ComponentDefinitions.Item(1)
Without posting the whole code here one of the processes here is:
'Process Assemblies
ElseIf CompDef.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject _
AndAlso CompDef.Document.IsModifiable _
AndAlso CompDef.Document.PropertySets.Item(4).Item("PartSet").Value = 0 _
AndAlso CompDef.BOMStructure = 51970
CompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = "A-" + Format(GlobalVariable.Assembly_Counter, "00")
CompDef.Document.PropertySets.Item(4).Item("PartSet").Value = 1
CompDef.Document.Save
'Logger.Info(GlobalVariable.Assembly_Counter)
GlobalVariable.Assembly_Counter += 1
A_Input = GlobalVariable.Assembly_Counter - 1
Logger.Info("Found Subbassembly - Beginning processing A-" + Format(A_Input, "00"))
SetPartNumbers(Row.ChildRows, A_Input)
'Logger.Info("Labelled new assembly as " + CompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value)
I've found few problems with this code that I think are related to the size of the assembly and possibly something else that I'm not sure of (for reference the assembly I'm testing on has 1668 total files and 783 unique files). The first issued seemed to be that the script worked correctly but inventor wasn't saving the complete list of changes properly. The script will affect every single editable part in the assembly, and every sub assembly that makes up the top level, which I think I've corrected by saving each part as it finishes processing.
I think the second problem is related to the IsModifiable property. Looking up the definition in the API Help (Inventor 2022 Help | PartDocument.IsModifiable Property | Autodesk) it says that a part may be non-modifiable if another document belonging to the file is being edited. I'm finding I can only run the script if I restart inventor and only have top level assembly open, and even then it seems to be working 99% of the time. In the current example there's over 100 assemblies but 2 of them aren't processing correctly. I'm wondering if somehow these two assemblies process as unmodifiable at the moment the script processes them, or if the error is related to the size of the assembly. If anyway has experience with this, any assistance would be appreciated. Note sure if its possible or not but if someone has an example of how to close each Bom line on completion it would be appreciated Kind Regards
Roydon Mackay