Gatan | AMETEKSkip to Main Content
No options found

Set all scale bars to same style

DigitalMicrograph Script

This automation script will set the style and size of the scale-bar annotations of all currently shown images.

Preview

// This script makes the scalebar of all displayed images the same 
// in style and relative size.
// Optionally, it adds/removes the scalebar at all
number kEnsureScalebar = 1

void SetScaleBarToStyle( image img, number doAddIfMissing )
{
	// Style to be used
	number relT = 0.8	// 80% from top
	number relL = 0.1	// 10% from left
	number relB = 0.9	// 10% from bottom 
	number relR = 0.9	// 10% from right
	number moveable = 0	// 'freeze' into place
	string font = "Arial"
	number fontSize = 12	
	rgbnumber fCol = RGB( 0, 100, 230 )
	
	// Get display and verify it is suitable 
	if ( !img.ImageIsValid() ) return
	if ( 0 == img.ImageCountImageDisplays() ) return
	imageDisplay disp = img.ImageGetImageDisplay( 0 )
	
	number kRASTER = 1	// Display Type Raster-image
	number kRGB    = 4	// Display Type RGB-Raster-image
	number dispType = disp.ImageDisplayGetDisplayType() 
	if ( !((kRGB == dispType)||(kRASTER == dispType)) ) return
	
	// Find scale-bar annotation
	number kSCALEBAR = 31
	component scalebar
	if ( 0 == disp.ComponentCountChildrenOfType( kSCALEBAR ) ) 
	{
		if ( !doAddIfMissing ) return
		
		scalebar = NewComponent( kSCALEBAR, 0, 0, 1, 1 )
		disp.ComponentAddChildAtEnd( scalebar )
	}
	
	
	number nComp = disp.ComponentCountChildren()
	for ( number i = 0 ; i < nComp; i++ )
	{
		scalebar = disp.ComponentGetChild( i )
		if ( kSCALEBAR == scalebar.ComponentGetType() )
			break;
	}
	
	// now adjust
	number imgW = img.ImageGetDimensionSize( 0 )
	number imgH = img.ImageGetDimensionSize( 1 )
	scalebar.ComponentSetRect( relT * imgH, relL * imgW, relB * imgH, relR * imgW )
	scalebar.ComponentSetFontFaceName( font )
	scalebar.ComponentSetFontSize( fontSize ) // annotion height & font-size are interlinked
	scalebar.ComponentSetForegroundColor( Red(fcol)/255, Green(fcol)/255, Blue(fcol)/255 )
	scalebar.ComponentSetMovable( moveable )
	scalebar.ComponentSetSelected( 0 )
}

// Perform action on all image displays
image img 
GetFrontImage( img )
while( img.ImageIsValid() )
{
	SetScaleBarToStyle( img, kEnsureScalebar )
	img := FindNextImage( img )
}