Creating an AutoCAD table linked to an Excel spreadsheet using .NET

 

In the last post I promised to tackle this issue, and so here we are again. :-)

Note: the code in this post relies on enhanced table functionality introduced in AutoCAD 2008, so please don't get frustrated trying to make this work in previous versions.

The following C# code follows on from yesterday's, taking the spreadsheet selected by the user and linking it to a newly-created table in the active AutoCAD drawing. I haven't bothered with line numbering in the below code, as it follows on almost exactly from the code shown last time (aside from renaming the namespace, the command and the function, as well as adding a string constant at the top of the function implementation).

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows;


namespace LinkToExcel

{

  public class Commands

  {

    [CommandMethod("TFS")]

    static public void TableFromSpreadsheet()

    {

      // Hardcoding the string

      // Could also select for it

      const string dlName =

        "Import table from Excel demo";


      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      OpenFileDialog ofd =

        new OpenFileDialog(

          "Select Excel spreadsheet to link",

          null,

          "xls; xlsx",

          "ExcelFileToLink",

          OpenFileDialog.OpenFileDialogFlags.

            DoNotTransferRemoteFiles

        );


      System.Windows.Forms.DialogResult dr =

        ofd.ShowDialog();


      if (dr != System.Windows.Forms.DialogResult.OK)

        return;


      ed.WriteMessage(

        "/nFile selected was /"{0}/".",

        ofd.Filename

      );


      PromptPointResult ppr =

        ed.GetPoint(

          "/nEnter table insertion point: "

        );

      if (ppr.Status != PromptStatus.OK)

        return;


      // Remove the Data Link, if it exists already


      DataLinkManager dlm = db.DataLinkManager;

      ObjectId dlId = dlm.GetDataLink(dlName);

      if (dlId != ObjectId.Null)

      {

        dlm.RemoveDataLink(dlId);

      }


      // Create and add the Data Link


      DataLink dl = new DataLink();

      dl.DataAdapterId = "AcExcel";

      dl.Name = dlName;

      dl.Description =

        "Excel fun with Through the Interface";

      dl.ConnectionString = ofd.Filename;

      dl.DataLinkOption =

        DataLinkOption.PersistCache;

      dl.UpdateOption |=

        (int)UpdateOption.AllowSourceUpdate;


      dlId = dlm.AddDataLink(dl);


      Transaction tr =

        doc.TransactionManager.StartTransaction();

      using (tr)

      {

        tr.AddNewlyCreatedDBObject(dl, true);


        BlockTable bt =

          (BlockTable)tr.GetObject(

            db.BlockTableId,

            OpenMode.ForRead

          );


        Table tb = new Table();

        tb.TableStyle = db.Tablestyle;

        tb.Position = ppr.Value;

        tb.SetDataLink(0, 0, dlId, true);

        tb.GenerateLayout();


        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            db.CurrentSpaceId,

            OpenMode.ForWrite

          );


        btr.AppendEntity(tb);

        tr.AddNewlyCreatedDBObject(tb, true);

        tr.Commit();

      }


      // Force a regen to display the table

      ed.Regen();

    }

  }

}

Here's what happens when you run the TFS command and select your favourite XLS for linking (I used mass-balance.xls from AutoCAD 2008's Sample/Mechanical Sample folder):

Table_linked_to_excel_spreadsheet

At this stage I haven't focused at all on formating - this is just coming in "as is", without any adjustment of cell alignments, column widths or row heights.

I chose to hardcode the name of the Data Link we use for the spreadsheet. You can run the DATALINK command to check on it, after the command has executed:

Data_link_for_excel_speadsheet_2 

It doesn't seem to be an issue if you repeat the command and bring in a different spreadsheet using the same link - the link appears to continue (although I haven't performed exhaustive testing). If it does prove to be a problem it should be simple enough to create a unique Data Link per spreadsheet imported (or even per time the command is run).

 
1.0下载地址: http://pan.baidu.com/s/1eQlahKe 新增零件清单工具 1.1下载地址: http://pan.baidu.com/s/1nt60GOD 零件清单工具可体现层级关系 零件清单工具多层清单零件数量计算原则调整 零件清单工具新增父级模型名称参数类型 零件清单工具新增零件重量及汇总参数类型 2.0下载地址: http://pan.baidu.com/s/1hqouI3q 新增工程图打开功能 新增工程图管理功能 修改零件清单” 在组件里使用了零件阵列或几个相同零件合成一个组,在bom里出现了零件重复”问题 修改零件清单” 组件重量没有出现”问题 修改零件清单” 重量单位不对,没有按模板要求的Kg显示,显示的是g单位”问题 修改零件清单” 按一定的规律点击图号按钮加排序”问题 修改零件清单” 子组件数量=单套数量X父组件数量”问题 修改零件清单” 同一个元件可能会出现在不同组件里,在汇总里就出现错误了”问题 修改零件清单” 对话框拖动”问题 新增零件清单可读取父元件模型参数功能. 2.1下载地址:http://pan.baidu.com/s/1sjG3lr3 修改零件清单插入工程图异常问题; 修改零件清单多层显示中, 阵列元件数量异常问题; 修改零件清单保存参数按钮可禁止; 修改零件清单元件所属错误问题. 新增零件清单导出时是否保存参数可配置; 新增零件清单总装模型显示到列表中, 层级0 新增零件清单加载数据时动态条显示; 新增零件清单选中行选择/取消 新增零件清单全部选择/取消 新增零件清单过滤 新增零件清单排序 修改工程图工具批量/打开工程图响应慢问题; 修改工程图环境下无法打开元件工程图时提示; 修改批量创建工程图时重新创建了已有工程图问题; 新增创建工程图后可配置是否自动保存; 新增创建工程图(右键)提示是否创建, 可以选择模板; 新增工程图缺省零件工程图模板; 新增工程图缺省装配工程图模板; 新增工程图管理界面可显示参数; 新增工程图管理总装模型显示到列表中; 新增工程图管理选中行选择/取消 新增工程图管理全部选择/取消 新增工程图管理过滤 新增工程图管理创建工程图后可自动保存关闭 新增工程图管理导出时自动创建文件夹 3.0 下载地址: http://pan.baidu.com/s/1mgDrtEw 修改零件清单工具重量小数点可配置; 修改零件清单工具排序后,点击打开文件错误问题; 修改零件清单工具参数保存异常问题; 修改零件清单工具系统崩溃问题; 新增工程图管理工具删除工程图时提示; 新增工程图管理工具导出PDF时名称可以读取参数; 新增批量参数工具; 3.1下载地址: http://pan.baidu.com/s/1i3xi2VF 修改工程图管理导出PDF时可读取同名零件模型的参数; 修改零件清单单重及总重改为通用计数方式 修改零件清单总装放在第一行 3.2下载地址: http://pan.baidu.com/s/1qWFRI1I 修改参数工具Exlce参数模板打开出现只读问题; 修改参数工具模型中提取参数,添加参数值不能写入指定模型里问题; 修改工程图管理工具, 导出PDF名称可配置多个参数; 修改零件清单显示异常问题; 常见问题: http://www.mcadex.com/bbs_1431046_ss20130113/thread_328190_1.html 零件清单工具介绍: http://www.mcadex.com/bbs_1431046_ss20130113/thread_315304_1.html 工程图工具介绍: http://www.mcadex.com/bbs_1431046_ss20130113/thread_328189_1.html 参数工具介绍: http://www.mcadex.com/bbs_1431046_ss20130113/thread_352273_1.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值