iLogic code to create a User Parameter

iLogic code to create a User Parameter

Anonymous
Not applicable
31,298 Views
35 Replies
Message 1 of 36

iLogic code to create a User Parameter

Anonymous
Not applicable

I want to use iLogic to create User Parameters with Multi-Value lists filled with info from an excel spreadsheet. What is the code to create a new User Parameter? I can't seem to find it in the help wiki.

 

Thanks!

0 Likes
31,299 Views
35 Replies
Replies (35)
Message 21 of 36

WCrihfield
Mentor
Mentor

Hi @Biju.Veedu.  I don't know why I didn't think about this earlier, but the FlatPattern object kind of acts like a ComponentDefinition object, because it is derived from it, so it has it's own separate set of parameters and user parameters.  It's user parameters aren't even available to the main parent component definition, because they are at a deeper level.  Here is an iLogic rule I threw together to help you write a series of entries into the flat pattern's own user parameters.  I included several lines of comments to help guide you through what's going on.  You will need to manually edit the parameter specs to the way you want them, after determining that the code works.  I just specified 4 different types of user parameters for a somewhat dynamic test (simple numeric, equation, text, & boolean).  Also, if you're not already aware, any plain numbers typed in iLogic, that represent measurements, will be understood as 'database' units (cm for length, radians for angle, etc), so you may have to either add them as an Expression, or include some math to convert its value to suit your needs.

Here's the rule:

 

iLogicVb.UpdateWhenDone = True
Parameter.UpdateAfterChange = True
MultiValue.UpdateAfterChange = True

Dim oPDoc As PartDocument = ThisDoc.Document
'only work with a Sheet Metal part
If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Exit Sub
Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
If Not oSMDef.HasFlatPattern Then Exit Sub
Dim oFP As FlatPattern = oSMDef.FlatPattern
Dim oUParams As UserParameters = oFP.Parameters.UserParameters
Dim oUParam As UserParameter

