Set Parameter Doesn't Create / How to Create Parameter

Set Parameter Doesn't Create / How to Create Parameter

cstarker6VCG4
Collaborator Collaborator
266 Views
3 Replies
Message 1 of 4

Set Parameter Doesn't Create / How to Create Parameter

cstarker6VCG4
Collaborator
Collaborator

For some reason, parameter("name") function doesn't create parameters that don't exist, which is not something I've seen before. Regardless, I need a parameter to be created and set via iLogic. I don't see anything obvious in Snippets > Parameters and all solutions on the forum are cumbersome, horrendously out of date, or don't work.

What is the current method to set parameters if they exist or create and set if they don't exist then export it to iProperties?

 

cstarker6VCG4_0-1730309832514.png

 

0 Likes
267 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

None of the standard/installed iLogic 'snippets' will actually 'create' a new parameter, if it does not already exist.  That is generally done using Inventor API code, since there are no iLogic shortcut snippets for that task.  But there are several different types of parameters, and multiple different types of units that a parameter can have, and some can have a list of possible values to choose from.  So, there are two main methods used for creating them.

UserParameters.AddByExpression 

UserParameters.AddByValue 

Which one is right for a specific situation depends on what units you want it to have, and how you need to define its value/expression.  If it is going to have an 'equation' in its 'expression', or refer to the name of another parameter, then you will need to use the AddByExpression method.  Otherwise you can usually use the AddByValue method.  Below is just one possible example code that is designed to work for any type of document.  Parts, assemblies, and drawings can all have parameters, but they are differently, depending on the document type you are working with.  Not only that, but when using Inventor's API to create parameters, you have to keep units conversion in mind.  Because all 'raw numbers' in a rule, that are supposed to represent any type of 'measurement' value, will be understood as 'database units'.  For length/distance that is centimeters, for angles that is radians, and so on, no matter what units your document is set to.

 

Dim oDoc As Document = ThisDoc.Document
Dim oUParams As UserParameters = Nothing
If (TypeOf oDoc Is AssemblyDocument) OrElse (TypeOf oDoc Is PartDocument) Then
	oUParams = oDoc.ComponentDefinition.Parameters.UserParameters
ElseIf TypeOf oDoc Is DrawingDocument Then
	oUParams = oDoc.Parameters.UserParameters
End If
If oUParams Is Nothing Then Return
Dim sParamName As String = "MyParam1"
Dim oParamValue As Double = 15.125
Dim oUParam As UserParameter = Nothing
Try
	oUParam = oUParams.Item(sParamName)
Catch
	oUParam = oUParams.AddByValue(sParamName, oParamValue, UnitsTypeEnum.kInchLengthUnits)
End Try
If oUParam.Value <> oParamValue Then
	Try
		oUParam.Value = oParamValue
	Catch
		'what to do if setting its value fails
	End Try
End If

 

The only 'iLogic' in that example is the 'ThisDoc.Document' phrase used in the first line.  All the rest is Inventor API.

This example does not include exposing the parameter to create a custom iProperty, but that is also done using additional Inventor API code.

UserParameter.IsKey 

UserParameter.ExposedAsProperty 

After the above property is set to True for that UserParameter, then you will be able to access the following property, and the properties of the object types it returns.  The below line exposes all the additional possible settings you can see within the Format Custom Properties type dialog.

UserParameter.CustomPropertyFormat 

CustomPropertyFormat object

...and so on

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

cstarker6VCG4
Collaborator
Collaborator

Good God, I was expecting a 2-line block at most. All of that to just create and set a parameter? There is no way I am putting that down each time a parameter is written so now this discussion becomes...

How do I make a global (accessible by any iLogic rule) function that sets a pre-existing parameter or creates and sets a non-existing parameter? I understand how to declare a function in the scope of a single rule. Now I need a to create a library to contain this and access it with another rule. This is what I have so far. It doesn't work, of course.

 

Class thisRule
	Sub main()
		Function createSetParameter(sParamName As String, sParamType As String, oParamValue As Decimal)
				Dim oDoc As Document = ThisDoc.Document
			Dim oUParams As UserParameters = Nothing
			If (TypeOf oDoc Is AssemblyDocument) OrElse (TypeOf oDoc Is PartDocument) Then
				oUParams = oDoc.ComponentDefinition.Parameters.UserParameters
			ElseIf TypeOf oDoc Is DrawingDocument Then
				oUParams = oDoc.Parameters.UserParameters
			End If
			If oUParams Is Nothing Then Return
			Dim sParamName As String = "MyParam1"
			Dim oParamValue As Double = 15.125
			Dim oUParam As UserParameter = Nothing
			Try
				oUParam = oUParams.Item(sParamName)
			Catch
				oUParam = oUParams.AddByValue(sParamName, oParamValue, sParamType)
			End Try
			If oUParam.Value <> oParamValue Then
				Try
					oUParam.Value = oParamValue
				Catch
					MessageBox.Show("Could not set parameter.", "Error")
				End Try
			End If
		End Function
	End Sub
end class

 

Secondary question: where is all the official documentation? I looked up how to create function libraries and all I'm getting is forum posts for very specific situations, often 3+ years old. Otherwise, I would not need to ask so often here.

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

😂🤣  I tend to agree.  There is definitely more to it than what we want there to be.  But with more control comes more complexity.  There are lots of possible ways to change the code process, and using a 'referenced' resource is likely the way to go, for the sake of repeatability and efficiency.

I'm on my way out for the day, or I would post more stuff.  Unfortunately, if you are using Inventor 2022 or newer, we also have to work around the possibility that ModelStates may be involved, and whether or not the Document our routine is attempting to work with is either 'the factory' version, or a 'member' version of that Document.  We will not be able to 'write' directly to a 'member' version, so a universal routine for creating/setting a parameter unfortunately needs to be even more complicated than what I showed an example of.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes