1. 程式人生 > >AutoCAD二次開發&實現簡單的自動編號

AutoCAD二次開發&實現簡單的自動編號

今天一早上看了群裡有小夥伴有這樣的需求,是實現使用者用滑鼠點選拾取AutoCAD平面,拾取點然後作為插入編號文字位置,按住esc鍵後,退出自動編號。按照他的需求,我這裡大致想了一下,如果按照該小夥伴提出的用多執行緒和委託,想必有點複雜了。首先AutoCAD是不支援多執行緒的,至於使用委託,我想大概意思是使用滑鼠的點選事件回撥而已。還有他另外提到的使用鉤子,這估計就更復雜了。我做了一個簡單的實現,使用的while迴圈,然後拾取螢幕上面的點,插入編號的文字即可。至於設定文字的樣式什麼,可以按照需求進行調整。這裡參考了AutoCAD二次開發(.Net)之設定MText字型(1)這篇部落格進行修改。具體實現效果如下圖所示。

好了,現在來看一下實現的原始碼。

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrderAuto
{
    public class Class1
    {
        public int orderIndex = 1;
        [CommandMethod("zdbh")]
        public void demo() {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            while (true)
            {
                Transaction trans = db.TransactionManager.StartTransaction();
                using (trans)
                {
                    BlockTable blockTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = trans.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    MText mt = new MText();
                    mt.Contents = Convert.ToString(orderIndex);

                    PromptPointResult ppr = ed.GetPoint("\n指點編號的插入點: ");
                    Point3d location;
                    if (ppr.Status!=PromptStatus.OK)
                    {
                        return;
                    }else{
                        location = ppr.Value;
                    }

                    DBText acText = new DBText();
                    acText.Position = location;
                    acText.Height = 50;
                    acText.TextString = Convert.ToString(orderIndex);
                    modelSpace.AppendEntity(acText);
                    trans.AddNewlyCreatedDBObject(acText, true);

                    /*
                    mt.TextStyleId = AddTextStyle("宋體", "1", "3", 20, 20);
                    mt.Width = 50;
                    mt.Height = 50;
                    mt.Location = location;

                    modelSpace.AppendEntity(mt);

                    trans.AddNewlyCreatedDBObject(mt, true);*/
                    trans.Commit();

                    orderIndex++;

                }
            }
        }

        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;
        }
    }
}

                                                                                     更多內容,請關注公眾號