How to Remove User Defined Tagged Values for All Model Elements Using API

Using tagged values is a convenient way to define additional details to model element. You can define tagged values on individual elements as a one-off usage, or define it under Stereotypes and reuse it by apply the stereotype to model elements. But to remove the tag, say you found that one of the tagged values in your model is no longer valid, you will have to go through model by model to locate and remove them. It is OK for a small project, but a very time consuming job for a large project. Since the Open API provides full access to the model data within the project, we can make use of it to remove the unwanted tagged values. In this article will show you how to remove the user defined tagged values from model element by developing a plugin.

Locate and delete the user defined tagged values

Once you retrieved the model elements from the project, you can use call modelElement.getTaggedValues() to retrieve the ITaggedValueContainer. This container will store all the tagged values for this model element.

ITaggedValueContainer lContainer = aModelElement.getTaggedValues();

After that you can ask the ITaggedValueContainer to return you the tagged value array so that you can loop through the tagged values one by one.

ITaggedValue[] lTaggedValues = lContainer.toTaggedValueArray();

Now try to get the tag definition from the tagged value. If it returns null that means this tag is not attached to any stereotype, so it must be a user defined tag.

if (lTaggedValues[i].getTagDefinition() == null) {
  …
}

After you located the tagged values, you can simply call the delete method to remove it.

lTaggedValues[i].delete();

Sample Plugins

This sample plugin demonstrate how to locate the model elements which having user defined tags and let you to remove them. After deploy the plugin to VP applications, you can click on the plugin button in application toolbar to trigger it.

Trigger the Clear User Defined Tagged Value Plugin

It will then loop through all the model elements in your project and list out those with user defined tagged values, as well as the count.

Clear User-Defined Tagged Values dialog

You can select the model element form the list and press the Clear button to remove its user defined tags, or simply press the Clear All button to remove the user defined tags for all of them.

Remove user defined tagged values form model elements

Download Sample Plugin

You can click this link to download the sample plugin.

Related Articles

26 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply