(Archive) Quick One-Step Direct Graphics Printing
Home
Register, Login   

     Tech Tips
 

Printable Version
Article Title: (Archive) Quick One-Step Direct Graphics Printing
Article ID: 1748
Article Name: IDE5
Last Updated: 12/7/2009 11:25:18 AM
Products: IDL
OS Platforms: UNIX OS; WIN

Topic:

 

Below is a procedure called "PRINTWINDOW" which provides a quick-and-easy way to print the current IDL Direct Graphics window (!D.WINDOW) to the printer device centered on a 8.5" x 11" piece of paper with 1" margins (if needed). These margins will only be accurate if the unprintable area for the printer is set to zero. If the printer and/or its driver has margins (i.e. unprintable area) already specified this will offset the IDL output. This routine attempts to match the size of the print to the size of the graphics window on the monitor and will print only the currently active graphics window. If multiple graphics windows are open, you must first use the WSET command to set the active window ID. If no graphics windows are open you will see an error.

This routine is used to quickly print a rasterized copy of the graphics window using bitmap graphics, as opposed to vector-drawn graphics. If you require high resolution vector-drawn output we suggest writing your own code using either the PRINTER, PS or PCL device.

This routine will work in IDL version 5.2 and newer and is supported on both UNIX and Windows platforms.The information contained within this webpage will help you set up a macro within the IDLDE that allows you to execute the PRINTWINDOW routine through either a menu bar option or by simply clicking on a toolbar button !


Discussion:

PRINT SCREEN ICON


The IDL Development Environment menu and toolbars can be customized using Macros. To setup a Macro for the PRINTWINDOW routine follow the instructions for your operating system below.

WINDOWS Platform

  1. Save the code below in an ASCII text file called 'printwindow.pro' and place this file somewhere within your IDL path
  2. Download the bitmap button icon (printwindow.bmp) and save it in your resource\bitmaps subdirectory of the IDL installation
  3. Open the IDLDE, select Macros->Edit..., and click on the Add... button
  4. When prompted to enter the name of the new macro, enter 'PrintWindow'
  5. In the field labelled IDL command, enter 'printwindow'
  6. In the field labelled Menu item name, enter 'PrintWindow'
  7. In the field labelled Toolbar bitmap file, enter the exact path to printwindow.bmp. For example, C:\Rsi\Idl56\resource\bitmaps\printwindow.bmp
  8. In the field labelled Tooltip text, enter 'Print the current Direct Graphics window'
  9. In the field labelled Status bar text, enter 'Printing the current Direct Graphics window'
  10. Click OK

If everything was successfully setup, you will now see a new button on your toolbar that looks like an IDL graphics window displaying the word 'Print'. You will also see the word PrintWindow under the Macro button on the IDL menu bar. If these do not exist, go to Macros->Edit... on the IDL menu bar. You should see a macro named PrintWindow. Select this item and repeat the steps above. If you are still having problems, please contact Research Systems' Technical Support <support@ittvis.com> for assistance.

UNIX Platform

  1. Save the code below in an ASCII text file called 'printwindow.pro' and place this file somewhere within your IDL path
  2. Download the UNIX button icon (printwindow.xpm) and save it in your resource\bitmaps subdirectory of the IDL installation
  3. Open the IDLDE, select Macros->Edit...
  4. Fill-in the Macro Attributes for your new macro... in the field labelled Name, enter 'PrintWindow'
  5. In the field labelled Label, enter 'PrintWindow'
  6. In the field labelled Bitmap, enter the exact path to printwindow.xpm. For example, /usr/local/rsi/idl/resource/bitmaps/printwindow.xpm
  7. In the field labelled Status Bar text, enter 'Printing current Direct Graphics window'
  8. In the field labelled Tip text, enter 'PrintWindow'
  9. In the field labelled IDL Command, enter 'printwindow'
  10. Select both the Menu and the Toolbar buttons at the bottom of the window (as this will create both the Toolbar bitmap button and an option under the Macros menu bar button)
  11. Click on Add...

If your bitmap toolbar button is not displayed immediately, exit IDL and restart IDLDE. If you still do not see the toolbar button, open Macros->Edit... and select PrintWindow from the Toolbar Macros list. Make sure the path for the Bitmap field is correct.

If you see the error:

% Attempt to call undefined procedure/function:
'PRINTWINDOW'.
% Execution halted at: $MAIN$

