TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: luisternou on March 22, 2017, 03:45:55 PM

Title: Replace text with existing block with attribute
Post by: luisternou on March 22, 2017, 03:45:55 PM
Hello everyone!
I have several bridge-numbers in my drawing(2000+). I want to replace the text numbers with an existing block with an attribute in it who's value becomes de replaced text.
Is there someone who can help me to write a lisp?
Thanks in advance,
Kind regards, Luis
Title: Re: Replace text with existing block with attribute
Post by: ChrisCarlson on March 22, 2017, 03:59:36 PM
This isn't too bad if you're willing to learn. You need to break it down into steps first. This is how I would break it down

1) Select text entities and filter on entities with the string you want to replace
    1a) Create a txtstr variable
    1b) Create a variable for insertion point based on the text entity
2) Insert the block
4) Input the string into the attribute of the block entity
5) Delete the text entity
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 22, 2017, 05:07:19 PM
  :idea:  eeeeh oké, thanks, that looks very simple! But to be honest. I tried in the past to write lisp but I don't have the talent and/or brains to get it right.  :idiot2: I hope you or someone else can help me. Anyhow, thanks for replying!
Title: Re: Replace text with existing block with attribute
Post by: ChrisCarlson on March 23, 2017, 08:13:45 AM
Research and examine the following;

Code - Auto/Visual Lisp: [Select]
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 23, 2017, 01:13:59 PM
Sorry Master_Shake but I am in a hurry! So what I did is: I did the command Tcircle and selected all the text. Then with an old lisp I have I converted all the 2014 circles to a circle block. Then in Express-tools I replaced all the circle blocks with the one I want with the attribute. With another lisp I copy the text to the attribute value one by one...  :embarrassed2:
Title: Re: Replace text with existing block with attribute
Post by: ronjonp on March 23, 2017, 01:32:33 PM
If you still have text near the blocks, THIS (https://www.theswamp.org/index.php?topic=52187.msg571779#msg571779) could be modified to copy text value into the attribute.
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 23, 2017, 01:59:27 PM
Thanks ronjonp! That looks promising. I will look in to that first thing in the morning!
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 24, 2017, 07:27:23 AM
Hi Ronjonp! I tried the lisp but it not working in my drawing. I attached a small part of it. Maybe you can take a look?
Thanks in advance!

Kind regards,
Luis
Title: Re: Replace text with existing block with attribute
Post by: ronjonp on March 24, 2017, 09:03:09 AM
Hi Ronjonp! I tried the lisp but it not working in my drawing. I attached a small part of it. Maybe you can take a look?
Thanks in advance!

Kind regards,
Luis
Here you go:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ blocks el fuzz p text txt x)
  2.   (setq fuzz 0.001)
  3.   (if (and (setq text (ssget "_x" '((0 . "text") (8 . "!Kunstwerken_Watervaknummer"))))
  4.            (setq blocks (ssget "_x" '((0 . "insert") (66 . 1) (2 . "Symbool_Kunstwerk"))))
  5.            (setq blocks (mapcar 'vlax-ename->vla-object (mapcar 'cadr (ssnamex blocks))))
  6.            (setq
  7.              text (mapcar
  8.                     '(lambda (x) (setq el (entget x)) (list (cdr (assoc 11 el)) (cdr (assoc 1 el)) x))
  9.                     (mapcar 'cadr (ssnamex text))
  10.                   )
  11.            )
  12.       )
  13.     (foreach block blocks
  14.       (setq p (vlax-get block 'insertionpoint))
  15.       (if (setq txt (vl-some '(lambda (x)
  16.                                 (if (equal p (car x) fuzz)
  17.                                   x
  18.                                 )
  19.                               )
  20.                              text
  21.                     )
  22.           )
  23.         (progn (mapcar '(lambda (x) (vla-put-textstring x (cadr txt)))
  24.                        (vlax-invoke block 'getattributes)
  25.                )
  26.                (entdel (last txt))
  27.         )
  28.       )
  29.     )
  30.   )
  31.   (princ)
  32. )
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 24, 2017, 10:22:49 AM
Hello again Ronjonp!
For you it is maybe nothing but for me this is amazing  :yay!: :yay!: :yay!: Thank you very much!! :-D

In the meantime I was also examining this routine. It did something but not as smooth and fast as your routine. Maybe it is interesting for you to study.
http://cadtips.cadalyst.com/attributed-blocks/automatically-convert-text-or-mtext-attributes (credits to Ricky Medley).

Anyway, thangs again! I am glad I get my deadline in time!  :-D
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 24, 2017, 10:23:25 AM
 :yay!: :yay!: :yay!:
Title: Re: Replace text with existing block with attribute
Post by: ronjonp on March 24, 2017, 10:31:35 AM
Hello again Ronjonp!
For you it is maybe nothing but for me this is amazing  :yay!: :yay!: :yay!: Thank you very much!! ;D

In the meantime I was also examining this routine. It did something but not as smooth and fast as your routine. Maybe it is interesting for you to study.
http://cadtips.cadalyst.com/attributed-blocks/automatically-convert-text-or-mtext-attributes (http://cadtips.cadalyst.com/attributed-blocks/automatically-convert-text-or-mtext-attributes) (credits to Ricky Medley).

Anyway, thangs again! I am glad I get my deadline in time!  ;D
Glad to help :) .. now you can go home early today!  ;)
Title: Re: Replace text with existing block with attribute
Post by: luisternou on March 24, 2017, 11:31:04 AM
 :-D Exacly, I go now, have a nice weekend Ronjonp!
Title: Re: Replace text with existing block with attribute
Post by: ronjonp on March 24, 2017, 11:40:34 AM
;D Exacly, I go now, have a nice weekend Ronjonp!
You too  :)
Title: Re: Replace text with existing block with attribute
Post by: symoin on January 03, 2024, 11:56:06 AM
Dear RonJonp,

How to modfiy or is there any way for converting the Normal block without attribute to a block with attribute and attach the nearest text on the same layer as its value.
dOESN'T MATTER IF THE BLOCK IS REPLACE WITH A NEW BLOCK.
Title: Re: Replace text with existing block with attribute
Post by: BIGAL on January 03, 2024, 07:12:23 PM
Yes you can add a attribute to a block via lisp, do a google. "add attribute to existing block autocad lisp"

You can always bedit a block and add an attribute, then use existing code.