Get all occurrences from assembly with respect to the browser pane order

Get all occurrences from assembly with respect to the browser pane order

checkcheck_master
Advocate Advocate
828 Views
4 Replies
Message 1 of 5

Get all occurrences from assembly with respect to the browser pane order

checkcheck_master
Advocate
Advocate

When getting all parts and sub assemblies with 'all occurrences' it's hard to get the browser pane order.

Especially component patterns kept there place where they are created.

I'm trying to get the exact order as represented in the browser.

I'm able to do that by traverse the browser pane but then all browser modes are passed which takes too much time with a big assembly compared to use 'all occurrences'.

What is the best way to traverse the browser pane and get the parts and sub assemblies?

Or is there a smarter way to get this data in the order the browser pane is organised?

 

0 Likes
829 Views
4 Replies
Replies (4)
Message 2 of 5

JelteDeJong
Mentor
Mentor

I'm not sure what you think is too slow or what you have now. the following rule will find everything 10 times. on average it takes 300 milliseconds to find 1000+ occurrences. I think that is reasonable. My output in the log:

JelteDeJong_0-1640618099099.png

Public Sub Main()

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

    Dim Stopwatch As New Stopwatch()
    
	
	Dim tries = 10
	Dim totalTime As Double = 0
	Dim nodesCount = 0
	
	For i = 1 To tries
		Stopwatch.Reset()
		Stopwatch.Start()
		
		' This line does all the work
		Dim nodes = GetModelsRecursive(topNode)
		
		Stopwatch.Stop()
    	Dim ts = Stopwatch.Elapsed
		totalTime = totalTime + ts.Milliseconds
		nodesCount = nodes.Count
		Logger.Info("RunTime " & i & ": " &  ts.Milliseconds)
	Next
	
    Logger.Info("RunTime mean: " & (totalTime/tries))
	Logger.Info("Occurences found: " & nodesCount)

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

Message 3 of 5

checkcheck_master
Advocate
Advocate

Thank you Jelte for your response, very instructive.
How can I best do this in VBA?
And how can I get to the path of the file itself?
I've tried subNode.FullPath which gives the path of the node instead of the final file.

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor

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

0 Likes
Message 5 of 5

checkcheck_master
Advocate
Advocate

Thanks again Jelte for your contribution, very instructive again.
I have read your blogs, very interesting, nice site by the way with many nice topics.
It's been a desire of mine to do everything in iLogic for some time, but the comfort and habituation of VBA is exactly as you describe.
The last IV updates have brought good things to the iLogic editor, I commit myself to moving forward with iLogic.

 

Thanks for the code, it takes me a little further.
Would you like to shed some light on the following:
I am talking about a composition with about 2200 items.
When I use the browser nodes method I have quite a difference from the first time the assembly is run or the times after that.
The first time takes about 22 seconds, the subsequent times about 6 seconds.
When I apply the method based on the occurrences it stays below 1 second and the first time makes no difference compared to the following times.
Ideally I would like to use the browser node method because of the order that corresponds to that of the browser pane.
I find the 6 seconds for such a composition entirely acceptable, but it only works after the composition is run through for the second time, as said, the first time lasts 22 seconds.
Can this be improved?

0 Likes