TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: antistar on July 03, 2012, 08:45:44 AM

Title: I need a lisp that changes the first character of attributes
Post by: antistar on July 03, 2012, 08:45:44 AM
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.
Title: Re: I need a lisp that changes the first character of attributes
Post by: Lee Mac 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))
Title: Re: I need a lisp that changes the first character of attributes
Post by: Tharwat 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.  
Title: Re: I need a lisp that changes the first character of attributes
Post by: antistar 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))))
Title: Re: I need a lisp that changes the first character of attributes
Post by: Lee Mac on July 03, 2012, 09:30:40 AM
It would be like this?

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

Title: Re: I need a lisp that changes the first character of attributes
Post by: dong95 on July 03, 2012, 11:01:15 AM
Very clear instruction, Lee.
I learned a lot. Thanks.
Title: Re: I need a lisp that changes the first character of attributes
Post by: CAB 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?
Title: Re: I need a lisp that changes the first character of attributes
Post by: Lee Mac 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  :-)
Title: Re: I need a lisp that changes the first character of attributes
Post by: Tharwat 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. )
Title: Re: I need a lisp that changes the first character of attributes
Post by: antistar 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
Title: Re: I need a lisp that changes the first character of attributes
Post by: Tharwat on July 03, 2012, 01:10:05 PM
UPDATED :
:-)
Title: Re: I need a lisp that changes the first character of attributes
Post by: antistar 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.  :-)
Title: Re: I need a lisp that changes the first character of attributes
Post by: antistar 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.
Title: Re: I need a lisp that changes the first character of attributes
Post by: Tharwat 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:
Title: Re: I need a lisp that changes the first character of attributes
Post by: Lee Mac 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.
Title: Re: I need a lisp that changes the first character of attributes
Post by: antistar 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.
Title: Re: I need a lisp that changes the first character of attributes
Post by: pBe 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*")                             
 .....
)
Title: Re: I need a lisp that changes the first character of attributes
Post by: CAB 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-)
Title: Re: I need a lisp that changes the first character of attributes
Post by: pBe 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  :-)
Title: Re: I need a lisp that changes the first character of attributes
Post by: ahsattarian 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 
Title: Re: I need a lisp that changes the first character of attributes
Post by: roy_043 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?