Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Collapse Children Macro in Inventor 23

e_frissell
Advocate
Advocate

Collapse Children Macro in Inventor 23

e_frissell
Advocate
Advocate

Hi guys, curious if anyone's had this issue or if it's been solved.  Used to use this macro that I found on the forums that I believe was called Collapse Children and it worked really well until we updated to Inventor 23 and it hasn't worked since.  Curious if anyone might know why or if there's a solution for it that can be implemented

 

Public Sub CollapseDesignTree()

    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveEditDocument
        Dim oTopNode As BrowserNode
        Set oTopNode = oDoc.BrowserPanes.ActivePane.TopNode
        Dim oNode As BrowserNode
            'If the node is visible and expanded, collapse it
            If oNode.Visible = True And oNode.Expanded = True Then
                oNode.Expanded = False
            End If
        Next

End Sub

 

0 Likes
Reply
Accepted solutions (1)
940 Views
18 Replies
Replies (18)

mikek3QMMC
Participant
Participant

Just copying it into a quick rule, I got a couple of errors involving the "set" callouts.  It appears those have been sunset.  Also, it had an issue with "next" not having any kind of For loop callout, so that also may be an issue, but try copying this in place of the code and seeing what pops up:

 

 

 
Public Sub CollapseDesignTree()	
Dim oDoc As Document = ThisApplication.ActiveEditDocument Dim oTopNode As BrowserNode = oDoc.BrowserPanes.ActivePane.TopNode 'Dim oNode As BrowserNode 'If the node is visible and expanded, collapse it If (oTopNode.Visible = True And oTopNode.Expanded = True) Then oTopNode.Expanded = False End If
'next
End Sub
 

I commented out the "next" portion in case it was part of a larger section.  Simply delete the ' and it will work.

 

Also, I don't know why you specifically need this.  If you hover over the section of the tree you want to collapse, right clicking will now yield both expand all and collapse all children options.  That may negate the need of this macro.

0 Likes

e_frissell
Advocate
Advocate

Interesting, I think I've seen the right click and collapse all children but in my opinion there's too much clicking around in this program - my favorite thing about other programs is that they will utilize a left-hand action to perform the same action that Inventor tends to take several clicks to.  Just a quick example in Solidworks by hitting ctrl+q you can rebuild a document, then ctrl+s is save, so there is no movement required from the right hand which is required for you to update a model in Inventor (anything requiring the mouse is slower than the keyboard).  So I wrote a macro which updates and saves a model via ctrl+s and it auto-exits your sketches too.  Some don't understand it and say it only saves a second.  It probably saves 2~3 seconds, especially if a sketch is involved, but it saves 2.5 seconds probably 100 times a day which is 4 minutes, or 20 minutes a week...  Couple that with several other macros and you're saving 30 minutes a day.

 

Back to the macro... the macro was written for the VBA environment rather than iLogic.  I know VBA was semi-killed off by Inventor however that macro worked in 2019, 2020 and 2021 but stopped working when the update to 2023 was had.  The macro is currently failing at Set oTopNode = oDoc.BrowserPanes.ActivePane.TopNode stating the "Method 'TopNode' of object 'BrowserPaneObject' failed.  It seems the TopNode method was tied to something previously that was removed

0 Likes

mikek3QMMC
Participant
Participant

I downloaded the 2023 version of the VBA add-on, and I'm still getting two issue copying the exact code you've provided.

1: Next is still looking for the beginning of a For loop, so I commented that out

2: im getting a pause at a different line at the IF statement.  Topnode looks like it's still a valid object in VBA for Active pane (which you should be able to confirm by erasing the .TopNode and typing the period.  This will let you see the list of possible objects/options for ActivePane)

 

I get this error:

mikek3QMMC_0-1680785665109.png

 

0 Likes

e_frissell
Advocate
Advocate

Interesting, my error was before that at setting oTopNode... I agree it's missing a for each, and it compiled previously without it but starting giving me the same error but the macro ran fine for years without it.  I get the impression something changed in the program and the method was never updated

 

