search all occurrences for a part file name using ilogic

search all occurrences for a part file name using ilogic

jaypieper
Enthusiast Enthusiast
2,234 Views
7 Replies
Message 1 of 8

search all occurrences for a part file name using ilogic

jaypieper
Enthusiast
Enthusiast

I am trying to figure out how to search thru all occurrences for a part filename called "Glass" and have it go thru the whole tree. I was able to make it work before and i thought that the code was going thru all occurrences in the tree but it is not. I had to move things around in my assembly and it has pushed the "glass" part file lower in the tree and my script cannot find it now. I have attached 2 pictures with the code and the path of my assembly while it was working and i have attached another picture showing the updated path of the "Glass file location. Any help would be appreciated.

 

Thanks

 

 

 

oDoc = ThisDoc.Document
Dim doc = ThisDoc.Document : If doc.DocumentType = kAssemblyDocumentObject Then 'Check we are in an IAM
Else : Return :End If

Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence

OccCounter = 0
For Each oCompOcc In oDef.Occurrences
GetOccName = oCompOcc.Name
OccCounter = OccCounter + 1
SubOccCount = 0

Try
	'Top Level Occurrences
If GetOccName.Contains("GLASS") Then
	MsgBox(GetOccName)
Else If GetOccName.Contains("Glass") Then
	MsgBox(GetOccName)
End If

'Sub level Occurrences
   For Each oSubOcc As ComponentOccurrence In oCompOcc.SubOccurrences
	   SubOccCount = SubOccCount + 1
	   If oSubOcc.Name.Contains("GLASS") Then
		   If GL1 = "" Then
			   GL1 = oSubOcc.Name

 

0 Likes
Accepted solutions (1)
2,235 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @jaypieper.  Here is a similar iLogic rule that will iterate down through every component in every level of an assembly and find the first component which represents a document named "Glass".  At the end, it just lets you know if it was found or not, and does not take into account if there may be multiple.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	RecurseComponents(oOccs, "Glass")
	If IsNothing(oFoundComp) Then
		MsgBox("The Document named 'Glass' was Not found.", vbInformation, "")
	Else
		MsgBox("The Document named 'Glass' WAS found.", vbInformation, "")
	End If
End Sub

Dim oFoundComp As ComponentOccurrence

Sub RecurseComponents(oComps As ComponentOccurrences, oName As String)
	If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
	If oName = "" Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		If oComp.Suppressed Then Continue For
		If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
		Dim oCompDoc As Document = oComp.ReferencedDocumentDescriptor.ReferencedDocument
		Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oCompDoc.FullFileName)
		If oFileName = oName Then
			oFoundComp = oComp
			Exit Sub
		End If
		If oComp.SubOccurrences.Count > 0 Then
			RecurseComponents(oComp.SubOccurrences, oName)
		End If
	Next
	
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

jaypieper
Enthusiast
Enthusiast

.

0 Likes
Message 4 of 8

jaypieper
Enthusiast
Enthusiast
Thanks for answering so quickly. I may have miss spoke on the filename. The occurrence file name contains Glass in the name of the file and it will have to find every multiple file similar to how i have it written in my code. I used the word glass in the name so i can easily find the correct file. I will try to modify your code to see if i can make it work.
0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor
Accepted solution

I must have miss-understood your request then.  It is a little confusing because you are using terms like 'file name', 'filename', 'part file', 'file location', so I assumed your intent was to check actual 'file' names, not just assembly component names, because assembly components can easily be renamed, and often are renamed for stability.  So, my code actually digs down into the actual document object that each component represents, then gets its real file name, to check it for the 'Glass' text.  I changed my main variable to a List(Of ComponentOccurrence) object, so it can collect every one it finds, which is representing a document/file whose name now 'contains' (instead of 😃 the text "Glass".  Then, when it is done processing, you not only have a count of how many it found, but a collection that you can use, in case you may need to do something else with them.

You can give this modified code  a try if you want.  Hopefully this version will suit your needs better.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	oFoundComps = New List(Of ComponentOccurrence) 'initiates the List object
	RecurseComponents(oOccs, "Glass")
	If oFoundComps.Count = 0 Then
		MsgBox("No components were found representing documents with 'Glass' in their name.", vbInformation, "")
	Else
		MsgBox(oFoundComps.Count & " components were found represenging documents with 'Glass' in there name.", vbInformation, "")
	End If
	
End Sub

Dim oFoundComps As List(Of ComponentOccurrence) 'declares the variable (shared)

Sub RecurseComponents(oComps As ComponentOccurrences, oName As String)
	If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
	If oName = "" Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		If oComp.Suppressed Then Continue For
		If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
		Dim oCompDoc As Document = oComp.ReferencedDocumentDescriptor.ReferencedDocument
		Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oCompDoc.FullFileName)
		If oFileName.Contains(oName) Then
			oFoundComps.Add(oComp)
			Exit Sub
		End If
		If oComp.SubOccurrences.Count > 0 Then
			RecurseComponents(oComp.SubOccurrences, oName)
		End If
	Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 8

jaypieper
Enthusiast
Enthusiast
Thanks for all your help i was able to get it working. There is one caveat, I have to re-name the browser node because inventor adds a :1 to the end of the file name in the browser. IF i go thru and remove the :1 in the browser it works. I will try to see if i can revise the filename to browser nodes.
0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor

I don't know if you are aware of this or not, but there is a built-in tool in the user interface for renaming browser nodes.  On the Assemble tab > Productivity tab (may not be visible), there is a drop-down button for a series of tools.  One of them is simply called 'Rename Browser Nodes'.  A lot of people use that to 'stabilize' their component names, for various reasons.  When stabilized, the name will stay the same, even if the component gets replaced by another component which represents a different document.  This is necessary is some automation solutions.  I have no idea if it will help you with your situation though.  Just passing on a tip.

WCrihfield_0-1663958916100.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 8

jaypieper
Enthusiast
Enthusiast

Untitled.png

Thank you for the info and yes i am aware of the browser node rename. Because the browser node has a :1 at the end of it, the program errors saying that it cannot find UTest-Unit G112 Glass1. I will have to rename the node or try to concatenate the :1 to the end of the variable in the program. Inventor uses the browser node name to write the code to get the parameters. 

0 Likes