'create a List of Arrays of Object
'of Object Type, because the Array will include numbers, strings (text), boolean, and Enum Types 
Dim oToBeCreated As New List(Of Object())
'in each Array we add to the main list, we will be entering the following items, in order
'(0) = specify if the value (below) should be understood as a plain value, or as an expression (involves an equation)
'(1) = the parameter's name
'(2) = the parameter's value or equation (if it's an equation, "enclose it within quotes")
'(3) = specify the units of the parameter
oToBeCreated.Add({"Expression", "Param_1", 25.75 in, UnitsTypeEnum.kInchLengthUnits})
oToBeCreated.Add({"Expression", "Equation_1", "Param_1 + 4.25 in", UnitsTypeEnum.kInchLengthUnits})
oToBeCreated.Add({"Value", "Text_1", "A Text Value", UnitsTypeEnum.kTextUnits})
oToBeCreated.Add({"Value", "Boolean_1", True, UnitsTypeEnum.kBooleanUnits})

'now try to either update or create these user parameters
'remember, an Array is zero (0) based, not 1 pased, so its first item is zero (0)
For Each oSpecs As Object() In oToBeCreated
	Try
		'try to find and update the value of the existing user parameter
		If oSpecs(0) = "Value" Then
			oUParams.Item(oSpecs(1)).Value = oSpecs(2)
		ElseIf oSpecs(0) = "Expression" Then
			oUParams.Item(oSpecs(1)).Expression = oSpecs(2)
		End If
	Catch
		'that failed, so it doesn't exist yet, so create it
		If oSpecs(0) = "Value" Then
			oUParam = oUParams.AddByValue(oSpecs(1), oSpecs(2), oSpecs(3))
		ElseIf oSpecs(0) = "Expression" Then
			oUParam = oUParams.AddByExpression(oSpecs(1), oSpecs(2), oSpecs(3))
		End If
	End Try
Next

RuleParametersOutput
oPDoc.Update2(True) ' True = accept any errors and continue

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 22 of 36

c.neutschK4ZB6
Explorer
Explorer

Is it also possible to use that code with a little modification in drawings?

 

I want to create a parameter that is filled with values by an excel, without manually creating that parameter in the drawing template. E.g. to create a list of coatings as multi value parameter list.

0 Likes
Message 23 of 36

theo.bot
Collaborator
Collaborator

For drawings there is no component definition. Parameters are directly available from the document definition. 

 

here is a quick sample to create a parameter in a drawing. of course it's better to add some checks etc. like the previous codes in this post.

Dim oDoc As DrawingDocument
oDoc = ThisDoc.Document 

'add number
oDoc.Parameters.UserParameters.AddByExpression("Number", "10", UnitsTypeEnum.kMillimeterLengthUnits)

'add Boolean
oDoc.Parameters.UserParameters.AddByValue("Istrue", True, UnitsTypeEnum.kBooleanUnits)

'add text
oDoc.Parameters.UserParameters.AddByValue("Text", "New", UnitsTypeEnum.kTextUnits)
0 Likes
Message 24 of 36

WCrihfield
Mentor
Mentor

Hi @c.neutschK4ZB6.  There is a one line iLogic snippet within the 'System' side of the Snippets in a folder called "Excel Data Links" which is labeled "MultiValue list from Excel".  You might be able to use that to get a list of values from an Excel sheet to a multi-value user parameter in your drawing.

Here is what that snippet's code looks like:

MultiValue.List("d0") = GoExcel.CellValues("filename.xls", "Sheet1", "A2", "A10")

WCrihfield_0-1636742512890.png

You would have to change the name of the Parameter being specified, and that parameter would have to already exist.  Then you would have to fill in the real name of your Excel file (full path may be needed), sheet name, and cell addresses.

And if the parameter doesn't already exist, you could add a line of code above that to create the needed parameter like this.

ThisDrawing.Document.Parameters.UserParameters.AddByValue("ExcelValues", "", "Text")
MultiValue.List("ExcelValues") = GoExcel.CellValues("C:\Temp\Parameter Values.xlsx", "Sheet1", "A2", "A10")

Notice that the parameter is created as a Text type parameter, because I am expecting text type values.  If you are expecting different data type, such as numbers, you would need to change the value and units being specified at the end of the parameter creation line.  There are lots of ways of doing that, but that's about the quickest & simplest.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 25 of 36

urban.ostirZ44XW
Participant
Participant

Hello, is it possible that we have a code like one above that work just in the assembly? So when I would start a code I would like to sum up the area of all the components in the assembly and then multiplied it by the number 0.5 and then wrote the resulting number in the table of parameters. Does someone have an idea how to do it?

0 Likes
Message 26 of 36

WCrihfield
Mentor
Mentor

Hi @urban.ostirZ44XW.  If I am fully understanding your request then I believe I have an iLogic rule that should be able to do a task like that for you.  I also put a few messages in the code for feedback, but commented them out.  Let me know if this works for you as you were wanting.  See below code:

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oTotalArea As Double = oADoc.ComponentDefinition.MassProperties.Area
'MsgBox("Total Area in 'database units' = " & oTotalArea,,"")
oTotalArea = (oTotalArea * .5)
'MsgBox("Half of Total Area in 'database units' = " & oTotalArea,,"")
'<<< convert units here if needed >>>
UOM = oADoc.UnitsOfMeasure
oLengthUnitsStr = UOM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)
If UOM.LengthUnits <> UnitsTypeEnum.kDatabaseLengthUnits Then
	oTotalArea = UOM.ConvertUnits(oTotalArea, "cm^2", oLengthUnitsStr & "^2")
End If
'MsgBox("Half of Total Area in 'document units' = " & oTotalArea,,"")
oUParams = oADoc.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter = Nothing
For Each oUP As UserParameter In oUParams
	If oUP.Name = "HALF_TOTAL_AREA" Then
		oUParam = oUP
		oUParam.Expression = oTotalArea.ToString
	End If
Next
If IsNothing(oUParam) Then
	oUParam = oUParams.AddByExpression("HALF_TOTAL_AREA", oTotalArea.ToString, oLengthUnitsStr & "^2")
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 27 of 36

pawel.swol
Explorer
Explorer

Hello everyone

I do not know how to write that after finding the DL, SZR, GR user parameters, it does not add the next….

below my ilogic:

 

Sub main

nazwa = ThisDoc.FileName(False)
iProperties.Value("custom", "kod_podst") = nazwa
iProperties.Value("custom", "segment") = nazwa



oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParameter=oMyParameter.AddByExpression("DL", "1", UnitsTypeEnum.kMillimeterLengthUnits)
oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParameter=oMyParameter.AddByExpression("SZR", "1", UnitsTypeEnum.kMillimeterLengthUnits)
oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
oParameter=oMyParameter.AddByExpression("GR", "1", UnitsTypeEnum.kMillimeterLengthUnits)

