How can I create these "Parameters" with iLogic instead of using the Parameters window?

How can I create these "Parameters" with iLogic instead of using the Parameters window?

chris
Advisor Advisor
1,442 Views
21 Replies
Message 1 of 22

How can I create these "Parameters" with iLogic instead of using the Parameters window?

chris
Advisor
Advisor

I'm trying to see if there is an easier way to create "User Defined Parameters" instead of using the Parameters window in Inventor. I need to be able to create each parameter with an "initial" value, but then obviously be able to change that value later. The below example shows a numeric in, numeric angle, unitless, text and true/false.

 

I'd like to know if I can also set up the multi-value list when creating these in iLoigc, as well as add the notes, and also if it is a key or to be exported?

 

Is this possible and can someone show an example?

 

I have a project where I have to enter a ton of user defined parameters and I want to group them, and inevitably you always forget a parameter when trying to group them and of course we can't we order in the parameters window... ;-(

 

chris_0-1739418960979.png

 

0 Likes
Accepted solutions (2)
1,443 Views
21 Replies
Replies (21)
Message 2 of 22

mateusz_baczewski
Advocate
Advocate

Hi,

I wrote an example code that displays a window where we create user parameters. Take a look and let me know if this is what you were looking for. It's just a sample code that you'll need to adjust more to your requirements, e.g., add validation, check if the user has entered data, etc.

 

 

AddReference "System.Drawing"
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Class UserSetsForm
    Inherits System.Windows.Forms.Form
    Private WithEvents oForm As New Form
    Dim TextBox1 As New System.Windows.Forms.TextBox()
    Dim TextBox2 As New System.Windows.Forms.TextBox()
    Dim TextBox3 As New System.Windows.Forms.TextBox()
    Dim TextBox4 As New System.Windows.Forms.TextBox()
    Dim CheckBox1 As New System.Windows.Forms.CheckBox()

    Dim Label1 As New Label()
    Dim Label2 As New Label()
    Dim Label3 As New Label()
    Dim Label4 As New Label()
    Dim Label5 As New Label()

    Dim ButtonOk As New Button()

    Sub Main
        oForm = Me
        With oForm
            ' Set up form    
            .FormBorderStyle = FormBorderStyle.FixedToolWindow
            .StartPosition = FormStartPosition.CenterScreen
            .Width = 400
            .Height = 300
            .TopMost = True
            .Text = "Custom Form"
            .Name = "Custom Form"
        End With

        ' Label and TextBox1 for Name
        With Label1
            .Text = "User_parameter1:"
            .Top = 25
            .Left = 25
            .Width = 100
        End With

        With TextBox1
            .TextAlign = HorizontalAlignment.Left
            .Height = 20
            .Width = 200
            .Top = 25
            .Left = 125
        End With

        ' Label and TextBox2 for Email
        With Label2
            .Text = "User_parameter2:"
            .Top = 60
            .Left = 25
            .Width = 100
        End With

        With TextBox2
            .TextAlign = HorizontalAlignment.Left
            .Height = 20
            .Width = 200
            .Top = 60
            .Left = 125
        End With

        ' Label and TextBox3 for Date of Birth
        With Label3
            .Text = "User_parameter3:"
            .Top = 95
            .Left = 25
            .Width = 100
        End With

        With TextBox3
            .TextAlign = HorizontalAlignment.Left
            .Height = 20
            .Width = 200
            .Top = 95
            .Left = 125
        End With

        ' Label and TextBox4 for Address
        With Label4
            .Text = "User_parameter4"
            .Top = 130
            .Left = 25
            .Width = 100
        End With

        With TextBox4
            .TextAlign = HorizontalAlignment.Left
            .Height = 20
            .Width = 200
            .Top = 135
            .Left = 125
        End With

        ' Label and TextBox5 for Phone Number
        With Label5
            .Text = "User_parameter5"
            .Top = 165
            .Left = 25
            .Width = 100
        End With

        With CheckBox1
            .Height = 20
            .Width = 200
            .Top = 165
            .Left = 125
        End With

        ' Button OK
        With ButtonOk
            .Text = "OK"
            .Width = 100
            .Height = 30
            .Top = 210
            .Left = 125
        End With

        ' Add controls to form
        oForm.Controls.Add(Label1)
        oForm.Controls.Add(TextBox1)
        oForm.Controls.Add(Label2)
        oForm.Controls.Add(TextBox2)
        oForm.Controls.Add(Label3)
        oForm.Controls.Add(TextBox3)
        oForm.Controls.Add(Label4)
        oForm.Controls.Add(TextBox4)
        oForm.Controls.Add(Label5)
        oForm.Controls.Add(CheckBox1)
        oForm.Controls.Add(ButtonOk)

        ' Add event handler for OK button
        AddHandler ButtonOk.Click, AddressOf ButtonOk_Click

        ' Display the form as a dialog
        oForm.ShowDialog()
    End Sub

    ' Event handler for OK button click
    Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
	
	Dim oDoc As Document = ThisDoc.Document	
	Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition
	Dim oParams As Parameters = oCompDef.Parameters
	Dim oUserParams As UserParameters = oParams.UserParameters
	Dim oUserParam1 As UserParameter
	Dim oUserParam2 As UserParameter
	Dim oUserParam3 As UserParameter
	Dim oUserParam4 As UserParameter
	Dim oUserParam5 As UserParameter
	
	Try
		oUserParams.Item("User_parameter_001").Expression = TextBox1.text
		oUserParam1 = oUserParams.Item("User_parameter_001")
	Catch
		oUserParam1 = oUserParams.AddByExpression("User_parameter_001", TextBox1.text, UnitsTypeEnum.kInchLengthUnits)
	End Try

	oUserParam1.IsKey = True
	oUserParam1.ExposedAsProperty = True
	
	Try
	oUserParams.Item("User_parameter_002").Expression = TextBox1.text
	oUserParam2 = oUserParams.Item("User_parameter_002")
	Catch
		oUserParam2 = oUserParams.AddByExpression("User_parameter_002", TextBox1.text, UnitsTypeEnum.kDegreeAngleUnits)
	End Try

	oUserParam2.IsKey = True
	oUserParam2.ExposedAsProperty = True
	oUserParam2.Comment= "Draft Angle"
	
	' User_parameter_003
	Try
	    oUserParams.Item("User_parameter_003").Expression = TextBox3.Text
	    oUserParam3 = oUserParams.Item("User_parameter_003")
	Catch
	    oUserParam3 = oUserParams.AddByExpression("User_parameter_003", TextBox3.Text, "ul")
	End Try
	
	oUserParam3.IsKey = True
	oUserParam3.ExposedAsProperty = True
	oUserParam3.Comment = "Number of Holes"
	
