I'm using a TagGroup to store flags and parameters, and I encountered an issue when trying to read a tag's value. When using TagGroupGetTagAsBoolean(tag_path, value)
, if the tag_path
does not exist, nothing is returned and the variable retains its initial value (e.g., 0
).
My Problem:
I need a reliable way to determine whether a given tag_path
exists. One approach would be to use a command to check for the tag's existence, but I have a large number of tags to process. I'm looking for a better or more efficient solution.
Code Example:
String config_path = "C:\\Program Files\\Gatan\\Plugins\\config.gtg";
TagGroup ui_config_tg = NewTagGroup();
ui_config_tg.TagGroupLoadFromFile(config_path);
Number jpg_flag = 0; // Initialized to 0
ui_config_tg.TagGroupGetTagAsBoolean("flag:jpg_flag", jpg_flag);
// At this point, if "flag:jpg_flag" does not exist,
// jpg_flag remains 0. How can I determine whether the tag exists?
Questions:
tag_path
exists in a TagGroup?Any suggestions or alternative approaches would be greatly appreciated!
In the documentation, you may notice that every function of the form TagGroupGetTagAsXXX returns a Boolean value. It is this returned boolean that tells you whether the function was able to find the tagged value and return it successfully. In other words, if the specified tag path does not exist, then the function will return false, otherwise it returns true. So a better way to implement your sample code would be as follows:
String config_path = "C:\\Program Files\\Gatan\\Plugins\\config.gtg"
TagGroup ui_config_tg = NewTagGroup()
ui_config_tg.TagGroupLoadFromFile(config_path)
Number jpg_flag
Number tagIsValid = ui_config_tg.TagGroupGetTagAsBoolean("flag:jpg_flag", jpg_flag)
if (!tagIsValid)
{
// Insert code here to initialize jpg_flag with a default value
// You may also wish to set this default value at the missing tag
// path through the use of the TagGroupSetTagAsBoolean function.
}