Logger.Info("Nie udało dodać się parametrów GR, SZR, DL")

 

 

dd= Round(Measure.ExtentsLength,2)
ss= Round(Measure.ExtentsWidth,2)
gg= Round(Measure.ExtentsHeight,2)

Dim Komunikat As String

Komunikat = "DL-" & Parameter("DL") & " SZR-" & Parameter("SZR") & " GR-" & Parameter("GR")& "."

MessageBox.Show(Komunikat, "WYMIARY")


'MessageBox.Show(Parameter("SZR"), "SZR")
'MessageBox.Show(Parameter("GR"), "GR")


If gg<ss And gg<dd Then
Parameter("GR") = gg
DL = InputRadioBox("Wybierz DL", ss, dd, booleanParam, nazwa)
If DL=False Then
Parameter("DL")=dd
Parameter("SZR")=ss
End If
If DL=True Then
Parameter("DL")=ss
Parameter("SZR")=dd
End If
End If

If dd<ss And dd<gg Then
Parameter("GR") = dd
DL = InputRadioBox("Wybierz DL", ss, gg, booleanParam, nazwa)
If DL=False Then
Parameter("DL")=gg
Parameter("SZR")=ss
End If
If DL=True Then
Parameter("DL")=ss
Parameter("SZR")=gg
End If
End If

 


If ss<gg And ss<dd Then
Parameter("GR") = ss
DL = InputRadioBox("Wybierz DL", dd, gg, booleanParam, nazwa)
If DL=False Then
Parameter("DL")=gg
Parameter("SZR")=dd
End If
If DL=True Then
Parameter("DL")=dd
Parameter("SZR")=gg
End If
End If

If ss=gg Or ss=dd Or dd=gg Then


If ss<dd Or ss<gg Then
Parameter("GR") = ss
If dd>gg Then
Parameter("DL")=dd
Parameter("SZR")=gg
Else
Parameter("DL")=gg
Parameter("SZR")=dd
End If
End If


If gg<ss Or gg<dd Then
Parameter("GR") = gg
If dd>ss Then
Parameter("DL")=dd
Parameter("SZR")=ss
Else
Parameter("DL")=ss
Parameter("SZR")=dd
End If
End If


If dd<gg Or dd<ss Then
Parameter("GR") = dd
If gg>ss Then
Parameter("DL")=gg
Parameter("SZR")=ss
Else
Parameter("DL")=ss
Parameter("SZR")=gg
End If
End If
End If

 

End Sub

 

thanks

 

0 Likes
Message 28 of 36

cubbanec
Observer
Observer

Hello everyone, thank you very much for these threads, they help me a lot at work and iLogic. I can't program but I manage to make small progress with code. Using the instructions from the thread, I was able to establish user parameters using a rule. But I can't figure out how to set the parameter as exported. Can you help me please? Thank you very much and greetings from the Czech Republic

 

oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
'Setting an userParameter as text parameter
oParameter=oMyParameter.AddByValue("Hmotnost", "", UnitsTypeEnum.kTextUnits)
oParameter = oMyParameter.AddByValue("Povrch", "", UnitsTypeEnum.kTextUnits)
iLogicVb.UpdateWhenDone = True

Param.PNG
0 Likes
Message 29 of 36

WCrihfield
Mentor
Mentor

Hi @cubbanec.  We were just given the ability to 'expose/export' UserParameter objects that represent 'text' or 'Boolean' units in the 2024 version o....  So, if you are using 2024, then you should be able to add another line of code after you create the parameter, just for setting its 'ExposedAsProperty' property to True.  After you set that to True, you can then access its UserParameter.CustomPropertyFormat property, and all of its added functionality, which is similar to the settings you see when you right click on a parameter in the Parameters dialog box, after checking that box, and choose Custom Property Format from the right-click menu.

Dim oUParams As UserParameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
'Setting an userParameter as text parameter
Dim oUParam1, oUParam2 As UserParameter
oUParam1 = oUParams.AddByValue("Hmotnost", "", UnitsTypeEnum.kTextUnits)
oUParam1.ExposedAsProperty = True
oUParam2 = oUParams.AddByValue("Povrch", "", UnitsTypeEnum.kTextUnits)
oUParam2.ExposedAsProperty = True
iLogicVb.UpdateWhenDone = True

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

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 30 of 36

n_lindner
Community Visitor
Community Visitor

Hello