'	 User_parameter_004

	Dim inputString As String = TextBox4.text
	Dim words As List(Of String) = inputString.Split(",").ToList()

	Try
		oUserParam4 = oUserParams.Item("User_parameter_004")
		MultiValue.SetList("User_parameter_004", words.ToArray)
	Catch
	    oUserParam4 = oUserParams.AddByValue("User_parameter_004", "", UnitsTypeEnum.kTextUnits)
		oUserParam4.Value = words(1)
		MultiValue.SetList("User_parameter_004", words.ToArray)
	End Try

	oUserParam4.IsKey = True
	oUserParam4.Comment = "Types of Holes"
	
	' User_parameter_005
	Try
	    oUserParams.Item("User_parameter_005").Value = CheckBox1.Checked
	    oUserParam5 = oUserParams.Item("User_parameter_005")
	Catch
	    oUserParam5 = oUserParams.AddByValue("User_parameter_005", CheckBox1.Checked, "Boolean")
	End Try
	
	oUserParam5.IsKey = True
	oUserParam5.Comment = "Show holes"
    End Sub
	
End Class

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

Message 3 of 22

Curtis_Waguespack
Consultant
Consultant

Hi @chris , here's an example that will create the 5 parameters as the defaults. 

Hope that helps, Curtis

 

Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment
	ParamList.Add("User_Parameter_001|1|in|")
	ParamList.Add("User_Parameter_002|1|deg|Draft Angle")
	ParamList.Add("User_Parameter_003|1|ul|Number of Holes")
	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Type of Holes")
	ParamList.Add("User_Parameter_005|True|Boolean|Show Holes")

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)

	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf ParameterValue = "1" Then	'convert ParameterValue string to number if possible
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)
	End Try

	'set value and comment for existing params
	param.Value = ParameterValue
	param.Comment = ParameterComment

	'if multivalue then set list
	If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
