Jump to content

Problem with Date & time stamp


ajax123

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

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.

Link to comment
Share on other sites

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 "%<\\AcVar Date \\f \"dd/MM/yyyy\">%")
         )
         ( (eq "TIME" tag)
           (vla-put-textstring att "%<\\AcVar Date \\f \"HH:mm:ss\">%")
         )
         ( (eq "BY" tag)
           (vla-put-textstring att "%<\\AcVar Login \\f \"%tc4\">%")
         )
       )
     )
     (vl-cmdf "_.updatefield" (vlax-vla-object->ename blk) "")
   )
 )
 (princ)
)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

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]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...