VB.Net Treeview / Depth First Search (DFS)

VB.Net Treeview / Depth First Search (DFS)

ssyXENBT
Contributor Contributor
747 Views
8 Replies
Message 1 of 9

VB.Net Treeview / Depth First Search (DFS)

ssyXENBT
Contributor
Contributor

Hi,

I have programmed a treeView. In this tree structure are all components, which are also in the assembly. (see photo)

TreeView.png

After opening the form, the first level is added to the treeView. 

Only after expanding other Nodes, the sub-elements are searched to save time. 

However, this still takes a bit of time. Is there no way to export the tree structure immediately from Inventor,

without checking everything with the algorithm? 

 

Thanks for any tips!

 

 

0 Likes
Accepted solutions (1)
748 Views
8 Replies
Replies (8)
Message 2 of 9

bradeneuropeArthur
Mentor
Mentor

How have you programmed it?

  • Vb.net
  • VBA
  • Ilogic

Can you share the code so that we can show you the modification needed for filling in the TreeView fully expanded directly?

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 9

ssyXENBT
Contributor
Contributor

Hi,  

 

after opening the form and when a new node gets expand , this Function is executed.

but i have seen tools where the whole tree was loaded in a few seconds

 

The code is a bit dirty and without comments, but I hope you can do something with it. 

Thank you.

    Private Sub DFS(Occurrences As ComponentOccurrences, Level As Integer, boolRoot As Boolean)
        Cursor = Cursors.WaitCursor
        Dim oOcc As ComponentOccurrence
        Dim childs As ArrayList = New ArrayList()
        For Each oOcc In Occurrences
            Dim oPartPropsetUser As Inventor.PropertySet = oOcc.Definition.Document.PropertySets.Item(4)
            Dim newNode As TreeNode

            If Level = 1 Then
                rootNode = rootNodes.Item(0)
            ElseIf boolRoot = True Then
                Dim oParentOccName = oOcc.Definition.Document.ComponentDefinition.ParentOccurrence.Name
                For Each element As TreeNode In rootNodes
                    If element.Text = oParentOccName Then
                        rootNode = element
                    End If
                Next
            End If
            If Not childs.Contains(oOcc.Definition.Document.DatabaseRevisionId) Then
                childs.Add(oOcc.Definition.Document.DatabaseRevisionId)
                Try
                    If oPartPropsetUser("PF_DOC_WF").Value <> "freigegeben (CAD Admin)" Then
                        newNode = rootNode.Nodes.Add(oOcc.Name)
                    Else
                        Continue For
                    End If
                Catch ex As Exception
                    newNode = rootNode.Nodes.Add(oOcc.Name)
                End Try
#Region "Datainfo"
                '{756AB4A0-49C8-623D-3998-22AEAF1623AC} 175105
                '              : DatabaseRevisionId : "{237B47AD-434B-6733-A854-DE8CA7D46B90}" : String
                '{756AB4A0-49C8-623D-3998-22AEAF1623AC} 174937
                '              : DatabaseRevisionId : "{CB71B317-48AC-048E-7C73-70953DCD56A1}" : String
#End Region
                newNode.Tag = oOcc

                If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    newNode.ImageIndex = 0
                    newNode.Nodes.Add("-")
                ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
                    If TypeOf oOcc.Definition.Document.ComponentDefinition Is SheetMetalComponentDefinition Then
                        Try
                            newNode.ImageIndex = 2
                        Catch ex As Exception
                        End Try
                    Else
                        Try
                            newNode.ImageIndex = 1
                        Catch ex As Exception
                        End Try
                    End If

                End If
            End If
        Next
        If Level = 1 Then
            rootNode.Expand()
        End If
        Cursor = Cursors.Default
    End Sub

 

 

0 Likes
Message 4 of 9

bradeneuropeArthur
Mentor
Mentor
Can you also share that code that calls this code?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 9

ssyXENBT
Contributor
Contributor

Sure, 

 

Form Shown: 

 

    Private Sub asmbDesignation_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        MainForm.Close()
        abzugswertTreeView = Me.Width - 15

        Dim oAsmDoc As AssemblyDocument
        oAsmDoc = ivExe.ActiveDocument
        rootNode = TreeView1.Nodes.Add(oAsmDoc.DisplayName)
        rootNode.Tag = oAsmDoc
        rootNodes.Add(rootNode)
        TreeView1.ImageList = ImageList1

        DFS(oAsmDoc.ComponentDefinition.Occurrences, 1, True)
    End Sub

 

 

