Author Topic: text find and replace  (Read 3492 times)

0 Members and 1 Guest are viewing this topic.

gisdude

  • Newt
  • Posts: 23
text find and replace
« on: February 25, 2020, 06:16:40 PM »
Hi all,
I feel like I go here more than I should...but I digress.

I have a .dwg that has MANY lines of text, mtext, blocks, etc. In all of this organized chaos are wire tags that are labeled with the format-  (XXXX). The problem is that we have hundreds of wires with hundreds of different wire tags. Our company standards don't have wire standards that allow this and we'd like to remove ANY TEXT that contains parentheses AND the characters inside the parentheses.

I've got some of the job done, but this is beginning to be more tricky. Lee-Mac has a wonderful LISP routine that does a lot of what I need, but a routine that will strip the format I just showed is still lacking.

example:

TBI-1 (1322) 43SL:21
TBI-2 (1049) 52CSR:TB3
TBI-2 (654) 43MA:20

After processing it should look like,
TBI-1 43SL:21
TBI-2 52CSR:TB3
TBI-2 43MA:20
Like I said, I've done what I could do with some routines I've collected on the various forums, but this last part is the most daunting.

Thanks for any help,

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: text find and replace
« Reply #1 on: February 25, 2020, 06:43:30 PM »
Here's a quick one -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:fixit ( / e i s v x )
  2.     (if
  3.         (setq s
  4.             (ssget "_:L"
  5.                '(
  6.                     (-04 . "<OR")
  7.                         (-04 . "<AND")
  8.                             (000 . "TEXT,MTEXT")
  9.                             (001 . "*(*)*")
  10.                         (-04 . "AND>")
  11.                         (-04 . "<AND")
  12.                             (000 . "INSERT")
  13.                             (066 . 1)
  14.                         (-04 . "AND>")
  15.                     (-04 . "OR>")
  16.                 )
  17.             )
  18.         )
  19.         (repeat (setq i (sslength s))
  20.             (setq i (1- i)
  21.                   e (ssname s i)
  22.                   x (entget e)
  23.             )
  24.             (if (wcmatch (cdr (assoc 0 x)) "*TEXT")
  25.                 (progn
  26.                     (setq v (assoc 1 x))
  27.                     (entmod (subst (cons 1 (removeit (cdr v))) v x))
  28.                 )
  29.                 (progn
  30.                     (setq e (entnext e)
  31.                           x (entget  e)
  32.                     )
  33.                     (while (= "ATTRIB" (cdr (assoc 0 x)))
  34.                         (if (wcmatch (cdr (setq v (assoc 1 x))) "*(*)*")
  35.                             (entmod (subst (cons 1 (removeit (cdr v))) v x))
  36.                         )
  37.                         (setq e (entnext e)
  38.                               x (entget  e)
  39.                         )
  40.                     )
  41.                 )
  42.             )
  43.         )
  44.     )
  45.     (princ)
  46. )
  47. (defun removeit ( s )
  48.     (if (wcmatch s "*(*)*")
  49.         (strcat
  50.             (vl-string-right-trim " "(substr s 1 (vl-string-position 40 s)))
  51.             (removeit (substr s (+ 2 (vl-string-position 41 s))))
  52.         )
  53.         s
  54.     )
  55. )

gisdude

  • Newt
  • Posts: 23
Re: text find and replace
« Reply #2 on: February 25, 2020, 08:05:57 PM »
Lee,

thx for the help. I'm home now. I'll give it a shot in the morning.


thx again

ronjonp

  • Needs a day job
  • Posts: 7531
Re: text find and replace
« Reply #3 on: February 26, 2020, 09:31:47 AM »
Here's a quick one -
Code - Auto/Visual Lisp: [Select]
  1. ...
  2. (defun removeit ( s )
  3.     (if (wcmatch s "*(*)*")
  4.         (strcat
  5.             (vl-string-right-trim " "(substr s 1 (vl-string-position 40 s)))
  6.             (removeit (substr s (+ 2 (vl-string-position 41 s))))
  7.         )
  8.         s
  9.     )
  10. )
Nice Lee  8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gisdude

  • Newt
  • Posts: 23
Re: text find and replace
« Reply #4 on: February 26, 2020, 11:15:36 AM »
Lee,
I tried your code on a copy of my original file. It looks good, but it doesn't work on MTEXT. So, on a hunch, I exploded the MTEXT and ran the code on just all the single line text - voila! It works.

I was wondering if the line:
Code: [Select]
(000 . "TEXT,MTEXT")could be the issue? I'm a LISP dilletante, so, bear with me.

I attached a copy of the file.


Thanks again.
« Last Edit: February 26, 2020, 11:32:20 AM by gisdude »

ronjonp

  • Needs a day job
  • Posts: 7531
Re: text find and replace
« Reply #5 on: February 26, 2020, 12:18:01 PM »
The problem is long mtext strings get stored in code 3 and the code is changing code 1.

Replace this line:
Code - Auto/Visual Lisp: [Select]
  1. (progn (setq v (assoc 1 x)) (entmod (subst (cons 1 (removeit (cdr v))) v x)))
With this:
Code - Auto/Visual Lisp: [Select]
  1.   (vlax-ename->vla-object e)
  2.   (removeit (vla-get-textstring (vlax-ename->vla-object e)))
  3. )

You will also need to modify the selection filter to not include the text pattern:
Code - Auto/Visual Lisp: [Select]
  1. (setq s (ssget "_:L"
  2.                '((-04 . "<OR")
  3.                  (000 . "TEXT,MTEXT")
  4.                  (-04 . "<AND")
  5.                  (000 . "INSERT")
  6.                  (066 . 1)
  7.                  (-04 . "AND>")
  8.                  (-04 . "OR>")
  9.                 )
  10.         )
  11. )
« Last Edit: February 26, 2020, 12:24:45 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gisdude

  • Newt
  • Posts: 23
Re: text find and replace
« Reply #6 on: February 26, 2020, 01:34:57 PM »
Awwwww snap!!

Many thanks ronjonp! 8-)

That did the trick!

I owe you a beverage!

ronjonp

  • Needs a day job
  • Posts: 7531
Re: text find and replace
« Reply #7 on: February 26, 2020, 01:36:09 PM »
Awwwww snap!!

Many thanks ronjonp! 8-)

That did the trick!

I owe you a beverage!
Glad to help.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: text find and replace
« Reply #8 on: February 26, 2020, 02:43:50 PM »

gisdude

  • Newt
  • Posts: 23
Re: text find and replace
« Reply #9 on: February 26, 2020, 03:42:58 PM »
@Lee,
thx to you to bro!

ur07

  • Mosquito
  • Posts: 1
Re: text find and replace
« Reply #10 on: May 13, 2023, 04:57:58 PM »
how can i modify this code to change
g108 (12/35) (1 xyzn)
g2308 (12/35) (13 xyzn)
g208 (12/35) (33 xyzn)
to

g108 (12/35)
g2308 (12/35)
g208 (12/35)


thanks a lot...