AutoCAD二次開發(.Net)之設定MText字型(1)

淡定vs冷漠發表於2018-01-03
        [CommandMethod("SetFont")]
        public void CreateMText()
        {
            var db = HostApplicationServices.WorkingDatabase;
            var doc = Application.DocumentManager.GetDocument(db);
            var ed = doc.Editor;
            DocumentLock dl = doc.LockDocument();
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    MText mt = new MText();
                    mt.Contents = "ABCDE";
                    mt.TextStyleId = AddTextStyle("宋體", "1", "3", 20, 20);
                    mt.Width = 3.5;
                    mt.Height = 3.5;
                    mt.Location = point;
                    modelSpace.AppendEntity(mt);
                    tr.AddNewlyCreatedDBObject(mt, true);
                    tr.Commit();
                }
            }
            catch { }
            dl.Dispose();
        }
        public static ObjectId AddTextStyle(string name, string smallfont, string bigfont, double height, double xscale)
        {
            Database dbH = HostApplicationServices.WorkingDatabase;

            using (Transaction trans = dbH.TransactionManager.StartTransaction())
            {
                TextStyleTable TST = (TextStyleTable)trans.GetObject(dbH.TextStyleTableId, OpenMode.ForWrite);
                ObjectId id = GetIdFromSymbolTable(TST, name);
                if (id == ObjectId.Null)
                {
                    TextStyleTableRecord TSTR = new TextStyleTableRecord();
                    TSTR.Name = name;
                    TSTR.FileName = smallfont;
                    TSTR.BigFontFileName = bigfont;
                    TSTR.TextSize = height;
                    TSTR.XScale = xscale;
                    TST.UpgradeOpen();
                    id = TST.Add(TSTR);
                    trans.AddNewlyCreatedDBObject(TSTR, true);
                }
                return id;
            }
        }

        //取得符號表的Id
        public static ObjectId GetIdFromSymbolTable(SymbolTable st, string key)
        {
            Database dbH = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = dbH.TransactionManager.StartTransaction())
            {
                if (st.Has(key))
                {
                    ObjectId idres = st[key];
                    if (!idres.IsErased)
                        return idres;
                    foreach (ObjectId id in st)
                    {
                        if (!id.IsErased)
                        {
                            SymbolTableRecord str = (SymbolTableRecord)trans.GetObject(id, OpenMode.ForRead);
                            if (str.Name == key)
                                return id;
                        }
                    }
                }
            }
            return ObjectId.Null;
        }

相關文章