e_frissell_0-1680787590462.png

 

0 Likes

mikek3QMMC
Participant
Participant

I was able to get this working.  Let me know if this is or isn't what you were looking for. I'm not sure what broke between then and now, but I am able to bind this to a key input, and it will collapse the entire tree fully.  If that does not work, you might need to download the 2023 or 2024 versions of the VBA add-on, but that might break other things you have going on.

 

Public Sub Collapse_Tree()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveEditDocument
Dim oTopNode As BrowserNode
Set oTopNode = oDoc.BrowserPanes.ActivePane.TopNode
If oTopNode.Expanded = True Then
oTopNode.Expanded = False
End If
End Sub

0 Likes

e_frissell
Advocate
Advocate

Thanks Mike,

 

I do have the 2023 version of the add on but it still fails with the method topnode of object browserpaneobject failed

 

No clue what's broken

0 Likes

WCrihfield
Mentor
Mentor

Just curious about one little detail...  I noticed that you are using 'ActiveEditDocument' instead of 'ActiveDocument'.  Are you planning on using this code to effect the referenced document of an assembly component that you are in 'Edit Mode' of within an assembly?  That 'ActiveEditDocument' term is basically made for that scenario, but will work in other scenarios too.  If you are editing a component within an assembly when you run this macro, that may (or may not) be the problem.  I am not sure this will work on a document that is not 'visibly' open (so you can actually see its browser tree on your screen at the time).  I know you can access things in another document's browser tree without it being visibly open, but I am not sure if you can effect it in this way, without it actually being the 'active' document on your screen at the time.  Have you tested that theory?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

A.Acheson
Mentor
Mentor

Here is a handy function sourced from here to get the top browser node for each document. I am assuming your looking to close the whole tree or is it just when editing a document in place in the assembly? I believe I had issue with the active pane method before and always use the specific browser pane object. 

