I did actually do a bit of work on this yesterday, but did not have anything ready to present yet. This can be a fairly complex task to automate correctly, without any errors. I have this code, but I have not tested it out yet, because I just haven't had time, or a good test subject, outside of our production stuff to give it a really good test on. If you test it, maybe test in on something that is not critical first, just to be safe.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oAllLeafOccs As ComponentOccurrencesEnumerator = oOccs.AllLeafOccurrences
'first suppress all parts only, in all levels
For Each oLeafOcc As ComponentOccurrence In oAllLeafOccs
If oLeafOcc.Suppressed = False Then
Try : oLeafOcc.Suppress : Catch : End Try
End If
Next
'now recustively suppress all of the remaining sub assemblies, from bottom ot top
RecurseComponents(oOccs, AddressOf ProcessComponent)
End Sub
Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
For Each oComp As ComponentOccurrence In oComps
ComponentProcess(oComp)
If oComp.Suppressed = False AndAlso _
oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
RecurseComponents(oComp.SubOccurrences, ComponentProcess)
End If
Next
End Sub
Sub ProcessComponent(oComp As ComponentOccurrence)
If IsNothing(oComp) OrElse oComp.Suppressed Then Exit Sub
If TypeOf oComp.Definition Is VirtualComponentDefinition Or _
TypeOf oComp.Definition Is WeldsComponentDefinition Or _
oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Try : oComp.Suppress(True) : Catch : End Try 'True = skip saving the source document
ElseIf oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject And _
oComp.SubOccurrences.Count > 0 Then
'if all of the sub assembly's components are not suppressed yet, to not suppress this sub assembly yet
Dim bAllOccsSuppressed As Boolean = True
For Each oSubOcc As ComponentOccurrence In oComp.SubOccurrences
If oSubOcc.Suppressed = False Then
bAllOccsSuppressed = False
Exit For
End If
Next 'oSubOcc
If bAllOccsSuppressed = True Then
Try : oComp.Suppress(True) : Catch : End Try 'True = skip saving the source document
End If
End If
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.
Wesley Crihfield

(Not an Autodesk Employee)