Add tagged values to a model
To add a tagged value to a model, use the SetTag
method of the IModel
object. Since IModel
inherits IObject
, you can call the IObject.SetTag
method.
By adding a tag, you can retrieve the model without relying on the set value or profile.
tips
Since the tagged value is not added to the model class, you cannot check the tagged value added to the model from 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", $"Tag exists: {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 the name of an existing tag
model.SetTag("SomeTag", "SomeValue_Update");
tag = model.GetTag("SomeTag");
c.App.Workspace.Output.WriteLine("sample", $"Tag exists: {tag != null}");
c.App.Workspace.Output.WriteLine("sample", $" Key: {tag.Key}");
c.App.Workspace.Output.WriteLine("sample", $" Value: {tag.Value}");
//Remove tag
model.RemoveTag("SomeTag");
tag = model.GetTag("SomeTag");
c.App.Workspace.Output.WriteLine("sample", $"Tag exists: {tag != null}");
}