首页 > 解决方案 > 重叠实体的文本 (AutoCAD/C#)

问题描述

我正在尝试修改MText.Contents实体,但是您可以看到新字符串“123”与原始字符串完全重叠,你好。

在此处输入图像描述

这是代码(我已经尝试了注释部分,但到目前为止还没有运气)。

//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,这是新的。

谢谢!

标签: c#textautocadoverlapping

解决方案


这对我有用:

    [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();
        }
    }

推荐阅读