Grabbing File Name & Path of Components from Assembly using iLogic

Grabbing File Name & Path of Components from Assembly using iLogic

felix.cortes5K3Y2
Advocate Advocate
6,264 Views
17 Replies
Message 1 of 18

Grabbing File Name & Path of Components from Assembly using iLogic

felix.cortes5K3Y2
Advocate
Advocate

Hi all,

 

I am attempting to write a piece of code that will search the assembly for any part number beginning with "19-0189-W" inside each assembly, however, I am running into issues. So far, here's what I got in my code

 

Dim oAsmDef As AssemblyDocument
doc = ThisDoc.Document


Dim oComp As ComponentOccurrence
oComps = doc.ComponentDefinition.Occurrences
Dim FileNameandPath


For Each oComp In oComps
	'FileNameandPath = oComp.????????

Next

 

For the FileNameandPath line, I do not know how to follow it up to get the path and file name. I've tried oComp.Definition.PathAndFileName but that does not work. 

 

Once I have the FileNameandPath, I would like to do an IF statement to see that if the file name begins with "19-0189-W" then to store that file name.

 

Best regards,

Felix

0 Likes
Accepted solutions (1)
6,265 Views
17 Replies
Replies (17)
Message 2 of 18

mcgyvr
Consultant
Consultant

Something to get you started..

' set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    Dim oName As String
    oName = oOccurrence.Name
    MessageBox.Show(oOccurrence.Name, "Component Name")
Next


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 3 of 18

Sergio.D.Suárez
Mentor
Mentor
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim doc As Document

Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Default Entry")

For Each doc In openDoc.AllReferencedDocuments
	
    Dim oPartNumber As String = doc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
   
    If InStr(1, oPartNumber, searchTerm) > 0 Then
	MessageBox.Show(doc.displayname, "FileName")
	MessageBox.Show(doc.fullfilename, "FullFileName")

	End If
Next

Hi, this code searches for a text in each of the reference files of the assembly file or drawing file, if it exists it will return the file name. I hope it helps to solve your problem. regards


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 4 of 18

felix.cortes5K3Y2
Advocate
Advocate

Just ran the code. Is there a way to give a count those occurrences in that If statement?

 

I would like to get a count of the number in that assembly

 

Thanks

0 Likes
Message 5 of 18

mcgyvr
Consultant
Consultant

@felix.cortes5K3Y2 wrote:

Just ran the code. Is there a way to give a count those occurrences in that If statement?

 

I would like to get a count of the number in that assembly

 

Thanks


Dim openDoc As Document
openDoc = ThisDoc.Document
Dim doc As Document

myCount = 0 'create a counter
Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Default Entry")

For Each doc In openDoc.AllReferencedDocuments
	
    Dim oPartNumber As String = doc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
   
    If InStr(1, oPartNumber, searchTerm) > 0 Then
	myCount = myCount+1 'if found increment counter by 1
	MessageBox.Show(doc.DisplayName, "FileName")
	MessageBox.Show(doc.FullFileName, "FullFileName")
	MessageBox.Show(myCount, "Count")

	End If
Next


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 6 of 18

felix.cortes5K3Y2
Advocate
Advocate

What if I wanted to count the number of occurrences for each part number?  For example, I have the following parts,

 

11002-W-001

11002-W-002

11002-W-003

 

When I hit search 11002-W with the tool you provided, I get all of these to successfully show up in my Inventor interface.

 

I have multiples of 11002-W-002 inside my assembly. How could I count the number of 11002-W-002 inside my assembly? I imagine this could be done inside the IF statement.

 

Thanks a ton Sergio,

Felix

0 Likes
Message 7 of 18

Sergio.D.Suárez
Mentor
Mentor

The rule that we were accommodating with @mcgyvr  searches all file references can be these parts within subassemblies, subassemblies
I ask, do you want to get the number of individual parts of the bill of materials? if you need this we have to make another approach through the assembly BOM


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 8 of 18

felix.cortes5K3Y2
Advocate
Advocate

Whoops! I thought it was you in the last message, didn't realize it was @mcgyvr. No disrespect intended!

 

We'll, I'd like to get a list of all the used Sub Assemblies that are being shared across each assembly. I don't think a BOM does a great job in that and it's a bit slow to load. So far, I think this method is extremely much quicker and would complete the purpose if I knew how to write a piece of string that could count all the occurrences of a specific 11002-W-002 inside the assembly, which is distributed in different assemblies.

 

