Get a list of field types
This is an example that gets the field types and the number of data points for reference fields from the model selected in the editor.
public void OutputFields(ICommandContext c, ICommandParams p)
{
//Get the first model selected in the model editor
var model = c.App.Window.EditorPage.CurrentEditorView.SelectedModels.FirstOrDefault();
if (model == null) {
c.App.Window.UI.ShowMessageBox("Please select one target model in the model editor");
return;
}
c.App.Output.WriteLine("Default", $"Model name: {model.Name}, Metamodel name: {model.Metaclass.DisplayName}({model.Metaclass.Name})");
//Get the field and its type name of the target model
//For reference fields, get the number of reference data as well
var fields = model.Metaclass.GetFields(); //IFieldCollection type
foreach (var field in fields)
{
if (field.Name.StartsWith("___"))
{
//Ignore system fields
continue;
}
c.App.Output.WriteLine("Default", $" Field: {field.DisplayName}({field.Name}), <Type name: {field.Type}>");
if (field.TypeClass != null)
{
c.App.Output.WriteLine("Default", $" Number of reference data: {model.GetFieldValues(field.Name).Count}");
}
}
}
Note
When retrieving fields using the API, system-defined fields can also be retrieved at the same time. System-defined fields follow the following rules, so exclude them if you do not need them.
- The name (
IField.Name
) is prefixed with "$" - The name (
IField.Name
) is prefixed with "___"