I have made this vba macro code, which does the same as my c# addin is doing. It takes the top node and recursively runs through the tree and finds the selected items. The macro works fine, but the c# addin does not. When I do a activeDocument.Update() before traversing the tree, it works fine. It seems like this is a bug in Inventor, not updating the structure.
Option Explicit
Private Sub GetSelected()
GetSelectionFromBrowserNodes ThisDocument.BrowserPanes.ActivePane.TopNode
End Sub
Private Sub GetSelectionFromBrowserNodes(node As BrowserNode)
On Error GoTo Error_GetSelectionFromBrowserNodes
If node.Selected Then
Dim componentDef As ComponentDefinition
'On Error Resume Next
If TypeName(node.NativeObject) = "ComponentDefinition" Then
Set componentDef = node.NativeObject
Else
Set componentDef = Nothing
End If
On Error GoTo Error_GetSelectionFromBrowserNodes
If componentDef Is Nothing Then
Dim occ As ComponentOccurrence
'On Error Resume Next
If TypeName(node.NativeObject) = "ComponentOccurrence" Then
Set occ = node.NativeObject
Else
Set occ = Nothing
End If
On Error GoTo Error_GetSelectionFromBrowserNodes
If Not occ Is Nothing Then
Set componentDef = occ.Definition
End If
End If
If InStr(node.FullPath, ":Crop") > 0 Then
Debug.Print node.FullPath
Stop
End If
Dim doc As Document
If Not componentDef Is Nothing Then
Set doc = componentDef.Document
End If
If Not doc Is Nothing Then
Dim docId As String
docId = doc.FullFileName
Debug.Print "--> " & docId
End If
End If
If node.Expanded Then
Dim subnode As BrowserNode
For Each subnode In node.BrowserNodes
GetSelectionFromBrowserNodes subnode
Next
End If
Exit Sub
Error_GetSelectionFromBrowserNodes:
Debug.Print Err.Description
Stop
Exit Sub
Resume
End Sub