乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 9|回复: 9

[编程交流] Problem with Date & time stamp

[复制链接]

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 09:10:34 | 显示全部楼层 |阅读模式
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
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:20:22 | 显示全部楼层
Use a Field.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-6 09:28:37 | 显示全部楼层
Or perhaps rtext with diesel functions
回复

使用道具 举报

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 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
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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.
回复

使用道具 举报

4

主题

2143

帖子

2197

银币

限制会员

铜币
-24
发表于 2022-7-6 09:45:58 | 显示全部楼层
 
I prefer using RTEXT with Diesel myself.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-6 09:56:34 | 显示全部楼层
Example to insert the block and populate with fields:
 
  1. (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))
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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.
回复

使用道具 举报

1

主题

3

帖子

2

银币

初来乍到

Rank: 1

铜币
5
发表于 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
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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:
 
  1. (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:
 
  1. ([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]
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-7 05:19 , Processed in 0.446372 second(s), 83 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表