CADTutor: The best free help for AutoCAD on the web

Lee AmbrosiusMichael’s Corner #61
January 2008

Great Mysteries of AutoCAD

by Lee Ambrosius

Congrats to Michael and his achievement of writing this column for the past 5 years. After 5 years and 60 articles later, he has decided it was time to take a break. So while Michael is enjoying his time off and being away from his computer (I know time off for me equals more time with mine), I was asked to step in for this month (no small task I must say). So on with the show…

Customization or programming are often two of the great mysteries of AutoCAD; many people do not know about it or they want to learn to do it, but decide it is not for them before they even try it so it is often left to what seems like a select or "elite" group of users. Being able to change the way you work in AutoCAD is one of the benefits to using AutoCAD; not only can you decide where or how you might access a command, but you can create your own commands. This month, I am going to pop open AutoCAD's hood and take a look around and you might just be surprised how easy some of the things can be when it comes to customization and programming AutoCAD.

In the main article I am going to look at using AutoLISP to batch automate the draw order of single line and multiline text in all the drawings located in a specified folder and then in Power Tool I am going to talk about a utility called ScriptPro which allows you to run a script file on a set of drawings in a slightly different way to batch process drawings. I then round off this issue with creating simple custom commands with AutoLISP to run commands with options which cannot be done with command aliases alone and how to create a basic script file that can be run on a drawing or as part of ScriptPro.

Take control of your design tools today and be more productive tomorrow!

Lee

Batch Automating Draw Order

Reference file: Batch Draw Order.lsp

Being able to batch process a set of drawings is very powerful and being able to control things like draw order across many drawings is even more powerful. The following AutoLISP routine while on the advanced side, moves single line and multiline text to the very top in the draw order. This allows you to make sure that text is always drawn last, so if you have text that contains a fill it is always drawn above other objects in your drawing. I have included comments for each line of code and what it does.

;; Loads the Visual LISP library
(vl-load-com)

