Ilogic - Add Standard Virtual Parts From an Excel File

Ilogic - Add Standard Virtual Parts From an Excel File

brendan.henderson
Advisor Advisor
2,804 Views
24 Replies
Message 1 of 25

Ilogic - Add Standard Virtual Parts From an Excel File

brendan.henderson
Advisor
Advisor

I'm having a crack at using code supplied by @Curtis_Waguespack but it keeps producing an error, shown below.

 

I have changed the code to reflect the location and name of the excel file, and commented out some of the iProp rows I don't need.  

 

Also attached is the excel file and the customised code. Really appreciate anybody having a look at this for me.

 

cw.jpg

Brendan Henderson
CAD Manager


New Blog | Old Blog | Google+ | Twitter


Inventor 2016 PDSU Build 236, Release 2016.2.2, Vault Professional 2016 Update 1, Win 7 64 bit


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Accepted solutions (3)
2,805 Views
24 Replies
Replies (24)
Message 2 of 25

dean.morrison
Advocate
Advocate

Brendan,

 

I tried it - saved the xls in c:\ - changed the path in the rule.

 

pasted the rule in a blank assembly.

 

everything worked as expected.

 

tried it also in one of my own assemblies - no issues there either.

 

Dean.

Message 3 of 25

brendan.henderson
Advisor
Advisor

Hi Dean.

 

Well I'm stumped. No doubt it will be something simple I have screwed up 🙂

 

Video here of my attempt at the same :-

 

  1. Saved xls file in c:\
  2. Opened the rule text file and see the changed path
  3. Copied the rule text
  4. Started a new assy
  5. Saved the assy
  6. Created a new rule
  7. Pasted in the code
  8. Code ran, selected line, entered qty, error message

Can you see what I have done wrong?

Brendan Henderson
CAD Manager


New Blog | Old Blog | Google+ | Twitter


Inventor 2016 PDSU Build 236, Release 2016.2.2, Vault Professional 2016 Update 1, Win 7 64 bit


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Message 4 of 25

dean.morrison
Advocate
Advocate

Brendan,

 

No idea why it didn't work for you.

 

Pretty much exactly what i did..

 

Very strange.

 

I tried to video me doing the same - never used screencast before..

 

here is a url..

 

http://autode.sk/2bbWx0d

 

Dean.

 

Message 5 of 25

Curtis_Waguespack
Consultant
Consultant

Hi brendan.henderson,

 

I took a quick look at this using your *.xls file at the path you provided, and your modified illogic code, and it worked as expected. Nothing's coming to mind at the moment, as to why you're seeing this issue, but if I think of something I'll post back.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 6 of 25

brendan.henderson
Advisor
Advisor

I can't explain it. Started the PC up today and the code now works. Must have been a glitch that a restart fixed.

Brendan Henderson
CAD Manager


New Blog | Old Blog | Google+ | Twitter


Inventor 2016 PDSU Build 236, Release 2016.2.2, Vault Professional 2016 Update 1, Win 7 64 bit


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Message 7 of 25

Greatwhitenorth
Collaborator
Collaborator

Hi @Curtis_Waguespack,

I just tried your iLogic - Add Standard Virtual Parts From an Excel File program, and it works great for adding one virtual part and quantity at a time.  However, I'm looking for a way to bring a whole Excel sheet of virtual parts in to an assembly in one shot.

Are you able to help with this request?

Thanks in advance.

Bob

__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
0 Likes
Message 8 of 25

Curtis_Waguespack
Consultant
Consultant

HI @Greatwhitenorth 

 

Have a look at these links, I think they should do what you're after:

 

http://inventortrenches.blogspot.com/2013/07/ilogic-add-standard-virtual-parts-from_29.html

https://synergiscadblog.com/2015/09/21/adding-virtual-parts-from-an-excel-file-using-ilogic/

 

 

Edit:

oops, I see what you're asking now... give me a bit and i''' post a version that runs through each line in the excel file and adds them...

 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 9 of 25

Curtis_Waguespack
Consultant
Consultant

Hi @Greatwhitenorth 

 

Give this version a try, it uses the QTY specified in the Excel file.

 

Note that as written the virtual components are all deleted first, then put back in based on what is in the Excel file.

 

