文字使用相关
本帖最后由 雪山飞狐_lzh 于 2012-7-15 21:03 编辑MText使用起来是有点复杂,特别是格式的解析
下面是个生成堆叠的示例,也许有必要写个专门的格式解析类
效果图:
public enum ScriptType
{
Fraction,
Italic,
Tolerance
}
public static string MakeScript(string baseTextString, string superScript, string subScript, ScriptType scriptType, double scale)
{
string[] scriptTypStrings = new string[]{"/", "#", "^"};
return
string.Format
(
"\\A1;{0}{1}\\H{2:0.#}x;\\S{3}{4}{5};{6}",
baseTextString,
"{",
scale,
superScript,
scriptTypStrings[(int)scriptType],
subScript,
"}"
);
}
public static void Test()
{
var db = HostApplicationServices.WorkingDatabase;
var doc = Application.DocumentManager.GetDocument(db);
var ed = doc.Editor;
using(Transaction tr = db.TransactionManager.StartTransaction())
{
MText mt =
new MText
{
Contents = MakeScript("123", "+0.01", "-0.01", ScriptType.Tolerance, 0.5)
};
mt.SetDatabaseDefaults();
var btr = db.CurrentSpaceId.GetObject<BlockTableRecord>(OpenMode.ForWrite);
btr.AppendEntity(mt);
tr.AddNewlyCreatedDBObject(mt, true);
tr.Commit();
}
}
本帖最后由 你有种再说一遍 于 2024-11-12 03:20 编辑
我博客对于上面的收录
https://www.cnblogs.com/JJBox/p/18534134 飞狐老师,能不能请教下,如何用C#实现最简单的文字替换~弄了半天弄不出来 另外贴两个去除MText格式的函数
public static string GetMTextContents(string str)
{
string[] strs =
str.Split(
new string[] { "\\\\" },
StringSplitOptions.None);
for (int i = 0; i < strs.Length; i++)
{
strs =
Regex.Replace
(
strs,
@"(?<!\\)[{}]|\\|\\[^;]*;",
"",
RegexOptions.IgnoreCase
);
strs =
Regex.Replace
(
strs,
@"\\S(.*?)[/#\^](.*?)(;|$)",
"($1/$2)",
RegexOptions.IgnoreCase
);
strs =
Regex.Replace
(
strs,
@"\\([{}])",
"$1"
);
}
return string.Join("\\", strs);
}
public static string GetMTextContents2(string str)
{
str = "{" + str + "}";
Stack<string> stack = new Stack<string>();
while (str.Length > 0)
{
int n = (str == '\\') ? 2 : 1;
if (n == 1 && str == '}')
{
Stack<string> substack = new Stack<string>();
while (stack.Peek() != "{")
{
substack.Push(stack.Pop());
}
stack.Pop();
Queue<string> queue = GetRtfTextUnFormatString(substack);
while (queue.Count > 0)
{
stack.Push(queue.Dequeue());
}
}
else
{
stack.Push(str.Substring(0, n));
}
str = str.Substring(n);
}
string res = "";
foreach (string s in stack)
{
if (s.Length == 1)
{
res = s + res;
}
else
{
res = s.Substring(1) + res;
}
}
return res;
}
private static Queue<string>
GetRtfTextUnFormatString(Stack<string> stack)
{
Queue<string> queue = new Queue<string>();
while (stack.Count > 0)
{
string str = stack.Pop();
if (str.Length == 1)
{
queue.Enqueue(str);
}
else
{
switch (str.Substring(1).ToUpper())
{
case '\\':
case '{':
case '}':
case 'U':
queue.Enqueue(str);
break;
case 'A':
case 'C':
case 'F':
case 'H':
case 'Q':
case 'T':
case 'W':
while (stack.Pop() != ";") ;
break;
case 'S':
string s = "";
queue.Enqueue("(");
while ((s = stack.Peek()) != ";")
{
if (s == "^" || s == "#")
{
stack.Pop();
queue.Enqueue("/");
}
else
{
queue.Enqueue(stack.Pop());
}
if (stack.Count == 0)
{
break;
}
}
queue.Enqueue(")");
if (stack.Count > 0)
{
stack.Pop();
}
break;
}
}
}
return queue;
}
<p>飞狐出品,必是精品!顶一个!呵呵!</p> <p>请问版主,怎么设置MText的基点为文本中心呢?</p> <font face="Verdana"> mt.Attachment = AttachmentPoint.MiddleCenter;<br/></font> 如何判断已经存在的MText是否为斜体? <p><a name="WSfacf1429558a55de8b299cffc1fb20d34080"></a>如果使用替换文字编辑器,则通过输入格式代码应用格式。可为文字加下划线、删除线和创建堆叠文字。用户可以修改颜色、字体和文字高度,还可以修改文字字符间距或增加字符本身宽度。要应用格式,请使用下表中列出的格式代码:</p><a name="WS1a9193826455f5ffa23ce210c4a30acaf-63b8"></a>
<table class="ruled" cellspacing="0" cellpadding="0">
<colgroup>
<col style="WIDTH: 0.917in">
<col style="WIDTH: 1.416in">
<col style="WIDTH: 1.509in">
<col style="WIDTH: 1.398in">
<tbody>
<tr class="RuledHeading">
<th class="table-heading" colspan="4"><a name="WSfacf1429558a55de8b299cffc1fb20d34081"></a>
<p class="table-heading">段落格式代码 </p></th></tr>
<tr class="RuledHeading">
<th class="table-heading"><a name="WSfacf1429558a55de8b299cffc1fb20d34082"></a>
<p class="table-heading">格式代码</p></th>
<th class="table-heading"><a name="WSfacf1429558a55de8b299cffc1fb20d34083"></a>
<p class="table-heading">作用</p></th>
<th class="table-heading"><a name="WSfacf1429558a55de8b299cffc1fb20d34084"></a>
<p class="table-heading">输入...</p></th>
<th class="table-heading"><a name="WSfacf1429558a55de8b299cffc1fb20d34085"></a>
<p class="table-heading">要生成 ...</p></th></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34086"></a>
<p class="table-body">\0...\o</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34087"></a>
<p class="table-body">打开和关闭 </p><a name="WSfacf1429558a55de8b299cffc1fb20d34088"></a>
<p class="table-body">下划线</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34089"></a>
<p class="table-body">Autodesk \OAutoCAD\o</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3408a"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex001v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3408b"></a>
<p class="table-body">\L...\l</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3408c"></a>
<p class="table-body">打开和关闭 </p><a name="WSfacf1429558a55de8b299cffc1fb20d3408d"></a>
<p class="table-body">下划线</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3408e"></a>
<p class="table-body">Autodesk \LAutoCAD\l</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3408f"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex002v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34090"></a>
<p class="table-body">\~</p><a name="WSfacf1429558a55de8b299cffc1fb20d34091"></a>
<p class="table-body"></p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34092"></a>
<p class="table-body">插入不间断 </p><a name="WSfacf1429558a55de8b299cffc1fb20d34093"></a>
<p class="table-body">空格</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34094"></a>
<p class="table-body">Autodesk AutoCAD\~LT</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34095"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex003v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34096"></a>
<p class="table-body">\\</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34097"></a>
<p class="table-body">插入反斜杠</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34098"></a>
<p class="table-body">Autodesk \\AutoCAD</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d34099"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex004v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409a"></a>
<p class="table-body">\{...\}</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409b"></a>
<p class="table-body">插入左大括号和右大括号</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409c"></a>
<p class="table-body">Autodesk \{AutoCAD\} </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409d"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex005v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409e"></a>
<p class="table-body">\C<em class="mild">value;</em></p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d3409f"></a>
<p class="table-body">修改为 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340a0"></a>
<p class="table-body">指定的颜色</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a1"></a>
<p class="table-body">Autodesk \C2;AutoCAD </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a2"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex006v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a3"></a>
<p class="table-body">\ <em class="mild">File name</em>;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a4"></a>
<p class="table-body">修改为 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340a5"></a>
<p class="table-body">指定的字体文件</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a6"></a>
<p class="table-body">Autodesk \Ftimes; AutoCAD</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a7"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex007v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a8"></a>
<p class="table-body">\H<em class="mild">value</em>; </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340a9"></a>
<p class="table-body">修改为 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340aa"></a>
<p class="table-body">以图形单位表示的 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340ab"></a>
<p class="table-body">指定文字高度</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340ac"></a>
<p class="table-body">Autodesk \H2;AutoCAD</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340ad"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex008v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340ae"></a>
<p class="table-body">\H<em class="mild">valuex</em>; </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340af"></a>
<p class="table-body">将文字高度修改为 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340b0"></a>
<p class="table-body">当前样式文字高度的 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340b1"></a>
<p class="table-body">数倍</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b2"></a>
<p class="table-body">Autodesk \H3x;AutoCAD</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b3"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex009v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b4"></a>
<p class="table-body">\S...^...;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b5"></a>
<p class="table-body">堆叠 \、# 或 ^ 符号后的文字</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b6"></a>
<p class="table-body">1.000\S+0.010^-0.000;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b7"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex010v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b8"></a>
<p class="table-body">\T<em class="mild">value</em>;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340b9"></a>
<p class="table-body">调整字符之间的间距。有效值范围为字符间原始间距的 0.75 倍到字符间原始间距的 4 倍。</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340ba"></a>
<p class="table-body">\T2;Autodesk</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340bb"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex011v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340bc"></a>
<p class="table-body">\Q<em class="mild">angle</em>;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340bd"></a>
<p class="table-body">修改倾斜角度</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340be"></a>
<p class="table-body">\Q20;Autodesk</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340bf"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex012v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c0"></a>
<p class="table-body">\W<em class="mild">value</em>;</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c1"></a>
<p class="table-body">修改宽度因子生成宽字</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c2"></a>
<p class="table-body">\W2;Autodesk </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c3"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex013v.png"/></p></td></tr>
<tr class="RuledEvenRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c4"></a>
<p class="table-body">\A</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c5"></a>
<p class="table-body">设置对齐方式值,有效值为:0、1、2 </p><a name="WSfacf1429558a55de8b299cffc1fb20d340c6"></a>
<p class="table-body">(底端对正、居中对正、顶端对正)</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c7"></a>
<p class="table-body">\A1;1\S1/2</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c8"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex014v.png"/></p></td></tr>
<tr class="RuledOddRow">
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340c9"></a>
<p class="table-body">\P</p><a name="WSfacf1429558a55de8b299cffc1fb20d340ca"></a>
<p class="table-body"></p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340cb"></a>
<p class="table-body">结束段落</p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340cc"></a>
<p class="table-body">Autodesk\PAutoCAD </p></td>
<td class="table-body"><a name="WSfacf1429558a55de8b299cffc1fb20d340cd"></a>
<p class="table-body"><img src="mk:@MSITStore:D:\Program%20Files\AutoCAD%202008\help\acad_aug.chm::/images/PTDCPM/Spago-AUG/Simplified_Chinese/mtex015v.png"/></p></td></tr></colgroup></tbody></table>
<p><a name="WSfacf1429558a55de8b299cffc1fb20d340ce"></a>大括号最多可以嵌套八层。 </p>
<p><a name="WSfacf1429558a55de8b299cffc1fb20d340cf"></a>也可以使用控制代码添加特殊的字符,例如公差和标注符号。请参见 <a href="ms-its:ACAD_ACR.chm::/MTEXT.htm">MTEXT</a>。 </p> 谢谢提供,相知恨晚! 好贴,顶个!