Author Topic: I need a lisp that changes the first character of attributes  (Read 7349 times)

0 Members and 1 Guest are viewing this topic.

antistar

  • Guest
I have many blocks of attributes with values as: F521, G622, D773...
I need a lisp that changes the first character of these attributes to the same value: A521, A622, A773...

Does anyone have a routine that does this?

Thanks in advance.
« Last Edit: July 03, 2012, 09:28:50 AM by antistar »

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: I need a lisp that changes the first character of attributes
« Reply #1 on: July 03, 2012, 09:07:44 AM »
Use the substr function to remove the first character, then the strcat function to add an 'A' to the front.

i.e.
Code - Auto/Visual Lisp: [Select]
  1. (strcat "A" (substr <attribute value> 2))

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: I need a lisp that changes the first character of attributes
« Reply #2 on: July 03, 2012, 09:13:09 AM »
This .... ?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s)
  2.   (if (setq s (ssget "_:L" '((0 . "ATTDEF"))))
  3.     ((lambda (i / n)
  4.        (while (setq n (ssname s (setq i (1+ i))))
  5.          (entmod
  6.            (subst
  7.              (cons 2
  8.                    (strcat "A"
  9.                            (substr (cdr (assoc 2 (entget n)))
  10.                                    2
  11.                                    (strlen (cdr (assoc 2 (entget n))))
  12.                            )
  13.                    )
  14.              )
  15.              (assoc 2 (entget n))
  16.              (entget n)
  17.            )
  18.          )
  19.        )
  20.      )
  21.       -1
  22.     )
  23.     (princ)
  24.   )
  25.   (princ)
  26. )
  27.  
  28.  

antistar

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #3 on: July 03, 2012, 09:27:59 AM »
This .... ?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s)
  2.   (if (setq s (ssget "_:L" '((0 . "ATTDEF"))))
  3.     ((lambda (i / n)
  4.        (while (setq n (ssname s (setq i (1+ i))))
  5.          (entmod
  6.            (subst
  7.              (cons 2
  8.                    (strcat "A"
  9.                            (substr (cdr (assoc 2 (entget n)))
  10.                                    2
  11.                                    (strlen (cdr (assoc 2 (entget n))))
  12.                            )
  13.                    )
  14.              )
  15.              (assoc 2 (entget n))
  16.              (entget n)
  17.            )
  18.          )
  19.        )
  20.      )
  21.       -1
  22.     )
  23.     (princ)
  24.   )
  25.   (princ)
  26. )
  27.  
  28.  

Tharwat, thanks for your reply.
I need to select the blocks.
It would be like this?

...(setq s (ssget "_: L" '((0. "INSERT") (66. 1))))

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: I need a lisp that changes the first character of attributes
« Reply #4 on: July 03, 2012, 09:30:40 AM »
It would be like this?

...(setq s (ssget "_: L" '((0. "INSERT") (66. 1))))

  • Select the blocks (ssget with filter list)
  • Iterate over the Selection Set (repeat / while, ssname)
  • For each block entity in the set, iterate over the attributes (entnext)
  • Retrieve the attribute values (cdr, assoc DXF group code 1)
  • Modify the value (as demonstrated above, substr, strcat)
  • Update the value (subst, entmod, entupd)
« Last Edit: July 03, 2012, 11:54:16 AM by Lee Mac »

dong95

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #5 on: July 03, 2012, 11:01:15 AM »
Very clear instruction, Lee.
I learned a lot. Thanks.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I need a lisp that changes the first character of attributes
« Reply #6 on: July 03, 2012, 11:30:23 AM »
Is there more than one attribute in the inserts?
If so how will that attribute be identified?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: I need a lisp that changes the first character of attributes
« Reply #7 on: July 03, 2012, 11:54:47 AM »
Very clear instruction, Lee.
I learned a lot. Thanks.

I'm glad my post was beneficial to your learning dong95  :-)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: I need a lisp that changes the first character of attributes
« Reply #8 on: July 03, 2012, 12:48:49 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ selectionset integer selectionsetname v)
  2.   ;;; Tharwat 03. July. 2012 ;;;
  3.   (if (not acdoc)
  4.   )
  5.   (if (setq selectionset (ssget "_:L" '((0 . "INSERT") (66 . 1))))
  6.     (progn
  7.       (vla-StartUndoMark acdoc)
  8.       (repeat (setq integer (sslength selectionset))
  9.         (setq selectionsetname  (ssname selectionset (setq integer (1- integer)) ) )
  10.         (foreach x (vlax-invoke
  11.                      (vlax-ename->vla-object selectionsetname)  'GetAttributes )
  12.                           (vla-put-textstring x
  13.                                    (strcat "A" (substr (setq v (vla-get-textstring x)) 2 (strlen v)) )) )
  14.                   )
  15.       (vla-EndUndoMark acdoc)
  16.     )
  17.     (princ)
  18.   )
  19.   (princ)
  20. )
« Last Edit: July 03, 2012, 01:09:20 PM by Tharwat »

antistar

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #9 on: July 03, 2012, 01:06:33 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ selectionset integer selectionsetname v)
  2.   ;;; Tharwat 03. July. 2012 ;;;
  3.   (cond (not acdoc)
  4.   )
  5.   (if (setq selectionset (ssget "_:L" '((0 . "INSERT") (66 . 1))))
  6.     (progn
  7.       (vla-StartUndoMark acdoc)
  8.       (repeat (setq integer (sslength selectionset))
  9.         (setq selectionsetname  (ssname selectionset (setq integer (1- integer)) ) )
  10.         (foreach x (vlax-invoke
  11.                      (vlax-ename->vla-object selectionsetname)  'GetAttributes )
  12.                           (vla-put-textstring x
  13.                                    (strcat "A" (substr (setq v (vla-get-textstring x)) 2 (strlen v)) )) )
  14.                   )
  15.       (vla-EndUndoMark acdoc)
  16.     )
  17.     (princ)
  18.   )
  19.   (princ)
  20. )

Tharwat, is not working.
Show this error:
ERROR: bad argument type: VLA-OBJECT nil


antistar

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #11 on: July 03, 2012, 01:32:36 PM »
It would be like this?

...(setq s (ssget "_: L" '((0. "INSERT") (66. 1))))

  • Select the blocks (ssget with filter list)
  • Iterate over the Selection Set (repeat / while, ssname)
  • For each block entity in the set, iterate over the attributes (entnext)
  • Retrieve the attribute values (cdr, assoc DXF group code 1)
  • Modify the value (as demonstrated above, substr, strcat)
  • Update the value (subst, entmod, entupd)

Lee,
Thank you for your explanations.  :-)

antistar

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #12 on: July 03, 2012, 01:35:56 PM »
UPDATED :
:-)

Perfect, exactly what I need. It will be very useful to me.
Thank you for your attention and patience.

Regards.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: I need a lisp that changes the first character of attributes
« Reply #13 on: July 03, 2012, 01:37:29 PM »
UPDATED :
:-)

Perfect, exactly what I need. It will be very useful to me.
Thank you for your attention and patience.

Regards.

You're welcome anytime .  :wink:

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: I need a lisp that changes the first character of attributes
« Reply #14 on: July 03, 2012, 01:41:32 PM »
Lee,
Thank you for your explanations.  :-)

Evidently wasting my time since the code has been provided on a plate.

antistar

  • Guest
Re: I need a lisp that changes the first character of attributes
« Reply #15 on: July 03, 2012, 07:42:46 PM »
Lee,
Thank you for your explanations.  :-)

Evidently wasting my time since the code has been provided on a plate.

And you, never provided any code on a plate? You've helped so many people.

pBe

  • Bull Frog
  • Posts: 402
Re: I need a lisp that changes the first character of attributes
« Reply #16 on: July 04, 2012, 01:26:26 AM »
Is there more than one attribute in the inserts?
If so how will that attribute be identified?

I was thinking of the same thing, guess there's only one attribute on the block and then the  OP wants to be prompted for selection , otherwise the code would be needing a conditional test.

Tried Tharwats code on a block with mulitple Attributes and each and every values now has an "A" prefix including blanks.
So if that is the case, something like this would be a good addition. <that is if "_X" selection method is prefered>

(if (wcmatch (vla-get-textstring x) "pattern*")                             
 .....
)
« Last Edit: July 04, 2012, 01:57:29 AM by pBe »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I need a lisp that changes the first character of attributes
« Reply #17 on: July 04, 2012, 08:06:51 AM »
Good idea.
Perhaps if the attribute had a unique TAG name it could be used for identification.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

pBe

  • Bull Frog
  • Posts: 402
Re: I need a lisp that changes the first character of attributes
« Reply #18 on: July 04, 2012, 09:08:38 AM »
Good idea.
Perhaps if the attribute had a unique TAG name it could be used for identification.  8-)

Better  :-)

ahsattarian

  • Newt
  • Posts: 112
Re: I need a lisp that changes the first character of attributes
« Reply #19 on: December 07, 2016, 12:42:12 AM »
I can help u



Hello
 if u r still needing that program i can help u.
 Tell me to guide u.
 Amir 

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: I need a lisp that changes the first character of attributes
« Reply #20 on: December 07, 2016, 04:47:12 AM »
Perfect, exactly what I need.
@ahsattarian:
Reviving very old threads that have already been solved does not make much sense.
Why don't you try contributing to more recent threads?