So if there is a qty of zero in the Excel file the virtual part is removed if it existed. And if the virtual part is not in the Excel file but is in the assembly, the virtual part will be removed and not added back.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

Capture.JPG

 

Capture.JPG

 

 

 

 

'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
 
Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix

Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues("C:\Temp\Virtual Part List.xlsx", "Sheet1", "A2", "A200")
Dim sVirtPart As String

'get info from the XLS file
For MyRow = 2 To 200 'index row 2 through 200
	
    iQTY = GoExcel.CellValue("A" & MyRow)
    oProp1 = GoExcel.CellValue("B" & MyRow)	
    oProp2 = GoExcel.CellValue("C" & MyRow)
    oProp3 = GoExcel.CellValue("D" & MyRow)
    oProp4 = GoExcel.CellValue("E" & MyRow)	
	sVirtPart = oProp1 'defines the virtual part name
	
	'Iterate through all of the occurrences in the assembly
	Dim asmOcc As ComponentOccurrence
	For Each asmOcc  In oAsmCompDef.Occurrences
		'get name of occurence only (sees only everything left of the colon)
		Dim oOcc As Object
		oOcc = asmOcc.Name.Split(":")(0)
		'look at only virtual components
		If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
			'compare name selected from list to the
			'existing virtual parts
			If oOcc = sVirtPart Then
				'delete existing virtual parts if name matches
				asmOcc.delete
			End If
		End If
	Next



	'create first instance of the virtual part
	Dim virtOcc As ComponentOccurrence
	If  iQTY >= 1 Then
		virtOcc = occs.AddVirtual(sVirtPart, identity)
		
		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1
		Catch 'catch error when oProp1 = nothing
		End Try
			
		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp2
		Catch 'catch error when oProp2 = nothing
		End Try

		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Vendor") = oProp3
		Catch 'catch error when oProp4 = nothing
		End Try
		
		Try
		iProperties.Value(sVirtPart & ":1", "Summary", "Comments") = oProp4
		Catch 'catch error when oProp5 = nothing
		End Try
	End If
	

	'add next instance starting at instance2 (if applicable)
	Dim index As Integer
	index = 2
	Do While index <= iQTY
		occs.AddByComponentDefinition(virtOcc.Definition, identity)
		index += 1
	Loop

Next

 

 

EESignature

Message 10 of 25

Greatwhitenorth
Collaborator
Collaborator

That was fast!  Thanks @Curtis_Waguespack, I'll give this a go and report back.

Bob

__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
0 Likes
Message 11 of 25

Greatwhitenorth
Collaborator
Collaborator

Hi @Curtis_Waguespack,

I modified this rule by adding some lines copied from one of your website programs, so it would allow selectively picking an Excel file.  This is what I did:

Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix

'Browse for the Excel file
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg.DialogTitle = "Select a Project Information Excel File"
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
'exit if file not selected
Return
ElseIf oFileDlg.FileName <> "" Then
myXLS  = oFileDlg.FileName
End If


Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues(myXLS, "Sheet1", "A2", "A200")
Dim sVirtPart As String

Unfortunately, it throws an error regarding line 24 "On Error Resume Next".

compile error.png

Do you know how to solve this error?

Please advise.

Thanks,

Bob

__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
Message 12 of 25

Curtis_Waguespack
Consultant
Consultant
Accepted solution

 

 

Hi @Greatwhitenorth 

 

Give this a try... note I put in some status bar lines to let the user know the rule was doing work... on my system at least the reading of the XLS file is kinda slow and it made it seem as if it was freezing up, so the status bar helps communicate that things are happening, but it's not really needed for the rule to work.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

'[ Browse for the Excel file
oMsg = "Select a Project Information Excel File"

'update the status bar 
ThisApplication.StatusBarText = oMsg

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg.DialogTitle = oMsg
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True	

Try	
	oFileDlg.ShowOpen()
Catch
	'catch error when no file is selected
	Return 'exit rule
End Try

If Err.Number <> 0 Then	
	MessageBox.Show("A problem occured when getting the Excel file.", "iLogic",MessageBoxButtons.OK,MessageBoxIcon.Error)	
	Return 'exit if file not selected
	
ElseIf oFileDlg.FileName <> "" Then
	myXLS  = oFileDlg.FileName
	
	'update the status bar with the Excel file name
	ThisApplication.StatusBarText = "...reading info from " & myXLS
End If
']

Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues(myXLS, "Sheet1", "A2", "A200")

'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
 
Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix

Dim sVirtPart As String

'get info from the XLS file
For MyRow = 2 To 200 'index row 2 through 200	
	
    iQTY = GoExcel.CellValue("A" & MyRow)
    oProp1 = GoExcel.CellValue("B" & MyRow)	
    oProp2 = GoExcel.CellValue("C" & MyRow)
    oProp3 = GoExcel.CellValue("D" & MyRow)
    oProp4 = GoExcel.CellValue("E" & MyRow)	
	sVirtPart = oProp1 'defines the virtual part name
	
	'update the status bar with the name
	ThisApplication.StatusBarText = sVirtPart
	
	'Iterate through all of the occurrences in the assembly
	Dim asmOcc As ComponentOccurrence
	For Each asmOcc  In oAsmCompDef.Occurrences
		'get name of occurence only (sees only everything left of the colon)
		Dim oOcc As Object
		oOcc = asmOcc.Name.Split(":")(0)
		'look at only virtual components
		If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
			'compare name selected from list to the
			'existing virtual parts
			If oOcc = sVirtPart Then
				'delete existing virtual parts if name matches
				asmOcc.delete
			End If
		End If
	Next



	'create first instance of the virtual part
	Dim virtOcc As ComponentOccurrence
	If  iQTY >= 1 Then
		virtOcc = occs.AddVirtual(sVirtPart, identity)
		
		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1
		Catch 'catch error when oProp1 = nothing
		End Try
			
		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp2
		Catch 'catch error when oProp2 = nothing
		End Try

		Try
		iProperties.Value(sVirtPart & ":1", "Project", "Vendor") = oProp3
		Catch 'catch error when oProp4 = nothing
		End Try
		
		Try
		iProperties.Value(sVirtPart & ":1", "Summary", "Comments") = oProp4
		Catch 'catch error when oProp5 = nothing
		End Try
	End If
	

	'add next instance starting at instance2 (if applicable)
	Dim index As Integer
	index = 2
	Do While index <= iQTY
		occs.AddByComponentDefinition(virtOcc.Definition, identity)
		index += 1
	Loop

Next

'update the status bar 
ThisApplication.StatusBarText = "Virtual components added!"

EESignature

Message 13 of 25

Greatwhitenorth
Collaborator
Collaborator

Thanks @Curtis_Waguespack!  I updated the rule with your changes, and it works great!

Cheers!

Bob

__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
Message 14 of 25

Greatwhitenorth
Collaborator
Collaborator

Hi again @Curtis_Waguespack,

I have a need of your expertise.  I would like this code to read the Excel rows until it hits an empty row.  Currently I have it set to 500, but there are times it is much less or could be more.  Would you please modify it so it checks if the row is empty?

Thanks in advance.

Bob

'Virtual Parts Excel Import
'based on a Curtis Waguespack rule (2019)

'[check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from an assembly file.", "iLogic")
Exit Sub
End If
']

'[information
MessageBox.Show("This rule will read an Excel file of Virtual Parts and add them to the assembly." _
& vbLf & vbLf & "Use '$\Styles\MyCompany\iLogic\External Rules\Virtual Parts Excel Import Template.xlsx' as a template." _
& vbLf & vbLf & "Wait for Completed message.  It may take some time to finish.", "Virtual Parts Import", _
MessageBoxButtons.OK)
']

'[browse for the Excel file
oMsg = "Select a Virtual Parts Excel File"

'update the status bar
ThisApplication.StatusBarText = oMsg

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg.DialogTitle = oMsg
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True	

Try	
	oFileDlg.ShowOpen()
Catch
	'catch error when no file is selected
	Return 'exit rule
End Try

If Err.Number <> 0 Then	
	MessageBox.Show("A problem occured when getting the Excel file.", "iLogic",MessageBoxButtons.OK,MessageBoxIcon.Error)	
	Return 'exit if file not selected
	
ElseIf oFileDlg.FileName <> "" Then
	myXLS  = oFileDlg.FileName
	
	'update the status bar with the Excel file name
	ThisApplication.StatusBarText = "...reading info from " & myXLS