End Sub

 

EESignature

Message 4 of 22

chris
Advisor
Advisor

@Curtis_Waguespack This works great, can I request a way that once these are created, if I add any additional ones, that it doesn't replace/overwrite the existing ones?

0 Likes
Message 5 of 22

chris
Advisor
Advisor

@mateusz_baczewski I'm interested in seeing what you did, but when I try to run that it gives me a "security alert" and doesn't allow that rule to run, can you share a couple screen shots of what yours looks like?

0 Likes
Message 6 of 22

Curtis_Waguespack
Consultant
Consultant

@chris , this version will only set the values if the parameter does not exist and is created when run. If it exists, it does nothing.

 

Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment
	ParamList.Add("User_Parameter_001|1|in|")
	ParamList.Add("User_Parameter_002|1|deg|Draft Angle")
	ParamList.Add("User_Parameter_003|1|ul|Number of Holes")
	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Type of Holes")
	ParamList.Add("User_Parameter_005|True|Boolean|Show Holes")

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)

	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf ParameterValue = "1" Then	'convert ParameterValue string to number if possible
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)

		'set value and comment for existing params
		param.Value = ParameterValue
		param.Comment = ParameterComment

		'if multivalue then set list
		If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
	End Try

End Sub

EESignature

0 Likes
Message 7 of 22

chris
Advisor
Advisor

@Curtis_Waguespack I don't need anything changed, but how come when I add a user parameter to the code and I try to put in a value other than "1" for the inches or deg, the rule returns an error. Not a big deal, I'm just trying to understand.

Message 8 of 22

Curtis_Waguespack
Consultant
Consultant

@chris , ah yeah I had it sorta hardcoded for 1

 

there is this in the sub, that catches the 1 and converts it from string to double, to prevent a data type mismatch error later

 

	ElseIf ParameterValue = "1" Then	'convert ParameterValue string to number 

 

You can replace that line with this so that it is not just looking for 1, but is looking at the string to see if it is numeric, and then you can set your default inch, angle and and unitless to any number, so it's more flexible.

 

Note too, I added an example for setting a default text value of nothing, just in case it is of use.

 

Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment
	ParamList.Add("User_Parameter_001|7.125|in|")
	ParamList.Add("User_Parameter_002|8|deg|Draft Angle")
	ParamList.Add("User_Parameter_003|9|ul|Number of Holes")
	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Type of Holes")
	ParamList.Add("User_Parameter_005|True|Boolean|Show Holes")
	ParamList.Add("User_Parameter_999|Null|Text|Test 999")

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)

	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf IsNumeric(ParameterValue) Then	'convert ParameterValue string to number
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)

		'set value and comment for existing params
		param.Value = ParameterValue
		param.Comment = ParameterComment

		'if multivalue then set list
		If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
	End Try


End Sub

 

EESignature

0 Likes
Message 9 of 22

chris
Advisor
Advisor

@Curtis_Waguespack So when I try to run this, I get an error on line 31? Am I entering in something wrong or in the wrong way? 

 
Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment
	ParamList.Add("Length|9.125|in|Length")
	ParamList.Add("Width|7.375|in|Width")
	ParamList.Add("Thk|1.125|in|Thickness")
	ParamList.Add("User_Parameter_002|8|deg|Draft Angle")
	ParamList.Add("User_Parameter_003|9|ul|Number of Holes")
	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Color Options")
	ParamList.Add("User_Parameter_005|True|Boolean|Show Holes")
	ParamList.Add("User_Parameter_999|Null|Text|Test 999")
	ParamList.Add("UDBH|5.621|in")
	ParamList.Add("UTBHA|5.5|deg|Incline Angle")
	ParamList.Add("Paint_TF|True|Boolean|Show Paint")
	ParamList.Add("Hole_Sizes|1/2, 5/8, 3/4|Hole Size Options")

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)

	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf IsNumeric(ParameterValue) Then	'convert ParameterValue string to number
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)

		'set value and comment for existing params
		param.Value = ParameterValue
		param.Comment = ParameterComment

		'if multivalue then set list
		If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
	End Try


