Is it possible to export parameter values to Excel for a certain parameter range?

Is it possible to export parameter values to Excel for a certain parameter range?

L.Greft
Advocate Advocate
1,476 Views
6 Replies
Message 1 of 7

Is it possible to export parameter values to Excel for a certain parameter range?

L.Greft
Advocate
Advocate

Hello, is it possible to drive a parameter for a certain range and export the values and values of related parameters to an Excel sheet?

 

For example: I have a parameter called "Height" and two related parameters called "Middle" & "Quarter".

Middle = Height / 2

Quarter = Height / 4

I want to know all outcomes for "Middle" and "Quarter" for the Height 0 - 100 and export that to an Excel list.

Is there an easy way to do this? In my case it's not a simple formula and there are a lot more related parameters.

 

Check out my ideas: https://autode.sk/2TKe6LH
0 Likes
Accepted solutions (1)
1,477 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

The short answer is yes.  It could possibly be done with a custom iLogic rule, which could contain a loop,  and within that loop it could increment the value of the driving parameter, then update the other driven parameters, then writes the current values of those other driven parameters out to Excel, then loops again until it meets the limits defined for the loop.  If you need a more specific or detailed answer, you'll need to provide more specific detailed information about the target scenario.  The example given could also just be set up in an Excel spreadsheet by itself fairly easily too, without needing any custom programming.

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

L.Greft
Advocate
Advocate

Thank you for your reply @WCrihfield. I wasn't at work yesterday so I couldn't reply.

 

Attached you can find the part for my specific example, not the simple example I mentioned in the post. I also attached an Excel sheet to show the values and range I want.

In my part it looks at different values, some are within the flat pattern and some of them are in sketch2 in the 3D model. If you open Form1 you will see the parameter "RESTSTROOK" this is the input value, the range of it is 65 - 184. But this value doesn't have to be exported to Excel, the parameter "RESTHOOGTE" does (range 32,5 - 92).

 

If you need more information please let me know.

Check out my ideas: https://autode.sk/2TKe6LH
0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

After opening the files you posted and seeing how complicated the situation was, I decided to create my own simpler model file to test this process on first.  In part you posted, I was also getting an error message every time I attempted to change that driving parameter's value from the Parameters dialog box, but not when doing so from the iLogic Form, for some reason.  So I created a simple rectangle and extruded it, resulting in 3 total model parameters (Width, Depth, & Height). With Width being the driving parameter, and Depth = Width/2, and Height = Depth/2.  When I finally got all the kinks worked out, then I migrated the proposed solution over to your posted part, and made the necessary adjustments.

This code only writes two parameters out to the Excel file right now, but it shows you how to do it, so you can continue to customize it to suit your needs.  Full/complete solution would require way too much time investment for some random person on the forums to spend on someone else's unique challenge...and I'm leaving for the day, so...

Here's the iLogic rule for what I've got so far:

AddReference "Microsoft.Office.Interop.Excel.dll"
Imports Microsoft.Office.Interop.Excel
Sub Main
	GetExcel 'runs the Sub below which sets the value of oExcel variable
	
	'Get/Create a Workbook (file) to work with
	If oExcel.Workbooks.Count = 0 Then
		oWB = oExcel.Workbooks.Add()
	Else
		oWB = oExcel.ActiveWorkbook
	End If

	'Get/Create a Worksheet (a sheet/tab) to work with
	If oWB.Worksheets.Count = 0 Then
		oWS = oWB.Worksheets.Add
	Else
		oWS = oWB.ActiveSheet
	End If
	
	oCells = oWS.Cells
	
	'specify the two column headers in first row of Excel sheet
	'First number is Row index, second number is Column Index
	oCells.Item(1, 1).Value = "RESTSTROOK"
	oCells.Item(1, 2).Value = "RESTHOOGTE"
	
	Parameter.UpdateAfterChange = True
	MultiValue.UpdateAfterChange = True
	
	'set driving parameter to its lower limit to start
	Parameter("RESTSTROOK") = 65 mm
	RuleParametersOutput
	Dim oV1, oV2 As Double
	
	'define amount to increment driving parameter's value per loop
	Dim oInc As Double = 1 mm
	'the following starts the loop
	Do While Parameter("RESTSTROOK") <= 184 mm
		oV1 = Parameter("RESTSTROOK")
		oV2 = Parameter("RESTHOOGTE")

		WriteValsToExcel(oV1, oV2) 'runs the Sub below
		'increment the driving parameter, before looping again
		Parameter("RESTSTROOK") = Parameter("RESTSTROOK") + oInc
		RuleParametersOutput
	Loop
	
	'Make the width of all columns fit their contents
	oWS.Columns.AutoFit
End Sub

Private oExcel As Microsoft.Office.Interop.Excel.Application
Private oWB As Microsoft.Office.Interop.Excel.Workbook
Private oWS As Microsoft.Office.Interop.Excel.Worksheet
Private oCells As Microsoft.Office.Interop.Excel.Range

