- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
This code was inspired by a Tony Tanzillo's one using the Mtext.ExplodeFragments() method to remove all Mtext formatting.
A command: STRIPMT to unformat all selected Mtexts
A LISP function: gc-GetStrippedMtextString to return the unformated text string of the mtext which ename is passed as argument to the function using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace StripMtextSample
{
public class StripMtext
{
private StringBuilder m_sb = null;
private string GetStrippedMtextContents(MText mt)
{
m_sb = new StringBuilder();
mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
return m_sb.ToString();
}
private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
{
m_sb.Append(fragment.Text);
return MTextFragmentCallbackStatus.Continue;
}
[CommandMethod("STRIPMT", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void StripSelSet()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "MTEXT") });
ed.WriteMessage("\nSelect mtexts (or type 'Enter' for 'all').");
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.Error)
psr = ed.SelectAll(filter);
if (psr.Status != PromptStatus.OK)
return;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in psr.Value.GetObjectIds())
{
MText mt = (MText)tr.GetObject(id, OpenMode.ForWrite);
mt.Contents = GetStrippedMtextContents(mt);
}
tr.Commit();
}
}
catch (System.Exception e)
{
ed.WriteMessage(string.Format("\nError: {0}\n", e.Message));
}
}
[LispFunction("gc-GetStrippedMtextString")]
public string GetStrippedMtextString(ResultBuffer resbuf)
{
try
{
if (resbuf == null)
throw new LispException("to few arguments");
TypedValue[] arg = resbuf.AsArray();
if (arg.Length > 1)
throw new LispException("to many arguments");
if (arg[0].TypeCode != (short)LispDataType.ObjectId)
throw new LispException(string.Format("incorrect argument type: lentityp: {0}", arg[0].Value));
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
MText mt = tr.GetObject(((ObjectId)arg[0].Value), OpenMode.ForRead) as MText;
if (mt == null)
throw new LispException("incorrect argument type: not a mtext");
return GetStrippedMtextContents(mt);
}
}
catch (LispException e)
{
AcAp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: {0}\n", e.Message);
return null;
}
}
}
class LispException : System.Exception
{
public LispException(string message)
: base(message)
{
}
}
} |
|