Message 1 of 1
Faster Method for finding Components in Assembly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
here is my version that keeps track of your previous search so you can continue searching for the next one.
it is also smart enough to know when the occurrence is 1 of 1 to not continue searching for the next one.
it sets a temp file for the search if there are multiple occurrences then checks for the temp when the search is run next time.
Imports System
Imports System.Type
Imports System.Activator
Imports System.Buffer
Imports System.Runtime.InteropServices
Imports System.Runtime.GCSettings
Imports System.Windows.Forms
Imports System.Windows.Input
Imports Inventor
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
Imports Microsoft.CSharp
Imports IO = System.IO
AddReference "System.Drawing.dll"
AddReference "System.Xml.dll"
AddReference "System.Windows.Forms.dll"
Sub Main()
On Error Resume Next
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
If Err.Number <> 0 Then
MsgBox("Run in assembly only.")
Exit Sub
End If
Dim oAsmCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
SendKeys.SendWait("{ESC}")
AppActivate(ThisApplication.Caption)
Dim tempFilePath As String = IO.Path.Combine(IO.Path.GetTempPath(), "ComponentSearchState.txt")
Dim componentName As String = ""
Dim occurrencesList As New List(Of ComponentOccurrence)
Dim currentIndex As Integer = 0
Dim continueSearch As Boolean = False
' Check if temp file exists
If IO.File.Exists(tempFilePath) Then
' Read the state from the temp file
Dim lines() As String = IO.File.ReadAllLines(tempFilePath)
If lines.Length >= 2 Then
componentName = lines(0)
currentIndex = Integer.Parse(lines(1))
' Ask the user if they want to continue searching for the previous component
Dim result As MsgBoxResult
result = MsgBox("Do you want to continue searching for '" & componentName & "'?", MsgBoxStyle.YesNo, "Continue Search?")
If result = MsgBoxResult.Yes Then
' Reconstruct the list of occurrences
occurrencesList = FindAllOccurrencesByName(oAsmCompDef.Occurrences, componentName)
continueSearch = True
Else
' User wants to start a new search
IO.File.Delete(tempFilePath)
End If
Else
' If file is corrupted, start a new search
IO.File.Delete(tempFilePath)
End If
End If
' If not continuing previous search, start a new one
If Not continueSearch Then
componentName = InputBox("Enter the name of the component (you can enter a partial name):", "Component Name")
If componentName = "" Then Exit Sub
occurrencesList = FindAllOccurrencesByName(oAsmCompDef.Occurrences, componentName)
currentIndex = 0 ' Reset current index for new search
End If
If occurrencesList.Count = 0 Then
MsgBox("No occurrences found for '" & componentName & "'.", vbCritical, "")
If IO.File.Exists(tempFilePath) Then IO.File.Delete(tempFilePath)
Exit Sub
End If
' Check if current index is within the range
If currentIndex >= occurrencesList.Count Then
MsgBox("No more occurrences found for '" & componentName & "'.", vbInformation, "")
If IO.File.Exists(tempFilePath) Then IO.File.Delete(tempFilePath)
Exit Sub
End If
' Get the current occurrence
Dim oOcc As ComponentOccurrence = occurrencesList(currentIndex)
' Select the occurrence directly
Dim oSS As SelectSet = oDoc.SelectSet
oSS.Clear()
oSS.Select(oOcc)
' Optionally, zoom to the selected occurrence
' ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute()
' ThisApplication.ActiveView.Fit(True)
' Expand and collapse the Origin node if needed
ExpandCollapseOriginNode(oOcc)
' Increment the index
currentIndex += 1
' Check if there are more occurrences
If occurrencesList.Count > 1 And currentIndex < occurrencesList.Count Then
' Save the state to the temp file for the next search
Dim linesToWrite As String() = {componentName, currentIndex.ToString()}
IO.File.WriteAllLines(tempFilePath, linesToWrite)
MsgBox("Found occurrence " & currentIndex & " of " & occurrencesList.Count & " for '" & componentName & "'.", vbInformation, "Component Search")
Else
' No more occurrences, delete the temp file and inform the user
If IO.File.Exists(tempFilePath) Then IO.File.Delete(tempFilePath)
MsgBox("Found occurrence " & currentIndex & " of " & occurrencesList.Count & " for '" & componentName & "'. Search completed.", vbInformation, "Component Search")
End If
End Sub
Function FindAllOccurrencesByName(oOccurrences As ComponentOccurrences, componentName As String) As List(Of ComponentOccurrence)
Dim occurrencesList As New List(Of ComponentOccurrence)
For Each oOcc As ComponentOccurrence In oOccurrences
If oOcc.Name.Contains(componentName) Then
occurrencesList.Add(oOcc)
End If
If oOcc.SubOccurrences.Count > 0 Then
occurrencesList.AddRange(FindAllOccurrencesByName(oOcc.SubOccurrences, componentName))
End If
Next
Return occurrencesList
End Function
Sub ExpandCollapseOriginNode(oOcc As ComponentOccurrence)
Dim oDoc As Document = oOcc.Parent.Document
Dim oPane As BrowserPane = oDoc.BrowserPanes.Item("AmBrowserArrangement")
Dim oOccNode As BrowserNode = oPane.GetBrowserNodeFromObject(oOcc)
If oOccNode Is Nothing Then Exit Sub
Dim oFMNode As BrowserNode = GetBrowserNodeByLabel(oOccNode, "Folded Model")
Dim oOriginNode As BrowserNode = Nothing
If IsNothing(oFMNode) Then
oOriginNode = GetBrowserNodeByLabel(oOccNode, "Origin")
Else
oOriginNode = GetBrowserNodeByLabel(oFMNode, "Origin")
End If
If Not IsNothing(oOriginNode) Then
oOriginNode.Expanded = True
oOriginNode.Expanded = False
oOccNode.Expanded = False
End If
End Sub
Function GetBrowserNodeByLabel(oStartBNode As BrowserNode, oLabel As String) As BrowserNode
If IsNothing(oStartBNode) Then Return Nothing
' Look at this level first
For Each oNode As BrowserNode In oStartBNode.BrowserNodes
If oNode.BrowserNodeDefinition.Label = oLabel Then Return oNode
Next
' Step down into lower levels
For Each oNode As BrowserNode In oStartBNode.BrowserNodes
If oNode.BrowserNodes.Count > 0 Then
Dim resultNode As BrowserNode = GetBrowserNodeByLabel(oNode, oLabel)
If Not IsNothing(resultNode) Then Return resultNode
End If
Next
Return Nothing
End Function