Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lisp to Replace MText Style Override {\fCentury|b0.. with {\fArial Narrow|b0..

3 REPLIES 3
Reply
Message 1 of 4
goodjuju
2077 Views, 3 Replies

Lisp to Replace MText Style Override {\fCentury|b0.. with {\fArial Narrow|b0..

Hi,

 

I have searched the forums high and low for an answer to this but have only come up with very advanced solutions...

 

I have an existing Lisp Routine which changes the font of all text styles to Arial Narrow.  However, where the text style has been changed in the MText editor, it overrides the font defined in the style and retains the original font.

 

I know there are various solutions that people have listed before, StripMtext and a few postings by Lee Mack, but I don't think I need anything as in depth as this and to strip out what I need would take me a lifetime. I think I need a regular expression to find the Mtext contents between \f*| (i.e. \fCentury| or \fRomans| (and replace with \fArial Narrow|

 

Does anyone know a simple way to achieve this 'Find / Replace' operation in Lisp using a regexp?

 

An example of the existing text contents is:  

{\fCentury|b0|i0|c0|p34;2. ALL ELEVATIONS ARE IN METRES}

 

I would like it to be:

{\fArial Narrow|b0|i0|c0|p34;2. ALL ELEVATIONS ARE IN METRES}

 

Here is my existing lisp to create a new text style and replace the font for existing styles:

 

(vl-load-com)

(vlax-for x (vla-get-TextStyles
       (vla-get-activedocument (vlax-get-acad-object))
     )
  (princ)
  (vla-setfont x "Arial Narrow" :vlax-False :vlax-False 0 32)
)
(Command "-style" "ArialN" "ArialN.ttf" "0" "1" "0" "N" "N")
(defun c:ChangeTxtStyle (/ a ts n index b1 b c d b2)
 (setq a (ssget "_X" '((0 . "*TEXT,ATTDEF"))))
 (setq ts "ArialN")
 (setq n (sslength a))
 (setq index 0)
  (repeat n
   (setq b1 (entget (ssname a index)))
   (setq index (1+ index))
   (setq b (assoc 0 b1))
    (if (= "TEXT" (cdr b))
     (progn
      (setq c (assoc 7 b1))
      (setq d (cons (car c) ts))
      (setq b2 (subst d c b1))
      (entmod b2)
     )
    )
  )
)
ChangeTxtStyle

 

I would also like it to be completely unattended without having to select text etc., just apply to all MText objects globally.

 

Many thanks in advance for anyones help!

GoodJuJu

3 REPLIES 3
Message 2 of 4
Kent1Cooper
in reply to: goodjuju


@goodjuju wrote:

.... I need a regular expression to find the Mtext contents between \f*| (i.e. \fCentury| or \fRomans| (and replace with \fArial Narrow|

.... 

An example of the existing text contents is:  

{\fCentury|b0|i0|c0|p34;2. ALL ELEVATIONS ARE IN METRES}

 

I would like it to be:

{\fArial Narrow|b0|i0|c0|p34;2. ALL ELEVATIONS ARE IN METRES}

....


(vl-string-subst "fArial Narrow" "fCentury" YourTextString)

(vl-string-subst "fArial Narrow" "fRomans" YourTextString)

 

[or, if you don't expect any of your Mtext to contain "Century" or "Romans" other than as this kind of font assignment, you could even omit the f's].  Use those to revise the content, and impose the revised content on the Mtext entity by any of several methods.

Kent Cooper, AIA
Message 3 of 4
Ajilal.Vijayan
in reply to: goodjuju

Try with this code

 [Edit: Updated to add multi text styles]

(vl-load-com)
(defun ChangeMtextStyle (mtxt)
(setq ntxt "fArial Narrow" )
(setq oldtxts '("fCentury" "Fromans"));list of text styles to search
(setq mcont (cdr (assoc 1 mtxt)))

(foreach n oldtxts
(if (vl-string-search n mcont)
(progn
(setq nwtxt (vl-string-subst ntxt n mcont))
(setq mtxt (subst (cons 1 nwtxt) (assoc 1 mtxt) mtxt ))
(entmod mtxt)
(princ)
);progn
);if
);for

);defun


(defun c:ChangeTxtStyle (/ a ts n index b1 b c d b2)
(setq a (ssget "_X" '((0 . "*TEXT,ATTDEF"))))
(setq ts "ArialN")
(setq n (sslength a))
(setq index 0)
(repeat n
(setq b1 (entget (ssname a index)))
(setq index (1+ index))
(setq b (assoc 0 b1))
(if (= "TEXT" (cdr b))
(progn
(setq c (assoc 7 b1))
(setq d (cons (car c) ts))
(setq b2 (subst d c b1))
(entmod b2)
);progn
);if
(if ( = "MTEXT" (cdr b))
(ChangeMtextStyle b1)
);if
);repeat
);defun );defun

 

Message 4 of 4
Lineabove
in reply to: goodjuju

If I am out of line requesting this - please say so.

 

This routine is wonderful but why not add the provision (dialog box) to Select the Font you wish to change to and also Select the font(s) you wish to replace rather than hardcoding both into the lisp file.

 

That would be a killer routine.

 

 

Best regards,

 

Mel

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost