Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Owner2229
in reply to: timothy_berg

Alright, little correction in the code. As for the function, I've tested it and it will get the occurence count top-down from the top assembly, so it doesn't matter if the document is in the top assy or in the deepest one, it'll get them all.

Orange is what I've changed.

 

Sub Main
'Get the active document

Dim oDoc As Document = ThisApplication.ActiveDocument
'Check if it's a drawing
If oDoc.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
'Get the drawing sheets
Dim oSheets As Sheets = oDoc.Sheets
'Get the top/sub doc from 1./2. sheet, 1. view
Dim oTopDoc As Document = GetViewDoc(oSheets, 1, 1)
Dim oSubDoc As Document = GetViewDoc(oSheets, 2, 1)
If oTopDoc Is Nothing Or oSubDoc Is Nothing Then Exit Sub
Dim oTopCD As AssemblyComponentDefinition = oTopDoc.ComponentDefinition Dim oPartEnum As ComponentOccurrencesEnumerator = oTopCD.Occurrences.AllReferencedOccurrences(oSubDoc) Dim QTY As Integer = oPartEnum.Count
MsgBox(QTY)
End Sub
Private Function GetViewDoc(oSheets As Sheets, iSheet As Integer, iView As Integer) As Document
Dim oDoc As Document = Nothing
Try
Dim oSheet As DrawingSheet = oSheets(iSheet)
Dim oView As DrawingView = oSheet.DrawingViews(iView)
oDoc = oView.ReferencedFile.ReferencedDocument
Catch
End Try
Return oDoc
End Function

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods