Link Excel file to Parameters via file dialog and Ilogic

Link Excel file to Parameters via file dialog and Ilogic

j3scat
Enthusiast Enthusiast
1,382 Views
15 Replies
Message 1 of 16

Link Excel file to Parameters via file dialog and Ilogic

j3scat
Enthusiast
Enthusiast

We control the parameters for a lot of our files via linked excel files.

 

I'm trying to make a 'wizard' so to speak to step a user through a number of choices regularly made with the start of a new assembly or part. For Example; choose the correct units, identify a key characteristic of the file, etc.

 

One step I want to add to my wizard is selecting and linking an excel file.

 

I've got the file name from the file dialog;

FName = FDialog.FileName

but when I try to link it,

RefFile = rDoc.ReferencedOLEFileDescriptors.Add(FName, kOLEDocumentLinkObject
where RefFile is
Dim RefFile As Inventor.ReferencedOLEFileDescriptor

 I see my file in the browser as a 3rd Party item, but within the Parameters dialog, the file is not 'loaded/referenced/linked

 

What do i need to do to link my spreadsheet/.xlsx file?

0 Likes
Accepted solutions (3)
1,383 Views
15 Replies
Replies (15)
Message 2 of 16

A.Acheson
Mentor
Mentor
Accepted solution

I haven’t actually done this in practice but ParameterTables.AddExcelTable Method looks to be the one you want. And here is a sample for Table Parameters

 

Syntax

ParameterTables.AddExcelTable( ExcelDocument As String, StartCell As String, Link As Boolean ) As ParameterTable

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 16

j3scat
Enthusiast
Enthusiast

You gave me the answer, but I don't understand why I can't make it work.

 

When I run the code, I get;

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.ParameterTables.AddExcelTable(String ExcelDocument, String StartCell, Boolean Link)
at ThisRule.Main() in external rule: Initialize:line 84
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

The selected file shows in the browser under 3rd Party, but the spreadsheet doesn't show up in parameters.

Dim rDoc As Document = ThisApplication.ActiveDocument
	Dim pDoc As Inventor.PartDocument
	Dim aDoc As Inventor.AssemblyDocument
	Dim PType As Boolean
	Dim Qtn As System.Windows.Forms.DialogResult

	If rDoc.DocumentType = kPartDocumentObject Then
		PType = True
		pDoc = ThisApplication.ActiveEditDocument
	Else
		PType = False
		Try
			aDoc = ThisApplication.ActiveEditDocument
		Catch
			GoTo TheEnd
		End Try
	End If
'... do some things, then we get to my link file section
Dim RefFile As Inventor.ReferencedOLEFileDescriptor Dim Params As Parameters Dim ParamTable As ParameterTable Dim FName As String If PType Then Params = pDoc.ComponentDefinition.Parameters MessageBox.Show("Part", "Debug") Else Params = aDoc.ComponentDefinition.Parameters Messagebox.Show("Assy", "Debug") End If

'if no file is linked, lets add
If rDoc.ReferencedOLEFileDescriptors.Count = 0 Then Qtn = MessageBox.Show("Load Spreadsheet?", "Add External OLE", MessageBoxButtons.YesNo) If Qtn = vbYes Then Dim FDialog As Inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(FDialog)
'our files are stored on a NAS, I tried referencing the file both ways shown below, this makes no difference
'I don't think this is the problem as I can edit the file through the browser as it gets linked even though the command fails 'FDialog.InitialDirectory = "P:\CAD\" '<-way1 FDialog.InitialDirectory = "\\192.168.1.153\Serverdata\CAD\" '<-way2 FDialog.Filter = "Excel Files (*.xlsx)|*.xlsx" FDialog.CancelError = True FDialog.ShowOpen() FName = FDialog.FileName If FName <> "" Then 'MessageBox.Show("Name: " & FDialog.FileName, "Debug") ParamTable = Params.ParameterTables.AddExcelTable(FName, "A1", True)
' it also fails if i try the following
'Params.ParameterTables.AddExcelTable(FName, "A1", True)
' End If End If End If If rDoc.ReferencedOLEFileDescriptors.Count > 0 Then MessageBox.Show("loaded") end if TheEnd

I can manually add the table fine, and I have confirmed "A1" is how inventor references the starting cell.  Trying "A2" doesn't help. Neither does setting the 'Link' boolean to False

I'm currently on 2022

 

In the error I see this;

at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)

I am currently debugging  a part file.

0 Likes
Message 4 of 16

WCrihfield
Mentor
Mentor

Hi @j3scat.  According to the error text at the start of that last post, the error is happening at the line where you are trying to use that AddExcelTable method.  I wander if it might possibly have a problem with the location of the file.  Perhaps as a test, you could copy the file locally, just for a temporary test, then try linking to it in that local location, just to see if that makes any difference.  As a process of elimination step.  Is that excel file 'macro enabled'?  Is it possible that there might be some sort of 'access permissions' on that file, or on that file location that may be preventing this linking?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 16

j3scat
Enthusiast
Enthusiast
I copied the file local and tried on a fresh part file and an assembly file. Fails in both instances.

I can't see how my file is macro enabled. It is a simple excel file with column headings and data only.

My system does have an issue with microsoft permissions. I've reformated my hard drive and reinstalled everything a number of times but I can't have a projects folder on my NAS drive, or it will take hours to load a simple assembly. (If i load an old version of inventor 2015, projects on the NAS works fine. 2022/2023? nope). I've run out of google power attempting to fix the issue. I have migrated all my projects to local as a workaround, except my linked .xlsx files as that has never created an issue when manually linked. In this case local did not fix my problem
0 Likes
Message 6 of 16

A.Acheson
Mentor
Mentor

This is working for me. Have you tried to see if the document is accepting a manually added link to parameters?

Dim Params As Parameters = ThisDoc.Document.ComponentDefinition.Parameters
Dim ParamTable As ParameterTable = Params.ParameterTables.AddExcelTable("C:\Temp\LinkedParameters.xlsx", "A2", True)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 16

j3scat
Enthusiast
Enthusiast

I get the same error on your second line,

However, when I type your first line;

Dim Params as Parameters = ThisDoc.Document

types in cleanly, however the period(.) after Document does not bring up ComponentDefinition as a valid 'sub class'(?) to ThisDoc.Document

 

I tried Dim Doc as ThisDoc.Document and get 'ThisDoc.Document' is not defined.

 

Do I have something corrupt with my install?

0 Likes
Message 8 of 16

A.Acheson
Mentor
Mentor
Accepted solution

What inventor version are you using? I tested that in 2022. You can just remove ThisDoc.Document and replace with ThisApplication.ActiveDocument if you want but I wouldn't see that as the issue.  I'm starting to think it's excel based the issue. When you get the third party object in the browser do you also get the file link in the parameter box? This shows that it is linked. And if you get parameters underneath the link it shows that they were successfully added. If they fail to appear that means they are incorrectly formatted. 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 9 of 16

j3scat
Enthusiast
Enthusiast

I'm using 2022. I loaded and tried 2023, and receive the same results.

I was initially trying ThisApplication.ActiveDocument 😕

 

You can see my linked '30-0096-0001.xlsx' under 3rd third party, but nothing in the parameters box.

j3scat_0-1668563480029.png

If I delete the 3rd Party link and add via the 'Link' button in the Parameters box I end up with this.

j3scat_1-1668563651483.png

EDIT:

I created a new excel file, not one from template and just one line (no headers) and it loaded. I must have some hidden formating issue. Weirdly it fails with files from years ago, so my formating issue is very old.

Message 10 of 16

A.Acheson
Mentor
Mentor

I see earlier in your code your using "A1" cell reference for the start which would be at the column headers, could that be the issue?  At least your moving in the right direction getting a new file to work. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 11 of 16

j3scat
Enthusiast
Enthusiast

I have a strong suspicion as to what was causing my issue.

If you look at my linked spreadsheet you can see I have Parameters in equations for other parameters.

 

Looking at my picture above, you can see I have a parameter;

  EL_US_C1  that is equal to EL_Z_el + Z_GROUT.

I changed this equation to EL_Z_el + 40 mm

 

The file link will fail if EL_US_C1 comes before parameter EL_Z_el in my spreadsheet. (using a truncated .xlsx file for testing) If I move the EL_US_C1 definition below parameter EL_Z_el in my spreadsheet it loads fine.

 

This behavior will not allow me to do what I want to do. The only solution I see would be to create a parameter with a dummy value for each line in my spreadsheet, then link the excel file. <- this wouldn't be something you would know how to steer me to?

 

BTW, thanks for the help you provided

Message 12 of 16

WCrihfield
Mentor
Mentor

Hi @j3scat.  As a quick test, I created a new simple part document, sketched a rectangle, extruded it, and saved the part.  Created 3 new user parameters named "WIDTH", "HEIGHT", & "DEPTH", and have them values (6, 3, & 1), then put those user parameter names as the 'equation' of the 3 model parameters which control the 2D base size, and the extruded depth.  Then saved it again.  Then created a new Excel document, put those same 3 user parameter names as the first 3 cells in the first column, then different simple numerical values for them in the first 3 cells of the second column.  When I linked that Excel file to the part, it instantly replaced my 3 user parameters with the 3 newly linked parameters (exact same names), and the model parameters instantly reflected those new values.  Once I clicked the update button, the model reflected the new size, as expected.  Then I clicked UNDO to before linking the Excel file.  Modified the 3 values so that the second two used the name of the first one in their equation, then saved and closed that Excel file.  When I linked it, it also worked exactly as expected.  So, this proves that you can have a set of user parameters in your model with the same names as ones you are planning on linking from Excel, and as long as the equations can be calculated, it should work OK.  I'm thinking that you could use an iLogic rule to read the list of parameter names from the first column of the Excel file first, then create that set of parameters with default values (such as 1 for numerical) in a loop, then maybe do an update, then create the link to that Excel file right afterwards.  That could all be automated for you by a rule.

Edit:  However, if you are actually newly creating all those user parameters from a list like that, integrating their names into the equations of any model parameters that may already exist may be the more challenging task, unless maybe you have renamed all of your model parameters to more useful and identifiable names before this point.  But I guess if the model file is new, and there are no pre-existing model parameters, you could simply set each new dimension or feature spec to the name of one of these linked parameters after the fact, as you build the model, which would be pretty nice.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 16

j3scat
Enthusiast
Enthusiast
That is exactly what I was thinking. I just haven't learned how to
directly read excel files yet.

Thank-you for testing and proving the premise though.

(it will be a little while before I can place any more time on this
project. I have to accomplish a few practical items first)

0 Likes
Message 14 of 16

WCrihfield
Mentor
Mentor
Accepted solution

Hi @j3scat.  Here is a pretty basic iLogic rule for this task, which just uses the GoExcel tools that are available in the iLogic AddIn for accessing Excel stuff.  You will obviously need to change the path & file name of the Excel file, and/or sheet name.  And you will likely have to edit the cell address range too, to fit how many parameter names are in your list.  There are more advanced ways that could be used for determining that range size by code, so you don't have to hard-code cell addresses in there, but this should be a good starting point.  It creates the user parameters first, and just assigns 1 inch as their default starting values.  Then it sets up the link after that point.  However, for some reason, it is not updating the parameter values to match the excel file values yet when the rule finishes.  If I open the Excel file, save it, then close it, then update the part, that seems to update the parameter values to the values they should be though.

Dim oXLFile As String = "C:\Temp\Params 2.xlsx"
Dim oSheet As String = "Sheet1"
GoExcel.Open(oXLFile, oSheet)
GoExcel.DisplayAlerts = False
PNames = GoExcel.CellValues("A1", "A3")
If PNames.Count = 0 Then Exit Sub
Dim oPDoc As PartDocument = ThisDoc.Document
Dim oParams As Inventor.Parameters = oPDoc.ComponentDefinition.Parameters
Dim oUParams As UserParameters = oParams.UserParameters
For Each PName In PNames
	Dim oUParam As UserParameter = Nothing
	Try
		oUParam = oUParams.Item(PName)
	Catch
		oUParam = oUParams.AddByExpression(PName, "1", UnitsTypeEnum.kInchLengthUnits)
	End Try
Next
oPDoc.Rebuild
Dim oPTables As ParameterTables = oParams.ParameterTables
Dim oPTable As ParameterTable
Try
	oPTable = oPTables.AddExcelTable(oXLFile, "A1", True)
Catch
	MsgBox("Linking Excel table failed!", vbCritical, "Excel Link Failed")
End Try
oPDoc.Rebuild
GoExcel.ClearCache
GoExcel.Close
'GoExcel.QuitApplication

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 15 of 16

j3scat
Enthusiast
Enthusiast

Excellent. Absolutely a great starting point

0 Likes
Message 16 of 16

j3scat
Enthusiast
Enthusiast

If anybody cares, here is the Rule I created using the above suggestions;

 

Dim xDoc As Document = ThisDoc.Document
Dim pDoc As PartDocument
Dim aDoc As AssemblyDocument
Dim PType As Boolean = True
Dim Qtn As System.Windows.Forms.DialogResult

Dim Count As Integer = 1, Cell As String, StartCell As String
Dim UnitStr As String, UnitEnum As String

	If xDoc.DocumentType = kPartDocumentObject Then
		PType = True
	Else
		PType = False
	End If
	
Dim RefFile As Inventor.ReferencedOLEFileDescriptor
Dim Params As Parameters = xDoc.ComponentDefinition.Parameters

Dim FName As String
	If xDoc.ReferencedOLEFileDescriptors.Count = 0 Or Not(PType) Then
		Qtn = MessageBox.Show("Load Spreadsheet?", "Add External OLE", MessageBoxButtons.YesNo)
		If Qtn = vbYes Then
			Dim FDialog As Inventor.FileDialog = Nothing
			Dim IParams As Inventor.Parameters
			Dim UParams As UserParameters
			InventorVb.Application.CreateFileDialog(FDialog)
			'FDialog.InitialDirectory = "C:\CAD\"
			FDialog.InitialDirectory = "\\NAS\Serverdata\CAD\"
			FDialog.Filter = "Excel Files (*.xlsx)|*.xlsx"
			FDialog.CancelError = True
			'On Error Resume Next
			FDialog.ShowOpen()
			'If Err.Number <> 9 Then
			'	Return
			FName = FDialog.FileName
			If FName <> "" Then
				Dim Sheet As String = "Sheet1"
				GoExcel.Open(FName, Sheet)
				GoExcel.DisplayAlerts = False
				Cell = "A" & Count
				ParNames = GoExcel.CellValues(Cell, "A1000")
				If ParNames.Count = 0 Then Exit Sub
				If PType Then
					pDoc = ThisApplication.ActiveEditDocument
					IParams = pDoc.componentDefinition.Parameters
				Else
					aDoc = ThisApplication.ActiveEditDocument
					IParams = aDoc.ComponentDefinition.Parameters
				End If
				UParams = IParams.UserParameters
				
				For Each ParName In ParNames
					Dim Uparam As UserParameter = Nothing
					If UCase(ParName) = "PARAMETER_NAME" Or UCase(ParName) = "PARAMETER NAME" Then
						StartCell = "A2"
						GoTo SkipNext
					Else If ParName = "" Then
						Exit For
					Else
						StartCell = "A1"
					End If
					Try
						Uparam = UParams.Item(ParName)
					Catch
						Cell = "C" & Count
						UnitStr = GoExcel.CellValue(Cell)
						If UnitStr = "ul" Then
							UnitEnum = "UnitsTypeEnum.kUnitlessUnits"
						Else If UnitStr = "in" Then
							UnitEnum = "UnitsTypeEnum.kInchLengthUnits"
						Else If UnitStr = "mm" Then
							UnitEnum = "UnitsTypeEnum.kMillimeterLengthUnits"
						Else If UnitStr = "deg" Then
							UnitEnum = "UnitsTypeEnum.kDegreeAngleUnits"
						Else If UnitStr = "lbmass" Or UnitStr = "kg" Then
							UnitEnum = "UnitsTypeEnum.kLbMassMass"
						Else
							UnitStr = "UnitsTypeEnum.kInchLengthUnits"
						End If
						Uparam = UParams.AddByExpression(ParName, "1", UnitsTypeEnum.kInchLengthUnits)
					End Try
			SkipNext:
					Count = Count + 1
				Next
				'MessageBox.Show ("Count value reached: " & Count, "Debug ln85")
				If PType Then
					pDoc.Rebuild
				Else
					aDoc.Rebuild
				End If
				Try
					Dim ParamTable As ParameterTable = Params.ParameterTables.AddExcelTable(FName, StartCell, True)
				Catch
					MessageBox.Show("Edit Spreadsheet and/or Manualy load", "Spreadsheet Load Failed")
				End Try
				If PType Then
					pDoc.Rebuild
				Else
					aDoc.Rebuild
				End If
				GoExcel.ClearCache
				GoExcel.Close
				If xDoc.ReferencedOLEFileDescriptors.Count > 0 Then
					MessageBox.Show("Spreadsheet Loaded", "Success!")
				End If
			End If
		End If
	End If

 

0 Likes