AfterExpand: 

 

    Private Sub TreeView1_AfterExpand(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterExpand
        If e.Node.Tag IsNot oActiveDoc Then
            If Not vistited.Contains(e.Node) Then
                e.Node.Nodes.Clear()
                vistited.Add(e.Node)
                Dim oAsmDoc As ComponentOccurrence
                oAsmDoc = e.Node.Tag
                rootNode = e.Node
                Call DFS(oAsmDoc.Definition.Document.ComponentDefinition.Occurrences, 2, False)
            End If
        End If
    End Sub

 

 

DFS:

 

    Private Sub DFS(Occurrences As ComponentOccurrences, Level As Integer, boolRoot As Boolean)
        Cursor = Cursors.WaitCursor
        Dim oOcc As ComponentOccurrence
        Dim childs As ArrayList = New ArrayList()
        For Each oOcc In Occurrences
            Dim oPartPropsetUser As Inventor.PropertySet = oOcc.Definition.Document.PropertySets.Item(4)
            Dim newNode As TreeNode

            If Level = 1 Then
                rootNode = rootNodes.Item(0)
            ElseIf boolRoot = True Then
                Dim oParentOccName = oOcc.Definition.Document.ComponentDefinition.ParentOccurrence.Name
                For Each element As TreeNode In rootNodes
                    If element.Text = oParentOccName Then
                        rootNode = element
                    End If
                Next
            End If
            If Not childs.Contains(oOcc.Definition.Document.DatabaseRevisionId) Then
                childs.Add(oOcc.Definition.Document.DatabaseRevisionId)
                Try
                    If oPartPropsetUser("PF_DOC_WF").Value <> "freigegeben (CAD Admin)" Then
                        newNode = rootNode.Nodes.Add(oOcc.Name)
                    Else
                        Continue For
                    End If
                Catch ex As Exception
                    newNode = rootNode.Nodes.Add(oOcc.Name)
                End Try
#Region "Datainfo"
                '{756AB4A0-49C8-623D-3998-22AEAF1623AC} 175105
                '              : DatabaseRevisionId : "{237B47AD-434B-6733-A854-DE8CA7D46B90}" : String
                '{756AB4A0-49C8-623D-3998-22AEAF1623AC} 174937
                '              : DatabaseRevisionId : "{CB71B317-48AC-048E-7C73-70953DCD56A1}" : String
#End Region
                newNode.Tag = oOcc

                If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    newNode.ImageIndex = 0
                    newNode.Nodes.Add("-")
                ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
                    If TypeOf oOcc.Definition.Document.ComponentDefinition Is SheetMetalComponentDefinition Then
                        Try
                            newNode.ImageIndex = 2
                        Catch ex As Exception
                        End Try
                    Else
                        Try
                            newNode.ImageIndex = 1
                        Catch ex As Exception
                        End Try
                    End If

                End If
            End If
        Next
        If Level = 1 Then
            rootNode.Expand()
        End If
        Cursor = Cursors.Default
    End Sub

 

 

 

 

0 Likes
Message 6 of 9

bradeneuropeArthur
Mentor
Mentor

What I understand is that you need a treeview filled like the assembly structure, correct.

This needs to be done quicker.

Do you want it to be displayed directly expanded or not.

The main reason for your question is the performance, correct?

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 9

ssyXENBT
Contributor
Contributor

yes, so that the whole structure can be loaded directly without waiting 3min. 

 

0 Likes
Message 8 of 9

bradeneuropeArthur
Mentor
Mentor
Accepted solution

This is one of the options doing this:

 

 

TreeView1.BeginUpdate()
        For Each c In b.ComponentDefinition.Occurrences.AllLeafOccurrences

            TreeView1.Nodes.Add(c.Name)

        Next

        TreeView1.EndUpdate()

 

In special the BeginUpdate and the EndUpdate will help a lot!

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 9 of 9

ssyXENBT
Contributor
Contributor

Hi, 

the speed has become a bit faster thanks! 

0 Likes