Author Topic: Need a quick lisp to change imperial to metric (text entities)  (Read 5415 times)

0 Members and 1 Guest are viewing this topic.

econnerly

  • Newt
  • Posts: 42
Need a quick lisp to change imperial to metric (text entities)
« on: November 10, 2011, 12:18:18 PM »
We need to convert a bunch of text pipe sizes from imperial to metric. I was all stoked because I thought I had a good script that would work. It would use the Mtext.dvb and issue a bunch of "find and replace" commands. This is what it would replace:
1/4" to 8%%C
1/2" to 15%%C
3/4" to 20%%C
1" to 25%%C
11/4" to 32%%C
11/2" to 40%%C
2" to 50%%C
21/2" to 65%%C
3" to 80%%C
4" to 100%%C
6" to 150%%C
8" to 200%%C

I had to play with the order of the replace so it would not find entities already changed and change them again further down into the script. But now Im finding that my script is also changing such things as +14" to +1100%%C and +18" to +1200%%C (obviously %%c for the circumference symbol)

Can anyone help me out with this? I know of a few geniuses on here who could whip something up in a matter of seconds. We have about 300 drawings that we need to change. Any type of programming would work as long as it works on mtext and dtext.

Attached is the script that I was using.

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #1 on: November 10, 2011, 12:19:24 PM »
Here is the mtext.dvb

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #2 on: November 10, 2011, 01:31:44 PM »
Here's a quickie ... definitely test it out first!  8-)

Code: [Select]
(defun c:foo (/ _replace ss str)
  (vl-load-com)
  (defun _replace (txt old new)
    (while (vl-string-search old txt) (setq txt (vl-string-subst new old txt)))
    txt
  )
  (if (and (setq ss (ssget ":L" '((0 . "TEXT,MTEXT")))))
    (foreach x (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
      (setq str (vla-get-textstring x))
      (foreach i (vl-sort '(("1/4\"" . "8%%C")
    ("1/2\"" . "15%%C")
    ("3/4\"" . "20%%C")
    ("1\"" . "25%%C")
    ("11/4\"" . "32%%C")
    ("11/2\"" . "40%%C")
    ("2\"" . "50%%C")
    ("21/2\"" . "65%%C")
    ("3\"" . "80%%C")
    ("4\"" . "100%%C")
    ("6\"" . "150%%C")
    ("8\"" . "200%%C")
   )
  '(lambda (x y) (> (strlen (car x)) (strlen (car y))))
)
(setq str (_replace str (car i) (cdr i)))
      )
      (vl-catch-all-apply 'vla-put-textstring (list x str))
    )
  )
  (princ)
)
« Last Edit: November 10, 2011, 01:51:57 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #3 on: November 10, 2011, 01:34:52 PM »
Here is the mtext.dvb
It's password protected.  :\
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #4 on: November 10, 2011, 01:53:26 PM »
ronjonp!
You are the man. Works way faster than that script, but still does change things that I dont want it to. I guess I have the backbone to what I need and can just expand on it myself. Thank you very much. I knew it was either you or LeeMac that was going to get to it!

But if you want to see what I am talking about, run it on this sample dwg.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #5 on: November 10, 2011, 03:06:10 PM »
OK ... try this one:

Test it thoroughly  :-)

Attached is a quick color code to see what changed in the mtext.

*Just double checked and it's still a little screwy*

Code: [Select]
(defun c:foo2 (/ _replace ss str)
  (vl-load-com)
  (defun _replace (txt old new / i p tmp)
    (setq i 0)
    (while (and (setq tmp (substr txt (setq i (1+ i))))
(<= i (strlen txt))
(setq p (vl-string-search old tmp))
   )
      ;;Checks for number or plus character before string search index
      (if (and (not (zerop p)) (not (wcmatch (substr txt (+ p (1- i)) 1) "#,+")))
(setq txt (vl-string-subst new old txt))
      )
    )
    txt
  )
  (if (and (setq ss (ssget ":L" '((0 . "TEXT,MTEXT")))))
    (foreach x (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
      (setq str (vla-get-textstring x))
      (foreach i (vl-sort '(("1/4\"" . "8%%C")
    ("1/2\"" . "15%%C")
    ("3/4\"" . "20%%C")
    ("1\"" . "25%%C")
    ("11/4\"" . "32%%C")
    ("11/2\"" . "40%%C")
    ("2\"" . "50%%C")
    ("21/2\"" . "65%%C")
    ("3\"" . "80%%C")
    ("4\"" . "100%%C")
    ("6\"" . "150%%C")
    ("8\"" . "200%%C")
   )
  '(lambda (x y) (> (strlen (car x)) (strlen (car y))))
)
(setq str (_replace str (car i) (cdr i)))
      )
      (vl-catch-all-apply 'vla-put-textstring (list x str))
    )
  )
  (princ)
)
« Last Edit: November 10, 2011, 03:32:26 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #6 on: November 10, 2011, 03:36:01 PM »
The second one works well on the mtext, but does not work well with regular text. Run it on the model space text and you will see. Not a big deal. I can use them both. One for regular text and the foo2 for Mtext.
Thanks man! Super big help.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #7 on: November 10, 2011, 03:44:19 PM »
You've looked at this haven't you?

http://www.theswamp.org/index.php?topic=32777.0

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #8 on: November 10, 2011, 03:54:30 PM »
Nice! I'll give it a shot and see how that work out. Post back in a few.

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #9 on: November 10, 2011, 04:06:34 PM »
You've looked at this haven't you?

http://www.theswamp.org/index.php?topic=32777.0

This still modifies unnecessary entities. Your two routines work well in conjunction with one another.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #10 on: November 10, 2011, 04:25:31 PM »
I wouldn't use it quite yet ... I'm still getting mixed results.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #11 on: November 10, 2011, 04:47:19 PM »
Sounds good... and again, thanks a million for the effort.

econnerly

  • Newt
  • Posts: 42
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #12 on: November 11, 2011, 06:01:27 PM »
Quick question. Im modifying your routine a bit more. What would be the string to change 90%%D to 32%%D?
("90%%d" . "32%%d") didn't work.  :|

econnerly

  • Newt
  • Posts: 42
Change 90%%d to 32%%d... ASAP!
« Reply #13 on: November 11, 2011, 06:33:27 PM »
I have a lisp routine to change a bunch of imperial pipe sizes to metric (thanks ronjonp!) I need to add a temperature change into the lisp routine.
How do I change 90%%D to 32%%D? Here is a portion of the routine:

("4\"" . "100%%C")
("6\"" . "150%%C")
("8\"" . "200%%C")
("90%%d" . "32%%d")

I tried this but it won't change it.

Lee Mac

  • Seagull
  • Posts: 12925
  • London, England
Re: Change 90%%d to 32%%d... ASAP!
« Reply #14 on: November 11, 2011, 06:59:03 PM »
Double posting will not get your question answered any quicker, it merely irritates forum members, adds confusion (since there are now two threads for the same topic) and makes more work for the moderators.

Have a read of this informative article  :-)

@Moderator: suggest merge with this thread:
http://www.theswamp.org/index.php?topic=40013.0


econnerly

  • Newt
  • Posts: 42
Re: Change 90%%d to 32%%d... ASAP!
« Reply #15 on: November 11, 2011, 07:05:45 PM »
My deepest apologies. I will read it when I have some free time. I have a deadline that I'm trying to meet.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Need a quick lisp to change imperial to metric (text entities)
« Reply #16 on: November 11, 2011, 07:09:25 PM »

Topics Merged as requested.

Carry on :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.