我正在尝试修改MText.Contents
实体,但是正如您所看到的,新字符串“ 123”与原始字符串完全重叠,H e l l o。
这里是代码(我已经尝试过注释的部分,但到目前为止还算运气。)。
//BlockTableRecord btrr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(curdb), OpenMode.ForWrite);
BlockTableRecord btrr = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in btrr)
{
Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, false, true) as Entity;
//Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, true) as Entity;
if (currentEntity == null)
{
continue;
}
if (currentEntity.GetType() == typeof(MText))
{
if (((MText)currentEntity).Contents == "H e l l o")
{
//currentEntity.UpgradeOpen();
((MText)currentEntity).Contents = "123";
//currentEntity.DowngradeOpen();
// TextEditor textEditor = TextEditor.CreateTextEditor((MText)currentEntity);
// textEditor.SelectAll();
// TextEditorSelection selection = textEditor.Selection;
// selection.InsertString("123");
// textEditor.Close(TextEditor.ExitStatus.ExitSave);
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(((MText)currentEntity).Contents + "--MText");
}
}
}
[AlertDialog
文本是123,是新的。
谢谢!
这对我有用:
[CommandMethod("TEST")]
public static void Test()
{
var db = HostApplicationServices.WorkingDatabase;
using (var tr = db.TransactionManager.StartTransaction())
{
var ms = (BlockTableRecord)tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in ms)
{
if (id.ObjectClass == RXObject.GetClass(typeof(MText)))
{
var mtext = (MText)tr.GetObject(id, OpenMode.ForRead);
if (mtext.Contents == "H e l l o")
{
tr.GetObject(id, OpenMode.ForWrite);
mtext.Contents = "123";
}
}
}
tr.Commit();
}
}
BlockTableRecord btrec = (BlockTableRecord)blkid.GetObject(OpenMode.ForRead);
btrec.UpgradeOpen();
btrec.DowngradeOpen();
foreach (ObjectId index in btrec)
{
Entity en = (Entity)index.GetObject(OpenMode.ForRead);
AttributeDefinition adef = en as AttributeDefinition;
if (adef != null)
{
ed.WriteMessage("\n" + adef.Tag);
}
foreach (ObjectId id in btrec)
{
Entity currentEntity = (Entity)id.GetObject(OpenMode.ForWrite);
if (currentEntity == null)
{
continue;
}
if (currentEntity.GetType() == typeof(MText))
{
if (((MText)currentEntity).Contents == "H E L L O")
{
((MText)currentEntity).Contents = "123";
}
}
}