// Script function: Change units of calibration among ¦Ěm, nm, A, and pm.

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined 
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html

number EMGetUnitsExponent( string EMUnits )
{
number EMExp
if (EMUnits == "¦Ěm" || EMUnits == "um" )                                  
EMExp = -6

else if (EMUnits == "nm")
EMExp = -9

else if (EMUnits == "A" || EMUnits == "A")
EMExp = -10

else if (EMUnits == "pm" )
EMExp = -12

return EMExp
}

string EMGetNewUnit( string EMUnits )
{
if (EMUnits == "um" )                                  
return "¦Ěm"

else if (EMUnits == "A")
return "A"

return EMUnits
}

void EMUnitsChange( image GlobalSinoImage, string EMNewUnits )
{
number EMDimensions = GlobalSinoImage.ImageGetNumDimensions()  

number EMCopyUnitString                         
for (EMCopyUnitString=0; EMCopyUnitString<EMDimensions; EMCopyUnitString++)                                                                      
{      
string EMOldUnits = GlobalSinoImage.ImageGetDimensionUnitString( EMCopyUnitString )

number EMNewExp = EMGetUnitsExponent( EMNewUnits )                        
number EMOldExp = EMGetUnitsExponent( EMOldUnits )

number EMCalibration = GlobalSinoImage.ImageGetDimensionScale( EMCopyUnitString )
GlobalSinoImage.ImageSetDimensionScale( EMCopyUnitString, EMCalibration*(10**(EMOldExp-EMNewExp)))                 
GlobalSinoImage.ImageSetDimensionUnitString( EMCopyUnitString, EMGetNewUnit(EMNewUnits) )                    
}
}

void GlobalSinoChangeImageUnitsDialog()
{      
image GlobalSinoImage := GetFrontImage()
taggroup EMDialog, EMDialogItems
EMDialog = DLGCreateDialog( "Please select your units", EMDialogItems )

TagGroup GlobalSinoTagList = DLGCreateRadioList( )
GlobalSinoTagList.DLGAddRadioItem( "¦Ěm", 0 ).DLGSide("Left")
GlobalSinoTagList.DLGAddRadioItem( "nm", 1 ).DLGSide("Left")
GlobalSinoTagList.DLGAddRadioItem( "A", 2 ).DLGSide("Left")
GlobalSinoTagList.DLGAddRadioItem( "pm", 3 ).DLGSide("Left")

EMDialogItems.DLGAddElement( GlobalSinoTagList )

string EMOldUnitsString = GlobalSinoImage.ImageGetDimensionUnitString( 0 )
GlobalSinoTagList.DLGValue(EMGetUnitsExponent(EMOldUnitsString))
object GlobalSinoDialog = Alloc(UIframe).init(EMDialog)
if ( !GlobalSinoDialog.Pose() ) exit(0)

number EMCopyUnitString = GlobalSinoTagList.DLGGetValue()

string EMUnits
GlobalSinoTagList.DLGGetNthLabel(EMCopyUnitString,EMUnits)
EMUnitsChange( GlobalSinoImage, EMUnits )                                                            
}
GlobalSinoChangeImageUnitsDialog( )                     