Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio | Private | Search

 

Example: Controlling Annotations with Component Commands
Function

Simple example showing how to add text, oval and arrow annotations to images as components, and then set the appearance of the component using various commands.

Version
version:20070305, v1.0
Author
D. R. G. Mitchell
Acknowledgements
-
Comments
Creates a blank image on which to position components. Includes a listing of number codes by which DM describes various types of annotations and also lists the control point codes by which they are positioned.
System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
-
Supported
Yes
Included Files
Main Script File
Source Code

// Simple example showing how to add text, oval and arrow annotations as components.

// and then set the appearance of the component using various commands.

// version:20070305

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work)

// version 1.0, March 2007

 

 

/* These are the annotation types

 

2 - line annotation

3 - arrow annotation

4 - double arrow annotation

5 - box annotation

6 - oval annotation

8 - spot mask annotation

9 - array mask annotation

13 - text annotation

15 - bandpass mask annotation

17 - group annotation

19 - wedge mask annotation

 

 

These are the control points to which some of the methods refer

 

1 - start point of line

2 - end point of line

3 - position, used to move annotation ( the position may be any point in the annotation, so it is only good for moving )

4 - top-left of rectangular annotations, one of the bandpass ovals

5 - top-right of rectangular annotations, one of the bandpass ovals

6 - bottom-left of rectangular annotations, one of the bandpass ovals

7 - bottom-right of rectangular annotations, one of the bandpass ovals

8 - other spot of spot mask

9 - one vector of wedge and array masks

10 - other vector of wedge and array masks

11 - top-left of other bandpass oval

12 - top-right of other bandpass oval

13 - bottom-left of other bandpass oval

14 - bottom-right of other bnadpass oval

15 - one spot of spot mask

 

*/

 

// create and display an image to annotate

 

number xsize=512 // the x size of the image we'll annotate

number ysize=512 // the y size of the image we'll annotate

 

image front=integerimage("", 2,1,xsize, ysize)

front=icol+irow // give the image some shading

showimage(front)

setwindowposition(front, 142, 24)

showalert("Three text, five oval and one double arrow annotation will be added to this image.",2)

 

 

// work out the spacings of the text labels

 

number ystep, xstep, ovalsize, annotposx, annotposy

ystep=ysize/4 // position the text annotations on a diagonal equally spaced

xstep=xsize/4

 

 

//Image displays are components too

 

component imgdisp=imagegetimagedisplay(front, 0)

number i

number fontsize =16 // the size of the font we'll use

string fonttype="Microsoft Sans Serif" // the name of the font we'll use

ovalsize=128 // the size of the oval annotation

 

 

// we are going to add three text annotations - labels 1, 2 and 3

 

for(i=0; i<3;i++)

{

annotposx=xstep+(i*xstep)

annotposy=ystep+(i*ystep)

component textannot=newtextannotation(annotposx, annotposy, ""+i, fontsize) // create the labels

 

textannot.componentsetdrawingmode(1) // sets the background to on - ie numbers appear in a filled box

textannot.componentsetforegroundcolor(1,1,0) // sets the foreground colour to yellow

textannot.componentsetbackgroundcolor(0,0,0) // sets the background colour to black

textannot.componentsetfontfacename(fonttype) // sets the font face

imgdisp.componentaddchildatend(textannot) // adds the labels to the image

}

 

 

 

 

// first work out the spacings of the text oval annotations

 

xstep=xsize/6

ystep=ysize/6

 

 

// add five oval annotations

 

for(i=0; i<5; i++)

{

annotposx=xstep+(i*xstep)

annotposy=ystep+(i*ystep)

component ovalannot=newovalannotation(annotposy-ovalsize/2, annotposx-ovalsize/2, annotposy+ovalsize/2, annotposx+ovalsize/2)

ovalannot.componentsetdrawingmode(2) // sets the background to off ie lines are not outlined

ovalannot.componentsetforegroundcolor(1,0,1) // sets the foreground colour to magenta

imgdisp.componentaddchildatend(ovalannot) // add the annotation to the image display

}

 

 

// Set up the parameters for a double arrow annotation

 

number top, bottom, left, right

top=450

bottom=450

left=50

right=450

 

// create the arrow annotation and set the colour and fill mode

 

component arrowannot=newdoublearrowannotation(top, left, bottom, right) // create a double headed arrow bounded by the rectangle

defined by top, left etc.

arrowannot.componentsetforegroundcolor(1,1,0) // make the arrow yellow

arrowannot.componentsetdrawingmode(1) // turn background fill to on

arrowannot.componentsetbackgroundcolor(0,0,0) // set the background fill to black

imgdisp.componentaddchildatend(arrowannot) // add te arrow to the image

 

 

 

// Grab the first oval annotation added and send it on a little trip

 

showalert("The Death Star will soon be in position Lord Vader!",2)

component ovaltomove=imgdisp.componentgetnthchildoftype(6,0)

 

number currentovalx, currentovaly, newovalx

ovaltomove.componentgetcontrolpoint(3, currentovalx, currentovaly)

 

newovalx=currentovalx

 

while(newovalx<xsize)

{

ovaltomove.componentsetcontrolpoint(3, newovalx,currentovaly,0)

updateimage(front)

newovalx=newovalx+10

delay(5)

}

 

 

newovalx=-70

while(newovalx<currentovalx)

{

ovaltomove.componentsetcontrolpoint(3, newovalx,currentovaly,0)

updateimage(front)

newovalx=newovalx+10

delay(5)

}

 

ovaltomove.componentsetcontrolpoint(3, currentovalx,currentovaly,0)

 

 

// Use the control point of an annotation to subsequently reposition it

 

showalert("Top left corner of arrow annotation will now be repositioned.",2)

number newarrowx=50

number newarrowy=50

number contpoint=4

arrowannot.componentsetcontrolpoint(contpoint,newarrowx, newarrowy,0)

 

// control point 4 is the top left of annotations bounded by a rectangle

// the zero parameter restricts style - whatever that means

 

 

 

// This section counts and removes the various annotation types

 

// count the number of text annotations

 

number textannots=imgdisp.componentcountchildrenoftype(13) // counts the number of text annotations (their type is 13)

showalert("There are "+textannots+" text annotations on this image which will now be removed.",2)

 

 

// remove the text annotations

 

for (i=0; i<textannots; i++)

{

component annotid=imgdisp.componentgetnthchildoftype(13,0)

annotid.componentremovefromparent()

}

 

 

// count the number of oval annotations

 

number ovalannots=imgdisp.componentcountchildrenoftype(6) // counts the number of oval annotations (their type is 6)

showalert("There are "+ovalannots+" oval annotations on this image which will now be removed.",2)

 

 

// remove the oval annotations

 

for (i=0; i<ovalannots; i++)

{

component annotid=imgdisp.componentgetnthchildoftype(6,0)

annotid.componentremovefromparent()

}

 

// count the number of oval annotations

 

number arrowannots=imgdisp.componentcountchildrenoftype(4) // counts the number of double arrow annotations (their type is 4)

showalert("There is "+arrowannots+" double arrow annotation on this image which will now be removed.",2)

 

 

// remove the double arrow annotations

 

for (i=0; i<arrowannots; i++)

{

component annotid=imgdisp.componentgetnthchildoftype(4,0)

annotid.componentremovefromparent()

}

 

 

// Clean up

 

showalert("All text, oval and arrow annotations have now been removed. Goodbye.",2)

deleteimage(front)