End Sub

 

0 Likes
Message 10 of 22

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@chris ,  there are 2 entries that do not have comments, these are where the issues are. To fix it you just need to add a vertical bar (  |   ) to the end of the string, when you are not including a parameter comment. i.e. every line should have 3 vertical bars separators, even if there is no comment

 

I added an error handler message for this in this example

 

 

Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment
	ParamList.Add("Length|9.125|in|Length")
	ParamList.Add("Width|7.375|in|Width")
	ParamList.Add("Thk|1.125|in|Thickness")
	ParamList.Add("User_Parameter_002|8|deg|Draft Angle")
	ParamList.Add("User_Parameter_003|9|ul|Number of Holes")
	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Color Options")
	ParamList.Add("User_Parameter_005|True|Boolean|Show Holes")
	ParamList.Add("User_Parameter_999|Null|Text|Test 999")
	ParamList.Add("UDBH|5.621|in|")
	ParamList.Add("UTBHA|5.5|deg|Incline Angle")
	ParamList.Add("Paint_TF|True|Boolean|Show Paint")
	ParamList.Add("Hole_Sizes|1/2, 5/8, 3/4|Text|Hole Size Options")

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)
	
	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	
	ArrayCount = UBound(StringArray) 
	If ArrayCount < 3 Then 
		MsgBox(ParamString & vbLf & "Only " & ArrayCount & " ' | ' seperator chrs in string" & vblf & "Expected 3, you might need to add one to the end", , "ilogic")
		Exit Sub
	End If
	
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf IsNumeric(ParameterValue) Then	'convert ParameterValue string to number
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)

		'set value and comment for existing params
		param.Value = ParameterValue
		param.Comment = ParameterComment

		'if multivalue then set list
		If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
	End Try


End Sub

 

EESignature

0 Likes
Message 11 of 22

mateusz_baczewski
Advocate
Advocate

Hey,

You know what? It seems to me that my code and the solution itself might be a bit too complicated, especially since there are still many things to improve. To show it to you now, I had to copy the same parts of the code, and it still doesn’t work that well 😄 Additionally, the @Curtis_Waguespack code should be more than enough for you case, it's more elegant! 😊 but roughly, it looks like this:

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

Message 12 of 22

Curtis_Waguespack
Consultant
Consultant

@mateusz_baczewski , just a quick note to say, I like your solution and plan to "steal" it for future reference Curtis_Waguespack_0-1739470065755.png  so thanks for providing it!

 

EESignature

Message 13 of 22

chris
Advisor
Advisor

@Curtis_Waguespack Okay, that worked great, loved the "specific" error message if not done correctly. I'd also like to see that other solution from @mateusz_baczewski , I really wish the Parameters environment was more like excel where we could copy and paste easier, group stuff and re-order Parameters. I'd love to be able to make a "folder" of parameters, general parameters and specific parameters.

0 Likes
Message 14 of 22

mateusz_baczewski
Advocate
Advocate

