1. 程式人生 > >AutoCad 二次開發 .net 之建立Table

AutoCad 二次開發 .net 之建立Table

我使用了COM物件來在cad2018中建立table表格,需要的ObjectArx開發包可以在官網上下載,並且需要使用.netframework4.6的庫才行。 

專案裡除了引用常規的Cad開發dll,還要引用COM元件: Autodesk.AutoCAD.Interop.dll和Autodesk.AutoCAD.Interop.Common.dll 

 

ObjectArx下載地址:

https://www.autodesk.com/developer-network/platform-technologies/autocad/objectarx-license-download 

需要先填表並同意條款,才能跳入下載地址,下載頁面可見的有2018到2020三個版本可供下載。 

 

歷史的版本的下載可參考:

https://blog.csdn.net/flyfun2000/article/details/7065446 

 

如果要參考COM物件的API可到網址:

https://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-35CC52D6-03C1-48EE-90A3-97DFBBAC33C3

先放出程式碼執行的結果圖:

這裡我只試了幾種方法:

建立table:doc.ActiveLayout.Block.AddTable(vertices, 4, 2, 3, 10);

設定文字高度: myTable.SetTextHeight(1, 0.5);

合併單元格:myTable.MergeCells(1, 2, 0, 0);

設定列寬: myTable.SetColumnWidth(0, 5);

設定文字顏色:myTable.SetContentColor(2, color);

設定文字對齊方式: myTable.SetAlignment(1, AcCellAlignment.acMiddleCenter);

插入文字:myTable.SetText(0, 0, "我的表格測試");

插入塊引用:myTable.SetBlockTableRecordId(3, 0, br.BlockTableRecord.OldIdPtr.ToInt64(), true);

後面會給出完整的程式碼。

 

需要注意的是:在設定這些單元格時,分成了通過 row和coloum來定位一個單元格,和根據列舉型別RowType來確定: AcRowType acRowType = new AcRowType();按F12檢視定義可見這個類有4個值如圖:

另外在插入塊定義的時候,不能直接插入實體的ObjectId,要插入的實體必須得是塊參照,見程式碼:

其中oId就是getEntity得到得ObjectId。

這個AcadTable有很多的方法見:

https://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-7B82400C-53D0-4D1A-94FA-66BB3040F0AA

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;

namespace CreateExcelTable
{
    public class CreateTable
    {

        Document AcadDoc = Application.DocumentManager.MdiActiveDocument;
        Editor AcadEd = Application.DocumentManager.MdiActiveDocument.Editor;
        Database AcadDb = Application.DocumentManager.MdiActiveDocument.Database;

        [CommandMethod("ECDCreate")]
        public void Create()
        {
            AcadApplication acadApp = null;
            AcadDocument doc = null;
            AcadTable myTable = null;

            acadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
            doc = acadApp.ActiveDocument;

            PromptPointOptions ppOps = new PromptPointOptions("請選擇表格插入位置\n");

            PromptPointResult ppRes = AcadEd.GetPoint(ppOps);

            double[] vertices = new double[3];
            vertices[0] = 0;
            vertices[1] = 0;
            vertices[2] = 0;

            if (ppRes.Status == PromptStatus.OK)
            {

                vertices[0] = ppRes.Value[0];
                vertices[1] = ppRes.Value[1];
                vertices[2] = ppRes.Value[2];

            }
            AcRowType acRowType = new AcRowType();
            /*acUnknownRow = 0,
              acDataRow = 1,
              acTitleRow = 2,
              acHeaderRow = 4*/

            myTable = doc.ActiveLayout.Block.AddTable(vertices, 4, 2, 3, 10);
            //設定文字高度
            myTable.SetTextHeight(1, 0.5);
            myTable.SetTextHeight(2, 1.5);
            myTable.SetTextHeight(4, 1);
            //合併單元格
            myTable.MergeCells(1, 2, 0, 0);
            //設定列寬
            myTable.SetColumnWidth(0, 5);
            myTable.SetColumnWidth(1, 25);
            //插入資料
            myTable.SetText(0, 0, "我的表格測試");
            myTable.SetText(1, 0, "Data1");
            myTable.SetText(1, 1, "這是一條資料");
            myTable.SetText(2, 1, "這是一條測試資料");
            myTable.SetText(3, 1, "左邊是個塊定義");
             
            //設定文字顏色            
            AcadAcCmColor color = new AcadAcCmColor();
            color.ColorIndex = AcColor.acYellow;

            myTable.SetContentColor(2, color);

            //設定單元格中文字顏色
            AcadAcCmColor color2 = new AcadAcCmColor();
            color2.ColorIndex = AcColor.acGreen;

            myTable.SetContentColor2(3, 1, 0, color2);

            //設定單元格對其方式
            myTable.SetAlignment(1, AcCellAlignment.acMiddleCenter);

            PromptEntityOptions propEnt = new PromptEntityOptions("請選擇實體\n");

            PromptEntityResult propRes = AcadEd.GetEntity(propEnt);
            
            if (propRes.Status == PromptStatus.OK)
            {
                try
                {

                    //錯誤
                    // myTable.SetBlockTableRecordId(3, 0, propRes.ObjectId.OldIdPtr.ToInt64(), true);

                    ObjectId oId = propRes.ObjectId;
                    AcadEd.WriteMessage(oId.IsValid.ToString());

                    BlockReference br;
                    using (var trans = AcadDb.TransactionManager.StartTransaction())
                     { 

                         br = trans.GetObject(oId, OpenMode.ForRead) as BlockReference;

                         if (br == null)
                         {
                             Application.ShowAlertDialog("請選擇塊定義");

                             trans.Commit();

                             return;
                         }


                         trans.Commit();
                     }

                    //錯誤
                    //br = (BlockReference)oId.GetObject(OpenMode.ForRead);

                    //設定單元格塊引用
                    myTable.SetBlockTableRecordId(3, 0, br.BlockTableRecord.OldIdPtr.ToInt64(), true);

                }
                catch (System.Exception e)
                {

                    AcadEd.WriteMessage(e.ToString());
                }
            }
        }
    }
}

&n