乐筑天下

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

[编程交流] Help with code : Adding Prefix

[复制链接]

107

主题

615

帖子

575

银币

中流砥柱

Rank: 25

铜币
521
发表于 2022-7-5 16:00:19 | 显示全部楼层 |阅读模式
Hi David Bethel .I know that this is a very old post but i need a litle help with your code. I have some  attribute blocks and i use your code to add add Suffix or Prefix. The problem that most of them have thee tags and i want to add Suffix or Prefix only to the fist
 
In this 3 blocks i need to add Suffix or Prefix only in the tag with the name Point
 
  1. ;  define a function named addstr     ;  the c: creates a command line call     ;  (/ local_variable_list)(defun c:addstr (/ atype ns ss en ed an ad av nv i) ;  initialize a (getxxxx) call ;  if bit1 is set in (getkword), it forces an input of 1 of of the keywords ;  "Keyword List" (initget 1 "Suffix Prefix") ;  Select the type of action needed (setq atype (getkword "\nAdd Suffix or Prefix (S/P):   ")) ;  if bit 1 is set , this forces a non nil input (initget 1) ;  get user input of NewString ;  allow spaces , case sensitive (setq ns (getstring t "\nString To Add:  "))      ;  if user creates a SelectionSet of INSERTs with ATTRIButes (and (setq ss (ssget '((0 . "INSERT")(66 . 1))))      ;  Initialize an integer counter      (setq i 0)      ; step thru the selection SS 1 EName at a time      (while (setq en (ssname ss i))                   ;  retrieve the EntityDefinition             (setq ed (entget en)                   ;  retrieve the 1st Attribute eName                   an (entnext en)                   ;  retrieve the 1st Attribute Definition                   ad (entget an))             ;  step thru the INSERT definition and             ;  retrieve each attribute until a SEQEND entity is returned             (while (/= "SEQEND" (cdr (assoc 0 ad)))                    ;  retrieve the current Attribute Value                    (setq av (cdr (assoc 1 ad))                    ; concatenate the New Value                          nv (if (= atype "Prefix")                                 (strcat ns av)                                 (strcat av ns)))                    ;  modify the attribute entity definition with the new value                    (entmod (subst (cons 1 nv) (assoc 1 ad) ad))                    ; go to the next Attribute Name and Definition                    (setq an (entnext an)                          ad (entget an)))             ;  update the main insert Entity Name             (entupd en)             ;  increase the integer counter             (setq i (1+ i))));  exit the routine cleanly(prin1))
 
Any ideas. Thanks
Point.dwg
STATION.dwg
trigonom.dwg
回复

使用道具 举报

107

主题

615

帖子

575

银币

中流砥柱

Rank: 25

铜币
521
发表于 2022-7-5 16:10:51 | 显示全部楼层
I have and this code but i have the same problem
 
  1. defun c:PSBLOCK ( / as el en i ss str typ ) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Suffix or Prefix  : ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))     (repeat (setq i (sslength ss))         (setq en (ssname ss (setq i (1- i))))         (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))             (setq as (cdr (assoc 1 el)))             (if (eq "Prefix" typ)                 (if (not (wcmatch as (strcat str "*")))                     (entmod (subst (cons 1 (strcat str as)) (assoc 1 el) el))                 )                 (if (not (wcmatch as (strcat "*" str)))                     (entmod (subst (cons 1 (strcat as str)) (assoc 1 el) el))                 )             )         )     ) ) (princ))
回复

举报

58

主题

3353

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1761
发表于 2022-7-5 16:20:41 | 显示全部楼层
Try this:
  1. (defun c:psblock (/ as el en i n pat ss str tag typ) ;; Change tag to suit (setq tag "POINT") (initget "Prefix Suffix") (if (and (setq typ (cond ((getkword "\nAdd Suffix or Prefix  : "))                   ("Prefix")             )   )   (/= "" (setq str (getstring t (strcat typ " to Add: "))))   (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))     )   (repeat (setq i (sslength ss))     (setq en (ssname ss (setq i (1- i))))     (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))(setq as  (cdr (assoc 1 el))      pat (cond        ((eq "Prefix" typ) (strcat str as))                ((strcat as str))          )      n          (vl-string-search (strcase str) (strcase as)));; Check that tagname matches and the text is different(if (and (= tag (cdr (assoc 2 el)))         (or (null n)             (not (wcmatch (strcase as)                           (strcase (cond ((= 0 n) (strcat str "*"))                                          ((strcat "*" str))                                    )                           )                  )             )         )    )  (entmod (subst (cons 1 pat) (assoc 1 el) el)))     )   ) ) (princ))
回复

举报

107

主题

615

帖子

575

银币

中流砥柱

Rank: 25

铜币
521
发表于 2022-7-5 16:28:44 | 显示全部楼层
Hi ronjonp thanks for the help.For the  Prefix work fine but when i select Suffix delete all the exist text and insert only the Suffix leter.Any idea ?
回复

举报

58

主题

3353

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1761
发表于 2022-7-5 16:34:06 | 显示全部楼层
My bad .. give the code above a try again.
回复

举报

107

主题

615

帖子

575

银币

中流砥柱

Rank: 25

铜币
521
发表于 2022-7-5 16:44:52 | 显示全部楼层
Thank you for the help :D
回复

举报

58

主题

3353

帖子

33

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1761
发表于 2022-7-5 16:46:45 | 显示全部楼层
You're welcome
回复

举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 16:53:43 | 显示全部楼层
If you only want to change the 1st attribute you can do this without knowing the tag name, actaully can change any attribute by supplying its order number.
 
