Apologies, I was looking for better ways to accomplish your goal and overlooking the learning aspect - I find that its actually quite rare that members genuinely wish to learn.
Anyway, take a look at this code:
- (defun c:timestamp ( / *error* _GetDate block values vars ) (defun *error* ( msg ) (if values (mapcar 'setvar vars values)) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (defun _GetDate ( format ) (menucmd (strcat "m=$(edtime,$(getvar,DATE)," format ")")) ) (setq block "STAMP") ;; Block to be inserted (cond ( (not (or (tblsearch "BLOCK" block) (setq block (findfile (strcat block ".dwg"))) ) ) (princ "\n--> Block not Found.") ) ( t (setq vars '("CMDECHO" "ATTREQ") values (mapcar 'getvar vars) ) (mapcar 'setvar vars '(0 1)) (command "_.-insert" block "_S" 1.0 "_R" 0.0 "_non" '(0. 0. 0.) (getvar 'LOGINNAME) (_GetDate "DD/MO/YY") (_GetDate "HH:MM:SS") ) (mapcar 'setvar vars values) ) ) (princ))
I have dissected it into steps here:
- ([color=BLUE]defun[/color] c:timestamp ( [color=BLUE]/[/color] *error* _GetDate block values vars ) [color=GREEN];; Define function, localise local functions and variables[/color] [color=GREEN];; Localising variables is important, to see why, go to:[/color] [color=GREEN];; www.lee-mac.com/localising.html[/color] [color=GREEN];; Error Handler:[/color] [color=GREEN];;[/color] [color=GREEN];; This function will reset the System Variables should[/color] [color=GREEN];; anything go wrong in the code or if the user presses Esc[/color] [color=GREEN];; during program execution.[/color] ([color=BLUE]defun[/color] *error* ( msg ) ([color=BLUE]if[/color] values ([color=BLUE]mapcar[/color] '[color=BLUE]setvar[/color] vars values)) ([color=BLUE]or[/color] ([color=BLUE]wcmatch[/color] ([color=BLUE]strcase[/color] msg) [color=MAROON]"*BREAK,*CANCEL*,*EXIT*"[/color]) ([color=BLUE]princ[/color] ([color=BLUE]strcat[/color] [color=MAROON]"\n** Error: "[/color] msg [color=MAROON]" **"[/color]))) ([color=BLUE]princ[/color]) ) [color=GREEN];; _GetDate subfunction[/color] [color=GREEN];;[/color] [color=GREEN];; This function uses DIESEL to return a string[/color] [color=GREEN];; representing the date or time in a specified format[/color] ([color=BLUE]defun[/color] _GetDate ( format ) ([color=BLUE]menucmd[/color] ([color=BLUE]strcat[/color] [color=MAROON]"m=$(edtime,$(getvar,DATE),"[/color] format [color=MAROON]")"[/color])) ) [color=GREEN];; Name of Block to be inserted[/color] ([color=BLUE]setq[/color] block [color=MAROON]"STAMP"[/color]) ([color=BLUE]cond[/color] ( ([color=BLUE]not[/color] [color=GREEN];; Returns T if the next expression returns nil[/color] ([color=BLUE]or[/color] [color=GREEN];; Either of the following expressions must return non-nil for OR to return T[/color] ([color=BLUE]tblsearch[/color] [color=MAROON]"BLOCK"[/color] block) [color=GREEN];; Returns a non-nil value if Block Defintion already exists in the drawing[/color] ([color=BLUE]setq[/color] block ([color=BLUE]findfile[/color] ([color=BLUE]strcat[/color] block [color=MAROON]".dwg"[/color]))) [color=GREEN];; Returns the filepath of the Drawing file if it is in the AutoCAD support path[/color] ) ) [color=GREEN];; Block cannot be found, so print that to the user[/color] ([color=BLUE]princ[/color] [color=MAROON]"\n--> Block not Found."[/color]) ) ( [color=BLUE]t[/color] [color=GREEN];; Otherwise block is found, so this condition is now evaluated[/color] [color=GREEN];; T is used to ensure this condition is evaluated - the default condition if you like.[/color] ([color=BLUE]setq[/color] vars '([color=MAROON]"CMDECHO"[/color] [color=MAROON]"ATTREQ"[/color]) [color=GREEN];; Store a list of System Variable names[/color] values ([color=BLUE]mapcar[/color] '[color=BLUE]getvar[/color] vars) [color=GREEN];; Store a list of their values[/color] ) ([color=BLUE]mapcar[/color] '[color=BLUE]setvar[/color] vars '(0 1)) [color=GREEN];; Set CMDECHO=0, ATTREQ=1[/color] ([color=BLUE]command[/color] [color=MAROON]"_.-insert"[/color] block [color=MAROON]"_S"[/color] 1.0 [color=MAROON]"_R"[/color] 0.0 [color=MAROON]"_non"[/color] '(0. 0. 0.) ([color=BLUE]getvar[/color] 'LOGINNAME) (_GetDate [color=MAROON]"DD/MO/YY"[/color]) (_GetDate [color=MAROON]"HH:MM:SS"[/color]) ) [color=GREEN];; Insert the Block, Scale=1, Rotation=1, Insertion=0,0,0[/color] [color=GREEN];; Populate Attributes at prompts.[/color] ([color=BLUE]mapcar[/color] '[color=BLUE]setvar[/color] vars values) [color=GREEN];; Reset System Variables[/color] ) [color=GREEN];; End COND Condition[/color] ) [color=GREEN];; End COND[/color] ([color=BLUE]princ[/color]) [color=GREEN];; Exit Cleanly[/color] ) [color=GREEN];; End Timestamp[/color]
|