AutoCAD Tips: Time-Saving Toggle Routines6 Dec, 2006 By: Billy Wooten
Personalize your software with code that quickly switches a variety of variables
The term toggle usually applies to an item such as a light switch. If something toggles, it's always in one of two states: on or off. This is perfect for simple electrical or mechanical devices. We can apply the toggle concept in software programs -- even in a more versatile way. For example, we can write code to specify that if a setting is 1, change it to 2; if it is 2, then change it to 3; if it is not 3 and not 2, then change it to 4; and so forth. Following are a few simple toggle routines to help with our own daily routines.
Toggle UCSICON Variable
My first toggle routine is as much a demonstration as it is a useful routine. It toggles the UCSICON variable on and off (1 and 0 -- the only two modes available). It sets the variable (UCSICON) equal to a symbol (uc) and employs the "if" statement. The "if" statement is usually the key to a toggle routine. If the UCSICON is off (= uc 0), then cut it on (setvar "ucsicon" 1); otherwise (because it's set to 1), cut it off (setvar "ucsicon" 0).
(defun c:ui (/ uc);Toggle USC Icon (setq uc (getvar "ucsicon")); establish variable (if (= uc 0) (setvar "ucsicon" 1) ; if the ucsicon is off, then cut it on (setvar "ucsicon" 0) ; otherwise cut it off ) (princ) )
Toggle Zoom on a Wheel Mouse
For drawing navigation, using the ZOOMFACTOR variable can be a real time-saver. This variable controls the zoom feature of the wheel mouse. Setting it to 65 is a good starting option. (Your choice may vary.) For fine-tuning purposes, switch it to 5. To account for settings other than 5 or 65, use the "not equal to" logic sign ( /= ). The following routine states that if ZOOMFACTOR is not equal to 65, then set it to 65 and, using the (progn) function, echo the setting to the Command line. If it is equal to 65, then set it to 5. Without the "not equal to" sign, this routine would perform no function at all with settings other than 5 and 65.
(defun c:zf (/ zr);Toggle Zoom Factor 5 - 65 (setq zr (getvar "zoomfactor")) (if (/= zr 65); if ZOOMFACTOR is not equal to 65 (progn (setvar "zoomfactor" 65); then set it to 65 (princ "\nZoom Factor = 65");echo ) (progn (setvar "zoomfactor" 5);otherwise, set it to 5 (princ "\nZoom Factor = 5");echo ) ) (princ) )
Here is an example of the same routine, but with a 3-way toggle:
(defun c:zf2 (/ zr);Toggle Zoom Factor 5 - 35 - 65 (setq zr (getvar "zoomfactor")) (if (and(/= zr 65)(/= zr 35)) ; if ZOOMFACTOR is not equal to 65 AND not equal to 35 (progn (setvar "zoomfactor" 65); then set ZOOMFACTOR to 65 (princ "\nZoom Factor = 65");echo ) ) (if (= zr 65); if ZOOMFACTOR is equal to 65 (progn (setvar "zoomfactor" 35);then set ZOOMFACTOR to 35 (princ "\nZoom Factor = 35");echo ) ) (if (= zr 35); if ZOOMFACTOR is equal to 35 (progn (setvar "zoomfactor" 5);then set ZOOMFACTOR to 5 (princ "\nZoom Factor = 5");echo ) ) (princ) )
To quickly access one of these routines, use a key combination such as Ctrl+Shift+Right Mouse Button, which can be set with menu customization or assigned to a toolbar button. Be sure to precede the command with an apostrophe (') for transparent application.
Toggle Osnap Setting
A running osnap setting of 4207 (endpoint, midpoint, center, node, intersection, insertion and extension) eliminates the need to pick individual osnaps. Sometimes a setting of 0 is also needed. The OSMODE variable setting is determined via the sum of the individual osnaps used. This routine is similar to the ZOOMFACTOR routine above. Individual osnap settings are as follows:
(defun c:ros (/ os);Toggle OSmode 0 - 4207 (setq cmd (getvar "cmdecho") os (getvar "osmode") ) (setvar "cmdecho" 0) (if (/= os 4207);if osmode is not equal to 4207 (progn (setvar "osmode" 4207);then set osmode to 4207 (princ "\nO-Snap Mode: 4207");echo ) ) (if (= os 4207);if osmode is equal to 4207 (progn (setvar "osmode" 0);then set osmode to 0 (princ "\nO-Snap Mode: 0");echo ) ) (setvar "cmdecho" cmd) (princ) )
Additional Helpful Toggle Routines
Here are a few others that could prove useful.
(defun c:cs (/ cur);Toggle Cursor Size 5 - 100 (setq cur (getvar "cursorsize"));establish variable (if (/= cur 100) (setvar "cursorsize" 100) ; if it's not equal to 100, then set it to 100 (setvar "cursorsize" 5);otherwise, set it to 5 ) (princ) )
Two common linear units settings (lunits) are Decimal and Architectural. Others are listed below. Command Units inputs are as follows. (The AutoCAD Text Screen [F2] shows all details. Just type in -Units and follow the prompts.)
- Number of Decimal Places
- Systems of Angular Measurement
- Number of Fractional Places for Display of Angles
- Direction for Angle
- Measure Angles Clockwise?
(defun c:ut (/ qt);Toggle Units Decimal - Architectural (setvar "cmdecho" 0) (setq qt (getvar "lunits")) (if (/= qt 2) (progn (setvar "lunits" 2) (princ "\nLinear Units: Decimal") (command "-units" "2" "3" "1" "0" "0" "no") ) (progn (setvar "lunits" 4) (princ "\nLinear Units: Architectural") (command "-units" "4" "64" "1" "0" "0" "no") ) ) (setvar "cmdecho" 1) (princ) )
- Scientific 1.55E+01
- Decimal 15.50
- Engineering 1'-3.50"
- Architectural 1'-3 1/2"
- Fractional 15 1/2
This routine, which toggles the Dimension Toolbar on/off, was written by former Cadalyst Contributing Editor Dave Pitzer. For more information, refer to his article, "Applying Customization: Toolbar Toggle," on the Autodesk Web site.
(defun c:tbm (/ cmdecho);Toggle Dimension Toolbar On/Off (setq oldcmd (getvar "cmdecho")) (setvar "cmdecho" 0) (if (null -Tb) (setq -TB 0) ) (setq -TB (abs (- 1 -Tb))) (if (= -TB 0) (COMMAND "-TOOLBAR" "Dimension" "hide") (COMMAND "-TOOLBAR" "Dimension" "show") ) (setvar "cmdecho" oldcmd) (princ) )
When Qtext mode is activated, all text is represented by rectangles. This routine is handy when working on heavily texted drawings that can bog down regens.
(defun c:qt (/ qtm); Toggle Quick Text Mode On/Off (setvar "cmdecho" 0) (setq qtm (getvar "qtextmode")) (if (= qtm 0) (progn (setvar "qtextmode" 1) (princ "\nQTEXT Mode, On") ) (progn (setvar "qtextmode" 0) (princ "\nQTEXT Mode, Off") ) ) (command "regen") (setvar "cmdecho" 1) (princ) )
On some drawings, numerous dots may appear when Qtextmode is triggered. These dots represent null text. This feature varies slightly based on your version of AutoCAD. Mtext in 2006/2007 will not accept null text, but it will accept a single space, which will produce the aforementioned dot. Call up your text editors, leave them blank (or with a space), cut QTEXT on and observe the results.
Some other variables to consider toggling are: Blipmode, Demandload, File Dialog, Command Dialog, Attribute Dialog, Highlight, Polar Mode, Isometric Mode, Trim Mode and Edge Mode. Include these routines in your ACAD200x.DOC file for quick access. They have been tested on releases 2002, 2006 and 2007. My apologies for repeating certain commercials, but your results may vary. AutoCAD is highly "personalizable" -- therefore, it can be customized to the user's desires. There are no doubt better ways to write these routines. Having said that, I welcome your input.
About the Author: Billy Wooten
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!