ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AutoCad 二次开发 .net 之创建Table

2019-11-12 13:55:38  阅读:822  来源: 互联网

标签:AutoCAD Autodesk myTable new vertices using 二次开发 AutoCad net


我使用了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());
                }
            }
        }
    }
}

 

标签:AutoCAD,Autodesk,myTable,new,vertices,using,二次开发,AutoCad,net
来源: https://www.cnblogs.com/HelloQLQ/p/11841281.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有