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

猜你喜欢

转载自www.cnblogs.com/HelloQLQ/p/11841281.html
今日推荐