Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic to Create User Parameters

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
al.popovich
554 Views, 2 Replies

iLogic to Create User Parameters

Good evening, Friends,

Is there an iLogic code that can create user parameters when they don't exist in a part or assembly file?

A simple example would be a file that currently has no user parameters created. The iLogic would then create user parameters named pThickness, pWidth, and pLength. If those parameters already exist they would be skipped. For our use, these would be numeric parameters.

 

Thank You,  Al

2 REPLIES 2
Message 2 of 3
A.Acheson
in reply to: al.popovich

Here is two samples. Sample 1 is just repetition of the same code so time consuming if you have many parameters. 

Sample 2&3  is using a for loop to work with list/string array. Less code and less errors changing variable names. 

 

Sample 1: 

 

'Get the document 
Dim Doc As Document = ThisDoc.Document

'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters

'Set Paramter Name
Dim ParamName1 As String = "pThickness"
Dim ParamName2 As String = "pWidth"
Dim ParamName3 As String = "pLength"

'Declare as parameter for later use
Dim Param1,Param2,Param3 As UserParameter

Try
	Param1 = userParams.Item(ParamName1)
Catch 'Parameter not found, so create it.
	Param1 = userParams.AddByValue(ParamName1, 0, UnitsTypeEnum.kMillimeterLengthUnits)
	'Param1.Value
End Try
Try
	Param2 = userParams.Item(ParamName2)
Catch 'Parameter not found, so create it.
	Param2 = userParams.AddByValue(ParamName2, 0, UnitsTypeEnum.kMillimeterLengthUnits)
	'Param2.Value
End Try
Try
	Param3 = userParams.Item(ParamName3)
Catch 'Parameter not found, so create it.
	Param3 = userParams.AddByValue(ParamName3, 0, UnitsTypeEnum.kMillimeterLengthUnits)
	'Param3.Value
End Try

 

 

 Sample:2  For loop with String Arrays.

 

'Get the document the rule is launched from.
Dim Doc As Document = ThisDoc.Document

'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters

'Declare as parameter for later use.
Dim Param As UserParameter

'Set Paramter Name Method 2: 1-dimensional string array.
Dim ParamList() As String = {"pThickness", "pWidth", "pLength" }

For Each ParamName In ParamList
	
	Try
		Param = userParams.Item(ParamName)
	Catch 'Parameter not found, so create it.
		Param = userParams.AddByValue(ParamName, 0, UnitsTypeEnum.kMillimeterLengthUnits)
		'Param.Value
	End Try
	
Next

 

 

Sample:3 For loop with List Of String.

 

'Get the document the rule is launched from.
Dim Doc As Document = ThisDoc.Document

'Get the UserParameters collection.
Dim userParams As UserParameters = Doc.ComponentDefinition.Parameters.UserParameters

'Declare as parameter for later use.
Dim Param As UserParameter

'Set Paramter Name Method 1: List Of String.
Dim ParamList As New List(Of String)
ParamList.Add("pThickness")
ParamList.Add("pWidth")
ParamList.Add("pLength")

For Each ParamName In ParamList
	
	Try
		Param = userParams.Item(ParamName)
	Catch 'Parameter not found, so create it.
		Param = userParams.AddByValue(ParamName, 0, UnitsTypeEnum.kMillimeterLengthUnits)
		'Param.Value
	End Try
	
Next

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 3
al.popovich
in reply to: A.Acheson

Thank You very much. I will try these options on Monday at the office.

Best Regards,  Al

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report