明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 24385|回复: 40

[图元] MText使用相关

  [复制链接]
发表于 2010-5-15 13:41 | 显示全部楼层 |阅读模式
本帖最后由 雪山飞狐_lzh 于 2012-7-15 21:03 编辑

MText使用起来是有点复杂,特别是格式的解析
下面是个生成堆叠的示例,也许有必要写个专门的格式解析类
效果图:
  1.         public enum ScriptType
  2.         {
  3.             Fraction,
  4.             Italic,
  5.             Tolerance
  6.         }
  7.         public static string MakeScript(string baseTextString, string superScript, string subScript, ScriptType scriptType, double scale)
  8.         {
  9.             string[] scriptTypStrings = new string[]{"/", "#", "^"};
  10.             return
  11.                 string.Format
  12.                 (
  13.                     "\\A1;{0}{1}\\H{2:0.#}x;\\S{3}{4}{5};{6}",
  14.                     baseTextString,
  15.                     "{",
  16.                     scale,
  17.                     superScript,
  18.                     scriptTypStrings[(int)scriptType],
  19.                     subScript,
  20.                     "}"
  21.                 );
  22.         }
  23.         [CommandMethod("tt")]
  24.         public static void Test()
  25.         {
  26.             var db = HostApplicationServices.WorkingDatabase;
  27.             var doc = Application.DocumentManager.GetDocument(db);
  28.             var ed = doc.Editor;
  29.             using(Transaction tr = db.TransactionManager.StartTransaction())
  30.             {
  31.                 MText mt =
  32.                     new MText
  33.                     {
  34.                         Contents = MakeScript("123", "+0.01", "-0.01", ScriptType.Tolerance, 0.5)
  35.                     };
  36.                 mt.SetDatabaseDefaults();
  37.                 var btr = db.CurrentSpaceId.GetObject<BlockTableRecord>(OpenMode.ForWrite);
  38.                 btr.AppendEntity(mt);
  39.                 tr.AddNewlyCreatedDBObject(mt, true);
  40.                 tr.Commit();
  41.             }
  42.         }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1威望 +1 明经币 +2 金钱 +20 贡献 +5 激情 +5 收起 理由
lzx838 + 1 + 2 + 20 + 5 + 5 【精华】好程序

查看全部评分

发表于 2016-10-5 02:43 | 显示全部楼层
飞狐老师,能不能请教下,如何用C#实现最简单的文字替换~弄了半天弄不出来
发表于 2018-2-3 10:42 | 显示全部楼层
2008去除格式,直接:
string content=MText.Text
MText.Text=content
发表于 2022-11-4 10:31 | 显示全部楼层
论坛搜索,结果跑来.NET,有没有LISP 的,抓狂
 楼主| 发表于 2010-5-15 13:47 | 显示全部楼层
