Suppress all library components (VB)

Suppress all library components (VB)

shrey9GP3F
Enthusiast Enthusiast
723 Views
4 Replies
Message 1 of 5

Suppress all library components (VB)

shrey9GP3F
Enthusiast
Enthusiast

Hello,

 

I want to create a code that will suppress all the library components. The following code is not working.

 

Please help!

 

Sub Main
	On Error Resume Next
	
	If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub
	
	Dim parentDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	Call AssemblyRunner(parentDoc)
	
End Sub

Sub AssemblyRunner(aDoc As AssemblyDocument)
	On Error Resume Next
	
	If aDoc.ComponentDefinition.Object.Type = "50447872" Then
		Call CCRunner(aDoc.ComponentDefinition.Object)
	Else
		For Each oOcc As ComponentOccurrence In aDoc.ComponentDefinition.Occurrences
			If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
				Call AssemblyRunner(oOcc.Definition.Document)
			Else If oOcc.DefinitionDocumentType = kPartDocumentObject
				If oOcc.Object.Type = "50447872" Then
					Call CCRunner(oOcc.Definition.Document)
				Else
					Return
				End If
			End If
	Next
	End If
	
End Sub

Sub CCRunner(pDoc As PartDocument)
	On Error Resume Next
	
	Component.IsActive(pDoc) = False

End Sub
0 Likes
Accepted solutions (1)
724 Views
4 Replies
Replies (4)
Message 2 of 5

JelteDeJong
Mentor
Mentor

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.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 5

shrey9GP3F
Enthusiast
Enthusiast

Hello,

 

Thank you for your reply. I tried running your code. It throws this error. I want to suppress all components that are in 

C:\Users\Public\Documents\Autodesk\Inventor Electrical Library 2020

or in 

C:\Users\Public\Documents\Autodesk\Inventor Electrical Library 2020

 

shrey9GP3F_0-1626874934111.png

 

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor
Accepted solution

I don't know what the message is about. But I changed the code for your exact situation. I also tried to test it and I did not run into problems.

Sub Main()

    Dim doc As AssemblyDocument = ThisDoc.Document
    Dim list As IEnumerable(Of ComponentOccurrence)

    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 libraryFolderName1 As String = "C:\Users\Public\Documents\Autodesk\Inventor Electrical Library 2020"
		Dim libraryFolderName2 As String = "C:\Users\Public\Documents\Autodesk\Inventor Electrical Library 2020"
        Return (occ.ReferencedFileDescriptor.FullFileName.Contains(libraryFolderName1) Or
		       occ.ReferencedFileDescriptor.FullFileName.Contains(libraryFolderName2))
		
    Catch ex As Exception
        MsgBox("Could not check 'IsInLibaryFolder' 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.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 5

shrey9GP3F
Enthusiast
Enthusiast
Thank you so much.
0 Likes