Public Function GetDocumentBrowserPane(oDoc As Document) As BrowserPane

    Dim oModelBP As BrowserPane
    
    'Get the Model BrowserPane for Part
    If (TypeOf oDoc Is PartDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("PmDefault")
    End If
    
    'Get the Model BrowserPane for Assembly
    If (TypeOf oDoc Is AssemblyDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("AmBrowserArrangement")
    End If
    
    'Get the Model BrowserPane for Drawing
    If (TypeOf oDoc Is DrawingDocument) Then
        Set oModelBP = oDoc.BrowserPanes.Item("DlHierarchy")
    End If
    
    Set GetDocumentBrowserPane = oModelBP

End Function

 

If this solved a problem, please click (accept) as solution.ā€Œā€Œā€Œā€Œ
Or if this helped you, please, click (like)ā€Œā€Œ
Regards
Alan

e_frissell
Advocate
Advocate

@WCrihfield I was strictly using this code to replicate the mouse function collapse all children except I tie it to a keyboard input.  The person who originally created this used ActiveEditDocument and unfortunately I'm not sure why they would have chosen that over ActiveDocument

0 Likes

e_frissell
Advocate
Advocate

Thanks @A.Acheson I'll have to write this in when I get a few minutes to try it but it looks promising

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Hi @e_frissell.  I knew your post seemed familiar somehow but couldn't recall why.  I had used some code pretty much identical to the one you first posted years ago when I was first getting started into this stuff, but my code was in iLogic instead of VBA.  At the time I used a simple iLogic rule to investigate what those internal names were in other document types, because when I first started responding on these forums, I found out that there were folks on here from many other countries, and that the 'English' names for some stuff I was working with did not always work some of those other folks who had other language versions of Inventor.  Those internal names were stable across all languages, unlike the 'local' regular names of some objects.  I did not use a Function like the nice one posted above by @A.Acheson, but came to similar conclusion.  I had two different versions of the iLogic code.  One version looped through all top level nodes under the top node, and made sure they were not expanded.  The other version selected that top node, then executed a command, similar to the one that gets executed when you use the right-click method manually.  Below are both versions of code, but in VBA instead of iLogic, since your original code above was in VBA.

Version 1:

Sub CollapseModelTreeNodes()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    Dim oTopNode As BrowserNode
    If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("AmBrowserArrangement").TopNode
    ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("DlHierarchy").TopNode
    ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("PmDefault").TopNode
    End If
    If oTopNode Is Nothing Then Exit Sub
    'If oTopNode.Expanded Then oTopNode.Expanded = False 'can not undo manually
    If oTopNode.BrowserNodes.Count > 0 Then
        Dim oNode As BrowserNode
        For Each oNode In oTopNode.BrowserNodes
            If oNode.Visible And oNode.Expanded Then oNode.Expanded = False
        Next
    End If
End Sub

Version 2:

Sub CollapseModelTreeNodes2()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    Dim oTopNode As BrowserNode
    If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("AmBrowserArrangement").TopNode
    ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("DlHierarchy").TopNode
    ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
        Set oTopNode = oDoc.BrowserPanes.Item("PmDefault").TopNode
    End If
    If oTopNode Is Nothing Then Exit Sub
    oDoc.SelectSet.Clear
    oDoc.SelectSet.Select (oTopNode.NativeObject)
    ThisApplication.CommandManager.ControlDefinitions.Item("AppBrowserCollapseChildrenCmd").Execute
End Sub

 Since a similar amount of code is used for both versions, I greatly prefer the version that does not call the command to execute though.  But since my installation of Inventor is for English language, I tend to simply use the name "Model" in my working code, to keep the code shorter and simpler, and that works just fine for me, without needing to know the internal names.  Plus, in my iLogic versions, I tend to use 'ThisDoc.Document' instead of 'ThisApplication.ActiveDocument', because it tends to work better in more scenarios...just a personal preference.  I have also been avoiding the use of VBA as much as possible, in favor of iLogic the past couple years, for security reasons.  Below is a link to an article about that.

https://www.autodesk.com/support/technical/article/caas/sfdcarticles/sfdcarticles/Microsoft-Visual-B... 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

e_frissell
Advocate
Advocate

@A.Acheson unfortunately the function doesn't work - I really think that something happened between Inventor 2021 and 2023 because the original macro worked well (I can't take credit for writing it, I just happened to find it) and after making a few updates to let the macro run, the collapsed design tree is not what we're used to.  Example: the first photo is the design tree prior to running the macro, then the second photo is the design tree after.


e_frissell_0-1681235756269.png

e_frissell_1-1681235929884.png

 

 

0 Likes

e_frissell
Advocate
Advocate

For reference here's the code in case I made any mistakes

Public Sub CollapseDesignTree()

Dim oDoc As Document

Set oDoc = ThisApplication.ActiveDocument
Call GetDocumentBrowserPane(oDoc)

Dim oTopNode As BrowserNode
Set oTopNode = oDoc.BrowserPanes.ActivePane.TopNode
If oTopNode.Expanded = True Then
oTopNode.Expanded = False
End If

 '   Dim oDoc As Document
 ''   Set oDoc = ThisApplication.ActiveEditDocument
 '       Dim oTopNode As BrowserNode
 '       Set oTopNode = oDoc.BrowserPanes.ActivePane.TopNode
 '       Dim oNode As BrowserNode
 '       'If the node is visible and expanded, collapse it
 '       If oTopNode.Expanded = True Then
 '           oNode.Expanded = False
 '       End If

End Sub

Public Function GetDocumentBrowserPane(oDoc As Document) As BrowserPane

    Dim oModelBP As BrowserPane
    
    'Get the Model BrowserPane for Part
    If (oDoc.DocumentType = kPartDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("PmDefault")
    End If
    
    'Get the Model BrowserPane for Assembly
    If (oDoc.DocumentType = kAssemblyDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("AmBrowserArrangement")
    End If
    
    'Get the Model BrowserPane for Drawing
    If (oDoc.DocumentType = kDrawingDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("DlHierarchy")
    End If
    
    Set GetDocumentBrowserPane = oModelBP

End Function

 

0 Likes

e_frissell
Advocate
Advocate

@WCrihfield Thanks!  Unfortunately I can't take credit for this code unfortunately, but you helped a ton on a previous account!  I got a brand spanking new username as I went back to a previous employer recently.  Either way kind of strange to hear that a lot of the API methods vary based on the language, I don't think I would have ever known that otherwise

WCrihfield
Mentor
Mentor

Hi @e_frissell.  That last version you posted was not using the function correctly.  When you call a Sub routine to run in VBA, you just use the keyword 'Call' followed by the name of the Sub routine, but when calling a Function routine to run, those are designed to 'return' something to the 'main' routine that called it to run, so you need to prepare a variable to hold what the function is supposed to return.  Below is a slightly edited version of what you just posted, that captures what the function returns, then uses that result when getting the 'TopNode', instead of going ahead and using the 'ActivePane' which you were doing before all of this.

Public Sub CollapseDesignTree()

    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oModelPane As BrowserPane
    Set oModelPane = GetDocumentBrowserPane(oDoc)
    
    Dim oTopNode As BrowserNode
    Set oTopNode = oModelPane.TopNode
    
    If oTopNode.Expanded = True Then
        oTopNode.Expanded = False
    End If
    
'    Dim oNode As BrowserNode
'    For Each oNode In oTopNode.BrowserNodes
'        If oNode.Visible = True And oNode.Expanded = True Then
'            oNode.Expanded = False
'        End If
'    Next
End Sub

Public Function GetDocumentBrowserPane(oDoc As Document) As BrowserPane
    Dim oModelBP As BrowserPane
    'Get the Model BrowserPane for Part
    If (oDoc.DocumentType = kPartDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("PmDefault")
    End If
    'Get the Model BrowserPane for Assembly
    If (oDoc.DocumentType = kAssemblyDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("AmBrowserArrangement")
    End If
    'Get the Model BrowserPane for Drawing
    If (oDoc.DocumentType = kDrawingDocumentObject) Then
        Set oModelBP = oDoc.BrowserPanes.Item("DlHierarchy")
    End If
    Set GetDocumentBrowserPane = oModelBP
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

e_frissell
Advocate
Advocate

Ah, you're right, thanks for the correction!  Been getting a lot better at VBA but it's still a work in progress sometimes!  I just ran your script and get the same result where it's as if it collapses everything into the top node and only shows the top level assembly in the design tree.  Naturally if you (or anyone) has 2023 and is able to run this and get the same (or a different) result I'd be very interested in finding it out.

0 Likes

WCrihfield
Mentor
Mentor

I also had multiple previous Autodesk accounts/profiles, but I wasn't really as active in the online stuff with some of those previous accounts as a have been with this one.  VBA is an older coding system that was centered around Microsoft's Office products, while the VB.NET system (also used by iLogic) is newer and more dynamic.  In VBA you usually have to use the 'Call' keyword to call routines to run, but there are actually two different ways to do it too (use 'Call' keyword then enclose input variables within the () brackets ... or ... don't use the 'Call' keyword, then do not enclose input variables within the () brackets), but that is not necessary in vb.net (always enclose input variables within () brackets).  In VBA you have to use the 'Set' keyword on reference type variables, but not on variables for String, Integer, Double, and such data types.  The 'Set' keyword is not needed in vb.net.  There are also several object types that are not readily available in VBA, that are available in vb.net (and the other way around).  One of the main attractions for using VBA with Inventor instead of iLogic is that the VBA editor has a much more advanced user interface and tools available to work with though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

I personally do not like to collapse everything into just that one top node like that, because it is difficult to undo that manually, if needed.  I personally prefer to not collapse that 'TopNode' itself, and just collapse all of its 'children' (BrowserNodes under it), because that is much more natural looking, and can easily be undone manually if I want to.  That is what the commented out part would have done, by itself, if the top node collapse part was commented out instead.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes