乐筑天下

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

[编程交流] Modify Picked Attrbute Height(

[复制链接]

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 18:53:31 | 显示全部楼层
 
Here is a full annotated Vanilla AutoLISP example for you to study:
  1. [color=GREEN];; Define function & declare local variables[/color]([color=BLUE]defun[/color] c:atthgt ( [color=BLUE]/[/color] att atx blk bln ent enx hgt tag )   [color=GREEN];; If the following expression returns a non-nil value[/color]   ([color=BLUE]if[/color]       [color=GREEN];; If all of the following expressions return a non-nil value[/color]       ([color=BLUE]and[/color]           [color=GREEN];; Prompt the user to select an attribute[/color]           ([color=BLUE]setq[/color] att ([color=BLUE]car[/color] ([color=BLUE]nentsel[/color] [color=MAROON]"\nSelect attribute: "[/color])))           [color=GREEN];; Retrieve the DXF data for the selected object[/color]           ([color=BLUE]setq[/color] atx ([color=BLUE]entget[/color] att))           [color=GREEN];; Check that the selected object is indeed an attribute[/color]           ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx)))           [color=GREEN];; Evaluate the following expressions and return the value[/color]           [color=GREEN];; returned by the last evaluated expression[/color]           ([color=BLUE]progn[/color]               [color=GREEN];; Restrict the following prompt to positive non-zero values[/color]               ([color=BLUE]initget[/color] 6)               [color=GREEN];; Prompt the user for a new text height[/color]               ([color=BLUE]setq[/color] hgt ([color=BLUE]getdist[/color] [color=MAROON]"\nSpecify new attribute height: "[/color]))           ) [color=GREEN];; end progn[/color]       ) [color=GREEN];; end and[/color]       [color=GREEN];; Then:[/color]       [color=GREEN];; Evaluate the following expressions within a single 'progn'[/color]       [color=GREEN];; expression constituting the THEN argument for the 'if' function[/color]       ([color=BLUE]progn[/color]           [color=GREEN];; Retrieve the attribute tag of the selected attribute[/color]           ([color=BLUE]setq[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 atx))           [color=GREEN];; Retrieve the parent entity of the attribute reference (i.e. the block reference)[/color]                 blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 330 atx))           [color=GREEN];; Retrieve the block name (non-dynamic blocks only!)[/color]                 bln ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 ([color=BLUE]entget[/color] blk)))           [color=GREEN];; Retrieve the corresponding block record entity for the block[/color]                 ent ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] bln)           ) [color=GREEN];; end setq[/color]           [color=GREEN];; While the following expression returns a non-nil value[/color]           ([color=BLUE]while[/color]               [color=GREEN];; Step through the entities following the block record entity in the database[/color]               ([color=BLUE]setq[/color] ent ([color=BLUE]entnext[/color] ent))               [color=GREEN];; If the following expression returns a non-nil value[/color]               ([color=BLUE]if[/color]                   [color=GREEN];; If all of the following expressions return a non-nil value[/color]                   ([color=BLUE]and[/color]                       [color=GREEN];; Retrieve the DXF data for the block component[/color]                       ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ent))                       [color=GREEN];; Check that it's an ATTDEF[/color]                       ([color=BLUE]=[/color] [color=MAROON]"ATTDEF"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx)))                       [color=GREEN];; Check that is has the correct tag[/color]                       ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)))                   ) [color=GREEN];; end and[/color]                   [color=GREEN];; Modify the following DXF data in the drawing database[/color]                   ([color=BLUE]entmod[/color]                       [color=GREEN];; Substitute the following group[/color]                       ([color=BLUE]subst[/color]                           [color=GREEN];; Construct a dotted pair to form DXF group 40[/color]                           ([color=BLUE]cons[/color] 40 hgt)                           [color=GREEN];; For the existing DXF group 40 entry[/color]                           ([color=BLUE]assoc[/color] 40 enx)                           [color=GREEN];; Within the ATTDEF dxf data[/color]                           enx                       ) [color=GREEN];; end subst[/color]                   ) [color=GREEN];; end entmod[/color]               ) [color=GREEN];; end if[/color]           ) [color=GREEN];; end while[/color]                      [color=GREEN];; Use the ATTSYNC command to apply the changes to all references of the block[/color]           ([color=BLUE]command[/color] [color=MAROON]"_.attsync"[/color] [color=MAROON]"_n"[/color] bln)                  ) [color=GREEN];; end progn[/color]       [color=GREEN];; Else the initial conditions were not met[/color]       [color=GREEN];; No real need to inform the user - they will know the error of their ways...[/color]          ) [color=GREEN];; end if[/color]   [color=GREEN];; Suppress the value returned by the last evaluated expression[/color]   ([color=BLUE]princ[/color])) [color=GREEN];; end defun[/color]
Given that I have donated my time to writing this code and commenting every line with an explanation, please take the time to carefully read the comments and study the code.
 
Lee
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 18:58:02 | 显示全部楼层
Jeepers Lee! Christmas has come early for me, loads of comments added! Thank you very much indeed.
 
I'd actually got slightly further than in my last post, realised that it's the block that needs the entmod & to use tblobjname to search within it for the relevant attribute., but that was as far as my thinking got really.
 
It's far more than I was expecting & far more complex that I thought the code would be. I created a lisp to change text/mtext heights & it was a doddle, then I thought to add attribute capability....
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 19:00:30 | 显示全部楼层
 
Just a bit more as an alternative you can change a attribute by its creation order rather than say nentsel & Tagname. Change 3rd attribute. You should be able to apply this to the block definition.
 
  1. ; Change attribute value by created position; By Alan H(vl-load-com)(setq y 1)(setq ss1 (car (entsel)))(setq bname (vla-get-name(vlax-ename->vla-object SS1))) (setq x (getint "\nEnter line no to pick")) ; change this line in block(SETQ newstrblank ".")(foreach att (vlax-invoke (vlax-ename->vla-object SS1) 'getattributes)   (if (= y x)   (progn   (setq newstr (vla-get-textstring att ))   (vla-put-textstring att newstrblank)   )   )   (setq y (+ Y 1)))(setq y 1)(setq x (getint "\nEnter line no to move to"))(foreach att (vlax-invoke (vlax-ename->vla-object SS1) 'getattributes)   (if (= y x)   (vla-put-textstring att newstr)   )   (setq y (+ Y 1)))(princ)
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:02:04 | 显示全部楼层
Further to the earlier reply it occurred to me that if there were duplicate tags within the block (quite common, the numbties) then the wrong attribute may get changed. Therefore I thought that if the lisp were using the entity name, which is to the best of my knowlege unique, then it would not amend the wrong attribute. But I'm having trouble making the entity names match.
I'll change the name of the symbol 'tag' to something more suitable later.
 
This:
  1. (setq tag (cdr (assoc 002 atx))
To this:
  1. (setq tag (cdr (assoc -1 atx))
 
And this:
  1. (= tag (cdr (assoc 2 enx)))
To this:
  1. (= tag (cdr (assoc -1 enx)))
 
As you can see from the log watch, the values don't match (not equal) for:
  1. (= tag (cdr (assoc -1 enx)))
  1. ...............LOG Watch...............ENT = ENX = ((-1 . ) (0 . "ATTDEF") ~ ~ ~TAG = ...............
But I'm working on it
PS Thanks again for all the commented code, comments are priceless to me when trying something new.
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 19:05:04 | 显示全部楼层
Did you see my reply ?
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:08:24 | 显示全部楼层
Yes I did & I apologise for not replying sooner Tharwat.
I admit that I got a bit frightened off by all those VL functions, ordinary lisp I'm fairly comfortable with when it comes to tweaking it, but I don't yet know enough about visual lisp to feel confident enough to attempt a tweak.
I've just tried your code & it works very well indeed. Please may I ask what criteria the lisp uses to pin-point which attribute is to have it's text height changed (Lee's uses the tag name).
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

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

铜币
358
发表于 2022-7-5 19:13:16 | 显示全部楼层
 
No problem, and its okay.
 
 
It may is at the first glance but it would become easy to you by time if you keep on coding and readying about functions you need to use.
 
Just ask if you have any question about my codes.
 
 
I am also used the tag string / name to pick the needed tag string's height to be changed.
 
Regards.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:16:04 | 显示全部楼层
 
Entity names & Object IDs are indeed unique within the active drawing session (but not persistent between drawing sessions - only handles are persistent). However, as noted earlier, the attribute selected by the user is the attribute reference (ATTRIB), whereas, the modifications are being performed on the attribute definition (ATTDEF), which is not the same entity.
 
 
You're welcome - I'm glad it helps.
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 2022-7-5 19:18:42 | 显示全部楼层
Here is one possible method to account for duplicate tags:
  1. [color=GREEN];; Define function & declare local variables[/color]([color=BLUE]defun[/color] c:atthgt ( [color=BLUE]/[/color] att atx blk bln cnt ent enx flg han hgt tag )   [color=GREEN];; If the following expression returns a non-nil value[/color]   ([color=BLUE]if[/color]       [color=GREEN];; If all of the following expressions return a non-nil value[/color]       ([color=BLUE]and[/color]           [color=GREEN];; Prompt the user to select an attribute[/color]           ([color=BLUE]setq[/color] att ([color=BLUE]car[/color] ([color=BLUE]nentsel[/color] [color=MAROON]"\nSelect attribute: "[/color])))           [color=GREEN];; Retrieve the DXF data for the selected object[/color]           ([color=BLUE]setq[/color] atx ([color=BLUE]entget[/color] att))           [color=GREEN];; Check that the selected object is indeed an attribute[/color]           ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx)))           [color=GREEN];; Evaluate the following expressions and return the value[/color]           [color=GREEN];; returned by the last evaluated expression[/color]           ([color=BLUE]progn[/color]               [color=GREEN];; Restrict the following prompt to positive non-zero values[/color]               ([color=BLUE]initget[/color] 6)               [color=GREEN];; Prompt the user for a new text height[/color]               ([color=BLUE]setq[/color] hgt ([color=BLUE]getdist[/color] [color=MAROON]"\nSpecify new attribute height: "[/color]))           ) [color=GREEN];; end progn[/color]       ) [color=GREEN];; end and[/color]       [color=GREEN];; Then:[/color]       [color=GREEN];; Evaluate the following expressions within a single 'progn'[/color]       [color=GREEN];; expression constituting the THEN argument for the 'if' function[/color]       ([color=BLUE]progn[/color]           [color=GREEN];; Retrieve the attribute tag of the selected attribute[/color]           ([color=BLUE]setq[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 atx))           [color=GREEN];; Retrieve the handle of the selected attribute (in case there are duplicate tags)[/color]                 han ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 005 atx))           [color=GREEN];; Retrieve the parent entity of the attribute reference (i.e. the block reference)[/color]                 blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 330 atx))           [color=GREEN];; Retrieve the block name (non-dynamic blocks only!)[/color]                 bln ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 ([color=BLUE]entget[/color] blk)))           [color=GREEN];; Retrieve the corresponding block record entity for the block[/color]                 ent ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] bln)           [color=GREEN];; Retrieve the first attribute reference entity[/color]                 att ([color=BLUE]entnext[/color] blk)           [color=GREEN];; Retrieve the DXF data for the first attribute reference entity[/color]                 atx ([color=BLUE]entget[/color]  att)           [color=GREEN];; Initialise a counter variable[/color]                 cnt 0           ) [color=GREEN];; end setq[/color]                      [color=GREEN];; While the following expression returns a non-nil value[/color]           ([color=BLUE]while[/color]               [color=GREEN];; If all of the following expressions return a non-nil value[/color]               ([color=BLUE]and[/color]                   [color=GREEN];; We haven't found the selected attribute reference[/color]                   ([color=BLUE]not[/color] flg)                   [color=GREEN];; While the entity is an attribute reference[/color]                   ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx)))               ) [color=GREEN];; end and[/color]               [color=GREEN];; If the attribute tag matches that of the selected attribute[/color]               ([color=BLUE]if[/color] ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 atx)))                   [color=GREEN];; Then increment the counter variable[/color]                   ([color=BLUE]setq[/color] cnt ([color=BLUE]1+[/color] cnt)                   [color=GREEN];; Test whether the attribute reference handle matches that of the selected attribute[/color]                         flg ([color=BLUE]=[/color] han ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 5 atx)))                   ) [color=GREEN];; end setq[/color]               ) [color=GREEN];; end if[/color]               [color=GREEN];; Retrieve the next entity in the database[/color]               ([color=BLUE]setq[/color] att ([color=BLUE]entnext[/color] att)               [color=GREEN];; Retrieve the DXF data for the next entity in the database[/color]                     atx ([color=BLUE]entget[/color]  att)               ) [color=GREEN];; end setq[/color]          ) [color=GREEN];; end while[/color]                      [color=GREEN];; While the following expression returns a non-nil value[/color]           ([color=BLUE]while[/color]               [color=GREEN];; Step through the entities following the block record entity in the database[/color]               ([color=BLUE]setq[/color] ent ([color=BLUE]entnext[/color] ent))               [color=GREEN];; If the following expression returns a non-nil value[/color]               ([color=BLUE]if[/color]                   [color=GREEN];; If all of the following expressions return a non-nil value[/color]                   ([color=BLUE]and[/color]                       [color=GREEN];; Retrieve the DXF data for the block component[/color]                       ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ent))                       [color=GREEN];; Check that it's an ATTDEF[/color]                       ([color=BLUE]=[/color] [color=MAROON]"ATTDEF"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx)))                       [color=GREEN];; Check that is has the correct tag[/color]                       ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)))                       [color=GREEN];; And that it is the correct attribute (if duplicate tags exist)[/color]                       ([color=BLUE]zerop[/color] ([color=BLUE]setq[/color] cnt ([color=BLUE]1-[/color] cnt)))                   ) [color=GREEN];; end and[/color]                   [color=GREEN];; Modify the following DXF data in the drawing database[/color]                   ([color=BLUE]entmod[/color]                       [color=GREEN];; Substitute the following group[/color]                       ([color=BLUE]subst[/color]                           [color=GREEN];; Construct a dotted pair to form DXF group 40[/color]                           ([color=BLUE]cons[/color] 40 hgt)                           [color=GREEN];; For the existing DXF group 40 entry[/color]                           ([color=BLUE]assoc[/color] 40 enx)                           [color=GREEN];; Within the ATTDEF dxf data[/color]                           enx                       ) [color=GREEN];; end subst[/color]                   ) [color=GREEN];; end entmod[/color]               ) [color=GREEN];; end if[/color]           ) [color=GREEN];; end while[/color]                      [color=GREEN];; Use the ATTSYNC command to apply the changes to all references of the block[/color]           ([color=BLUE]command[/color] [color=MAROON]"_.attsync"[/color] [color=MAROON]"_n"[/color] bln)                  ) [color=GREEN];; end progn[/color]       [color=GREEN];; Else the initial conditions were not met[/color]       [color=GREEN];; No real need to inform the user - they will know the error of their ways...[/color]          ) [color=GREEN];; end if[/color]   [color=GREEN];; Suppress the value returned by the last evaluated expression[/color]   ([color=BLUE]princ[/color])) [color=GREEN];; end defun[/color]
回复

使用道具 举报

21

主题

155

帖子

135

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
105
发表于 2022-7-5 19:23:25 | 显示全部楼层
Thanks for that Lee, I'll give it a try.
When I started this post I expected someone to point out a simple error in my code. It's kinda snowballed!
 
I think I've broken my vlide playing with bits of code today - it no longer reports global variables on a 'Check Text in Editor' :-(
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-12 20:18 , Processed in 0.415739 second(s), 81 queries .

© 2020-2025 乐筑天下

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