End If
']

Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues(myXLS, "Sheet1", "A2", "A500")

'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument

'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
 
Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix

Dim sVirtPart As String

'[get info from the XLS file
For MyRow = 2 To 500 'index row 2 through 500
	
    iQTY = GoExcel.CellValue("A" & MyRow) 'QTY
 	oProp1 = GoExcel.CellValue("B" & MyRow) 'Part Number
    oProp2 = GoExcel.CellValue("C" & MyRow) 'UOM
    oProp3 = GoExcel.CellValue("D" & MyRow) 'Description
    oProp4 = GoExcel.CellValue("E" & MyRow) 'Item Description
    oProp5 = GoExcel.CellValue("F" & MyRow) 'Has Drawing
    oProp6 = GoExcel.CellValue("G" & MyRow) 'Product Code
    oProp7 = GoExcel.CellValue("H" & MyRow) 'Spare Part
    oProp8 = GoExcel.CellValue("I" & MyRow) 'Critical Spare
    oProp9 = GoExcel.CellValue("J" & MyRow) 'Manufacturer
    oProp10 = GoExcel.CellValue("K" & MyRow) 'Source Code
	oProp11 = GoExcel.CellValue("L" & MyRow) 'MyCompany Supplied
	sVirtPart = oProp1 'defines the virtual part name
	']
	
	'[Iterate through all of the occurrences in the assembly
	Dim asmOcc As ComponentOccurrence
	For Each asmOcc  In oAsmCompDef.Occurrences
		'get name of occurence only (sees only everything left of the colon)
		Dim oOcc As Object
		oOcc = asmOcc.Name.Split(":")(0)
		'look at only virtual components
		If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
			'compare name selected from list to the
			'existing virtual parts
			If oOcc = sVirtPart Then
				'delete existing virtual parts if name matches
				asmOcc.Delete
			End If
		End If
	Next
	']

	'[create first instance of the virtual part
	Dim virtOcc As ComponentOccurrence
	If  iQTY >= 1 Then
		virtOcc = occs.AddVirtual(sVirtPart, identity)
		
		    Try
		    iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp1
                Catch 'catch error when oProp1 = nothing
                End Try
            Try
            iProperties.Value(sVirtPart & ":1", "Custom", "UOM") = oProp2
                Catch 'catch error when oProp2 = nothing
                End Try
            Try
            iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp3
                Catch 'catch error when oProp3 = nothing
                End Try
            Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Item Description") = oProp4
                Catch 'catch error when oProp4 = nothing
                End Try
            Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Has Drawing") = oProp5
                Catch 'catch error when oProp5 = nothing
                End Try
 			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Product Code") = oProp6
                Catch 'catch error when oProp6 = nothing
                End Try
			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Spare Part") = oProp7
                Catch 'catch error when oProp7 = nothing
                End Try
			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Critical Spare") = oProp8
                Catch 'catch error when oProp8 = nothing
                End Try
			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Manufacturer") = oProp9
                Catch 'catch error when oProp9 = nothing
                End Try
			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "Source Code") = oProp10
                Catch 'catch error when oProp10 = nothing
                End Try
			Try
            iProperties.Value(sVirtPart & ":1", "Custom", "MyCompany Supplied") = oProp11
                Catch 'catch error when oProp11 = nothing
                End Try
	End If
	']
	
	'[add next instance starting at instance2 (if applicable)
	Dim index As Integer
	index = 2
	Do While index <= iQTY
		occs.AddByComponentDefinition(virtOcc.Definition, identity)
		index += 1
	Loop
	']

Next

'update the status bar 
ThisApplication.StatusBarText = "Virtual components added!"

MessageBox.Show("Finished adding Virtual Parts to assembly!", "Completed")
__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
0 Likes
Message 15 of 25

Curtis_Waguespack
Consultant
Consultant

Hi @Greatwhitenorth 

 

There is an example at this link that shows how to find the last used row ( i.e. first blank row ) 

https://forums.autodesk.com/t5/inventor-customization/ilogic-excel-next-available-line/m-p/6026696#M...

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 16 of 25

Greatwhitenorth
Collaborator
Collaborator

Hi @Curtis_Waguespack.  Thanks for the link.  I incorporated that code in to my program, and edited a 150 row Excel sheet by inserting a blank row at the 50 row mark. It appears the program is not honouring the empty line and runs for the 2000 rows.

