@WCrihfield that together with an additional little tweak appears so far to have done the trick.
I added in an extra If Then statement within the Function Statement that only adds the Part Number iProperty if the oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject therefore not adding any Sub Assembly files to the list. I'll give it a good tryout tomorrow but hopefully, that's sorted it.
Thanks again
Mike
Class ThisRule
Dim oDocs As List(Of Document) 'shared with all routines
Dim oPNList As List(Of String) 'shared with all routines
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oViews As DrawingViews = oDDoc.ActiveSheet.DrawingViews
If oViews.Count = 0 Then Exit Sub 'may want a feedback message or Log entry here.
Dim oBV As DrawingView
For Each oView As DrawingView In oViews
If oView.ViewType = DrawingViewTypeEnum.kStandardDrawingViewType Then
oBV = oView : Exit For
End If
Next 'oView
If IsNothing(oBV) Then Exit Sub 'may want a feedback message or Log entry here.
Dim oADoc As AssemblyDocument = oBV.ReferencedDocumentDescriptor.ReferencedDocument
Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
oDocs = New List(Of Document) 'to initiate it
oPNList = New List(Of String) 'to initiate it
RecursivelyProcessComponents(oOccs)
oMyPN = InputListBox("List Count " & oPNList.Count, oPNList, "", "Part Numbers")
End Sub
Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
For Each oComp As ComponentOccurrence In oComps
Dim ProcessFurther As Boolean = ProcessComponent(oComp) 'run our other routine
If ProcessFurther = True Then
If oComp.SubOccurrences.Count > 0 Then
RecursivelyProcessComponents(oComp.SubOccurrences)
End If
End If
Next
End Sub
Function ProcessComponent(oComp As ComponentOccurrence) As Boolean
If IsNothing(oComp) OrElse oComp.Suppressed Then Return False
'filter by BOMStructure values
'Note: BOMStructure of component can be different than its referenced document
If oComp.BOMStructure <> BOMStructureEnum.kNormalBOMStructure And _
oComp.BOMStructure <> BOMStructureEnum.kInseparableBOMStructure Then
Return False
End If
If oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
If oComp.Definition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Return False
ElseIf oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
If oComp.Definition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Return False
End If
Dim oDoc As Document = oComp.Definition.Document
If oDocs.Contains(oDoc) Then Return False 'do not process the same document again
Dim SOP = StandardObjectFactory.Create(oDoc) 'Inventor 2021 or later only
If oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim oPN As String = SOP.iProperties.Value("Project", "Part Number")
oPNList.Add(oPN)
oDocs.Add(oDoc)
End If
Return True 'return True, so it will process any sub occurrances of this component
End Function
End Class
Autodesk Certified Professional