Thank you both to @mcgyvr   and @Sergio.D.Suárez 

Felix

0 Likes
Message 9 of 18

mcgyvr
Consultant
Consultant

@felix.cortes5K3Y2  How about this..

 

Dim oAsmDef As AssemblyDocument
doc = ThisDoc.Document


myCount = 0 'create a counter
Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Default Entry")

Dim oComp As ComponentOccurrence
oComps = doc.ComponentDefinition.Occurrences


For Each oComp In oComps
	oName = oComp.Name
	If oName.Contains(searchTerm)
	myCount = myCount+1 'if found increment counter by 1
	MessageBox.Show(oName, "Component Name")
	End If
Next

If myCount = 0
	MessageBox.Show("None Found", "None Found Message")
	Else
		MessageBox.Show(myCount & " of " & searchTerm & " found", "Found This Many")
End If


-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 10 of 18

felix.cortes5K3Y2
Advocate
Advocate

With the new code, every time I do a search of 11002-W, it gives me none found where as before it would successfully display the component file

0 Likes
Message 11 of 18

mcgyvr
Consultant
Consultant

@felix.cortes5K3Y2 wrote:

With the new code, every time I do a search of 11002-W, it gives me none found where as before it would successfully display the component file


I made assumptions I shouldn't have..

Update coming soon to check for filename not component name..

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 12 of 18

mcgyvr
Consultant
Consultant

@felix.cortes5K3Y2  

How about this one?

Dim oAsmDef As AssemblyDocument
doc = ThisDoc.Document


myCount = 0 'create a counter
Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Default Entry")

Dim oComp As ComponentOccurrence
oComps = doc.ComponentDefinition.Occurrences


For Each oComp In oComps
	oName = oComp.ReferencedDocumentDescriptor.FullDocumentName
	If oName.Contains(searchTerm)
	myCount = myCount+1 'if found increment counter by 1
	MessageBox.Show(oName, "Component Name")
	End If
Next

If myCount = 0
	MessageBox.Show("None Found", "None Found Message")
	Else
		MessageBox.Show(myCount & " of " & searchTerm & " found", "Found This Many")
End If

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 13 of 18

felix.cortes5K3Y2
Advocate
Advocate

Hmm still not working. I noticed that the first code would tell me each occurrence inside each sub assembly.

 

This second code only searches on the top level assemblies and not inside each assembly 

 

Also, I was hoping to do a count for each individual assembly. When I do a search of 11002-W, it returns all the assemblies with a prefix of 11002-W and I would need a count of each individual assembly found in that search.

 

Thanks a ton,

Felix

0 Likes
Message 14 of 18

felix.cortes5K3Y2
Advocate
Advocate

I wrote this piece of code because I think it'll give the result I want, however, I have a problem with the strings in the line highlighted in red. I'd like to check the quantity of each filename produced by then searching that exact file name in the assembly

 

 

Dim openDoc As Document
openDoc = ThisDoc.Document
Dim doc As Document
Dim QTY As Integer
Dim FileName As String

Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Enter Prefix Entry")

For Each doc In openDoc.AllReferencedDocuments
	
    Dim oPartNumber As String = doc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
   
    If InStr(1, oPartNumber, searchTerm) > 0 Then
	FileName = doc.displayname
'	Messagebox.Show(FileName) ' SUCCESS
		For Each FileName In openDoc.AllReferencedDocuments ' THIS LINE DOES NOT WORK
			QTY = QTY + 1
			MessageBox.Show(QTY, FileName)
			Next
	
	End If
Next

 

0 Likes
Message 15 of 18

Sergio.D.Suárez
Mentor
Mentor
Accepted solution

Hi, I do not take it as a colleague offense, on the contrary, it's an honor for me to collaborate with @mcgyvr , look at your level, I've learned a lot from your contributions, it's one of my teachers in this forum!
Returning to the problem in question here I send you another code, it is probably incomplete, but it may help you to add things. It is another approach, accessing the BOM and trying to capture the quantity of the row, if it is an assembly, it will look for the assembly in "model data" and if it is only part it will search in "only parts"

 

Sub main
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim doc As Document

myCount = 0 'create a counter
Dim searchTerm As String = InputBox("Search text in Part Number", "Find text", "Default Entry")