Here is an example
  1. (defun blpos ( / ss1 blname x num inc)(setq blname  (cdr (assoc 2 (entget (car (entsel "pick a block"))))))(setq ss1 (ssget "x"  (list (cons 0  "INSERT") (cons 2  blname))))(setq num (- (getint "\nEnter attribute position") 1)) ; attributes start at zero hence -1(setq pref "ASDF")(repeat (setq inc (sslength ss1))(setq atts (vlax-invoke (vlax-ename->vla-object (ssname SS1 (setq inc (1- inc)) )) 'getattributes))(vla-put-textstring (nth num atts) (strcat pref (vla-get-textstring (nth num atts))))) ; end repeat) ; end defun  (blpos)
回复

举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 17:01:26 | 显示全部楼层
Here's something that may be useful :
 
  1. [color=#8b4513];;;   Edit ATTRIButes By Number 1st/2nd/3rd   Replace/Suffix/Prefix[/color][b][color=BLACK]([/color][/b]defun c:att-ean [b][color=FUCHSIA]([/color][/b]/ n et ns ss i en an ad x et nv[b][color=FUCHSIA])[/color][/b][color=#8b4513];  [b][color=FUCHSIA]([/color][/b]SetUndo[b][color=FUCHSIA])[/color][/b][/color] [b][color=FUCHSIA]([/color][/b]initget 7[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq n [b][color=NAVY]([/color][/b]getint [color=#2f4f4f]"\nATTRIBute Number To Edit:  "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]initget 1 [color=#2f4f4f]"Replace Prefix Suffix"[/color][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq et [b][color=NAVY]([/color][/b]getkword [color=#2f4f4f]"\nEdit Type - Replace Prefix Suffix:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]cond [b][color=NAVY]([/color][/b][b][color=MAROON]([/color][/b]= et [color=#2f4f4f]"Replace"[/color][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]setq ns [b][color=GREEN]([/color][/b]getstring t [b][color=BLUE]([/color][/b]strcat [color=#2f4f4f]"\nReplacement String For ATTRIBute "[/color] [b][color=RED]([/color][/b]itoa n[b][color=RED])[/color][/b] [color=#2f4f4f]":   "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]       [b][color=NAVY]([/color][/b][b][color=MAROON]([/color][/b]= et [color=#2f4f4f]"Prefix"[/color][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]setq ns [b][color=GREEN]([/color][/b]getstring t [b][color=BLUE]([/color][/b]strcat [color=#2f4f4f]"\nPrefix To Add To ATTRIBute "[/color] [b][color=RED]([/color][/b]itoa n[b][color=RED])[/color][/b] [color=#2f4f4f]":   "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]       [b][color=NAVY]([/color][/b][b][color=MAROON]([/color][/b]= et [color=#2f4f4f]"Suffix"[/color][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]setq ns [b][color=GREEN]([/color][/b]getstring t [b][color=BLUE]([/color][/b]strcat [color=#2f4f4f]"\nSuffix To Add To ATTRIBute "[/color] [b][color=RED]([/color][/b]itoa n[b][color=RED])[/color][/b] [color=#2f4f4f]":   "[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not ss[b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b]                              [b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq i 0[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq en [b][color=MAROON]([/color][/b]ssname ss i[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]setq an [b][color=MAROON]([/color][/b]entnext en[b][color=MAROON])[/color][/b]              ad [b][color=MAROON]([/color][/b]entget an[b][color=MAROON])[/color][/b]               x 1[b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 0 ad[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]               [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= x n[b][color=GREEN])[/color][/b]                    [b][color=GREEN]([/color][/b]setq nv [b][color=BLUE]([/color][/b]cond [b][color=RED]([/color][/b][b][color=PURPLE]([/color][/b]= et [color=#2f4f4f]"Replace"[/color][b][color=PURPLE])[/color][/b] ns[b][color=RED])[/color][/b]                                   [b][color=RED]([/color][/b][b][color=PURPLE]([/color][/b]= et [color=#2f4f4f]"Prefix"[/color][b][color=PURPLE])[/color][/b]  [b][color=PURPLE]([/color][/b]strcat ns [b][color=TEAL]([/color][/b]cdr [b][color=OLIVE]([/color][/b]assoc 1 ad[b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]                                   [b][color=RED]([/color][/b][b][color=PURPLE]([/color][/b]= et [color=#2f4f4f]"Suffix"[/color][b][color=PURPLE])[/color][/b]  [b][color=PURPLE]([/color][/b]strcat [b][color=TEAL]([/color][/b]cdr [b][color=OLIVE]([/color][/b]assoc 1 ad[b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b] ns[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                    [b][color=GREEN]([/color][/b]setq ad [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 1 nv[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 1 ad[b][color=RED])[/color][/b] ad[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                    [b][color=GREEN]([/color][/b]entmod ad[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]                [b][color=MAROON]([/color][/b]setq x [b][color=GREEN]([/color][/b]1+ x[b][color=GREEN])[/color][/b]                     an [b][color=GREEN]([/color][/b]entnext an[b][color=GREEN])[/color][/b]                     ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]entupd en[b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]setq i [b][color=MAROON]([/color][/b]1+ i[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][color=#8b4513];  [b][color=FUCHSIA]([/color][/b]EndUndo[b][color=FUCHSIA])[/color][/b][/color] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 
-David
回复

举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-15 03:04 , Processed in 0.482598 second(s), 71 queries .

© 2020-2025 乐筑天下

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