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
; 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 issetin (getkword), it forces an input of1ofof 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 1isset , 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
(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))
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.
[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]