找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 969|回复: 0

[分享] Creating an AutoCAD table using .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-11 17:03:07 来自手机 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 csharp 于 2014-5-12 08:14 编辑

http://through-the-interface.typepad.com/through_the_interface/2007/06/creating_an_aut.html

Creating an AutoCAD table using .NET
This suggestion came in a few weeks ago from Kélcyo Pereira, and I've borrowed some code from Sreekar Devatha, from DevTech India, to help implement the suggestion.
The following C# code creates a very simple table and inserts it at the position selected by the user. The table is really very simply - a 5 (row) x 3 (column) table created from string values, no other data-types. It picks up the current style and aligns each cell as "middle, center". That's really all there is to it.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace TableCreation
{
  public class Commands
  {
    [CommandMethod("CRT")]
    static public void CreateTable()
    {
      Document doc =
        Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

      PromptPointResult pr =
        ed.GetPoint("\nEnter table insertion point: ");
      if (pr.Status == PromptStatus.OK)
      {
        Table tb = new Table();
        tb.TableStyle = db.Tablestyle;
        tb.NumRows = 5;
        tb.NumColumns = 3;
        tb.SetRowHeight(3);
        tb.SetColumnWidth(15);
        tb.Position = pr.Value;

        // Create a 2-dimensional array
        // of our table contents
        string[,] str = new string[5, 3];
        str[0, 0] = "Part No.";
        str[0, 1] = "Name ";
        str[0, 2] = "Material ";
        str[1, 0] = "1876-1";
        str[1, 1] = "Flange";
        str[1, 2] = "Perspex";
        str[2, 0] = "0985-4";
        str[2, 1] = "Bolt";
        str[2, 2] = "Steel";
        str[3, 0] = "3476-K";
        str[3, 1] = "Tile";
        str[3, 2] = "Ceramic";
        str[4, 0] = "8734-3";
        str[4, 1] = "Kean";
        str[4, 2] = "Mostly water";

        // Use a nested loop to add and format each cell
        for (int i = 0; i < 5; i++)
        {
          for (int j = 0; j < 3; j++)
          {
            tb.SetTextHeight(i, j, 1);
            tb.SetTextString(i, j, str[i, j]);
            tb.SetAlignment(i, j, CellAlignment.MiddleCenter);
          }
        }
        tb.GenerateLayout();

        Transaction tr =
          doc.TransactionManager.StartTransaction();
        using (tr)
        {
          BlockTable bt =
            (BlockTable)tr.GetObject(
              doc.Database.BlockTableId,
              OpenMode.ForRead
            );
          BlockTableRecord btr =
            (BlockTableRecord)tr.GetObject(
              bt[BlockTableRecord.ModelSpace],
              OpenMode.ForWrite
            );
          btr.AppendEntity(tb);
          tr.AddNewlyCreatedDBObject(tb, true);
          tr.Commit();
        }
      }
    }
  }
}

And here's what you see when you run the CRT command and select a point:
0.jpg
I'd like to take this further by showing more advanced concepts around tables - please post a comment if you have a particular suggestion or request.


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-1-6 01:12 , Processed in 0.406844 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表