So Far with the Examples in this Thread is Managed to Build this Following Code.
But i just dont understand how to Change the added User Parameter from beeing an "Text" Value to a Lenght (mm) Value.

I mainly Modified Line 4 from "Of String" to "Of Double" as i need the User Parameter to be an "Lenght" Type
Accordingly to this Line 21 is Modifyed by me.

Help Please

Dim oUParams As UserParameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter
Dim sName As String = "Blechdicke_D1"
Dim oValues As New List(Of Double)
oValues.Add(0.6)
oValues.Add(0.75)
oValues.Add(0.8)
oValues.Add(1.0)
oValues.Add(1.25)
oValues.Add(1.5)
oValues.Add(2.0)
oValues.Add(3.0)
oValues.Add(4.0)
oValues.Add(5.0)
oValues.Add(8.0)
oValues.Add(10.0)
Try
    oUParam = oUParams.Item(sName)
    MultiValue.SetList(sName, oValues.ToArray)
Catch
	oUParam = oUParams.AddByValue(sName, "", UnitsTypeEnum.kMillimeterLengthUnits)
    MultiValue.SetList(sName, oValues.ToArray)
End Try

 

0 Likes
Message 31 of 36

WCrihfield
Mentor
Mentor

Hi @n_lindner.  In your code, Line 21 is specifying the initial value as an empty String (""), which should be a number like an unquoted 1.0, instead.  Everything else looks OK to me.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 32 of 36

kwilson_design
Collaborator
Collaborator

Hey everyone, some great stuff in this post! I'm running into a snag trying to get this to work with unitless parameters and keep getting an argument error. Will this not work with anything beyond text units?

 

oMyParameter=ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
'Setting an userparameter as unitless parameter
oParameter = oMyParameter.AddByValue("ESTIMATED_GALV_WEIGHT", "GALV_WEIGHT_MULTIPLIER * WEIGHT", UnitsTypeEnum.kUnitlessUnits)
oParameter=oMyParameter.AddByValue("GALV_WEIGHT_MULTIPLIER", "1.06", UnitsTypeEnum.kUnitlessUnits)
oParameter = oMyParameter.AddByValue("WEIGHT", "", UnitsTypeEnum.kUnitlessUnits)

 

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
Message 33 of 36

WCrihfield
Mentor
Mentor

Hi @kwilson_design.  The initial value you are specifying is within quotation marks.  That will not work if the value is supposed to be interpreted as a Unitless value.  When the value is in quotes, it is seen as a String (text).  Remove the quotes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 34 of 36

kwilson_design
Collaborator
Collaborator

Hey @WCrihfield I need the text to be populated and it still be unitless though since I'm telling it to multiply the Weight x 1.06. I couldn't create the Estimated Galv Weight parameter as a text parameter and it be able to detect or multiply using the * character.

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
0 Likes
Message 35 of 36

kwilson_design
Collaborator
Collaborator

Hey @WCrihfield I figured my issue out. In case anyone is trying to something similar using unitless parameters, I just changed it to expressions instead of values. I also changed the order in which the snippet is read as that was also causing some issues. If you're using parameters of other parameters to do conversions and math, be sure you have the order right so that the "math" parameter is after the other parameters are written.  This is what worked for me....

 

oParameter=oMyParameter.AddByExpression("GALV_WEIGHT_MULTIPLIER", "1.06", "ul")
oParameter=oMyParameter.AddByExpression("WEIGHT", "0", "ul")
oParameter=oMyParameter.AddByExpression("ESTIMATED_GALV_WEIGHT", "GALV_WEIGHT_MULTIPLIER * WEIGHT", "ul")

  

Regards,
Kenny
If this post solved your issue please mark "Accept as Solution". It helps everyone...really!
Message 36 of 36

WCrihfield
Mentor
Mentor

Glad to see that you figured it out.  Sorry, I did not realize you wanted to maintain the equation within the one Parameter.  For some reason I just assumed you only needed the numerical value that resulted from that equation to be recorded.  I do use the AddByExpression method the most myself, because it allows me to input values that I know will be interpreted the way I intend for them to be interpreted, instead of being automatically understood as 'database units'.  I also usually use the Parameter.Expression (instead of Parameter.Value) when setting the value of a Parameter/UserParameter/ModelParameter type API objects for the same reasons, but that only works good for setting a specific numerical value when there is not already an equation there, and that can be difficult to detect since it always contains both numerical values and text (units indicator).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)