DM-Script: Извлечение TagGroups переменной / неизвестной структуры - PullRequest
1 голос
/ 26 марта 2020

Моя задача казалась очень простой: используйте TagGroup и извлеките древовидную структуру и данные неизвестной структуры в журнал результатов. Поэтому я хочу получить имя и значение, а также все имена и значения дочерних элементов, которые будут отображаться в результатах. Как мне это сделать?


У меня есть следующий (пример) Структура тегов:

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 // 

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 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 последующее сообщение с вопросом

Ответы [ 2 ]

1 голос
/ 26 марта 2020

Проверка на "NULL" любого объекта сценария выполняется методом "IsValid ()". Это может показаться странным (как NULL может иметь метод?), Но это как он работает.

Итак, у вас есть:

  • image img -> img.ImageIsValid()
  • imageDocument doc -> doc.ImageDocumentIsValid()
  • ROI r -> r.ROIIsValid()
  • TagGroup tg -> tg.TagGroupIsValid()
  • et c. et c.
  • также: object ob -> ob.ScriptObjectIsValid()

Но более простым решением для вашей проблемы является использование:

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

Команды Get... возвращают логическое значение, указывающее на успех или неудачу операции.

0 голосов
/ 26 марта 2020

Так что я нашел какой-то ответ. Я сейчас использую

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

Это работает, но все же я не очень доволен этим. Есть ли способ сравнить с null или проверить класс объектов (есть какой-то instanceof)?

...