LearningLisP Posted December 9, 2010 Author Posted December 9, 2010 to those who want the code, here it is.. i incorporate the revision irneb provide. what i change is the Font style and the command.: (if (not (or StripMText (load "StripMtext v5-0b" nil))) (princ "\nStripMText couldn't be loaded.") ) (defun c:mtx ( / *error* ss ) (vl-load-com) ;; © Lee Mac 2010, www.lee-mac.com (defun *error* ( msg ) (LM:ReleaseObject RegEx) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (if (setq ss (ssget "_:L" '((0 . "MTEXT") (1 . "*\\P*")))) (progn (if StripMText (StripMtext ss '("F"))) ( (lambda ( i / RegEx e ) (setq RegEx (vlax-create-object "VBScript.RegExp")) (while (setq e (ssname ss (setq i (1+ i)))) (entupd (cdr (assoc -1 (entmod (subst (cons 1 (LM:RegExReplace RegEx "{\\fCalibri|b1|i0|c0|p34;$1\\P\\fCalibri|b0|i1|c0|p34;$2}" "^(.*)\\\\P(.*)$" (cdr (assoc 1 (entget e))) ) ) (assoc 1 (entget e)) (entget e) ) ) ) ) ) ) (LM:ReleaseObject RegEx) ) -1 ) ) ) (princ) ) (defun LM:RegExReplace ( regex new pat str ) ;; © Lee Mac 2010 (mapcar (function (lambda ( prop value ) (vlax-put-property regex prop value)) ) '(pattern global ignorecase) (list pat actrue acfalse) ) (vlax-invoke regex 'replace str new) ) ;;------------------=={ Release Object }==--------------------;; ;; ;; ;; Releases a VLA Object from memory via plentiful error ;; ;; trapping ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2010 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; obj - VLA Object to be released from memory ;; ;;------------------------------------------------------------;; ;; Returns: T if Object Released, else nil ;; ;;------------------------------------------------------------;; (defun LM:ReleaseObject ( obj ) (vl-load-com) ;; © Lee Mac 2010 (and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj)) (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-release-object) (list obj) ) ) ) ) ) All credits to Lee Mac, irneb, Steve Doman and Joe Burke for the wonderful work they have done. thanks guys! Quote
irneb Posted December 9, 2010 Posted December 9, 2010 You're welcome! Actually this was something that got me very interested. It seems to indicate an error in ACad (IMO at least). If you select and change any format inside the MText editor (e.g. make selection Bold) - then it saves the format string with a font name - even if you did not select any new font at all. But when you do select a font, ACad2008 pops up with a message that fonts can only be set for the MText as a whole. And then if I fiddle with this (seeing as the format code can start-and-stop wit { and }) ... that statement appear totally untrue! Quote
LearningLisP Posted December 9, 2010 Author Posted December 9, 2010 You're welcome! Actually this was something that got me very interested. It seems to indicate an error in ACad (IMO at least). If you select and change any format inside the MText editor (e.g. make selection Bold) - then it saves the format string with a font name - even if you did not select any new font at all. But when you do select a font, ACad2008 pops up with a message that fonts can only be set for the MText as a whole. And then if I fiddle with this (seeing as the format code can start-and-stop wit { and }) ... that statement appear totally untrue! Yeah it does appear. but i check the checkbox "Do not show this again". can this be disabled? Quote
irneb Posted December 9, 2010 Posted December 9, 2010 Yeah it does appear. but i check the checkbox "Do not show this again". can this be disabled?Yep, the message box can be disabled. But that does not stop it from applying the font to the entire MText. Here's an example: MTEXT ... Style = "AR-ARIAL"... Contents: LEADER ARROW \P& ANNOTATION Now I edit the MTEXT, select the word ARROW and click the bold: MTEXT... Style = "AR-ARIAL"... Contents: LEADER [color=dimgray][i]{[u][b]\fArial[/b][/u]|b1|i0|c0|p34;[/i][/color]ARROW[i][color=dimgray]}[/color][/i] \P& ANNOTATION But when I then edit it and change the style: MTEXT... Style = "[b][i][u][color=dimgray]CentGoth[/color][/u][/i][/b]"... Contents: LEADER ARROW \P& ANNOTATION You see ... the bold portion disappears. If I again edit it to replace the bold: MTEXT ... Style = "CentGoth"... Contents: LEADER [i][color=dimgray]{\[b][u]fCentury Gothic[/u][/b]|b1|i0|c0|p34;[/color][/i]ARROW[i][color=dimgray]}[/color][/i] \P& ANNOTATION Which is why I'm saying this is an error. Changing the style in the editor removes all the formatting, and conversely adding any formatting whatsoever forces a font onto the selected. It forces the current font though, but if I now select the MText and change the style in the properties palette: MTEXT ... Style = "[u][b][i][color=dimgray]AR-ARIAL[/color][/i][/b][/u]"... Contents: LEADER [i][color=dimgray]{[b][u]\fCentury Gothic[/u][/b]|b1|i0|c0|p34;[/color][/i]ARROW[color=dimgray][i]}[/i][/color] \P& ANNOTATION I would have thought that changing the Style in the editor would work similar to what happens when you use the palette (i.e. leave your formatting as is). But the "best" solution would be a combination of the 2: the style needs to change, but also the fonts inside any formatting. Or better yet, it needs to remove the font from the formatting code, so you have something like this instead: LEADER [i][color=dimgray]{\b1;[/color][/i]ARROW[i][color=dimgray]}[/color][/i] \P& ANNOTATION It would make the string shorter (thus saving on file size and ram use), but also allows for much easier modifications - since only one setting needs to change. But setting the text through coding that way makes for some weird stuff happening. Quote
Lee Mac Posted December 9, 2010 Posted December 9, 2010 All credits to Lee Mac, irneb, Steve Doman and Joe Burke for the wonderful work they have done. thanks guys! Just checked back - glad you got it working in the end, thanks Irne for providing the finishing touches Or better yet, it needs to remove the font from the formatting code, so you have something like this instead:LEADER [i][color=dimgray]{\b1;[/color][/i]ARROW[i][color=dimgray]}[/color][/i] \P& ANNOTATION It would make the string shorter (thus saving on file size and ram use), but also allows for much easier modifications - since only one setting needs to change. This is what I needed earlier - I was puzzled as to how to make the text bold, but without having to specify the font name (which I didn't know how to obtain - see earlier remarks). Quote
irneb Posted December 10, 2010 Posted December 10, 2010 Yep, I wish it would work that way. But ADDesk (in their infinite wisdom) decided to use the so-called "style-based" approach to text formatting codes. Similar to OOo.org's Write application, which has huge insurmountable problems because of this. As a "work-around" the "font" should usually be the font applied to the style as well. So here goes a test: Command: (setq en (entsel)) Select object: (<Entity name: -405f3960> (-122004.0 -30970.7 0.0)) Command: (setq ed (entget (car en))) ((-1 . <Entity name: -405f3960>) (0 . "MTEXT") (5 . "4F4C4") (102 . "{ACAD_XDICTIONARY") (360 . <Entity name: -405f3958>) (102 . "}") (330 . <Entity name: -4011a338>) (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "AR-TEXT-GENERAL") (100 . "AcDbMText") (10 -124046.0 -30136.2 0.0) (40 . 200.0) (41 . 8673.54) (46 . 240.784) (71 . 1) (72 . 5) (1 . "{\\C2;THIS IS A DIFFERENT COLOUR:\\P\\f[color=red]Century Gothic[/color]|b1|i0|c0|p34;\\C256;THIS ID BOLD} THIS IS NORMAL.\\P\\pxi2;THIS PARAGRAPH HAS AN INDENT\\P\\pi0;NORMAL AGAIN") [color=red](7 . "CentGoth")[/color] (210 0.0 0.0 1.0) (11 1.0 0.0 0.0) (42 . 4425.29) (43 . 1225.15) (50 . 0.0) (73 . 1) (44 . 1.0)) Command: (setq sn (tblobjname "style" (cdr (assoc 7 ed)))) <Entity name: -405f9a20> Command: (setq sd (entget sn '("*"))) ((-1 . <Entity name: -405f9a20>) (0 . "STYLE") (330 . <Entity name: -4011a3e8>) (5 . "4CB4C") (100 . "AcDbSymbolTableRecord") (100 . "AcDbTextStyleTableRecord") (2 . "CentGoth") (70 . 0) (40 . 0.0) (41 . 1.0) (50 . 0.0) (71 . 0) (42 . 2.1575) (3 . "GOTHIC.TTF") (4 . "") (-3 ("ACAD" (1000 . "[color=red]Century Gothic[/color]") (1071 . 34)))) So the font name is saved as a coode 1000 XData under the ACAD Registered App for the style entity. I don't know what the code (1071 . 34) means ... but does it matter for this case? And if you have a SHX based font in the style, the MText is disallowed from using bold in any case. In which case you have to set another TTF font name in the format code, maybe could simply be one of the "standard" windows fonts. Quote
Lee Mac Posted December 10, 2010 Posted December 10, 2010 So the font name is saved as a coode 1000 XData under the ACAD Registered App for the style entity. I don't know what the code (1071 . 34) means ... but does it matter for this case? Oh - I didn't think to look into the XData of the TextStyle - good call - I'll look to update the original code Quote
Lee Mac Posted December 10, 2010 Posted December 10, 2010 Maybe something like this: (if (not (or StripMText (load "StripMtext v5-0b" nil))) (princ "\nStripMText couldn't be loaded.") ) (defun c:mtx ( / *error* ss ) (vl-load-com) ;; © Lee Mac 2010, www.lee-mac.com (defun *error* ( msg ) (LM:ReleaseObject RegEx) (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **"))) (princ) ) (if (setq ss (ssget "_:L" '((0 . "MTEXT") (1 . "*\\P*")))) (progn (if StripMText (StripMtext ss '("F"))) ( (lambda ( i / RegEx e rep xd ) (setq RegEx (vlax-create-object "VBScript.RegExp")) (mapcar '(lambda ( x ) (vlax-put-property RegEx (car x) (cdr x))) (list (cons 'pattern "^(.*)\\\\P(.*)$") (cons 'global actrue) (cons 'ignorecase actrue) (cons 'multiline actrue) ) ) (while (setq e (ssname ss (setq i (1+ i)))) ( (lambda ( font ) (setq rep (strcat "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;$2}")) ) (if (setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (cdr (assoc 7 (entget e)))) '("ACAD"))))) (cdr (assoc 1000 xd)) "Arial" ) ) (entupd (cdr (assoc -1 (entmod (subst (cons 1 (vlax-invoke RegEx 'replace (cdr (assoc 1 (entget e))) rep) ) (assoc 1 (entget e)) (entget e) ) ) ) ) ) ) (LM:ReleaseObject RegEx) ) -1 ) ) ) (princ) ) ;;------------------=={ Release Object }==--------------------;; ;; ;; ;; Releases a VLA Object from memory via plentiful error ;; ;; trapping ;; ;;------------------------------------------------------------;; ;; Author: Lee Mac, Copyright © 2010 - www.lee-mac.com ;; ;;------------------------------------------------------------;; ;; Arguments: ;; ;; obj - VLA Object to be released from memory ;; ;;------------------------------------------------------------;; ;; Returns: T if Object Released, else nil ;; ;;------------------------------------------------------------;; (defun LM:ReleaseObject ( obj ) (vl-load-com) ;; © Lee Mac 2010 (and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj)) (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-release-object) (list obj) ) ) ) ) ) Quote
LearningLisP Posted December 13, 2010 Author Posted December 13, 2010 hi guys! i see you all wanted to make this code a better one Lee. . i will try this one.. btw, i tried to make revisions myself to the code but it seems i am lost. what i want to do is to delete the second line. can you help out here? thanks in advance.!! Quote
Lee Mac Posted December 13, 2010 Posted December 13, 2010 what i want to do is to delete the second line. can you help out here? Change: (cons 'pattern "^(.*)\\\\P(.*)$") to (cons 'pattern "^(.*)\\\\P.*$") And (setq rep (strcat "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;$2}")) to (setq rep (strcat "{\\f" font "|b1|i0|c0|p34;$1}")) Quote
LearningLisP Posted December 13, 2010 Author Posted December 13, 2010 AWESOME Lee!! . thank you very much! i hope that you will not stop from helping others.. God bless you more! btw, i figure out why my font did not change when i use your updated code.. you should replace the " font " to the name of the font including the quote symbol. . thank you once again Lee.. Quote
Lee Mac Posted December 13, 2010 Posted December 13, 2010 AWESOME Lee!! . thank you very much! i hope that you will not stop from helping others.. God bless you more! btw, i figure out why my font did not change when i use your updated code.. you should replace the " font " to the name of the font including the quote symbol. . thank you once again Lee.. There should be no changes needed with the font section - that was added to use the font used in your TextStyle, does this not happen? Quote
LearningLisP Posted December 14, 2010 Author Posted December 14, 2010 oh. after testing it Lee, it wont change to the style i chose in the text style. i tested it with the new mtext, i use standard style. after encoding, i make the Calibri font as the current style. i use the code and when i apply to it, it changes to areal, not the calibri font which is my current style. Quote
Lee Mac Posted December 14, 2010 Posted December 14, 2010 It uses the style that the MText object is set to - however, if that style uses a shape file font (.shx), it will use Arial instead. See the earlier posts by Irneb/Me on this topic Quote
LearningLisP Posted December 14, 2010 Author Posted December 14, 2010 still with no avail Lee. can't get it to work. and Calibri is a TTF font. well anyway, i will be using the previous version. . thanks a lot again Lee.. i will explore more on this new code.. and oh oh.. btw, I tried to add to something to the code. its the second line which i want to change the height. "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;[color="red"][b]\h0.75x;[/b][/color]$2}" but unfortunately, it did not work.. what is lacking here Lee? Quote
Lee Mac Posted December 14, 2010 Posted December 14, 2010 still with no avail Lee. can't get it to work. and Calibri is a TTF font. well anyway, i will be using the previous version. . thanks a lot again Lee.. i will explore more on this new code.. Did you create the MText in the style that uses the Calibri font? and oh oh.. btw, I tried to add to something to the code. its the second line which i want to change the height. "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;[color="red"][b]\h0.75x;[/b][/color]$2}" but unfortunately, it did not work.. what is lacking here Lee? "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;[color="red"][b]\\H0.75x;[/b][/color]$2}" Quote
irneb Posted December 14, 2010 Posted December 14, 2010 still with no avail Lee. can't get it to work. and Calibri is a TTF font. well anyway, i will be using the previous version. . thanks a lot again Lee.. i will explore more on this new code..Could you do a manual change to one of your MTexts, doing a list on before and after. Then copy the listings here? A format of {\fCalibri|b0|i0| works for me, so if the style is set to use the Calibri font this should work. If your style is set to using one of ACad's SHX fonts, then Lee's code defaults to Arial. If you want it different modify Lee's code where there's an "Arial" and change to "Calibri". Lee maybe have something like this: ;; Change this portion (if (setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (cdr (assoc 7 (entget e)))) '("ACAD"))))) (cdr (assoc 1000 xd)) "Arial" ) ;; To look like this (if [color=red](or [/color](setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (cdr (assoc 7 (entget e)))) '("ACAD"))))) [color=red](setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (getvar "TEXTSTYLE")) '("ACAD"))))))[/color] (cdr (assoc 1000 xd)) "[color=seagreen]Arial[/color]" ) That way the user can set the current style to use as default in case the MText is set to use a SHX font. BTW, the green would be where you need to change to Calibri as default in all other cases, though I'd prefer Arial myself. Quote
Lee Mac Posted December 14, 2010 Posted December 14, 2010 ;; To look like this (if [color=red](or [/color](setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (cdr (assoc 7 (entget e)))) '("ACAD"))))) [color=red](setq xd (cdadr (assoc -3 (entget (tblobjname "STYLE" (getvar "TEXTSTYLE")) '("ACAD"))))))[/color] (cdr (assoc 1000 xd)) "[color=seagreen]Arial[/color]" ) That way the user can set the current style to use as default in case the MText is set to use a SHX font. Excellent idea Quote
LearningLisP Posted December 15, 2010 Author Posted December 15, 2010 Did you create the MText in the style that uses the Calibri font? "{\\f" font "|b1|i0|c0|p34;$1\\P\\f" font "|b0|i1|c0|p34;[color="red"][b]\\H0.75x;[/b][/color]$2}" i'm applying it to an existing mtext Lee which has a style of Swiss Cn Bt. And thanks for this new code. . Quote
LearningLisP Posted December 15, 2010 Author Posted December 15, 2010 (edited) Could you do a manual change to one of your MTexts, doing a list on before and after. Then copy the listings here? Original Mtext: MTEXT Layer: "138kV" Space: Model space Color: 7 (white) Linetype: "BYLAYER" Handle = 1f96 Style = "Swis Cn BT" Annotative: No Location: X= 725.6133 Y= 90.9039 Z= 0.0000 Width: 13.9833 Normal: X= 0.0000 Y= 0.0000 Z= 1.0000 Rotation: 0 Text height: 2.0000 Line spacing: Multiple (1.000000x = 3.3333) Attachment: TopLeft Flow direction: ByStyle Contents: {\fSwis721 Cn BT|b1|i0|c0|p34;7-01CB24IDP\P\fSwis721 Cn BT|b0|i1|c0|p34;(71AL124)} New Mtext after applying Lee's new code: MTEXT Layer: "138kV" Space: Model space Color: 7 (white) Linetype: "BYLAYER" Handle = 1f96 Style = "Swis Cn BT" Annotative: No Location: X= 725.6133 Y= 90.9039 Z= 0.0000 Width: 13.9833 Normal: X= 0.0000 Y= 0.0000 Z= 1.0000 Rotation: 0 Text height: 2.0000 Line spacing: Multiple (1.000000x = 3.3333) Attachment: TopLeft Flow direction: ByStyle Contents: {\fSwis721 Cn BT|b1|i0|c0|p34;7-01CB24IDP\P\fSwis721 Cn BT|b0|i1|c0|p34;\H0.75x;(71AL124)} p.s. before i apply Lee's code, i made Calibri as the current Text style. but it did not change after i apply Lee's code. I will change the new code that you give Irn and see if it will work. thanks! edit: Irn, it did not change the style also after i updated the code you give. i test it to a new mtext using the Swiss Cn Bt style. Change the style to Calibri before applying. After applying, it change but it uses the Arial font not the Calibri font. Edited December 15, 2010 by LearningLisP Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.