DM-Script: Extract TagGroups of variable/unknown structure

拟墨画扇 提交于 2020-05-17 03:26:09

问题


My task seemed to be really easy: Use the TagGroup and extract the tree structure and the data of an unknown structure into the result log. So I want to get the name and the value and all the names and values of the children to be displayed in the results. How do I do this?


I have the following (example) Tag structure:

The documentation writes about the TagGroup and also contains one example using TagGroupGetTagType(). The returned value can then be used to find the structure. I wrote and executed the following script:

for(number i = 0; i < tg.TagGroupCountTags(); i++){
    String label = tg.TagGroupGetTagLabel(i);
    number type = tg.TagGroupGetTagType(i, 0);

    result("\nName: " + label + ", Type: " + type);
}

Which gives

Name: Acquisition, Type: 3 // <- should be 0?
Name: Calibration, Type: 3
Name: DataBar, Type: 3 // <- should be 0?
Name: GMS Version, Type: 3 // <- should be 0?
Name: Microscope Info, Type: 3 // <- should be 0?
Name: Session Info, Type: 3 // <- should be 0?

According to the documentation a TagGroup has the type 0. This is not correct for my example. As the image shows Acquisition has children so it should have the type 0 but it has type 3. The same for (most of) the other indices. Type 3 is a long.

(In fact I wrote my own dummy TagGroup. I filled it with data types I know and then I tested the return value TagGroupGetTagType(). For this it seems that the documentation is correct.)

I changed my script to always force to check if the tag group has children assuming that TagGroupCountTags() would return 0 for empty tags but it doesn't:

void showTags(tg){
    for(number i = 0; i < tg.TagGroupCountTags(); i++){
        String label = tg.TagGroupGetTagLabel(i);
        number type = tg.TagGroupGetTagType(i, 0);

        result("\nName: " + label + ", Type: " + type);

        TagGroup child_tg;
        tg.TagGroupGetIndexedTagAsTagGroup(i, child_tg);

        // if(child_tg != NULL){ // <- this does not work either
        showTags(child_tg);
        // }
    }
}

This script crashes because child_tg is null at some time. But also I can't test for null because the comparism is not allowed (Error "Unable to match this argument list to any existing function").

The documentation always knows its Tag structure, so they just use the path to get their values. Also I tried to find any other possibility on how to get if the TagGroup has children. But it seems there is no hasChildren() or any equivalent function. So how do I get the structure of the TagGroup?

Edit: Example data can be found at this follow-up question post


回答1:


Testing for "NULL" of any script object is done with the "IsValid()" method. This may seem odd (how can NULL have a method?) but it's how it works.

So you have:

  • image img --> img.ImageIsValid()
  • imageDocument doc --> doc.ImageDocumentIsValid()
  • ROI r --> r.ROIIsValid()
  • TagGroup tg --> tg.TagGroupIsValid()
  • etc. etc.
  • also: object ob --> ob.ScriptObjectIsValid()

But a simpler solution for your problem is to use:

TagGroup child_tg;
if ( tg.TagGroupGetIndexedTagAsTagGroup(i, child_tg) )
    showTags(child_tg);

The Get... commands return a Boolean indicating success or failure of the operation.




回答2:


So I found some kind of an answer. I am now using

try{
    showTags(child_tg);
}
catch{
    break;
}

This works but still I am not very happy with this. Is there a way to compare to null or check the objects class (is there some kind of instanceof)?



来源:https://stackoverflow.com/questions/60866079/dm-script-extract-taggroups-of-variable-unknown-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!