Sub GetExcel()
	If oExcel Is Nothing Then
		Try
			'try to find an already running instance of the Excel Application
			oExcel = GetObject(, "Excel.Application")
			'MsgBox("Found an instance of the Excel Application already open.",,"")
		Catch
			'it wasn't found open, so create an instance of it (start the application)
			'oExcel = New Microsoft.Office.Interop.Excel.Application
			oExcel = CreateObject("Excel.Application")
			'MsgBox("Created a new instance of the Excel Application.", , "")
		Catch
			MsgBox("Failed to Get/Create an instance of the Excel Application. Exiting.", , "")
			Exit Sub
		End Try
		'If it hasn't exited the Sub by now, oExcel has already been defined now, so set a couple settings for it
		oExcel.DisplayAlerts = False
		oExcel.Visible = True 'if you don't use this, you won't see the application
	End If
End Sub

Sub WriteValsToExcel(oVal1 As Double, oVal2 As Double)
	'determine last used row on the sheet
	Dim oLastRowUsed As Integer = oWS.UsedRange.Rows.Count
	Dim oRow As Integer = oLastRowUsed + 1
	'First number is Row index, second number is Column Index
	oCells.Item(oRow, 1).Value = oVal1
	oCells.Item(oRow, 2).Value = oVal2
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) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

Curtis_Waguespack
Consultant
Consultant

Hi @L.Greft 

 

If I understand correctly, I think something like this would work. See the attached Inventor 2021 example file also.

 

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

 

 

Sub main
	
	otrigger = RESTSTROOK
	
	If Parameter("RESTSTROOK") < 60 or Parameter("RESTSTROOK") > 184 Then
		MsgBox("range is between 60 and 184 only")
		Parameter("RESTSTROOK") = 100
		return 'exit rule
	End If
	
	'define the file to create/open
	oFolder = "C:\Temp\Test\"
	myXLS_File = oFolder & "Example Excel sheet.xlsx"

	Try
	GoExcel.Open(myXLS_File, "Blad1")
	Catch 
	 MsgBox("problem finding: " & myXLS_File, , "error")
	 GoExcel.Close
	 return
	End Try 

	oRowExists = False 

	'index row 2 through 150
	For row = 2 To 150
		'find the row that matches the value of RESTHOOGTE
		If (GoExcel.CellValue("A" & Row) = Parameter("RESTHOOGTE")) Then			
			GoExcel.CellValue("B" & Row) = Parameter("BREEDTE_PLAAT")
			GoExcel.CellValue("C" & Row) = Parameter("ZET1")
			GoExcel.CellValue("D" & Row) = Parameter("ZET2")
			GoExcel.CellValue("E" & Row) = Parameter("ZET4")
			GoExcel.CellValue("F" & Row) = Parameter("HOEK4")
			GoExcel.CellValue("G" & Row) = Parameter("ZET5")
			GoExcel.CellValue("H" & Row) = Parameter("HOEK5")
			oRowExists = True 
			Exit For		
		End If
	Next
	
	'[ ''''''''''''''''''''''''''''''''''''
	'this section might Not be needed, 
	'but there were some rows missing In the xlsx file
	' missing was 30 to 32
	If oRowExists = False 	
		'index row 2 through 250
		For row = 2 To 250
			'if the value doesn't exist in the table, add it to the end
			'find first empty cell in column A
			If (GoExcel.CellValue("A" & Row) Is Nothing) Then				
				GoExcel.CellValue("A" & Row) = Parameter("RESTHOOGTE")
				GoExcel.CellValue("B" & Row) = Parameter("BREEDTE_PLAAT")
				GoExcel.CellValue("C" & Row) = Parameter("ZET1")
				GoExcel.CellValue("D" & Row) = Parameter("ZET2")
				GoExcel.CellValue("E" & Row) = Parameter("ZET4")
				GoExcel.CellValue("F" & Row) = Parameter("HOEK4")
				GoExcel.CellValue("G" & Row) = Parameter("ZET5")
				GoExcel.CellValue("H" & Row) = Parameter("HOEK5")
				oRowExists = True 
				Exit For		
			End If
		Next
	End If
	']''''''''''''''''''''''''''''''''''''

	
	GoExcel.Save
	GoExcel.Close
	ThisDoc.Launch(oFolder)


End Sub

 

 

EESignature

0 Likes
Message 6 of 7

L.Greft
Advocate
Advocate

Thank you @WCrihfield! It's exactly what I needed and it works perfect. I will add the other parameters to the rule myself. Thank you for making the time to help me out!

Check out my ideas: https://autode.sk/2TKe6LH
0 Likes
Message 7 of 7

L.Greft
Advocate
Advocate

Thank you @Curtis_Waguespack! The code works really good, the only thing is that I want to have the sheet completely filled when running the rule. The code from @WCrihfield already does this, so I will use that code. Thank you for the help!

Check out my ideas: https://autode.sk/2TKe6LH