DBText 和 MText互转

本文介绍了一个用于AutoCAD的实用工具,该工具能够实现MText与DBText之间的相互转换。通过提供的代码示例,可以将MText实体转换为DBText实体,反之亦然。这有助于用户根据不同需求调整文本的显示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

        [CommandMethod("MTtoDB")]
        public void MTextToDBText()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tran.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                //Create a 'static' list of the BlockIds, before altering the BlockTableRecord with adding/removing entities
                foreach (ObjectId id in btr.Cast<ObjectId>().ToList())
                {
                   DBText text = null; 
                   MText mText = null; 

                   ///You can find the EntityType without opening the ID with GetObject
                    //Entity ent = tran.GetObject(id, OpenMode.ForWrite, false) as Entity;
                    //if (ent != null && ent.GetType() == typeof(MText))
                    if (id.ObjectClass.DxfName == "MTEXT")
                    {
                        text = tran.GetObject(id, OpenMode.ForRead, false) as DBText;
                        mText = new MText();
                        mText.SetDatabaseDefaults(db);
                        //Copy the DBText properties to the MText entity
                        mText.Contents = text.TextString;
                        mText.Location = text.Position;
                        //...

                        text.UpgradeOpen();
                        text.Erase();
                    }
                    //if (ent != null && ent.GetType() == typeof(DBText))
                    else if (id.ObjectClass.DxfName == "TEXT")
                    {

                        mText = tran.GetObject(id, OpenMode.ForRead, false) as MText;
                        text = new DBText();
                        //Copy the MText properties to the DBText entity
                        text.TextString = mText.Contents;
                        text.Position = mText.Location;
                        //....
                        mText.UpgradeOpen();
                        mText.Erase();
                    }
                }

                tran.Commit();
            }
        }
[CommandMethod("MTextToText")]
public static void MTextToText()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tran.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);  
                    foreach (ObjectId id in btr.Cast<ObjectId>().ToList())
                    {
                        DBText text = null;
                        MText mText = null;

                        if (id.ObjectClass.DxfName == "MTEXT")
                        {
                            mText = tran.GetObject(id, OpenMode.ForRead, false) as MText;
                            text = new DBText();
                            text.TextString = mText.Contents;
                            text.Position = mText.Location;
                            text.Layer = mText.Layer;
                            text.Color = mText.Color;
                            text.Height = mText.TextHeight;
                            btr.AppendEntity(text);
                            tran.AddNewlyCreatedDBObject(text, true);
                            mText.UpgradeOpen();
                            mText.Erase();
                        }   //Chưa xử lý lỗi cho font "Times New Roman"
                    }
                    tran.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Application.ShowAlertDialog(ex.ToString());
            }
        }

// Counterwork for "TextToMText"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值