Updating Field Values
This section explains the basics of updating field values in a model.
When you retrieve or set field values, the necessary model files are automatically loaded before processing continues. Therefore, execution time may be longer than before. However, model files with manual load settings will not be loaded even if field values are referenced, and changes will result in an error. For details, please refer to Changes in V5.1 and Behavior of Model Files with Manual Load Settings.
The field types covered in this explanation are primitive types such as strings and integers, and enumeration types. For updating class-type fields, please refer to the following depending on your purpose:
To update field values of primitive types and enumeration types, use the SetField method of the IModel object.
Updating String, Integer, and Real Number Field Values
To update string, integer, and real number 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
/ Sets the string "Sample" to a string field named "StringField"
model.SetField("StringField", "Sample");
/ Integer
/ Sets the integer value 100 to an integer field named "IntegerField"
model.SetField("IntegerField", 100);
/ Real Number
/ Sets the real number 123.45 to an integer field named "IntegerField"
model.SetField("DoubleField", 123.45);
}
It is also possible to process the field values of the current model and set them 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 Name field value by adding "_" to the end
model.SetField("Name", $"{name}_");
}
Updating Enumeration Field Values
To update an enumeration field value, specify the enumeration literal 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;
/ Get the enumeration type which is the field type
IField enumField = model.Metaclass.GetField("EnumField");
IEnum type = enumField.TypeEnum;
/ Get the enumeration literal value which is the setting value
/ To set an enumeration literal value named "SampleLiteralA"
IEnumLiteral literal = type.NameOf("SampleLiteralA");
model.SetField("EnumField", literal);
}
Updating Boolean Field Values
To update a boolean field value, specify the boolean value to 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);
}