Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.


How can I best do this in VBA?

Probably not the answer that you would expect but, I think you should not use VBA at all. I wrote a blog post "Is VBA in Inventor obsolete?". In that post, I explain why I think you should not use VBA. (And maybe you want to read the follow-up post "Writing and debugging iLogic rules" as well)

 


And how can I get to the path of the file itself?

Have a look at the following rule:

Public Sub Main()

    Dim doc As AssemblyDocument = ThisDoc.Document
    Dim topNode = doc.BrowserPanes.Item("Model").TopNode
    Dim nodes = GetModelsRecursive(topNode)

    For Each node As BrowserNode In nodes
		Try
			Dim occ As ComponentOccurrence = node.NativeObject
            Dim refDoc As Document = occ.Definition.Document
            Dim fileName = refDoc.FullFileName
            logger.Info(fileName)
		Catch ex As Exception
			' exceptions get thrown when a occurence is suppressed.
			logger.Info("Someting went wrong on node: " & node.FullPath)
		End Try
        
    Next

End Sub

Public Function GetModelsRecursive(node As BrowserNode) As List(Of BrowserNode)
    Dim nodes As New List(Of BrowserNode)

    For Each subNode As BrowserNode In node.BrowserNodes
        Try
            If (TypeOf subNode.NativeObject Is ComponentOccurrence) Then
                nodes.Add(subNode)
                nodes.AddRange(GetModelsRecursive(subNode))
            End If
            If (TypeOf subNode.NativeObject Is OccurrencePattern) Then
                nodes.AddRange(GetModelsRecursive(subNode))
            End If
            If (TypeOf subNode.NativeObject Is OccurrencePatternElement) Then
                nodes.AddRange(GetModelsRecursive(subNode))
            End If
        Catch ex As Exception
        End Try
    Next
    Return nodes
End Function

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com