OK. I just created this iLogic rule for you to try. I tested this myself first, on a folder with about 115 items in it (some folders, and some other types of files), and I have it set to only search within the same folder where the drawing's model document is saved. My test returned 4 part numbers of assemblies that were referencing the model in the active drawing, then it put a drawing note at the origin point of the active drawing sheet, as expected. Give this a try, and see how it works for you. If other changes may need to be made, we may be able to deal with that later.
Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oMDoc As Document = GetDrawingModel(oDDoc)
If IsNothing(oMDoc) Then
MsgBox("No 'Model' document found in Drawing. Exiting rule.", vbCritical, "iLogic")
Exit Sub
End If
Dim oPNsRefdBy As List(Of String) = GetWhereUsed(oMDoc)
If oPNsRefdBy.Count = 0 Then
MsgBox("No assemblies found with reference to the 'Model' document.", vbInformation, "iLogic")
Exit Sub
End If
Dim oReport As String = "USED IN: "
For Each oPNRefdBy In oPNsRefdBy
oReport = oReport & vbCrLf & oPNRefdBy
Next
oPos = ThisApplication.TransientGeometry.CreatePoint2d(0,0)
Dim oNote As GeneralNote = oDDoc.ActiveSheet.DrawingNotes.GeneralNotes.AddFitted(oPos, oReport)
End Sub
Function GetDrawingModel(oDrawing As DrawingDocument) As Document
If oDrawing.AllReferencedDocuments.Count = 0 Then Return Nothing
For Each oRefDoc As Document In oDrawing.AllReferencedDocuments
If oRefDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Return oRefDoc
Exit For
End If
Next
'just in case there were referenced documents,
'but none were a Part or Assembly (very rare)
Return Nothing
End Function
Function GetWhereUsed(oDoc As Document) As List(Of String)
Dim oPNs As New List(Of String)
'get the folder where the input document is currently saved
oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
'search within that folder for assemblies that reference the input document
Dim oFileNames() As String = System.IO.Directory.GetFiles(oPath, "*.iam", System.IO.SearchOption.TopDirectoryOnly)
'if no assemblies were found, were done here
If oFileNames.Length = 0 Then
'MsgBox("No assemblies found.",,"")
Return oFileNames.ToList 'returns an empty List
End If
For Each oFileName In oFileNames
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oOptions.Add("ExpressModeBehavior", "OpenExpress")
oOptions.Add("SkipAllUnresolvedFiles", True)
Dim oADoc As AssemblyDocument = ThisApplication.Documents.OpenWithOptions(oFileName, oOptions, False)
If oADoc.AllReferencedDocuments.Count > 0 Then
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
If oRefDoc.FullFileName = oDoc.FullFileName Then
Dim oPN As String = String.Empty 'just a cautionary reset
oPN = oADoc.PropertySets.Item(3).Item("Part Number").Value
If String.IsNullOrEmpty(oPN) Then oPN = ""
oPNs.Add(oPN)
Exit For 'exit the loop of oRefDoc's
End If
Next
End If
oADoc.ReleaseReference
Next
ThisApplication.Documents.CloseAll(True) 'close all 'unreferenced' documents only
Return oPNs
End Function
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)