I`m glad to hear that. I’m attaching the latest version of the code. There’s still a lot to do, including refactoring and and some parts of the code are duplicated but hopefully, it will be useful to you.

 

 

If you found it helpful, a "Like" would be much appreciated!
If this post solved your problem, please mark it as "Solution.".

Message 15 of 22

WCrihfield
Mentor
Mentor

Hi @chris.  Just a quick note about your wanting to be able to 'group' parameters or put them into a folder...

When using the Inventor API object model, and accessing the Document.ComponentDefinition.Parameters property to get the real Parameters object (as opposed to the uniquely iLogic Parameter 'Rule Object' that represents a 'IParamDynamic Interface'), you have access to the Parameters.CustomParameterGroups property.  Property returns a CustomParameterGroups object, which lets us either access the already existing CustomParameterGroup objects with its CustomParameterGroups.Item property, or create a new one with its CustomParameterGroups.Add method.  Once we have one, we can use its properties and methods to access, add, or remove parameters from that group.  Just so you know, in case you may want to explore into that area.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 22

chris
Advisor
Advisor

I really wish I understood the API and vb, it seems so far away , that is understanding it. someone needs to do a video series, (which I would love to be the "dummy" being trained, where someone goes through a logically conversation of why and how to set up a rule, where we talk about it, map it out, decide what's needed, where to find the code and why its being added, at the same time explaining what the code means.

 

Who wants to try this out?

0 Likes
Message 17 of 22

Curtis_Waguespack
Consultant
Consultant

@chris wrote:

Who wants to try this out?


I do mentoring of this type, send me a message if you'd like and we can set something up and we can discuss. 

EESignature

0 Likes
Message 18 of 22

chris
Advisor
Advisor

Sure thing!

 

0 Likes
Message 19 of 22

JBerns
Advisor
Advisor

@chris ,

 

Below please find another example of creating user parameters and a CustomParameterGroup.

 

''' Code to create user parameters and parameter groups
''' Sources:	https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=CustomParameterGroup_Add_Sample
'''				https://forums.autodesk.com/t5/inventor-programming-ilogic/custom-parameter-groups/td-p/10553284

Private Sub Main()
	Break
	Call CreateParametersAndGroup
End Sub

Public Sub CreateParametersAndGroup()
    ' Create a new Part document.
    Dim oPartDoc As PartDocument
        oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
                 ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
    
    ' Set a reference to the compdef.
    Dim oCompDef As PartComponentDefinition
        oCompDef = oPartDoc.ComponentDefinition

    ' Create a model parameter
    Dim oModelParam As ModelParameter
        oModelParam = oCompDef.Parameters.ModelParameters.AddByValue(2 * 2.54, kInchLengthUnits)
    
    ' Create a reference parameter
    Dim oReferenceParam As ReferenceParameter
        oReferenceParam = oCompDef.Parameters.ReferenceParameters.AddByValue(4 * 2.54, kInchLengthUnits)
    
    ' Create user parameters
    Dim oUserParam1 As UserParameter = oCompDef.Parameters.UserParameters.AddByExpression("Length", "6 in", kInchLengthUnits)
	Dim oUserParam2 As UserParameter = oCompDef.Parameters.UserParameters.AddByExpression("Width", "8 in", kInchLengthUnits)
    
    ' Create a new custom parameter group
    Dim oCustomParamGroup As CustomParameterGroup
        oCustomParamGroup = oCompDef.Parameters.CustomParameterGroups.Add("Custom Group", "CustomGroup1")
    
    ' Add the created parameters to this group
    ' Note that adding the parameters to the custom group
    ' does not remove it from the original group.
    Call oCustomParamGroup.Add(oModelParam)
    Call oCustomParamGroup.Add(oReferenceParam)
    Call oCustomParamGroup.Add(oUserParam1)
    Call oCustomParamGroup.Add(oUserParam2)
End Sub

 

Curtis' example has more flexibility, error handling, and you don't need to know things like enumerators, such as kInchLengthUnits. (UnitsTypeEnum)

Great example Curtis!

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 20 of 22

chris
Advisor
Advisor

@Curtis_Waguespack  First off let me say I use this all the time now, so much easier than the Parameters window.

Is there a way to add to this iLogic code that would allow me to assign a "parameter group"? sort of how I have it shown with:

'Parameter Group 1

'Parameter Group 2

'Parameter Group 3

Weight and Volume

 

even though I know those currently are just comments

 

'Add User Parameters

'Types of Parameters
'	ParamList.Add("User_Parameter_001|9.125|in|Notes")	
'	ParamList.Add("User_Parameter_002|8|deg|Notes")
'	ParamList.Add("User_Parameter_003|9|ul|Notes")
'	ParamList.Add("User_Parameter_004|Red, Blue, Green|Text|Notes")
'	ParamList.Add("User_Parameter_005|True|Boolean|Notes")
'	ParamList.Add("User_Parameter_006|Null|Text|Notes")

Sub Main
	Dim ParamList As New ArrayList
	'           Name  | Value | Unit | Comment

	'_____________Add Parameter Below This Line _________________________
	
	'Parameter Group 1
	ParamList.Add("User_Parameter_001|9.125|in|Notes")	
'	ParamList.Add("User_Parameter_002|8|deg|Notes")
'	ParamList.Add("User_Parameter_003|9|ul|Notes")

	'Parameter Group 2
	ParamList.Add("User_Parameter_004|9.125|in|Notes")	
'	ParamList.Add("User_Parameter_005|8|deg|Notes")
'	ParamList.Add("User_Parameter_006|9|ul|Notes")

	'Parameter Group 3
	ParamList.Add("User_Parameter_007|9.125|in|Notes")	
'	ParamList.Add("User_Parameter_008|8|deg|Notes")
'	ParamList.Add("User_Parameter_009|9|ul|Notes")
	
	'Weight and Volumes
'	ParamList.Add("WIP|1|lbs|Weight In Pounds")
'	ParamList.Add("VIG|1|gal|Volume In Gallons")
'	ParamList.Add("VIY|1|Text|Volume In Yards")
	

	'send striing to sub routine
	For Each ParamString In ParamList : CreateParams(ParamString) : Next

	iLogicVb.UpdateWhenDone = True
End Sub


Sub CreateParams(ParamString As String)
	
	'split each string using the | as the split character
	StringArray = Split(ParamString, "|")
	
	ArrayCount = UBound(StringArray) 
	If ArrayCount < 3 Then 
		MsgBox(ParamString & vbLf & "Only " & ArrayCount & " ' | ' seperator chrs in string" & vbLf & "Expected 3, you might need to add one to the end", , "ilogic")
		Exit Sub
	End If
	
	ParameterName = StringArray(0) 'first item in array
	ParameterValue = StringArray(1) 'second item in array
	ParameterUnit = StringArray(2) 'etc
	ParameterComment = StringArray(3) 'etc

	'set up conversion factors
	Dim DocumentUnitsOfMeasure As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Cnvt_cm_to_in = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kInchLengthUnits, UnitsTypeEnum.kCentimeterLengthUnits) 'cm to inch (2.54)
	Cnvt_Rad_to_Deg = DocumentUnitsOfMeasure.ConvertUnits(1, UnitsTypeEnum.kDegreeAngleUnits, UnitsTypeEnum.kRadianAngleUnits) 'rad to deg (57.2958)

	Dim UserParams As UserParameters = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
	Dim MultiList = New ArrayList
	MultiList.Clear

	If ParameterValue = "True" Then 'convert ParameterValue to boolean		
		ParameterValue = True
	ElseIf ParameterValue = "Null" Then 'convert Null to empty string
		ParameterValue = ""
	ElseIf IsNumeric(ParameterValue) Then	'convert ParameterValue string to number
		ParameterValue = CDblAny(ParameterValue)
		If ParameterUnit = "in" Then ParameterValue = ParameterValue * Cnvt_cm_to_in
		If ParameterUnit = "deg" Then ParameterValue = ParameterValue * Cnvt_Rad_to_Deg
	ElseIf ParameterValue.Contains(",") Then 'process list of values for multivalue		
		'split each string using comma as the split character
		StringArray2 = Split(ParameterValue, ",")
		'add strings to arraylist
		For Each ListItem In StringArray2 : MultiList.Add(Trim(ListItem)) : Next
		'set multivalue param value to the first item in the list
		ParameterValue = MultiList.Item(0)
	End If

	Dim param As Inventor.Parameter
	Try'try to get parameter		
		param = UserParams.Item(ParameterName)
	Catch'The parameter doesn't exist so add it		
		param = UserParams.AddByValue(ParameterName, ParameterValue, ParameterUnit)

		'set value and comment for existing params
		param.Value = ParameterValue
		param.Comment = ParameterComment

		'if multivalue then set list
		If MultiList.Count > 0 Then MultiValue.List(ParameterName) = MultiList
	End Try


End Sub
0 Likes