;; Creates a new command named BatchDrawOrder
(defun c:BatchDrawOrder ( / sDWGFolder sDWGFiles acadApp docsColl sFile dwgObj modelSpace
extDict sortentsTable cnt acObject lstTemp lstObjects cntStep) ;; Set the folder to update the drawings in (change this to the folder you want to process) (setq sDWGFolder "c:\\test") ;; Get the list of DWG files in the folder (setq sDWGFiles (vl-directory-files sDWGFolder "*.DWG" 1)) ;; Get the AutoCAD application object (setq acadApp (vlax-get-acad-object)) ;; Get the Documents collection (setq docsColl (vla-get-documents acadApp)) (foreach sFile sDWGFiles (progn ;; Open the drawing file (setq dwgObj (vla-open docsColl (strcat sDWGFolder "\\" sFile) :vlax-false)) ;; Get the Model Space object for the Drawing (setq modelSpace (vla-get-modelSpace dwgObj)) ;; Get the Extension Dictionary for Model Space (setq extDict (vla-getExtensionDictionary modelSpace)) ;; Check to see if the Sortents Table exists in the drawing (if (= (type (setq sortentsTable (vl-catch-all-apply 'vlax-invoke-method (list
extDict 'Item "ACAD_SORTENTS")))) 'VL-CATCH-ALL-APPLY-ERROR) (setq sortentsTable (vla-addObject extDict "ACAD_SORTENTS" "AcDbSortentsTable")) ) ;; Set counter for single line and multiline text to zero (setq cnt 0 lstObjects nil) ;; Loop through the ModelSpace collection (vlax-for acObject modelSpace ;; Check to see if the object is a text object (if (or (= (vla-get-objectName acObject) "AcDbText")(= (vla-get-objectName
acObject) "AcDbMText")) (progn ;; Adjust the array based on the number of objects in the drawing (if (>= cnt 1) (progn ;; Get the current values in the array and then adjust the size (setq lstTemp lstObjects) (setq lstObjects (vlax-make-safearray vlax-vbObject (cons 0 cnt))) ;; Step through the old array values and load them into the new array (setq cntStep 0) (repeat cnt (vlax-safearray-put-element lstObjects cntStep (vlax-safearray-get-element
lstTemp cntStep)) (setq cntStep (1+ cntStep)) ) ) (setq lstObjects (vlax-make-safearray vlax-vbObject '(0 . 0))) ) ;; Add the text object to the array and increment counter by 1 (vlax-safearray-put-element lstObjects cnt acObject) (setq cnt (1+ cnt)) ) ) ) ;; If the array is not empty then adjust the Draw Order value for the text objects (if (/= lstObjects nil) (progn (vla-moveToTop sortentsTable lstObjects) ;; Save the changes to the drawing (vla-save dwgObj) ) ) ;; Close the drawing and go to the next one (vla-close dwgObj) ) ) (princ) )

Instructions for Running the Sample Code

  1. Start the Notepad text editor; click Start[All] ProgramsAccessoriesNotepad.
  2. In Notepad, enter the sample code for the BatchDrawOrder command (you can copy and paste the listing above or download the reference file).
  3. Change the line (setq sDWGFolder "c:\\test") to reflect the location of the drawing files you want to process. For example to use the My Documents folder you would change "c:\\test" to "c:\Documents and Settings\user name\My Documents" where user name is your Windows login name.
  4. From the menu bar, choose FileSave As….
  5. In the Save As dialog box, select "All Files" from the Save As Type drop-down list.
  6. Browse to one of the support path folders of AutoCAD (or add a new support path on the Files tab of the Options dialog box). Enter the name BatchDrawOrder.lsp in the File Name box and then click Save.
  7. Switch to AutoCAD. Enter APPLOAD at the command prompt and press Enter.
  8. In the Load/Unload Applications dialog box, browse to the folder you saved the file BatchDrawOrder.lsp to. Select the file and click Open.
  9. At the command prompt, enter BatchDrawOrder and press enter.

Top of page

Power Tool

Running Scripts like a Pro

ScriptPro is a utility that has been around since about AutoCAD 2000, but in recent years has been lost in the shuffle of new releases. Back in the early 2000's around the time of AutoCAD 2000 and 2000i, AutoCAD shipped with an additional set of utilities other than Express Tools - which were very cool in their own right. ScriptPro, as its name suggests, allows you to work with script files. It does not turn you into a script writing professional though, as it does not actually help you write script files at all.

ScriptPro

You use ScriptPro to run a script file on a drawing or set of drawings which helps you to increase productivity and enforce consistency between drawings. So if you need to batch process a set of drawings to update layers, save the drawings to a specific file version, set the Model tab current, or perform a zoom extents on each of the drawings. You can download ScriptPro from the Autodesk website.

Instructions to Run a Script on a Set of Drawings

  1. After you install ScriptPro on your workstation, click Start[All] ProgramsAutodeskScriptProScriptPro.
  2. In the ScriptPro dialog box, under the Script area, click Browse.
  3. In the Open dialog box, browse to and select the script file that you want to run on each selected drawing. Script files have a SCR file extension.
  4. Click Open.
  5. On the toolbar, click Add Drawings.
  6. In the ScriptPro - Add/Remove Files in Drawing list dialog box, browse to the folder that contains the drawings you want to process.
  7. In the Drawings list, select a drawing and click >> to add it to the Files to Apply Script list. You can also remove files from the list by selecting the drawing in the Files to Apply Script list and then click <<. I recommend creating an archive of the drawings you want to run the script file on just in case something goes wrong, which never happens until you are doing something in the final hours of a project.
  8. After you added the drawing files to be processed, click OK to return to the ScriptPro dialog box.
  9. On the toolbar, click Save a ScriptPro Project.
  10. In the SaveAs dialog box, browse to the location that you want to save the script project file to and provide a file name for the project file. You can reload the project file later and process the same drawings again with the same script file or another script file.
  11. Click Save.
  12. After saving the script project file, click Run this Project.
  13. The ScriptPro - Running dialog box is displayed and shows the progress of the drawing each drawing. A new session of AutoCAD. The first drawing is opened and the script is run.
  14. Once all the drawings have been processed, the ScriptPro - Project Completed message box is displayed. Click Yes to view the log file to view if any problems were encountered and what they might have been.

Top of page

The Odd Spot

Command Aliases and Options

Reference file: Acaddoc.lsp

Command aliases allow you to quickly access commands by typing in a few letters instead of the full command name or locating the command on one of the user interfaces. While command aliases are great for accessing commands, they do have one limitation and that is they cannot be defined with command options. To workaround this issue, you can define commands with AutoLISP that have a name with a couple letters. For example, if you want to perform a Zoom Window you need to start the ZOOM command and then use the Window option or click Zoom Window in the user interface, but with AutoLISP you could define a command named ZW to do the same thing.

Once the command is defined, you can automatically load it each time AutoCAD starts up by creating a file with the special name Acaddoc.lsp, or create an AutoLISP file with a name of your choice and then add it to the Startup Suite in the Load/Unload Application dialog box.

Instructions to create a simple Custom Command with AutoLISP

  1. Start the Notepad text editor; click Start[All] ProgramsAccessoriesNotepad.
  2. In Notepad, enter the following text:
;; Defines two commands: one performs a zoom window
;; and the other a zoom previous
(defun c:ZW () (command "._zoom" "_w"))
(defun c:ZP () (command "._zoom" "_p"))
Acaddoc.lsp
  1. From the menu bar, choose FileSave As….
  2. In the Save As dialog box, select "All Files" from the Save As Type drop-down list.
  3. Browse to one of AutoCAD's support paths or add a new support path on the Files tab of the Options dialog box. Enter the name Acaddoc.lsp in the File Name box and then click Save.
  4. Switch to AutoCAD. Then save any files that are open and close AutoCAD.
  5. Restart AutoCAD and create a new drawing. Then type ZW or ZP at the command prompt. If Acaddoc.lsp was found by AutoCAD the new commands should work.

The example (defun c:ZW () (command "._zoom" "w")) breaks down as follows:

(defun c:ZW () - The Defun function creates a new command that can be typed at the command prompt named ZW.

(command "._zoom" "_w")) - The Command function tells AutoCAD that you are using the following commands and options. So the example uses the ZOOM command with the Windows option.

The lines that start with a ; (semicolon) designate lines in the file that are used as comments and do not get executed or loaded into AutoCAD.

Top of page

The Basics

Getting AutoCAD to Follow a Script

Reference file: MyScript.scr

Script files are one of the most natural forms of customizing AutoCAD because it uses the same input and the commands that you are already familiar with. There are a few differences, the first difference that you need to understand is that you cannot prompt for user input and the other main difference is that you cannot use commands that display dialog boxes. To work around the issue with dialog boxes, you use the commands that are prefixed with a dash. For example, to create a layer using a script you would use the -LAYER command instead of the LAYER command. Script files are created using a plain ASCII text editor such as Notepad.

The following is an example of a script file that performs some basic drawing setup:

._-UNITS 4 8 1 2 0 N
MEASUREMENT 0
._LIMITS 0,0 204,132
._-LAYER _M TITLEBLK _C 6  
._RECTANG 5,5 199,127
._TEXT 6,6 1.5 0 DRAWING SETUP COMPLETE
._ZOOM _E
MyScript.scr

In the above example, you do not see all of the special characters such as spaces and line returns or enters that are needed to create the script. The following shows the same script except with spaces represented as <SP> and enters or linefeeds represented as <ENTER>. Note the 2 spaces after the 6 on line 4.

._-UNITS<SP>4<SP>8<SP>1<SP>2<SP>0<SP>N<ENTER>
MEASUREMENT<SP>0<ENTER>
._LIMITS<SP>0,0<SP>204,132<ENTER>
._-LAYER<SP>_M<SP>TITLEBLK<SP>_C<SP>6<SP><SP><ENTER>
._RECTANG<SP>5,5<SP>199,127<ENTER>
._TEXT<SP>6,6<SP>1.5<SP>0<SP>DRAWING SETUP COMPLETE<ENTER>
._ZOOM<SP>_E<ENTER>

The first line ._-UNITS 4 8 1 2 0 N sets the units to Architectural to a precision of 1/8″ and angular units of Decimal Degrees to a precision of 2. The last two values control the angular direction.

The line MEASUREINIT 0 defines that the drawing should use Imperial measurement for the current drawing which affects the hatch pattern and linetype files that should be used along with a few other features.

The line ._LIMITS 0,0 204,132 sets the drawing limits.

The line ._-LAYER _M TITLEBLK _C 6 creates a new layer named TITLEBLK and sets it to the Magenta color.

The line ._RECTANG 5,5 199,127 draws a rectangle starting at 5,5 and ending at 199,127.

The line ._TEXT 6,6 1.5 0 DRAWING SETUP COMPLETE creates a single line text object that is 1.5 units in height at 6,6 with a rotation of 0 and with the text "DRAWING SETUP COMPLETE".

The line ._ZOOM _E performs a zoom extents.

Instructions for Writing and Running a Script

  1. Start the Notepad text editor; click Start[All] ProgramsAccessoriesNotepad.
  2. In Notepad, enter the text from the above sample script file (or just copy and paste).
  3. From the menu bar, choose FileSave As….
  4. In the Save As dialog box, select "All Files" from the Save As Type drop-down list.
  5. Browse to the location where you want to save the script file and enter the name of the script file in the File Name box. As an example, enter MyScript.scr.
  6. Click FileSave.
  7. To run the script file, switch to AutoCAD and create a new drawing.
  8. Select ToolsRun Script.
  9. In the Select Script File dialog box, browse to and select the script file.
  10. Click Open. A magenta rectangle is drawn and the text "DRAWING SETUP COMPLETE" is displayed in the lower-left corner of the rectangle.

If you press F2 after the script has run, you can see all the command prompts and how they are answered.

Top of page

Left Field

Why is Wind Chill Temperature Important? - Wind chill measures the rate at which the wind cools the bare skin and is an indication of how cold living objects such as humans and animals feel outside; inanimate objects such as the radiator in a car or a building are unaffected by wind chill as they are as warm or as cold as the air temperature around them. The reason for the observation of wind chill is because heat escapes through the skin, and with blowing wind comes greater loss of heat through the skin and increased risk of frost bite in those areas where the most heat is lost at the fastest rate. So when outdoors during the winter, dress appropriately for both daily changes in air temperature and wind chill.

Thank You… and have a Happy New Year

It has been a great honor to be the stand-in writer to keep such a wonderful column going while Michael takes some time off. Hopefully, you might have learned something new about customizing and programming in AutoCAD that might help to change the way you work everyday. I would like to thank Michael for such a stand-up job and the amount of time and energy he gives back to the industry each and every day and also, to wish everyone a great and Happy New Year.

Lee

Share this page:

Share at Facebook Submit a link at Reddit Share at StumbleUpon Share at del.icio.us Share at Technorati

Local Navigation

Sponsored Links

Accessibility statement

Valid XHTML 1.0!Valid CSS!Creative Commons Licence