How can I use an iLogic rule to automatically create custom parameters of a model or custom iProperties? How to handle situations when they already exist?
For both cases, you can use the coding construction Try Catch, ie. attempt to access the parameter or iProperty and catch any possible failure of this access.
The code for creating the iProperty "MyProperty" then looks as follows:
Dim propertyName As String = "MyProperty"
Dim propertyValue As String = "Test"
customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
Try
prop = customPropertySet.Item(propertyName)
Catch
customPropertySet.Add("", propertyName)
End Try
iProperties.Value("Custom", propertyName) = propertyValue
The iLogic rule code for conditional creation of a custom/user parameter "MyParameter" can look e.g. as follows (assuming running the rule in an IPT part document):
Dim oParams As Parameters
Dim oPartDoc As PartDocument = ThisDoc.Document
Dim oPartCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
oParams = oPartCompDef.Parameters
Dim oUserParams As UserParameters = oParams.UserParameters
Try
p = Parameter("MyParameter")
Catch
oUserParams.AddByValue("MyParameter","MyValue", UnitsTypeEnum.kTextUnits)
End Try