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

Hey, getting the qty from the top assembly is quite easy in fact (if you have the top assy and the part/sub assy references. e.g. from a view).

 

So, let's say the top assy document is the first view from the first sheet from the drawing and the searched document is in the first view from the second sheet. The code would then look like this:

 

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.ReferencedDocumentDescriptor
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