Author Topic: Macro Replace Text with Field  (Read 2484 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Macro Replace Text with Field
« on: June 01, 2015, 08:38:01 AM »
Has anyone seen a way to select an MTEXT and then click a button or command that will insert the SheetSet Title to replace the Mtext value?
Thanks
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Macro Replace Text with Field
« Reply #1 on: June 01, 2015, 08:56:29 AM »
I believe all you have to do is replace your text with: %<\AcSm Sheet.Title>% 
All of the Field Expressions are readily available at the bottom of the Field dialog box.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ChrisCarlson

  • Guest
Re: Macro Replace Text with Field
« Reply #2 on: June 01, 2015, 09:08:00 AM »
I don't think macro's can change the value of a mtext entity.

Code: [Select]
(defun c:TextChange (/ a b)
  (vl-load-com)
  (while T
      (setq a
(car
             (nentsel "\nSelect text or mtext entity to change")))
(if (or a
(setq b(entget a))
(= (cdr (assoc 0 b)) "TEXT")
(= (cdr (assoc 2 b)) "MTEXT")
) ; end check
        (vla-put-TextString(vlax-ename->vla-object a) "%<\AcSm Sheet.Title>% ")
        (princ "\n Invalid Selection")
        ); end if
) ; end while
   (princ)
 ); end
 

Quick and dirty.

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Macro Replace Text with Field
« Reply #3 on: June 01, 2015, 09:56:03 AM »
alternately replace that text with an attribute in the title block, block....then let that attribute be a field....
attsync...and save/resave all sheets should accomplish the desired result
Be your Best


Michael Farrell
http://primeservicesglobal.com/

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro Replace Text with Field
« Reply #4 on: June 01, 2015, 10:28:58 AM »
I don't think macro's can change the value of a mtext entity.

Code: [Select]
(defun c:TextChange (/ a b)
  (vl-load-com)
  (while T
      (setq a
(car
             (nentsel "\nSelect text or mtext entity to change")))
(if (or a
(setq b(entget a))
(= (cdr (assoc 0 b)) "TEXT")
(= (cdr (assoc 2 b)) "MTEXT")
) ; end check
        (vla-put-TextString(vlax-ename->vla-object a) "%<\AcSm Sheet.Title>% ")
        (princ "\n Invalid Selection")
        ); end if
) ; end while
   (princ)
 ); end
 

Quick and dirty.

Well I did try the above and I get the famous (####). When I double click on the field it shows as (UNKNOWN) at the top left of the dialogue box Field Category. Other than that, its great.
Civil3D 2020

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Macro Replace Text with Field
« Reply #5 on: June 01, 2015, 10:58:22 AM »
is this sheet part of a set?

if not then one should expect ####
Be your Best


Michael Farrell
http://primeservicesglobal.com/

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro Replace Text with Field
« Reply #6 on: June 01, 2015, 11:01:59 AM »
It is part of a sheet set. I just tried the command, saved the file, closed it and then thru the sheetset manager I re-opened the sheet. There still is the famous (####). Version of CAD shouldn't matter right?
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Macro Replace Text with Field
« Reply #7 on: June 01, 2015, 11:15:04 AM »
Just a quick look but the entity check will never be evaluated in the code above using OR:
Code - Auto/Visual Lisp: [Select]
  1. (or a (setq b (entget a)) (= (cdr (assoc 0 b)) "TEXT") (= (cdr (assoc 2 b)) "MTEXT")) ; end check
Instead I'd use:
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch (cdr (assoc 0 (entget a))) "*TEXT")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro Replace Text with Field
« Reply #8 on: June 01, 2015, 11:21:38 AM »
Thanks for checking that out. I think I found the issue. In the lisp above it calls for the full "%<\AcSm Sheet.Title>%". But when I go to edit the field it shows the field expression as "%<AcSm Sheet.Title>%" without the (\) in it.
Civil3D 2020

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Macro Replace Text with Field
« Reply #9 on: June 01, 2015, 11:23:11 AM »
Use: "%<\\AcSm Sheet.Title>%"

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro Replace Text with Field
« Reply #10 on: June 01, 2015, 11:27:22 AM »
Guess what. Solved! works awesome.
Civil3D 2020

ChrisCarlson

  • Guest
Re: Macro Replace Text with Field
« Reply #11 on: June 01, 2015, 01:50:44 PM »
Derp, was too early in the morning  :embarrassed2:

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Macro Replace Text with Field
« Reply #12 on: June 01, 2015, 01:52:45 PM »
lol. Thanks though
Civil3D 2020