Would you please look at what I've done wrong in this snippet?

Thank-you.

Bob

'[ get info from the XLS file
'Define Range
oRowStart = 2
oRowEnd = 2000
Dim count As Double
For count = oRowStart To oRowEnd
	' If it's blank count it
	If String.IsNullOrEmpty(GoExcel.CellValue("A" & count)) Then 
		i = i + 1
	End If
Next
' Next empty row is max rows minus blank rows plus one
oEmptyRow = oRowEnd - i + 1	

'index row 2 through next empty row
For MyRow = 2 To oEmptyRow 
	
    iQTY = GoExcel.CellValue("A" & MyRow) 'QTY
 	oProp1 = GoExcel.CellValue("B" & MyRow) 'Part Number
    oProp2 = GoExcel.CellValue("C" & MyRow) 'UOM
    oProp3 = GoExcel.CellValue("D" & MyRow) 'Description
    oProp4 = GoExcel.CellValue("E" & MyRow) 'Item Description
    oProp5 = GoExcel.CellValue("F" & MyRow) 'Has Drawing
    oProp6 = GoExcel.CellValue("G" & MyRow) 'Product Code
    oProp7 = GoExcel.CellValue("H" & MyRow) 'Spare Part
    oProp8 = GoExcel.CellValue("I" & MyRow) 'Critical Spare
    oProp9 = GoExcel.CellValue("J" & MyRow) 'Manufacturer
    oProp10 = GoExcel.CellValue("K" & MyRow) 'Source Code
    oProp11 = GoExcel.CellValue("L" & MyRow) 'MyCompany Supplied
    sVirtPart = oProp1 'defines the virtual part name
']
__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
0 Likes
Message 17 of 25

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @Greatwhitenorth ,

 

I think that empty row check solution was used to find the last used row, but would not find a row in the middle that was empty.

 

This example will return the first row with an empty A1 cell

 

 

myXLS = "C:\TEMP\Test.xlsx"
GoExcel.Open(myXLS, "Sheet1")

oRowStart = 2 'first row to check 
oRowEnd = 2000 'last row to check
For i = oRowStart To oRowEnd
	If String.IsNullOrEmpty(GoExcel.CellValue("A" & i)) Then 
		oEmptyRow = i
		Exit For
	End If
Next

MessageBox.Show("Fist Empty Row : " & oEmptyRow, "iLogic")

 

 

This one finds the first row with an empty cell for the first 6 cells in the that row

 

myXLS = "C:\TEMP\Test.xlsx"
GoExcel.Open(myXLS, "Sheet1")

oRowStart = 2 'first row to check 
oRowEnd = 2000 'last row to check
For i = oRowStart To oRowEnd
	oString = GoExcel.CellValue("A" & i) _
		& GoExcel.CellValue("B" & i) _
		& GoExcel.CellValue("C" & i) _
		& GoExcel.CellValue("D" & i) _
		& GoExcel.CellValue("E" & i) _
		& GoExcel.CellValue("F" & i)
	If String.IsNullOrEmpty(oString) Then
		'found a row with 6 empty cells so stop looking
		oEmptyRow = i
		Exit For
	End If
Next

MessageBox.Show("Fist Empty Row : " & oEmptyRow, "iLogic")

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 18 of 25

Greatwhitenorth
Collaborator
Collaborator

Thanks @Curtis_Waguespack!  It now stops at the first blank cell in column 'A'.

__________________________________________________________
Product Design & Manufacturing Collection 2023 | Vault Professional 2023
Dell Precision 7670 | Intel i7-12850HX - 2100 Mhz - 64GB
nVIDIA RTX A3000 12GB | Windows 10/64 Pro
Message 19 of 25

Shag_Bore
Advocate
Advocate

I realize this thread is quite old but I would like to implement it to my assemblies for adding virtual components. 

 

I am working locally and the .xlsx file is local and I get this error. 

 

Running on Inventor 2016 Pro and Office 365, Windows 10 x64

 

virtual_bom_error.PNG

 

Any ideas for the error? I used the working ilogic code that you provided

 

'[ Browse for the Excel file
oMsg = "Select a Project Information Excel File"

