How to remove the text with the line annotation

33 Views Asked by At

When I used line with length label to measure the distance, it will draw a line and a text on the image. The text is the length. I'd like to remove all the line and the text with line, while keep other text components. I can use the code below to remove all the line. But I have no idea how to remove the text with line.

Void clear_line_annot(Image img)
{
    Number k_line_annot = 2
    ImageDisplay img_disp = img.ImageGetImageDisplay(0)
    Number comp_num = img_disp.ComponentCountChildren()

    for (Number i = comp_num - 1; i >= 0; i--)
    {
        Component comp = img_disp.ComponentGetChild(i)
        if (comp.ComponentGetType()!= k_line_annot) Continue
        comp.ComponentRemoveFromParent()
    }
}

Image img := GetFrontImage()
clear_line_annot(img)
1

There are 1 best solutions below

3
BmyGuest On BEST ANSWER

This is indeed "tricky" as lines and their labels are just annotations are just regular line and text annotations with no easily accessible property telling about their linkage. Luckily, some commands exist to address line-labels:

  • LineAnnotationRemoveLengthLabel()
  • LineAnnotationAddLengthLabel()
  • LineAnnotationUpdateLengthLabel()

F1 help

So, this should work. (Note the use of the While() loop, as each iteration now removes two components.):

Void clear_line_annot(Image img)
{
    Number k_line_annot = 2
    ImageDisplay img_disp = img.ImageGetImageDisplay(0)
    while( 0 < img_disp.ComponentCountChildrenOfType(k_line_annot)  )
    {
       Component comp = img_disp.ComponentGetNthChildOfType(k_line_annot,0)
       comp.LineAnnotationRemoveLengthLabel()
       comp.ComponentRemoveFromParent()
    }
}

Image img := GetFrontImage()
clear_line_annot(img)

There is another pair of commands which I sometimes find useful: Component objects can serialize themselves to and from tagGroups - that's what happens when you save or load an imageDocument as well. This can also be done by script:

image img:=RealImage("Test",4,100,100) = icol
img.ShowImage()
imageDisplay disp = img.ImageGetImageDisplay(0)

component comp = NewTextAnnotation(disp,30,30,"Test text\n2nd line", 12)
disp.ComponentAddChildAtEnd(comp)

TagGroup tg = NewTagGroup()
comp.ComponentExternalizeProperties(tg)
If (TwoButtonDialog("Show Externalized?","Yes","No"))
    tg.TagGroupOpenBrowserWindow("Component serialized",0)

If (TwoButtonDialog("Modify ?","Yes","No"))
{
    tg.TagGroupSetTagAsLong("TextFormat:DrawingMode",1) 
    tg.TagGroupSetTagAsFloat("Transparency",0.6)
    comp.ComponentInternalizeProperties(tg)
}