Michael’s Corner #61
January 2008
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
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) )
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.
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.
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.
;; 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"))
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.
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
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.
If you press F2 after the script has run, you can see all the command prompts and how they are answered.
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.
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
If you found this article useful, you might like to consider making a donation. All content on this site is provided free of charge and we hope to keep it that way. However, running a site like CADTutor does cost money and you can help to improve the service and to guarantee its future by donating a small amount. We guess that you probably wouldn't miss $5.00 but it would make all the difference to us.
Note from Michael: I want to thank all of my customers for continuing to retain my training services (some for over three decades!) and let you know your donations do not go to me personally, but to the ongoing maintenance of the CADTutor ship as a whole and to support the yeoman efforts of my friend and CADTutor captain, David Watson, to whom I am grateful for this monthly opportunity to share a few AutoCAD insights.