Updating field values
This section explains the basics of updating field values for models. The field types covered are primitive types such as strings and integers, and enumerations. For updating class-type fields, see the following for your purpose.
- Adding a model
- Relating models
- Moving a model
- Deleting a model
To update primitive and enumeration field values, use the
SetField
method of theIModel
object.
Updating string, integer, and real field values
To update string, integer, and real field values, specify the setting value as the second argument of the SetField
method of the IModel
object.
public void SetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//String
//Set the string "Sample" to the string field named "StringField".
model.SetField("StringField", "Sample");
//Integer value
//Set the integer value 100 to the integer field named "IntegerField".
model.SetField("IntegerField", 100);
//Real value
//Set the real value 123.45 to the integer field named "IntegerField".
model.SetField("DoubleField", 123.45);
}
You can also modify the field value of the current model and set it to the same field.
public void SetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Get the value of the name field
var name = model.GetFieldString("Name");
//Update the value of the name field by adding "_" to the end
model.SetField("Name", $"{name}_");
}
Update an enumeration field value
To update an enumeration field value, specify the enumeration literal value to be set in the second argument of the SetField
method of the IModel
object.
public void SetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Get the enumeration type of the field.
IField enumField = model.Metaclass.GetField("EnumField");
IEnum type = enumField.TypeEnum;
//Get the enumeration literal value to be set.
//To set an enumeration literal value named "SampleLiteralA", use the following:
IEnumLiteral literal = type.NameOf("SampleLiteralA");
model.SetField("EnumField", literal);
}
Update a boolean field value
To update a boolean field value, specify the boolean value to be set as the second argument of the SetField
method of the IModel
object.
public void SetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Sets the boolean field named "BooleanField" to true
model.SetField("BooleanField", true);
}