Jump to content

Help with code : Adding Prefix or Suffix to block attribute (by David Bethel)


Guest

Recommended Posts

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 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

Link to comment
Share on other sites

I have and this code but i have the same problem

 

defun c:PSBLOCK ( / as el en i ss str typ )

 (initget "Prefix Suffix")
 (setq typ (cond ((getkword "\nAdd Suffix or Prefix  <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)
)

Link to comment
Share on other sites

Try this:

(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>: "))
		   ("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)
)

Edited by ronjonp
Link to comment
Share on other sites

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

(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)

Link to comment
Share on other sites

Here's something that may be useful :

 

[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

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...