Go to your File->Preferences and be sure that the directory where the file 'printgraphics.pro' resides is defined in your Path. If it is not, click on Add, and add it to your path. Then try again.

Example Code:

;+
; NAME:
;       PRINTWINDOW
;
; PURPOSE:
;       The purpose of this program is to provide a quick-and-easy way
;       to print the current IDL Direct Graphics window (!D.WINDOW) to
;       the printer device centered on a 8.5" x 11" piece of paper
;       with 1" margins (if needed). These margins will only be
;       accurate if the unprintable area for the printer is set to
;       zero. If the printer and/or its driver has margins
;       (i.e.unprintable area) already specified this will offset the
;       IDL output. This routine attempts to match the size of the
;       print to the size of the graphics window on the monitor.
;
; CATEGORY:
;       Visual
;
; CALLING SEQUENCE:
;       PRINTWINDOW
;
; INPUT PARAMETERS:
;       None.
;
; KEYWORD PARAMETERS:
;       None.
;
; OUTPUTS:
;       None.
;
; COMMON BLOCKS:
;       None.
;
; SIDE EFFECTS:
;       None.
;
; DISCLAIMER:
;       This procedure is provided "as is" and is not supported or
;       maintained by Research Systems Inc.
;
; MODIFICATION HISTORY:
;       Written by: Adam Bielecki, April 2000.
;       Added support for 8-bit color and Landscape orientation
;            printing: Adam Bielecki, January 2002.
;-
 
PRO PRINTWINDOW
COMPILE_OPT HIDDEN
;error catching:
!error_state.code=0
catch,error
if error NE 0 then begin
    help,/last_message,output=traceback
    errarray=['Error Caught',traceback]
    dummy=dialog_message(errarray,/error)
    if !D.NAME EQ 'PRINTER' and size(scale,/type) NE 0 then device,scale_factor=scale
    set_plot,currDevice
    return
endif
;check to make sure IDL graphics window is open:
if !D.WINDOW EQ -1 then begin
    dummy=DIALOG_MESSAGE('No IDL graphics windows currently open !',/error)
    return
endif
;obtain current device:
currDevice=!D.NAME
;obtain current device resolution:
xppi=!D.X_PX_CM*2.54
yppi=!D.Y_PX_CM*2.54
;query visual class and TVRD the current graphics window:
if !D.N_COLORS GT 256 then begin
    if !ORDER EQ 1 then image=TVRD(TRUE=3,/ORDER) else image=TVRD(TRUE=3)
endif else begin
    if !ORDER EQ 1 then pseudoImage=TVRD(/ORDER) else pseudoImage=TVRD()
    TVLCT,r,g,b,/GET
    imageSize=SIZE(pseudoImage,/DIMENSIONS)
    image=BYTARR(imageSize[0],imageSize[1],3)
    image[*,*,0]=r[pseudoImage]
    image[*,*,1]=g[pseudoImage]
    image[*,*,2]=b[pseudoImage]
endelse
;change to the PRINTER device:
SET_PLOT,'PRINTER'
;present the user with printer properties dialog:
dummy=DIALOG_PRINTERSETUP()
if (dummy EQ 0) then begin
    SET_PLOT,currDevice
    RETURN
endif else begin
                                ;obtain original scale factor for Printer device:
    HELP,/DEV,OUTPUT=orig
    scalepos=STRPOS(STRUPCASE(orig),'SCALE FACTOR')
    scaleindex=WHERE(scalepos NE -1,count)
    if count EQ 0 then begin
        dummy=DIALOG_MESSAGE('Indeterminable Scale Factor for PRINTER device.',/error)
        SET_PLOT,currDevice
        RETURN
    endif
    scaleindex=scaleindex[0]
    scalepos=scalepos[scaleindex]+13
    scale=STRTRIM(STRMID(orig[scaleindex],scalepos),2)
    scale=FLOAT(scale)
                                ;determine if output format selected
                                ;by user is Portrait or Landscape:
    orientpos=STRPOS(STRUPCASE(orig),'ORIENTATION')
    orientindex=WHERE(orientpos NE -1,count)
    if count EQ 0 then begin
        dummy=DIALOG_MESSAGE('Indeterminable Orientation for PRINTER device.',/error)
        SET_PLOT,currDevice
        RETURN
    endif
    orientindex=orientindex[0]
    orientpos=orientpos[orientindex]+12
    orient=STRUPCASE(STRTRIM(STRMID(orig[orientindex],orientpos),2))
                                ;set scale factor equal to one:
    DEVICE,scale_factor=1
                                ;compute appropriate size of graphic
                                ;in inches as it currently appears:
    ncols=(SIZE(image))[1]
  nrows=(SIZE(image))[2]
  xsize=ncols/xppi
  ysize=nrows/yppi
                                ;print the graphic:
  CASE orient OF
    'PORTRAIT' :  begin
                                ;scale output so there is at least a 1
                                ;inch border on each side of the
                                ;image:
        if xsize LE 6.5 and ysize LE 9 then begin 
;graphic does NOT have to be reduced in resolution:
            device,xoffset=0,yoffset=0,xsize=xsize,ysize=ysize,/inches
            xmarg=(8.5-xsize)/2
            ymarg=(11-ysize)/2
            TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=xsize,ysize=ysize,/inches
            DEVICE,/CLOSE
        endif else begin 
                                ;otherwise, find limiting dimension
                                ;and reduce resolution accordingly:
            xfac=6.5/xsize
            yfac=9/ysize
            if yfac LT xfac then begin ;the Y dimension is the limiting factor:
                newxsize=xsize*yfac
                newysize=ysize*yfac
                device,xoffset=0,yoffset=0,xsize=newxsize,ysize=newysize,/inches
                xmarg=(8.5-newxsize)/2
                ymarg=(11-newysize)/2
                TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=newxsize,ysize=newysize,/inches
                DEVICE,/CLOSE
            endif else begin  ;the X dimension is the limiting factor:
                newxsize=xsize*xfac
                newysize=ysize*xfac
                device,xoffset=0,yoffset=0,xsize=newxsize,ysize=newysize,/inches
                xmarg=(8.5-newxsize)/2
                ymarg=(11-newysize)/2
                TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=newxsize,ysize=newysize,/inches
                DEVICE,/CLOSE
            endelse
        endelse
    end
    'LANDSCAPE' : begin
                                ;scale output so there is at least a 1
                                ;inch border on each side of the
                                ;image:
        if xsize LE 9 and ysize LE 6.5 then begin 
                                ;graphic does NOT have to be reduced
                                ;in resolution:
            device,xoffset=0,yoffset=0,xsize=xsize,ysize=ysize,/inches
            xmarg=(11-xsize)/2
            ymarg=(8.5-ysize)/2
            TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=xsize,ysize=ysize,/inches
            DEVICE,/CLOSE
        endif else begin 
                                ;otherwise, find limiting dimension
                                ;and reduce resolution accordingly:
            xfac=9/xsize
            yfac=6.5/ysize
            if yfac LT xfac then begin ;the Y dimension is the limiting factor:
                newxsize=xsize*yfac
                newysize=ysize*yfac
                device,xoffset=0,yoffset=0,xsize=newxsize,ysize=newysize,/inches
                xmarg=(11-newxsize)/2
                ymarg=(8.5-newysize)/2
                TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=newxsize,ysize=newysize,/inches
                DEVICE,/CLOSE
            endif else begin  ;the X dimension is the limiting factor:
                newxsize=xsize*xfac
                newysize=ysize*xfac
                device,xoffset=0,yoffset=0,xsize=newxsize,ysize=newysize,/inches
                xmarg=(11-newxsize)/2
                ymarg=(8.5-newysize)/2
                TV,TEMPORARY(image),xmarg,ymarg,true=3,xsize=newxsize,ysize=newysize,/inches
                DEVICE,/CLOSE
            endelse
        endelse
    end
    else :        begin
        dummy=DIALOG_MESSAGE('Indeterminable Orientation for PRINTER device.',/error)
        DEVICE,scale_factor=scale
        SET_PLOT,currDevice
        RETURN
    end
ENDCASE
                                ;reset scale factor to original:
DEVICE,scale_factor=scale
endelse
                                ;reset current device to original:
SET_PLOT,currDevice
END


 
 Quick Links

   Contact a Representative
   Request Literature
   Events
   User Community
   Subscribe
 
 Product Information

   IDL
   ENVI
   ENVI EX
   IAS
   NITF for ArcGIS
   Professional Services
   Events & Training

Home   |   Company   |   Products & Services   |   Academic   |   Events & Training   |   Downloads   |   User Community   |   Support |   Site Map

Privacy Statement ©2008 ITT Visual Information Solutions