'update the status bar 
ThisApplication.StatusBarText = oMsg

Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
oFileDlg.DialogTitle = oMsg
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True    

Try    
    oFileDlg.ShowOpen()
Catch
    'catch error when no file is selected
    Return 'exit rule
End Try

If Err.Number <> 0 Then    
    MessageBox.Show("A problem occured when getting the Excel file.", "iLogic",MessageBoxButtons.OK,MessageBoxIcon.Error)    
    Return 'exit if file not selected
    
ElseIf oFileDlg.FileName <> "" Then
    myXLS  = oFileDlg.FileName
    
    'update the status bar with the Excel file name
    ThisApplication.StatusBarText = "...reading info from " & myXLS
End If
']

Dim MyArrayList As New ArrayList
MyArrayList = GoExcel.CellValues(myXLS, "Sheet1", "A2", "A200")

'define assembly
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
'define assembly Component Definition
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim occs As ComponentOccurrences
occs = asmDoc.ComponentDefinition.Occurrences
 
Dim identity As Matrix
identity = ThisApplication.TransientGeometry.CreateMatrix

Dim sVirtPart As String

'get info from the XLS file
For MyRow = 2 To 200 'index row 2 through 200    
    
    iQTY = GoExcel.CellValue("A" & MyRow)
    oProp1 = GoExcel.CellValue("B" & MyRow)    
    oProp2 = GoExcel.CellValue("C" & MyRow)
    oProp3 = GoExcel.CellValue("D" & MyRow)
    oProp4 = GoExcel.CellValue("E" & MyRow)    
    sVirtPart = oProp1 'defines the virtual part name
    
    'update the status bar with the name
    ThisApplication.StatusBarText = sVirtPart
    
    'Iterate through all of the occurrences in the assembly
    Dim asmOcc As ComponentOccurrence
    For Each asmOcc  In oAsmCompDef.Occurrences
        'get name of occurence only (sees only everything left of the colon)
        Dim oOcc As Object
        oOcc = asmOcc.Name.Split(":")(0)
        'look at only virtual components
        If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
            'compare name selected from list to the
            'existing virtual parts
            If oOcc = sVirtPart Then
                'delete existing virtual parts if name matches
                asmOcc.delete
            End If
        End If
    Next



    'create first instance of the virtual part
    Dim virtOcc As ComponentOccurrence
    If  iQTY >= 1 Then
        virtOcc = occs.AddVirtual(sVirtPart, identity)
        
        Try
        iProperties.Value(sVirtPart & ":1", "Project", "Description") = oProp1
        Catch 'catch error when oProp1 = nothing
        End Try
            
        Try
        iProperties.Value(sVirtPart & ":1", "Project", "Part Number") = oProp2
        Catch 'catch error when oProp2 = nothing
        End Try

        Try
        iProperties.Value(sVirtPart & ":1", "Project", "Vendor") = oProp3
        Catch 'catch error when oProp4 = nothing
        End Try
        
        Try
        iProperties.Value(sVirtPart & ":1", "Summary", "Comments") = oProp4
        Catch 'catch error when oProp5 = nothing
        End Try
    End If
    

    'add next instance starting at instance2 (if applicable)
    Dim index As Integer
    index = 2
    Do While index <= iQTY
        occs.AddByComponentDefinition(virtOcc.Definition, identity)
        index += 1
    Loop

Next

'update the status bar 
ThisApplication.StatusBarText = "Virtual components added!"
Sean Farr
Product Designer at Teksign Inc.
Inventor 2016 SP1
Dell Precision 3660
i7-12700 @ 2.40GHz-4.90GHz
32GB DDR5 4400MHz RAM
NIVDIA RTX A2000 6GB
0 Likes
Message 20 of 25

e_frissell
Advocate
Advocate

@Curtis_Waguespack thanks for posting this script - however, if you don't mind, was wondering if you might be able to help with something I'm running into with this.

 

I'm trying to re-write this script such that the file selection is part of a try-catch where the try is initially searching in the folder path of the assembly for an xlsx file called "Virtual Parts" - if that file is not found, it then prompts the file dialog asking where to be pointed to.  The hard part is I'm not sure how to set up the variable myXLS to determine if no file exists or to open it simply from myXLS = ThisDoc.Path & "\Virtual Parts.xlsx"

 

Any recommendations?

0 Likes