Suppress folder in assembly

<?xml encoding=”UTF-8″>By Adam Nagy

Inside an assembly you can create a folder in the browser and place multiple components in it. You can then control visibility and suppression of all those elements through the folder.

When you right-click on a folder you get a context menu with the “Suppress” menu item in it. If you click it Inventor will go through all the elements in the folder and suppress them. We can do the same thing through the API: iterate through the folder items and suppress them.
Note: the BrowserFolder object has no Suppress functionality in the API that could make the below code much simpler

In case of suppressing an occurrence pattern we have to go though each element, iterate through the occurrences they reference and suppress those, as shown in this blog post:
http://adndevblog.typepad.com/manufacturing/2012/05/how-can-i-suppress-the-circular-pattern-component-in-an-assembly-file.html 

Here is the VBA code that suppresses the contents of the “MyFolder” browser folder:

Sub SuppressFolderTest()
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oPane As BrowserPane
Set oPane = oDoc.BrowserPanes("Model")
Dim oTopNode As BrowserNode
Set oTopNode = oPane.TopNode
Dim oFolder As BrowserFolder
Set oFolder = oTopNode.BrowserFolders.Item("MyFolder")
Call SuppressFolder(oFolder)
End Sub
' Recursive function as there could be subfolders too
Sub SuppressFolder(oFolder As BrowserFolder)
Dim oItem As BrowserNode
For Each oItem In oFolder.BrowserNode.BrowserNodes
Dim oObj As Object
Set oObj = oItem.NativeObject
If TypeOf oObj Is BrowserFolder Then
Call SuppressFolder(oObj)
ElseIf TypeOf oObj Is ComponentOccurrence Then
Dim oOcc As ComponentOccurrence
Set oOcc = oObj
If Not oOcc.Suppressed Then Call oOcc.Suppress
ElseIf TypeOf oObj Is OccurrencePattern Then
Dim oPatt As OccurrencePattern
Set oPatt = oObj
Dim oElem As OccurrencePatternElement
For Each oElem In oPatt.OccurrencePatternElements
For Each oOcc In oElem.Occurrences
If Not oOcc.Suppressed Then Call oOcc.Suppress
Next
Next
End If
Next
End Sub

Here is the same in iLogic:

Sub Main()
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oPane As BrowserPane
oPane = oDoc.BrowserPanes("Model")
Dim oTopNode As BrowserNode
oTopNode = oPane.TopNode
Dim oFolder As BrowserFolder
oFolder = oTopNode.BrowserFolders.Item("MyFolder")
Call SuppressFolder(oFolder)
End Sub
' Recursive function as there could be subfolders too
Sub SuppressFolder(oFolder As BrowserFolder)
Dim oItem As BrowserNode
For Each oItem In oFolder.BrowserNode.BrowserNodes
Dim oObj As Object
oObj = oItem.NativeObject
If TypeOf oObj Is BrowserFolder Then
Call SuppressFolder(oObj)
ElseIf TypeOf oObj Is ComponentOccurrence Then
Dim oOcc As ComponentOccurrence
oOcc = oObj
If Not oOcc.Suppressed Then Call oOcc.Suppress
ElseIf TypeOf oObj Is OccurrencePattern Then
Dim oPatt As OccurrencePattern
oPatt = oObj
Dim oElem As OccurrencePatternElement
For Each oElem In oPatt.OccurrencePatternElements
For Each oOcc In oElem.Occurrences
If Not oOcc.Suppressed Then Call oOcc.Suppress
Next
Next
End If
Next
End Sub

And here is the code in action. As you can see we get exactly the same result as if you clicked the “Suppress” context menu item of the folder in the UI, and the “Suppress” item will be ticked: 

Suppressfolder

 


Comments

2 responses to “Suppress folder in assembly”

  1. DRichter Avatar
    DRichter

    Sub Main() is throwing me…is this the only way to do it? I have an if/then else if statement and trying to shoehorn the code in.
    If Brush = False Then
    Sub Main()
    Suppress folder and component pattern within the folder
    ElseIf Brush = True Then
    Sub
    Unsuppress folder and component pattern within the folder.
    End If
    Wish I knew more about coding…probably something I’m missing

  2. Have a look at this:
    http://knowledge.autodesk.com/support/inventor-products/learn-explore/caas/CloudHelp/cloudhelp/2014/ENU/Inventor/files/GUID-8DF6F761-1634-4D26-B13A-58AF275FD6F8-htm.html
    To paraphrase it, if you have multiple functions (Sub or Function) then the first one needs to be Sub Main() – you don’t call this function from anywhere it’s called by iLogic automatically, but you can call other functions from it. Also when calling a function you should not prefix it with “Sub “, just use the name of the function.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading