I'm not entirely sure what you mean by: "library components". Are these the content centre parts or the files you put in the library folder that you defined in your project file? Any way this code can check both cases. In the code, I commented on how to activate them. (Disclaimer I have no "library components" here on my test pc so I did not test this myself....)
Also, I used some functions that I explained in more detail in my blog post "Universal occurrence search function".
Sub Main()
Dim doc As AssemblyDocument = ThisDoc.Document
Dim list As IEnumerable(Of ComponentOccurrence)
list = findAllWhere(doc, AddressOf IsContentMember)
' uncomment next line if you want to search by foldername
'list = findAllWhere(doc, AddressOf IsInLibaryFolder)
For Each occ As ComponentOccurrence In list
occ.Suppress()
Next
End Sub
Public Function IsInLibaryFolder(occ As ComponentOccurrence) As Boolean
Try
' Change this to your library folder
Dim libraryFolderName As String = "C:\Users\Public\Documents\Autodesk\Inventor Electrical Library 2020"
Return occ.ReferencedFileDescriptor.FullFileName.Contains(libraryFolderName)
Catch ex As Exception
MsgBox("Could not check 'IsInLibaryFolder' for occurence " & occ._DisplayName)
Return False
End Try
End Function
Public Function IsContentMember(occ As ComponentOccurrence) As Boolean
Try
Dim def As ComponentDefinition = occ.Definition
Dim doc As Document = def.Document
If doc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Return False
Dim pDoc As PartDocument = doc
Return pDoc.ComponentDefinition.IsContentMember
Catch ex As Exception
MsgBox("Could not check 'IsContentMember' for occurence " & occ._DisplayName)
Return False
End Try
End Function
Public Function findAllWhere(doc As AssemblyDocument,
predicate As Func(Of ComponentOccurrence, Boolean)) As IEnumerable(Of ComponentOccurrence)
Dim list As IEnumerable(Of ComponentOccurrence) = doc.ComponentDefinition.
Occurrences.Cast(Of ComponentOccurrence).
Where(predicate)
Dim assemblyList As IEnumerable(Of ComponentOccurrence) = doc.ComponentDefinition.
Occurrences.Cast(Of ComponentOccurrence).
Where(Function(o) o.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject)
For Each assOcc As ComponentOccurrence In assemblyList
If TypeOf assOcc.Definition Is VirtualComponentDefinition Then Continue For
Dim assDoc As AssemblyDocument = assOcc.ReferencedDocumentDescriptor.ReferencedDocument
Dim listToAdd As IEnumerable(Of ComponentOccurrence) = findAllWhere(assDoc, predicate)
list = list.Concat(listToAdd)
Next
Return list
End Function
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog: hjalte.nl - github.com