另外贴两个去除MText格式的函数
  1.         public static string GetMTextContents(string str)
  2.         {
  3.             string[] strs =
  4.                 str.Split(
  5.                     new string[] { "\\\" },
  6.                     StringSplitOptions.None);
  7.             for (int i = 0; i < strs.Length; i++)
  8.             {
  9.                 strs[i] =
  10.                     Regex.Replace
  11.                     (
  12.                         strs[i],
  13.                         @"(?<!\\)[{}]|\\[OLP\~]|\\[CFHTQWA][^;]*;",
  14.                         "",
  15.                         RegexOptions.IgnoreCase
  16.                     );
  17.                 strs[i] =
  18.                     Regex.Replace
  19.                     (
  20.                         strs[i],
  21.                         @"\\S(.*?)[/#\^](.*?)(;|$)",
  22.                         "($1/$2)",
  23.                         RegexOptions.IgnoreCase
  24.                    );
  25.                 strs[i] =
  26.                     Regex.Replace
  27.                     (
  28.                         strs[i],
  29.                         @"\\([{}])",
  30.                         "$1"
  31.                     );
  32.             }
  33.             return string.Join("\", strs);
  34.         }
  35.         public static string GetMTextContents2(string str)
  36.         {
  37.             str = "{" + str + "}";
  38.             Stack<string> stack = new Stack<string>();
  39.             while (str.Length > 0)
  40.             {
  41.                 int n = (str[0] == '\\') ? 2 : 1;
  42.                 if (n == 1 && str[0] == '}')
  43.                 {
  44.                     Stack<string> substack = new Stack<string>();
  45.                     while (stack.Peek() != "{")
  46.                     {
  47.                         substack.Push(stack.Pop());
  48.                     }
  49.                     stack.Pop();
  50.                     Queue<string> queue = GetRtfTextUnFormatString(substack);
  51.                     while (queue.Count > 0)
  52.                     {
  53.                         stack.Push(queue.Dequeue());
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     stack.Push(str.Substring(0, n));
  59.                 }
  60.                 str = str.Substring(n);
  61.             }
  62.             string res = "";
  63.             foreach (string s in stack)
  64.             {
  65.                 if (s.Length == 1)
  66.                 {
  67.                     res = s + res;
  68.                 }
  69.                 else
  70.                 {
  71.                     res = s.Substring(1) + res;
  72.                 }
  73.             }
  74.             return res;
  75.         }
  76.         private static Queue<string>
  77.             GetRtfTextUnFormatString(Stack<string> stack)
  78.         {
  79.             Queue<string> queue = new Queue<string>();
  80.             while (stack.Count > 0)
  81.             {
  82.                 string str = stack.Pop();
  83.                 if (str.Length == 1)
  84.                 {
  85.                     queue.Enqueue(str);
  86.                 }
  87.                 else
  88.                 {
  89.                     switch (str.Substring(1).ToUpper()[0])
  90.                     {
  91.                         case '\\':
  92.                         case '{':
  93.                         case '}':
  94.                         case 'U':
  95.                             queue.Enqueue(str);
  96.                             break;
  97.                         case 'A':
  98.                         case 'C':
  99.                         case 'F':
  100.                         case 'H':
  101.                         case 'Q':
  102.                         case 'T':
  103.                         case 'W':
  104.                             while (stack.Pop() != ";") ;
  105.                             break;
  106.                         case 'S':
  107.                             string s = "";
  108.                             queue.Enqueue("(");
  109.                             while ((s = stack.Peek()) != ";")
  110.                             {
  111.                                 if (s == "^" || s == "#")
  112.                                 {
  113.                                     stack.Pop();
  114.                                     queue.Enqueue("/");
  115.                                 }
  116.                                 else
  117.                                 {
  118.                                     queue.Enqueue(stack.Pop());
  119.                                 }
  120.                                 if (stack.Count == 0)
  121.                                 {
  122.                                     break;
  123.                                 }
  124.                             }
  125.                             queue.Enqueue(")");
  126.                             if (stack.Count > 0)
  127.                             {
  128.                                 stack.Pop();
  129.                             }
  130.                             break;
  131.                     }
  132.                 }
  133.             }
  134.             return queue;
  135.         }
发表于 2010-5-15 13:53 | 显示全部楼层

飞狐出品,必是精品!顶一个!呵呵!

发表于 2010-9-9 10:56 | 显示全部楼层

请问版主,怎么设置MText的基点为文本中心呢?

 楼主| 发表于 2010-9-9 21:40 | 显示全部楼层
            mt.Attachment = AttachmentPoint.MiddleCenter;
发表于 2010-9-11 20:58 | 显示全部楼层
如何判断已经存在的MText是否为斜体?
 楼主| 发表于 2010-9-12 11:44 | 显示全部楼层

如果使用替换文字编辑器,则通过输入格式代码应用格式。可为文字加下划线、删除线和创建堆叠文字。用户可以修改颜色、字体和文字高度,还可以修改文字字符间距或增加字符本身宽度。要应用格式,请使用下表中列出的格式代码:

段落格式代码

格式代码

作用

输入...

要生成 ...

\0...\o

打开和关闭

下划线

Autodesk \OAutoCAD\o

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex001v.png"/>

\L...\l

打开和关闭

下划线

Autodesk \LAutoCAD\l

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex002v.png"/>

\~

插入不间断

空格

Autodesk AutoCAD\~LT

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex003v.png"/>

\\

插入反斜杠

Autodesk \\AutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex004v.png"/>

\{...\}

插入左大括号和右大括号

Autodesk \{AutoCAD\}

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex005v.png"/>

\Cvalue;

修改为

指定的颜色

Autodesk \C2;AutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex006v.png"/>

\ File name;

修改为

指定的字体文件

Autodesk \Ftimes; AutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex007v.png"/>

\Hvalue;

修改为

以图形单位表示的

指定文字高度

Autodesk \H2;AutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex008v.png"/>

\Hvaluex;

将文字高度修改为

当前样式文字高度的

数倍

Autodesk \H3x;AutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex009v.png"/>

\S...^...;

堆叠 \、# 或 ^ 符号后的文字

1.000\S+0.010^-0.000;

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex010v.png"/>

\Tvalue;

调整字符之间的间距。有效值范围为字符间原始间距的 0.75 倍到字符间原始间距的 4 倍。

\T2;Autodesk

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex011v.png"/>

\Qangle;

修改倾斜角度

\Q20;Autodesk

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex012v.png"/>

\Wvalue;

修改宽度因子生成宽字

\W2;Autodesk

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex013v.png"/>

\A

设置对齐方式值,有效值为:0、1、2

(底端对正、居中对正、顶端对正)

\A1;1\S1/2

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex014v.png"/>

\P

结束段落

Autodesk\PAutoCAD

MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex015v.png"/>

大括号最多可以嵌套八层。

也可以使用控制代码添加特殊的字符,例如公差和标注符号。请参见 MTEXT

发表于 2010-9-29 21:41 | 显示全部楼层
谢谢提供,相知恨晚!
发表于 2010-10-7 13:12 | 显示全部楼层
[em103]
发表于 2010-10-20 11:14 | 显示全部楼层
好贴,顶个!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-17 00:54 , Processed in 0.191328 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表