Author Topic: Override MText Colors  (Read 3261 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!
Override MText Colors
« on: July 17, 2015, 11:17:09 AM »
If I have an MText with most of the words / sentences on ByLayer and then I have some Titles that are Green, and misc. text that is magenta. Can there be a routine that can change just the Magenta Text to red or something similar?
Civil3D 2020

ChrisCarlson

  • Guest
Re: Override MText Colors
« Reply #1 on: July 17, 2015, 11:27:58 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:StripIt ( / entity )
  2. (defun LM:UnFormat ( str mtx / _replace rx )
  3.  
  4. ;;-------------------=={ UnFormat String }==------------------;;
  5. ;;                                                            ;;
  6. ;;  Returns a string with all MText formatting codes removed. ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  9. ;;------------------------------------------------------------;;
  10. ;;  Arguments:                                                ;;
  11. ;;  str - String to Process                                   ;;
  12. ;;  mtx - MText Flag (T if string is for use in MText)        ;;
  13. ;;------------------------------------------------------------;;
  14. ;;  Returns:  String with formatting codes removed            ;;
  15. ;;------------------------------------------------------------;;
  16.     (defun _replace ( new old str )
  17.         (vlax-put-property rx 'pattern old)
  18.         (vlax-invoke rx 'replace str new)
  19.     )
  20.     (if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
  21.         (progn
  22.             (setq str
  23.                 (vl-catch-all-apply
  24.                     (function
  25.                         (lambda ( )
  26.                             (vlax-put-property rx 'global     actrue)
  27.                             (vlax-put-property rx 'multiline  actrue)
  28.                             (vlax-put-property rx 'ignorecase acfalse)
  29.                             (foreach pair
  30.                                '(
  31.                                     ("\032"    . "\\\\\\\\")
  32.                                     (" "       . "\\\\P|\\n|\\t")
  33.                                     ("$1"      . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
  34.                                     ("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
  35.                                     ("$1$2"    . "\\\\(\\\\S)|[\\\\](})|}")
  36.                                     ("$1"      . "[\\\\]({)|{")
  37.                                 )
  38.                                 (setq str (_replace (car pair) (cdr pair) str))
  39.                             )
  40.                             (if mtx
  41.                                 (_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
  42.                                 (_replace "\\"   "\032" str)
  43.                             )
  44.                         )
  45.                     )
  46.                 )
  47.             )
  48.             (vlax-release-object rx)
  49.             (if (null (vl-catch-all-error-p str))
  50.                 (setq newstr str)
  51. ;;;;Changed str to (setq newstr str)
  52.             )
  53.         )
  54.     )
  55. )
  56.  
  57.  
  58.  
  59. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))
  60. ;;---------------------=={ Select if }==----------------------;;
  61. ;;                                                            ;;
  62. ;;  Provides continuous selection prompts until either a      ;;
  63. ;;  predicate function is validated or a keyword is supplied. ;;
  64. ;;------------------------------------------------------------;;
  65. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  66. ;;------------------------------------------------------------;;
  67. ;;  Arguments:                                                ;;
  68. ;;  msg  - prompt string                                      ;;
  69. ;;  pred - optional predicate function [selection list arg]   ;;
  70. ;;  func - selection function to invoke                       ;;
  71. ;;  keyw - optional initget argument list                     ;;
  72. ;;------------------------------------------------------------;;
  73. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  74. ;;------------------------------------------------------------;;  
  75.   (while
  76.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  77.       (cond
  78.         ( (= 7 (getvar 'ERRNO))
  79.           (princ "\nMissed, Try again.")
  80.         )
  81.         ( (eq 'STR (type sel))
  82.           nil
  83.         )
  84.         ( (vl-consp sel)
  85.           (if (and pred (not (pred sel)))
  86.             (princ "\nInvalid Object Selected.")
  87.           )
  88.         )
  89.       )
  90.     )
  91.   )
  92.   sel
  93. )
  94.  
  95.  
  96.   (if
  97.     (setq entity
  98.       (car
  99.         (LM:SelectIf "\nSelect a MText: "
  100.           (lambda ( x ) (eq "MTEXT" (cdr (assoc 0 (entget (car x)))))) entsel nil
  101.         )
  102.       )
  103.     )
  104.        (progn
  105.                                 (progn
  106.                                         ;;;;;Strip Formatting
  107.                                         (setq oldtext (vla-get-textstring o))
  108.                                         (LM:UnFormat oldtext nil)
  109.                                         (vla-put-TextString o newstr)
  110.                                 )      
  111.                         )
  112.             (vla-delete s)
  113.         )
  114.                 )
  115.                 (princ)
  116.         )
  117.  

Try this, it will strip the formatting of a mtext. It's a quick hobble and could easily be reduced or simplified.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Override MText Colors
« Reply #2 on: July 17, 2015, 11:45:33 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:nameit (/ e o txt)
  2.   (if (and (setq e (car (entsel "\nPick mtext: ")))
  3.            (= "MTEXT" (cdr (assoc 0 (entget e))))
  4.            (setq o (vlax-ename->vla-object e))
  5.       )
  6.     (progn
  7.       (setq txt (vla-get-textstring o))
  8.       (while (vl-string-search "\\C6;" txt) (setq txt (vl-string-translate "\\C6;" "\\C1;"  txt)))
  9.       (vla-put-textstring o txt)
  10.     )
  11.   )
  12.   (princ)
  13. )

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: Override MText Colors
« Reply #3 on: July 17, 2015, 12:07:13 PM »
Awesome! Thank you!
Civil3D 2020

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Override MText Colors
« Reply #4 on: July 17, 2015, 12:07:39 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:nameit (/ e o txt)
  2.   (if (and (setq e (car (entsel "\nPick mtext: ")))
  3.            (= "MTEXT" (cdr (assoc 0 (entget e))))
  4.            (setq o (vlax-ename->vla-object e))
  5.       )
  6.     (progn
  7.       (setq txt (vla-get-textstring o))
  8.       (while (vl-string-search "\\C6;" txt) (setq txt (vl-string-translate "\\C6;" "\\C1;"  txt)))
  9.       (vla-put-textstring o txt)
  10.     )
  11.   )
  12.   (princ)
  13. )

 :-(
Code - Auto/Visual Lisp: [Select]
  1. (vl-string-translate "\\C6;" "\\C1;"  "66")
  2. ==>> "11"

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Override MText Colors
« Reply #5 on: July 17, 2015, 12:17:08 PM »
Maybe this is better?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:nameit (/ e o txt)
  2.   (if (and (setq e (car (entsel "\nPick mtext: ")))
  3.            (= "MTEXT" (cdr (assoc 0 (entget e))))
  4.            (setq o (vlax-ename->vla-object e))
  5.       )
  6.     (progn (setq txt (vla-get-textstring o))
  7.            (while (vl-string-search "\\C6;" txt) (setq txt (vl-string-subst "\\C1;" "\\C6;" txt)))
  8.            (vla-put-textstring o txt)
  9.     )
  10.   )
  11.   (princ)
  12. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC