Scripting Resources for DigitalMicrograph™ |
Prepare for Printing |
|
Function |
Places the foremost image on a page and adds footer information based on the microscope info. A good way of printing a single image. |
Version |
version:20090517 |
Author |
D. R. G. Mitchell - based on the original script by Mike Kundmann, Gatan. |
Acknowledgements |
Mike Kundmann wrote this script - Dave Mitchell just tinkered with it a bit. |
Comments |
This script will look for an Gatan .dm3 file called 'Company Logo', (minus the quotes) inside the DigitalMicrograph folder - create such a file with your company/institue's logo. |
System Requirements |
Should work on all versions of DigitalMicrograph 3.3.1 and later. |
Known Issues |
If the Company Logo file (see above) is not found, an error is generated, but the page is still displayed for printing. |
Supported |
Yes. |
Included Files |
Script file. |
Source Code |
//*********************************************************************** // Prepare For Printing // v1.2 // // Enhances the page layout of the frontmost image, adding descriptive // information and a logo to yield a standard printout format. // The logo graphic must be in a DM image named "Company Logo" that // is stored in the DigitalMicrograph folder. Requires DM 3.3.1. or later // adminnospam@dmscripting.com (remove the nospam to make this address work) // version:20090517
// First written: 26 April 1998 // By: Mike Kundmann, Gatan TEM Applications Software // Last modified: 28 April 1999 // By: Mike Kundmann, Gatan TEM Applications Software //***********************************************************************
// Minor revisions by D. Mitchell Jul 2006, version 1.1 // Current DM does not default to the application directory // where the image 'Company Logo' is stored - fixed that // Also, cloned the image and printed that, leaving the original untouched. // Also added a direct link to the print dialog
// May 2009 v1.2 ensured that text is always black on white - later DM updates // seem to have caused text to default to white (on white!). //***********************************************************************
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Make sure a suitable image exists. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Image img := GetFrontImage() ImageDocument imgDoc = ImageGetOrCreateImageDocument(img) if (imgDoc.ImageDocumentCountImages() > 1) { String msg = "This print preparation script is only for " msg = msg + "image documents that contain one image." Throw(msg) }
// Clone the image and print the clone, leaving the original untouched number xsize, ysize showimage(img)
beep() if(twobuttondialog("Create a copy of this image before printing?","Yes", "No")) { choosemenuitem("Edit","","Duplicate Image") showimage(img) }
imgDoc = ImageGetOrCreateImageDocument(img) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Get the page bounds, check that portrait // orientation is selected, and display the // image document in page mode. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! imgDoc.ImageDocumentEnsurePlacedOnPage() Number pageT, pageL, pageB, pageR imgDoc.ImageDocumentGetPageBounds(pageT, pageL, pageB, pageR) Number pageW = pageR - pageL Number pageH = pageB - pageT if (pageW > pageH) { String msg = "Please select portrait orientation in Page Setup " msg = msg + "before running this print preparation script." Throw(msg) } imgDoc.ImageDocumentSwitchToPageMode()
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Get the image display and make sure it // is optimally placed. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Number border = .02 * min(pageW, pageH) Number dispT, dispL, dispB, dispR dispL = pageL + border dispR = pageR - border
Number imgW, imgH GetSize(img, imgW, imgH) dispT = pageT + 8 dispB = dispT + imgH * (dispR - dispL) / imgW
ImageDisplay imgDisp = img.ImageGetImageDisplayInImageDocument(imgDoc, 0) imgDisp.ImageDisplaySetImageRect(dispT, dispL, dispB, dispR) imgDoc.ImageDocumentUpdateDisplay()
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Add company logo. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! string applnpath getapplicationdirectory("application",0,applnpath) Image logoImg := OpenImage(applnpath+"Company Logo") ImageDisplay logoDisp = imgDoc.ImageDocumentAddImageDisplay(logoImg, 1) Number logoT, logoL, logoB, logoR logoDisp.ImageDisplayGetImageRect(logoT, logoL, logoB, logoR) Number logoAspect = (logoB - logoT) / (logoR - logoL)
Number logoSize = pageW / 5 logoR = pageR - border logoB = pageB - 8 logoL = logoR - logoSize logoT = logoB - logoSize * logoAspect logoDisp.ImageDisplaySetImageRect(logoT, logoL, logoB, logoR) imgDoc.ImageDocumentUpdateDisplay()
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Define functions for adding standardized // information annotations. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Number annotW = 0.45 * imgW Number annotH = max(imgW, imgH) / 30 void PlaceImageInfo(Image img, Number col, Number row, String info) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // NOTE: Keep in mind that the annotation // and its coordinates will always be // referred to its parent image. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Number annotT = imgH + 4 + row * (0.8 * annotH) Number annotL = col * annotW
number annotID = CreateTextAnnotation(img, annotT, annotL, info) SetAnnotationSize(img, annotID, 12) SetAnnotationFace(img, annotID, 0) SetAnnotationFont(img, annotID, "Times") SetAnnotationBackground(img, annotID, 2) }
void PlaceImageTagInfo(Image img, Number col, Number row, String tagName, String label) { Number numVal String strVal String infoText
if (GetNumberNote(img, tagName, numVal)) infoText = label + ": " + numVal else if (GetStringNote(img, tagName, strVal)) infoText = label + ": " + strVal else return
PlaceImageInfo(img, col, row, infoText) }
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // Add various info as text annotations. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Number numVal String strVal
PlaceImageTagInfo(img, 0, 0, "Microscope Info:Specimen", "Specimen") PlaceImageTagInfo(img, 0, 1, "Microscope Info:Name", "Instrument") if (GetNumberNote(img, "Microscope Info:Voltage", numVal)) PlaceImageInfo(img, 0, 2, "Beam Energy: " + numVal / 1000 + " keV") PlaceImageInfo(img, 0, 3, "Camera: Gatan MSC with DigitalMicrograph")
PlaceImageInfo(img, 0, 4, "Resolution: " + imgw + " x " + imgH + " pixels") if (GetNumberNote(img, "MSC:Exposure", numVal)) PlaceImageInfo(img, 0, 5, "Exposure: " + format(numVal, "%.2f") + " sec") PlaceImageTagInfo(img, 0, 6, "Microscope Info:Operator", "Recorded by") if (GetStringNote(img, "DataBar:Acquisition Date", strVal)) { String infoText = "Recorded on: " + strVal + " at " GetStringNote(img, "DataBar:Acquisition Time", strVal) infoText = infoText + strVal PlaceImageInfo(img, 0, 7, infoText) } PlaceImageInfo(img, 0, 8, "Filename: " + GetName(img)) imgDoc.ImageDocumentUpdateDisplay()
/*!!! Offer option to add comment to printout !!!*/ String tagName = "Session Manager:Prepare for Printing:Comment" if (!GetPersistentStringNote(tagName, strVal)) strVal = "" beep() if (GetString("Please enter a brief comment, if desired:", strVal, strVal)) { if (len(strVal) > 0) { SetPersistentStringNote(tagName, strVal) strVal = "Comment: " + strVal PlaceImageInfo(img, 0, 9, strVal) } }
beep() If(!twobuttondialog("Do you want to print this now?","Later","Print")) { choosemenuitem("File","","Print...") } |