AfraLISP - Learn AutoLISP for AutoCAD productivity

Redefining Commands

by Kenny Ramage

Most AutoCAD users know that it is possible to undefine an AutoCAD command and then redefine it to do something else. But, very few people seem to use this very useful feature. I find it very handy to stop people changing system settings that divert from drawing office standards. Let's have a look at an example of this. Say we had a block inserted into the drawing that we did not want to be exploded because it contains attributes. This is how we would go about using AutoLisp to undefine and then redefine the Explode command.

First, we would create an AutoLisp file called 'Redefs.Lsp'. Then we would add the following coding :

	(command "UNDEFINE" "EXPLODE")
	;undefine the Explode command

After undefining the explode command we would then redefine it :

(defun C:EXPLODE ( / lst1 en typ)
   (setq lst1 (list "DRGTITLE1" "DRGTITLE2"))
   ;list of block names that must NOT be exploded
   ;If you wish to add additional block names, and want an easy way to
   ;figure out which ones you screened for each release, use this line with
   ;your block names.
   ;(setq lst1 (append lst1 (list "DRGTITLE2" "DRGTITLE3" "DRGTITLE4")))
   (setq en (car (entsel "\n Select block reference, polyline, dimension,
      or mesh: ")))
   ;gets the block and mimics the explode prompt
   (setq typ (entget en))
   ;get the entity data
   (setq typ (cdr (assoc 2 typ)))
   ;get the block name
   (if (member typ lst1)
       ;if the selected block name is a member of our list
         (alert "\nThis Block Cannot be Exploded. 
                 \n    Refer to System Manager")
         ;inform the user
         ;if it is not
         ;do the following
         (command ^c^c) 
         ;cancel any commands
         (command ".EXPLODE" en)
         ;explode the block
;finish clean
;load clean

We would, of course, load Redefs.Lsp from our Acad.Lsp file to ensure that it would be available all of the time. Just please note, that because this routine uses the 'Command' function, we would have to load it from the S::STARTUP section of the Acad.Lsp file. It would look something like this :

(defun S::STARTUP ()
       (prompt "\nAfraLisp Custom Utilities Loaded....\n")
       (load "REDEFS" "\nREDEFS.LSP not found")

I'll tell you what we'll do next. Let's create our own Drawing AutoSave. First of all we need a way to check when a certain amount of time has elapsed. One of the simplest ways of doing this is to use one of the commonest used commands as a trigger. We'll use the Line command.

Add the following coding to Redefs.Lsp :

Firstly we need to undefine the Line command :

   (command "UNDEFINE" "LINE")
   ;undefine the Line command

The we need to redefine it :

(defun C:LINE ()
;define the function
    ;call the Autosave function
    (command ".LINE")
    ;call the line command

Next we need to write our Autosave function :

(defun AUTOSAVE ( / T1 ECC)
;define the function
      (setq ECC (getvar "CMDECHO"))
      ;get the value of the CMDECHO system variable
      (setvar "CMDECHO" 0)
      ;switch it off
      (if (not T3) (setq T3 (getvar "TDUSRTIMER")))
      ;check if we have the value of the drawing timer
      ;if we haven't got it, then get it.
      (if (not T2) (setq T2 (getreal "\nHow many minutes between saves? : ")))
      ;check if we have an AutoSave time.
      ;if we haven't got it, then get it.
      (setq T1 (getvar "TDUSRTIMER"))
      ;get the drawing timer again for comparison purposes.
      (if (> (- T1 T3) (/ T2 60.0 24.0))
      ;compare the drawing timer values
      ;if it is time to save
            (prompt "\nAutoSaving Drawing, Please Wait…")
            ;inform the user
            (command "QSAVE")
            ;save the drawing
            (setq T3 (getvar "TDUSRTIMER"))
            ;reset the timer
      (setvar "CMDECHO" ECC)
      ;reset CMDECHO

The first time you select the Line command, this function will ask you for an interval between saves. From then on, every time you use the Line command the function will check to see if you have exceeded the time interval. If you haven't, it will do nothing. But if you have, it will first inform you, and then save your drawing. Handy little thing, Hey?

To download source coding for Redefs.lsp click here. (1Kb)