You need a couple of changes to make it work, and there's a few different ways to do it. Here's a modified version of your rule showing AddByExpression as well as AddByValue:
Sub Main
Dim oPartDoc As Document
oPartDoc = ThisDoc.Document
If oPartDoc.DocumentType = kPartDocumentObject Then
Dim oPartCompDef As PartComponentDefinition
oPartCompDef = oPartDoc.ComponentDefinition
Dim oParams as Parameters
oParams=oPartCompDef.Parameters
Dim oUserParams as UserParameters
oUserParams=oParams.UserParameters
Dim oGravityX As Parameter
If (Not ParameterExists(oParams, "GravityX")) Then
oGravityX=oUserParams.AddByValue("GravityX", 9.81/10, "mm")
End If
Dim oGravityY As Parameter
If (Not ParameterExists(oParams, "GravityY")) Then
oGravityY=oUserParams.AddByExpression("GravityY", "9.81 mm", UnitsTypeEnum.kMillimeterLengthUnits)
End If
Else
Exit Sub
End If
End Sub
Function ParameterExists(oParams as Parameters, parameterName As String)
Try
Dim param as Parameter = oParams(parameterName)
Catch
Return False
End Try
Return True
End Function
1) Gravity is a predefined constant for parameter equations. So I changed it to GravityX and GravityY in this sample.
2) The AddByValue function needs a number for the second argument. It can't be in quotes.
3) In iLogic (and VB.NET), you have to add the enumeration type (in this case UnitsTypeEnum) in front of Inventor constants that start with "k". You can find the enum type to use by searching for the constant (in this case kMillimeterLengthUnits) in the API help. You can use the statement:
Option Explicit On
at the top of the rule to see where enumeration types are required.
4) Instead of using the constant, you can use a unit string such as "mm". You will need a unit string if you want to create a parameter with compound units (such as acceleration due to gravity).
5) I would recommend using AddByExpression instead of AddByValue. It gives you more flexibility. Note that in the AddByValue function, you have to specify the value in centimeters (or other predefined database units).
6) Since the rule might be run more than once, you should check to see if the parameter already exists. I added a function to do that.
Mike Deck
Software Developer
Autodesk, Inc.