AutoCad 二次開發 .net 之建立Table

我使用了COM對象來在cad2018中建立table表格,須要的ObjectArx開發包能夠在官網上下載,而且須要使用.netframework4.6的庫才行。 測試

項目裏除了引用常規的Cad開發dll,還要引用COM組件: Autodesk.AutoCAD.Interop.dll和Autodesk.AutoCAD.Interop.Common.dll  ui

 

ObjectArx下載地址:spa

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

須要先填表並贊成條款,才能跳入下載地址,下載頁面可見的有2018到2020三個版本可供下載。 3d

 

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

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

 

若是要參考COM對象的API可到網址: 對象

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

先放出代碼運行的結果圖:開發

這裏我只試了幾種方法:

建立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());
                }
            }
        }
    }
}
相關文章
相關標籤/搜索