1. ;;多行文字样式清除
    2. (defun C:mtx(/ ss i ent elist )
    3. (if (setq ss(ssget '((0 . "TEXT,MTEXT"))))
    4. (progn
    5. (setq i 0)
    6. (repeat (sslength ss)
    7. (setq ent (ssname ss i))
    8. (setq elist (entget ent))
    9. (entmod
    10. (subst
    11. (cons 1
    12. (mtext2text (cdr(assoc 1 elist)))
    13. )
    14. (assoc 1 elist)
    15. elist
    16. )
    17. )
    18. (setq i (1+ i))
    19. )
    20. )
    21. )
    22. )
    23. ;提取多行文字,去除无用格式符号--来自明经
    24. (defun mtext2text(MTextString / regex s)
    25. (setq regex(vlax-create-object "Vbscript.RegExp")) ;引用正则表达式控件
    26. (vlax-put-property regex "IgnoreCase" 0) ;不忽略大小写
    27. (vlax-put-property regex "Global" 1) ;匹配方式,全文字匹配
    28. (setq s MTextString)
    29. ;替换\\字符
    30. (vlax-put-property regex "Pattern" "\\\\\\\\")
    31. (setq s(vlax-invoke-method regex "Replace" s (chr 1)))
    32. ;替换\{字符
    33. (vlax-put-property regex "Pattern" "\\\\{")
    34. (setq s(vlax-invoke-method regex "Replace" s (chr 2)))
    35. ;替换\}字符
    36. (vlax-put-property regex "Pattern" "\\\\}")
    37. (setq s(vlax-invoke-method regex "Replace" s (chr 3)))
    38. ;删除段落缩进格式
    39. (vlax-put-property regex "Pattern" "\\\\pi(.[^;]*);")
    40. (setq s(vlax-invoke-method regex "Replace" s ""))
    41. ;删除制表符格式
    42. (vlax-put-property regex "Pattern" "\\\\pt(.[^;]*);")
    43. (setq s(vlax-invoke-method regex "Replace" s ""))
    44. ;删除堆迭格式
    45. (vlax-put-property regex "Pattern" "\\\\S(.[^;]*)(\\^|#|\\\\)(.[^;]*);")
    46. (setq s(vlax-invoke-method regex "Replace" s ""))
    47. ;删除字体、颜色、字高、字距、倾斜、字宽、对齐格式
    48. (vlax-put-property regex "Pattern" "(\\\\F|\\\\f|\\\\C|\\\\H|\\\\\T|\\\\Q|\\\\W|\\\\A)(.[^;]*);")
    49. (setq s(vlax-invoke-method regex "Replace" s ""))
    50. ;删除下划线、删除线格式
    51. (vlax-put-property regex "Pattern" "(\\\\L|\\\\O|\\\\l|\\\\o)")
    52. (setq s(vlax-invoke-method regex "Replace" s ""))
    53. ;删除不间断空格格式
    54. (vlax-put-property regex "Pattern" "\\\\~")
    55. (setq s(vlax-invoke-method regex "Replace" s ""))
    56. ;删除换行符格式
    57. (vlax-put-property regex "Pattern" "\\\\P")
    58. (setq s(vlax-invoke-method regex "Replace" s ""))
    59. ;删除换行符格式(针对Shift+Enter格式)
    60. (vlax-put-property regex "Pattern" "\n")
    61. (setq s(vlax-invoke-method regex "Replace" s ""))
    62. ;删除{}
    63. (vlax-put-property regex "Pattern" "({|})")
    64. (setq s(vlax-invoke-method regex "Replace" s ""))
    65. ;替换回\\,\{,\}字符
    66. (vlax-put-property regex "Pattern" "\\x01")
    67. (setq s(vlax-invoke-method regex "Replace" s "\\"))
    68. (vlax-put-property regex "Pattern" "\\x02")
    69. (setq s(vlax-invoke-method regex "Replace" s "{"))
    70. (vlax-put-property regex "Pattern" "\\x03")
    71. (setq s(vlax-invoke-method regex "Replace" s "}"))
    72. (vlax-release-object regex)
    73. s
    74. )
    1. public struct Strep
    2. {
    3. public string Patstr { get; set; }
    4. public string Repstr { get; set; }
    5. }
    6. public static string GetMTextRegstr(string input)
    7. {
    8. string tXTstr = input;
    9. List<Strep> replst = new List<Strep>()
    10. {
    11. new Strep(){Patstr= @"\\\\" ,Repstr=((char)1).ToString()},
    12. new Strep(){Patstr= @"\\{" , Repstr=((char)2).ToString()},
    13. new Strep(){Patstr= @"\\}" , Repstr=((char)3).ToString()},
    14. new Strep(){Patstr= @"\\pi(.[^;]*);" , Repstr=""},
    15. new Strep(){Patstr= @"\\pt(.[^;]*);" , Repstr=""},
    16. new Strep(){Patstr= @"\\S(.[^;]*)(^|#|\\)(.[^;]*);" , Repstr=""},
    17. new Strep(){Patstr= @"(\\F|\\f|\\C|\\H|\\T|\\Q|\\W|\\A)(.[^;]*);" , Repstr=""},
    18. new Strep(){Patstr= @"(\\L|\\O|\\K|\\l|\\o)" , Repstr=""},
    19. new Strep(){Patstr= @"\\~" , Repstr=""},
    20. new Strep(){Patstr= @"\\pql;" , Repstr=""},
    21. new Strep(){Patstr= @"\\pqc;" , Repstr=""},
    22. new Strep(){Patstr= @"\\pqr;" , Repstr=""},
    23. new Strep(){Patstr= @"\\pxqc;" , Repstr=""},
    24. new Strep(){Patstr="\\\\P" ,Repstr=" "},
    25. new Strep(){Patstr="\n" ,Repstr=" "},
    26. new Strep(){Patstr= @"({|})" , Repstr=""},
    27. new Strep(){Patstr= @"\x01" , Repstr=@"\\\\"},
    28. new Strep(){Patstr= @"\x02" , Repstr="{"},
    29. new Strep(){Patstr= @"\x03" , Repstr="}"}
    30. };
    31. foreach (Strep sr in replst)
    32. {
    33. if (Regex.IsMatch(tXTstr, sr.Patstr)) tXTstr = Regex.Replace(tXTstr, sr.Patstr, sr.Repstr);
    34. }
    35. return tXTstr;
    36. }

    2020.12.28优化版——-推荐

    1. public struct Strep
    2. {
    3. public string Patstr { get; set; }
    4. public string Repstr { get; set; }
    5. }
    6. public static string GetMTextRegstr(string input)
    7. {
    8. string tXTstr = input;
    9. List<Strep> replst = new List<Strep>()
    10. {
    11. //替换\\字符
    12. new Strep(){Patstr= @"\\\\" ,Repstr=((char)1).ToString()},
    13. //替换\{字符
    14. new Strep(){Patstr= @"\\{" , Repstr=((char)2).ToString()},
    15. //替换\}字符
    16. new Strep(){Patstr= @"\\}" , Repstr=((char)3).ToString()},
    17. //删除段落缩进格式
    18. new Strep(){Patstr= @"\\pi(.[^;]*);" , Repstr=""},
    19. //删除制表符格式
    20. new Strep(){Patstr= @"\\pt(.[^;]*);" , Repstr=""},
    21. //删除字体、颜色、字高、字距、倾斜、字宽、对齐格式
    22. new Strep(){Patstr= @"(\\F|\\f|\\C|\\H|\\T|\\Q|\\W|\\A)(.[^;]*);" , Repstr=""},
    23. //删除下划线、删除线格式
    24. new Strep(){Patstr= @"(\\L|\\O|\\K|\\l|\\o)" , Repstr=""},
    25. //删除不间断空格格式
    26. new Strep(){Patstr= @"\\~" , Repstr=""},
    27. new Strep(){Patstr= @"\\pse1;" , Repstr=""},
    28. new Strep(){Patstr= @"\\pql;" , Repstr=""},
    29. new Strep(){Patstr= @"\\pqc;" , Repstr=""},
    30. new Strep(){Patstr= @"\\pqr;" , Repstr=""},
    31. new Strep(){Patstr= @"\\pxqc;" , Repstr=""},
    32. //删除换行符格式
    33. new Strep(){Patstr="\\\\P" ,Repstr=" "},
    34. //删除换行符格式(针对Shift+Enter格式)
    35. new Strep(){Patstr="\n" ,Repstr=" "},
    36. //删除{}
    37. new Strep(){Patstr= @"({|})" , Repstr=""},
    38. //替换回\\,\{,\}字符
    39. new Strep(){Patstr= @"\x01" , Repstr=@"\\\\"},
    40. new Strep(){Patstr= @"\x02" , Repstr="{"},
    41. new Strep(){Patstr= @"\x03" , Repstr="}"}
    42. };
    43. foreach (Strep sr in replst)
    44. {
    45. if (Regex.IsMatch(tXTstr, sr.Patstr)) tXTstr = Regex.Replace(tXTstr, sr.Patstr, sr.Repstr);
    46. }
    47. return Stacking(tXTstr);
    48. /// <summary>
    49. /// 清除堆叠格式
    50. /// </summary>
    51. /// <param name="str">需清除格式的字符串</param>
    52. /// <returns></returns>
    53. string Stacking(string str)
    54. {
    55. MatchCollection mc = Regex.Matches(str, "\\\\S.*?;");
    56. foreach (Match match in mc)
    57. {
    58. string mvla = Regex.Match(match.Value, "(?<=\\\\S).*?(?=;)").Value;
    59. if (Regex.IsMatch(mvla, "\\^"))
    60. {
    61. if (Regex.IsMatch(mvla, "\\\\\\^"))
    62. {
    63. mvla = Regex.Replace(mvla, "\\\\\\^", @"\x01");
    64. mvla = Regex.Replace(mvla, "\\^", "");
    65. mvla = Regex.Replace(mvla, @"\\x01", "^");
    66. }
    67. else
    68. {
    69. mvla = Regex.Replace(mvla, "\\^", "");
    70. }
    71. str = str.Replace(match.Value, mvla);
    72. }
    73. else if (Regex.IsMatch(mvla, "\\#"))
    74. {
    75. if (Regex.IsMatch(mvla, "\\\\\\#"))
    76. {
    77. mvla = Regex.Replace(mvla, "\\\\\\#", @"\x01");
    78. mvla = Regex.Replace(mvla, "\\#", "");
    79. mvla = Regex.Replace(mvla, @"\\x01", "#");
    80. }
    81. else
    82. {
    83. mvla = Regex.Replace(mvla, "\\#", "");
    84. }
    85. str = str.Replace(match.Value, mvla);
    86. }
    87. else if (Regex.IsMatch(mvla, "\\/"))
    88. {
    89. if (Regex.IsMatch(mvla, "\\\\\\/"))
    90. {
    91. mvla = Regex.Replace(mvla, "\\\\\\/", @"\x01");
    92. mvla = Regex.Replace(mvla, "\\/", "");
    93. mvla = Regex.Replace(mvla, @"\\x01", "/");
    94. }
    95. else
    96. {
    97. mvla = Regex.Replace(mvla, "\\/", "");
    98. }
    99. str = str.Replace(match.Value, mvla);
    100. }
    101. }
    102. if (Regex.IsMatch(str, @"(?<=(m|M))\\U\+00B3")) str = Regex.Replace(str, @"(?<=(m|M))\\U\+00B3", "3");
    103. if (Regex.IsMatch(str, @"(?<=(m|M))\\U\+00B2")) str = Regex.Replace(str, @"(?<=(m|M))\\U\+00B2", "2");
    104. if (Regex.IsMatch(str, @"%%(d|D)")) str = Regex.Replace(str, @"%%(d|D)", "°");
    105. if (Regex.IsMatch(str, @"%%(c|C)")) str = Regex.Replace(str, @"%%(c|C)", "φ");
    106. if (Regex.IsMatch(str, @"%%(p|P)")) str = Regex.Replace(str, @"%%(p|P)", "±");
    107. return str;
    108. }
    109. }