Get field value
This section explains the basics of getting field values for a model.
Metamodel definition and field value type
In Next Design, the model holds field values in the C# language data type that corresponds to the field type defined in the metamodel. The relationship between the field type and the C# language data type exposed by the API is as follows.
Field Type | C# language data type |
---|---|
String | string |
Integer | int |
Double | double |
Boolean | bool |
Enum Type | IEnumLiteral |
Rich text type | *1 |
Class type (singular) | IModel |
Class type (plural) | IModelCollection |
The rich text type is held in a special data format that holds multiple texts for each format (text|html|xaml). This data format is not exposed in the API. Most APIs for retrieving field values use unformatted plain text as the string type. To handle rich text field values with formatting, see Working with Rich Text Fields.
The API exposes several methods in the IModel
interface to retrieve model field values.
Method | Description |
---|---|
GetField, GetFieldAt | Can be used to retrieve the C# data type exposed in the API. For details, see here. |
GetFieldString, GetFieldStringAt | Can be used to always retrieve the field as a string, regardless of the field type. For details, see here. |
GetFieldValues | Can be used to retrieve a model collection of class-type (multiple) field types. For details, see here. |
GetRichTextField | Can be used to specify a format and obtain a rich text type field value. For details, see here. |
Obtaining as an object type
To obtain a field value as an object type, use the GetField
method of the IModel
object. The actual object type obtained will vary depending on the type of the metamodel field, so you will need to cast it before using it. The relationship between the field type and the C# type is as follows:
Field Type | C# type to which object can be cast |
---|---|
String | string |
Integer | int |
Double | double |
Boolean | bool |
Enum Type | IEnumLiteral |
Rich Text Type | string *2 |
Class Type (Singular) | IModel |
Class Type (Plural) | IModel *3 |
When the GetField
method is called for a rich text type field, plain text without formatting is obtained as a string type.
When the GetField
method is called for a class type field with a multiplicity of 2 or more, the first model of the corresponding field is obtained. If you want to obtain a model other than the first, specify a 0-based index with the GetFieldAt
method, or obtain a collection of models using the GetFieldValues
method.
public void GetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//If the field name is "SomeField"
object someFieldValue = model.GetField("SomeField");
//...
}
Get as string type
Field values can be obtained as strings regardless of the field type. To get as string type, use the GetFieldString
method of the IModel
object. The relationship between the field type and the string that can be obtained is as follows.
Field Type | String that can be obtained |
---|---|
String | String of the field value |
Integer | String representation of the field value (integer) |
Double | String representation of the field value (real number). Depending on the value of the real number, it may be expressed in exponential notation. |
Boolean | "True" or "False" |
Enum Type | Name of enumeration literal |
Rich Text | Plain text without formatting |
Class Type (Singular) | Model name |
Class Type (Plural) | Model name *4 |
When the GetField
method is called for a class type field with a multiplicity of 2 or more, the first model name of the field is obtained. If you want to obtain a model other than the first, specify a 0-based index with the GetFieldStringAt
method.
public void GetFieldString(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
string someFieldValue = model.GetFieldString("SomeField");
//...
}
Get the literal value of an enumeration
In NextDesign, enumeration field values are stored as enumeration literal types (IEnumLiteral
).
The literal value of an enumeration can be obtained as an object
type using the GetField
method of the IModel
object, then cast to an enumeration literal type (IEnumLiteral
) and obtained using the Name
property of the IEnumLiteral
object.
public void GetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
IEnumLiteral someFieldValue = model.GetField("SomeField") as IEnumLiteral;
if (someFieldValue == null)
{
c.App.Window.UI.ShowMessageBox("Not an enumeration field value.");
return;
}
c.App.Window.UI.ShowMessageBox($"SomeField literal: {someFieldValue.Name}");
//...
}
You can also get the literal value directly by using the GetFieldString
method of the IModel
object.
Get boolean value
To get boolean value, use GetField
method of IModel
object to get object
type, then cast to boolean type.
public void GetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
bool? someFieldValue = model.GetField("SomeField") as bool?;
if (!someFieldValue.HasValue)
{
c.App.Window.UI.ShowMessageBox("Field value is not boolean type.");
}
//...
}
Get model
If the field type is a single class type, use GetField
method of IModel
object to get object
type, then cast to IModel
type.
public void GetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//If the field name is "Actor"
IModel actor = model.GetField("Actor") as IModel;
if (actor == null)
{
c.App.Window.UI.ShowMessageBox("Not a class type field value.");
}
//...
}
If the field type is a multiple class type (the upper limit of multiplicity is not 1), you can get the model at any position by specifying a 0-based index with the GetFieldAt
method of the IModel
object. The GetFieldAt
method returns a value of type object
, so cast it to type IModel
. The number of field values can be found with the Count
method of the IModel
object.
public void GetFieldAt(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//If the field name is "Usecases"
var count = model.Count("Usecases");
if (count > 0)
{
//Get the model at the end of the field
//The index is 0-based
var index = count - 1;
IModel usecase = model.GetFieldAt("Usecases", index) as IModel;
}
}
Get a collection of models
If the field type is a multiple class type (the upper limit of multiplicity is not 1), you can get a collection of models by using the GetFieldValues
method of the IModel
object.
public void GetField(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//If the field "SubComponents" is a collection of models
IModelCollection subComponents = model.GetFieldValues("SubComponents") ;
//subComponent is of type IModel
foreach (var subComponent in subComponents)
{
var someFieldValue = subComponent.GetFieldString("SomeField");
//...
}
}