Adding Tagged Values to a Model
To add tagged values to a model, use the SetTag method of the IModel object. Since IModel inherits from IObject, you can call the IObject.SetTag method.
By adding tags, you can retrieve the model without depending on the set values or profiles.
Since you are not adding tagged values to the model class itself, you cannot see the added tagged values in the Inspector.
public void SetTag(ICommandContext c, ICommandParams p)
{
/ Get the currently displayed model
IModel model = c.App.Workspace.CurrentModel;
/ Add a tag to the model
model.SetTag("SomeTag", "SomeValue");
ITag tag = model.GetTag("SomeTag");
c.App.Workspace.Output.WriteLine("sample", $"Does the tag exist: {tag != null}");
c.App.Workspace.Output.WriteLine("sample", $"Key: {tag.Key}");
c.App.Workspace.Output.WriteLine("sample", $"Value: {tag.Value}");
/ You can update the tag value by specifying an already existing tag name
model.SetTag("SomeTag", "SomeValue_Update");
tag = model.GetTag("SomeTag");
c.App.Workspace.Output.WriteLine("sample", $"Does the tag exist: {tag != null}");
c.App.Workspace.Output.WriteLine("sample", $"Key: {tag.Key}");
c.App.Workspace.Output.WriteLine("sample", $"Value: {tag.Value}");
/ Remove the tag
model.RemoveTag("SomeTag");
tag = model.GetTag("SomeTag");
c.App.Workspace.Output.WriteLine("sample", $"Does the tag exist: {tag != null}");
}
When you retrieve or set a tag value using the SetTag method or the GetTagValue method, the model file in the automatic load settings will be loaded. On the other hand, APIs such as GetTag, which checks for the presence of tags, and the RemoveTag method, which removes tags, do not load the model file. For unloaded models, even if tags are set in the model file, they will be treated as "untagged," and the RemoveTag method will do nothing. For details, please refer to Changes in V5.1.