ajax123 发表于 2022-7-6 09:10:34

Problem with Date & time stamp

I found a time & date stamp Lisp code from Afralisp site. I loaded this code to my AutoCAD 2009 electrical, the stamp block is shown on the drawing, but there are Date, Time & user name on the screen. Can any one tell me why?
Thank you so much,
 
Lena
STAMP.dwg
Timestamp.lsp

Lee Mac 发表于 2022-7-6 09:20:22

Use a Field.

Tharwat 发表于 2022-7-6 09:28:37

Or perhaps rtext with diesel functions

ajax123 发表于 2022-7-6 09:30:50

Thanks Lee,
My problem is: After the code has been loaded, the Stamp Block has shown on the screen. but I can not see the attributes (date, time, logname). Is there any problem in the code?
Thanks,
 
Lena

Lee Mac 发表于 2022-7-6 09:42:30

 
Its likely that the ATTREQ System Variable is set to 0, and so the attributes are not being populated; but there are many other things I would change with the code.
 
Using a field in an attribute would be the best way to approach this task however, since the field would update automatically without having to erase and reinsert the block.

SLW210 发表于 2022-7-6 09:45:58

 
I prefer using RTEXT with Diesel myself.

Lee Mac 发表于 2022-7-6 09:56:34

Example to insert the block and populate with fields:
 

(defun c:timestamp ( / blk block tag ) (setq block "STAMP");; Block to be inserted (cond   (   (not       (or         (tblsearch "BLOCK" (setq blk block))         (setq blk (findfile (strcat block ".dwg")))       )   )   (princ (strcat "\n--> " block ".dwg not found."))   )   (   (= 4       (logand 4         (cdr         (assoc 70             (tblsearch "LAYER" (getvar 'CLAYER))         )         )       )   )   (princ "\n--> Current Layer Locked.")   )   ( t   (foreach att       (vlax-invoke         (setq blk         (vlax-invoke             (vlax-get-property               (vla-get-activedocument (vlax-get-acad-object))               (if (= 1 (getvar 'CVPORT)) 'Paperspace 'Modelspace)             )             'InsertBlock '(0.0 0.0 0.0) blk 1.0 1.0 1.0 0.0         )         )         'getattributes       )       (cond         ( (eq "DATE" (setq tag (strcase (vla-get-tagstring att))))         (vla-put-textstring att "%%")         )         ( (eq "TIME" tag)         (vla-put-textstring att "%%")         )         ( (eq "BY" tag)         (vla-put-textstring att "%%")         )       )   )   (vl-cmdf "_.updatefield" (vlax-vla-object->ename blk) "")   ) ) (princ))

Lee Mac 发表于 2022-7-6 10:00:35

If you did want to go the RText route, here is a DIESEL reference:
 
http://docs.autodesk.com/ACD/2011/ENU/filesACG/WS73099cc142f4875513fb5cd10c4aa30d6b-7b3c.htm
 
You will require the edtime function.

ajax123 发表于 2022-7-6 10:07:20

Hi Lee,
Thank you very much for your help.
I just start to learn Autolisp. The reason I test this Stamp code is I want to know how to insert a block and then change the attributes for this block. I know how to insert a block, but couldn't replace attribute values. I set ATTREQ to 1, then a dialog pop up. if I set ATTREQ to 0, then no values for attribute. Could you guide me to do that?
I appreciate your help and your time.
 
Lena

Lee Mac 发表于 2022-7-6 10:16:24

 
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:
 

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