AfraLISP - Learn AutoLISP for AutoCAD productivity

Date and Time Stamping

by Kenny Ramage

When you plot a drawing (or insert an Xref, etc), it's nice to have the Date and Time of the plot stamped on the plotted drawing. This is easily done manually, but wouldn't it be nice to use AutoLisp and have the Time and Date automatically added to your plots? Here's how you would go about it.

First, we need to write a few functions that will calculate the Date and Time and then format them into readable text. Try out the following functions :

(defun TODAY ( / d yr mo day)
;define the function and declare all variabled local
 
     (setq d (rtos (getvar "CDATE") 2 6)
     ;get the date and time and convert to text
 
          yr (substr d 3 2)
	  ;extract the year
 
          mo (substr d 5 2)
	  ;extract the month
 
         day (substr d 7 2)
	 ;extract the day
 
     );setq
 
     (strcat day "/" mo "/" yr)
     ;string 'em together
 
  (princ)
 
);defun
;;;*-------------------------------------------
(defun TIME ( / d hr m s)
;define the function and declare all variables as local
 
     (setq d (rtos (getvar "CDATE") 2 6)
     ;get the date and time and convert to text
 
          hr (substr d 10 2)
	  ;extract the hour
 
          m (substr d 12 2)
	  ;extract the minute
 
          s (substr d 14 2)
	  ;extract the second
 
     );setq
 
     (strcat hr ":" m ":" s)
     ;string 'em together
 
  (princ)
 
);defun
;;;*-------------------------------------------

Try the two functions out. Load the functions and type : (today)

Lisp should return today's date : "23/03/99"

Now try the next function. Type : (time)

AutoLisp should return something like : "11:36:21"

OK great, we've got the date and time but now we need to add it to our drawing. The simplest way of doing this is by making use of attributes.

First we need to create a block containing 3 attributes, namely Date, Time and Who By. (If you don't know how to create a block with attributes, please refer to the AutoCad Reference Manual.)

Once you have created your attribute block, it is very easy to write an AutoLisp routine that inserts the block and automatically calculates and fills in the attribute data. The following example does just that.

(defun C:TIMESTAMP (/ ss1 count emax en ed blkn found thedate thetime plotby)
;define function and declare variables as local
 
	(setvar "HIGHLIGHT" 0)
  	;switch off highlight
 
	(setvar "CMDECHO" 0)
	;switch off command echo
 
	(setq ss1 (ssget "X" '((0 . "INSERT")(66 . 1))))
	;filter for all blocks with attributes
 
	(if ss1
	;if any are found
 
	  (progn
	  ;do the following
 
      		(setq count 0
		;set the counter to zero
 
	   	emax  (sslength ss1)
		;get the number of blocks
 
      		);setq
 
      		(while (< count emax)
		;while the counter is less than the
		;number of blocks
 
			(setq en   (ssname ss1 count)
			;get the entity name
 
	      		ed (entget en)
			;get the entity list
 
	      		blkn (dxf 2 ed)
			;get the block name
 
			);setq
 
			(if (= "STAMP")
			;if the block name is "STAMP"
 
	  			(setq	count emax
				;stop the loop
 
				found T
				;set the flag
 
	  			);setq
 
	  			(setq count (1+ count))
				;if not increment the counter
 
			);end if
 
      		);while & if
 
      		(if found
		;if the flag is set
 
		(command "ERASE" en "")
		;erase the block
 
      		);if
 
	   );progn
 
	);if
 
	(setvar "ATTDIA" 0)
	;switch off dialogue boxes
 
	(setq thedate (today))
	;calculate and format date
 
	(setq thetime (time))
	;calculate and format time
 
	(setq plotby (getvar "LOGINNAME"))
	;get the users name
	
	(command "Insert" "Stamp" "0,0" "" "" "" thedate thetime plotby)
	;insert the block and fill in the attribute data
 
	(setvar "ATTDIA" 1)
	;switch the dialogues back on
 
	(setvar "HIGHLIGHT" 1)
	;switch Highlight On
 
	(setvar "CMDECHO" 1)
	;switch Cmdecho On
 
  (princ)
 
);defun
 
;===============================================================
(defun dxf(code elist)
 
  (cdr (assoc code elist))
  ;finds the association pair, strips 1st element
 
);defun
;===============================================================
(defun TODAY ( / d yr mo day)
     (setq d (rtos (getvar "CDATE") 2 6)
          yr (substr d 3 2)
          mo (substr d 5 2)
         day (substr d 7 2)
     );setq
     (strcat day "/" mo "/" yr)
);defun
;;;*-----------------------------------------------------------
(defun TIME ( / d hr m s)
     (setq d (rtos (getvar "CDATE") 2 6)
          hr (substr d 10 2)
           m (substr d 12 2)
           s (substr d 14 2)
     );setq
     (strcat hr ":" m ":" s)
);defun
;;;*------------------------------------------------------------
(princ)

This routine basically does the following :

First of all, it checks if there are any blocks in the drawing that contain attributes. If there are, it loops through each block checking if it has the name "STAMP". If it has, it deletes the block. The routine then inserts a new "STAMP" block with the updated attribute data.

After running this routine, you should have a block similar to this in the lower left hand corner of your drawing.

Date Stamp

You can now plot your drawing with the new or updated Date and Time stamp.

You should find it relatively easy to modify this routine to suit any kind of Date/Time stamp that you would wish to add to a drawing. You could add a stamp that places the name and path of all Xrefs attached to the drawing, along with, the date they where added and who added them.

If you would like the source coding for the the Timestamp routine, just place your mouse here, say a prayer, and click… Enjoy.