For Each doc In openDoc.AllReferencedDocuments
	
    Dim oPartNumber As String = doc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
   
    If InStr(1, oPartNumber, searchTerm) > 0 Then
	myCount = myCount+1 'if found increment counter by 1
	MessageBox.Show(doc.DisplayName, "FileName")
	MessageBox.Show(doc.FullFileName, "FullFileName")
	MessageBox.Show(myCount, "Count")
	
	Search_Doc(doc)
	
	End If
Next
End Sub

Sub Search_Doc(doc As Document)

Dim oModelname As String = doc.DisplayName

Dim oDoc As Document
oDoc = ThisDoc.Document

		Dim oBOM As BOM
		oBOM = oDoc.ComponentDefinition.BOM
		    ' Set the structured view to 'all levels'
		oBOM.StructuredViewFirstLevelOnly = False
		    ' Make sure that the structured view is enabled.
		oBOM.StructuredViewEnabled = True
		    ' Make sure that the parts only view is enabled.
		oBOM.PartsOnlyViewEnabled = True
	
	
		If doc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
			
		Dim oBOMView As BOMView
		oBOMView = oBOM.BOMViews.Item(1)'BOM Model data

		  Call ListItems(oBOMView.BOMRows, 0, oModelname)
		  
		End If

		If doc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
			
		Dim oBOMView As BOMView
		oBOMView = oBOM.BOMViews.Item(3)'BOM only parts

		  Call ListItems(oBOMView.BOMRows, 0, oModelname)
		  
		End If

End Sub

Sub ListItems(Rows As BOMRowsEnumerator, indent As Integer,oModelname As String)
Dim oBOMRow As BOMRow


For Each oBOMRow In Rows
	Dim rDoc As Inventor.Document
	rDoc = oBOMRow.ComponentDefinitions.Item(1).Document
	Dim rDocName As String = rDoc.DisplayName 
	If rDocName <> oModelname Then
	    If Not oBOMRow.ChildRows Is Nothing Then
	        Call ListItems(oBOMRow.ChildRows, indent + 1,oModelname)
	    End If
	Else
	
		If rDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then oType = "Assembly_Quantity: "
		If rDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then oType="Part_Quantity: "
		oQty = oBOMRow.ItemQuantity
		MessageBox.Show(oType & oQty  ,"Total quantity")

	End If
Next
End Sub

 I hope it helps to solve the problem


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 16 of 18

felix.cortes5K3Y2
Advocate
Advocate

Hey thanks for getting back at me!

 

I ran the code on VBA but it gives me an error on the 3rd line saying: "Compile error, Invalid use of property". It points to the line that says openDoc = ThisDoc.Document

 

I really want to try your code out but it won't run.

 

 

I was trying to add some lines to one of the first codes you provided and I think it should theoretically work but I cant find the syntax to do it. On the line highlighted on red, I get a quantity of the complete number of files inside the assembly and not the quantity of that file name inside the assembly.

 

What I need to do is find that syntax that would search each individual item in the browser instead of searching each file inside the assembly. This is what I written:

 

Dim openDoc As Document = ThisDoc.Document
Dim doc As Document
Dim innerdoc As Document
Dim qty As Integer
Dim FileName As String
Dim InnerFileName As String
Dim SearchTerm As String = InputBox("Search text in Part Number", "Find text", "Enter Prefix Entry")



For Each doc In openDoc.AllReferencedDocuments
	qty = 0
	Dim oPartNumber As String = doc.PropertySets.item("Design Tracking Properties").Item("Part Number").Value
	
	If InStr(1, oPartNumber, SearchTerm) > 0 Then
		FileName = doc.displayname

'		Dim oPartDescription as String = doc.PropertySets.item("Design Tracking Properties").Item("Description").Value
		
		For Each innerDoc In openDoc.AllDocuments 'RETURNS NUMBER OF PARTS INSIDE ASSEMBLY, NOT QUANTITY OF INDIVIDUAL FILE
			InnerFileName = innerDoc.displayname
			If FileName = InnerFileName
				qty = qty + 1
				MessageBox.Show("Test")
			End If
		Next
		MessageBox.Show(qty, FileName)
		'save to column
		
	End If
Next

 

Thanks a lot for your help. I really want to try out the VBA code you've written.

Felix

0 Likes
Message 17 of 18

Sergio.D.Suárez
Mentor
Mentor

what happens is that the code is for rule ilogic, make it run as an ilogic rule and if it is what you need we will see to modify it for VBA, greetings


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 18 of 18

felix.cortes5K3Y2
Advocate
Advocate

Hey Sergio,

 

Okay I ran it and it works great. Really appreciate the help.

 

Thank you,

Felix