cadalyst
AutoCAD

Create Thumbnail Previews for Old Drawings (AutoLISP Solutions AutoCAD Tutorial)

30 Apr, 2008 By: Tony Hotchkiss

Routine automatically opens any number of DWG, DXF files and zooms them to 80% of extents.


Jim McSwain of Toone, Tennessee, asked for a program to convert some old drawing and DXF files to a format that has an 80% zoom and a thumbnail preview. Many of his old drawings are standard parts that have been saved under old versions of AutoCAD, and he cannot see a thumbnail preview because the zooming was set poorly or there is no preview at all.

The AutoLISP Solution is ZOOM-SAVE.LSP and ZOOM-SAVE.DCL, which opens any number of selected DWG or DXF files and automatically zooms them to 80% of the extents of the drawing before saving with a preview.

Get the Code!
Download the ZOOM-SAVE.LSP and ZOOM-SAVE.DCL files from Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file.

How to Use the ZOOM-SAVE.LSP Code
To start the program, enter ZS, and you will see the Zoom-Save dialog box.

 

figure
From AutoCAD's Zoom-Save dialog box, select the files to process.

You can select the file type to display using the DWG and DXF radio buttons. Select the drive and the directory to see a list of files in the right-hand half of the dialog box. To select files, use the standard Microsoft Windows selection procedures: Click on any file to select a single file; use the shift key to select a group of files; and hold down the Ctrl key and click to select arbitrary files.

Click the OK button to automatically open, zoom, and save each file in turn before returning to the Zoom-Save dialog box. You can then change the file type, drive, and directory to select another group of files to process. Click the All Done button to close the dialog box when all files have been converted.

Programming Notes
After running my usual error and system variable management functions, the program starts with the ZOOM-SAVE function that manages the dialog box. Note that the system variables include setting FILEDIA, LISPINIT, and SDI. These system variables allow the LIST program to run and remain loaded over any number of drawings, and also to inhibit the display of regular AutoCAD dialog boxes. ZOOM-SAVE uses a global variable, DTYPE, to track whether a DWG or DXF file type has been selected. It also initializes the dialog box and defines the actions taken when dialog box selections are made. ZOOM-SAVE calls many functions including INIT-DWG, DO-DRIVES, DO-DIRBOX, GET-FILES, and DO-MDWGS.

INIT-DWG calls MAKE-DRIVELIST, which returns a list of all drives on the user's computer. To do this, the Visual LISP function VL-FILE-SIZE is used as a test because it returns 0 if its argument is a directory. DO-DRIVES is used to populate the list of drives in the dialog box, and DO-DIRBOX returns a list of directories in any given drive.

GET-FILES uses a repeat loop to create a list of files selected by the user of the dialog box. The list is used subsequently by DO-MDWGS as follows:

(defun do-mdwgs (dlist / dname)
 (if dlist
 (progn
  (repeat (length dlist)
     (if dlist
      (progn
      (setq dname (car dlist)
           dlist (cdr dlist)
     ) ;_ end of setq
      (new-open)
      (vl-cmdf dname)
      (zoomsave)
;;; zoom and save operation on a drawing
      ) ;_ end of progn
     ) ;_ end of if
  ) ;_ end of repeat
 ) ;_ end of progn
 ) ;_ end of if
) ;_ end of do-mdwgs

This function calls NEW-OPEN, which is a modified version of an Autodesk function written more than 10 years ago. The modified version is:

(defun new-open ()
 (vl-cmdf "open")
 (if (not (= 0 (getvar "dbmod")))
 (vl-cmdf "N")
 ) ;_ end of if
 (princ)
) ;_ end of new-open

ZOOMSAVE is also called by DO-MDWGS and does the job of zooming and saving each of the drawings or DXF files as follows:

(defun zoomsave ()
 (vl-cmdf "ZOOM" "E")
 (vl-cmdf "ZOOM" "0.8XP")
 (if (= *DTYPE* "dwg")
 (vl-cmdf "SAVEAS" "2004" "" "Y")
 (vl-cmdf "SAVEAS" "DXF" "" "" "Y")
 )
) ;_ end of zoomsave

Here, the DTYPE variable is used to test for the file type DWG or DXF, and the files are saved accordingly.

As always, I look forward to receiving your comments and requests for "AutoLISP Solutions." Please contact me using the links below.


About the Author: Tony Hotchkiss


More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!


For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!