找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1081|回复: 3

[每日一码] .NET下自定义AutoCad表格样式

[复制链接]

已领礼包: 1个

财富等级: 恭喜发财

发表于 2017-6-1 09:15:02 | 显示全部楼层 |阅读模式

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

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

×

需要知道的是表格样式对象存储在一个特别的字典中,可通过Database的TableStyleDictionaryId属性进行访问。可以通过这个ObjectId属性来访问字典来查询存在的表格样式或添加一个新的样式。这样,指定表格样式的颜色和格式就变得简单了。下面的c#代码定义了一个新的表格样式 "Garish Table Style":一个红色的表头,黄色的数据区以及品红色的文字(kean有点太。。。不过他也提示了  运行本文程序的时候 最好戴上太阳镜):-)

  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.Colors;
  6. //trans by yiweimi.更多文章:http://hi.baidu.com/netarx qq:68006907 转载注明出处
  7. namespace TableAndStyleCreation

  8. {

  9. public class Commands

  10. {

  11. [CommandMethod("CTWS")]

  12. static public void CreateTableWithStyle()

  13. {

  14.    Document doc =

  15.       Application.DocumentManager.MdiActiveDocument;

  16.    Database db = doc.Database;

  17.    Editor ed = doc.Editor;

  18.    PromptPointResult pr =

  19.       ed.GetPoint("\nEnter table insertion point: ");

  20.    if (pr.Status == PromptStatus.OK)

  21.    {

  22.       Transaction tr =

  23.       doc.TransactionManager.StartTransaction();

  24.       using (tr)

  25.       {

  26.       // 如果表格样式不存在 则创建

  27.           // First let us create our custom style,

  28.       // if it doesn't exist

  29.       const string styleName = "Garish Table Style";

  30.       ObjectId tsId = ObjectId.Null;

  31.       DBDictionary sd =

  32.          (DBDictionary)tr.GetObject(

  33.             db.TableStyleDictionaryId,

  34.             OpenMode.ForRead

  35.          );

  36.       // 如果已经存在则使用 Use the style if it already exists

  37.       if (sd.Contains(styleName))

  38.       {

  39.          tsId = sd.GetAt(styleName);

  40.       }

  41.       else

  42.       {

  43.          // 不存在,进行创建 Otherwise we have to create it

  44.          TableStyle ts = new TableStyle();

  45.          // 表头设为红色 Make the header area red

  46.          ts.SetBackgroundColor(

  47.             Color.FromColorIndex(ColorMethod.ByAci, 1),

  48.             (int)(RowType.HeaderRow | RowType.TitleRow)

  49.          );

  50.          // 数据区域黄色 And the data area yellow

  51.          ts.SetBackgroundColor(

  52.             Color.FromColorIndex(ColorMethod.ByAci, 2),

  53.             (int)RowType.DataRow

  54.          );

  55.          // 文字品红色 With magenta text everywhere (yeuch :-)

  56.          ts.SetColor(

  57.             Color.FromColorIndex(ColorMethod.ByAci, 6),

  58.             (int)(RowType.HeaderRow |

  59.                   RowType.TitleRow |

  60.                   RowType.DataRow)

  61.          );

  62.          // 添加样式到字典

  63.             // Add our table style to the dictionary

  64.          // and to the transaction

  65.          sd.UpgradeOpen();

  66.          tsId = sd.SetAt(styleName, ts);

  67.          tr.AddNewlyCreatedDBObject(ts, true);

  68.          sd.DowngradeOpen();

  69.       }

  70.       BlockTable bt =

  71.          (BlockTable)tr.GetObject(

  72.             doc.Database.BlockTableId,

  73.             OpenMode.ForRead

  74.          );

  75.       Table tb = new Table();

  76.       // 使用表格样式 Use our table style

  77.       if (tsId == ObjectId.Null)

  78.          // 这个不可能发生 除非以上逻辑发生了变化

  79.             // This should not happen, unless the

  80.          // above logic changes

  81.          tb.TableStyle = db.Tablestyle;

  82.       else

  83.          tb.TableStyle = tsId;

  84.       tb.NumRows = 5;

  85.       tb.NumColumns = 3;

  86.       tb.SetRowHeight(3);

  87.       tb.SetColumnWidth(15);

  88.       tb.Position = pr.Value;

  89.       // 创建一个包含表格内容的2维数组

  90.           // Create a 2-dimensional array

  91.       // of our table contents

  92.       string[,] str = new string[5, 4];

  93.       str[0, 0] = "Part No.";

  94.       str[0, 1] = "Name ";

  95.       str[0, 2] = "Material ";

  96.       str[1, 0] = "1876-1";

  97.       str[1, 1] = "Flange";

  98.       str[1, 2] = "Perspex";

  99.       str[2, 0] = "0985-4";

  100.       str[2, 1] = "Bolt";

  101.       str[2, 2] = "Steel";

  102.       str[3, 0] = "3476-K";

  103.       str[3, 1] = "Tile";

  104.       str[3, 2] = "Ceramic";

  105.       str[4, 0] = "8734-3";

  106.       str[4, 1] = "Kean";

  107.       str[4, 2] = "Mostly water";

  108.       // 使用一个套嵌的循环添加和格式化每个表格

  109.           // Use a nested loop to add and format each cell

  110.       for (int i = 0; i < 5; i++)

  111.       {

  112.          for (int j = 0; j < 3; j++)

  113.          {

  114.             tb.SetTextHeight(i, j, 1);

  115.             tb.SetTextString(i, j, str[i, j]);

  116.             tb.SetAlignment(i, j, CellAlignment.MiddleCenter);

  117.          }

  118.       }

  119.       tb.GenerateLayout();

  120.       BlockTableRecord btr =

  121.          (BlockTableRecord)tr.GetObject(

  122.             bt[BlockTableRecord.ModelSpace],

  123.             OpenMode.ForWrite

  124.          );

  125.       btr.AppendEntity(tb);

  126.       tr.AddNewlyCreatedDBObject(tb, true);

  127.       tr.Commit();

  128.       }

  129.    }

  130. }

  131. }

  132. }



如下为加载NET模块、运行CTWS命令、选取放置点后获得的"garish"表:

0.jpg
并且如果我们运行TABLESTYLE命令,我们可以在列表中看到自定义的表格样式:

1.jpg

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

已领礼包: 1304个

财富等级: 财源广进

发表于 2017-6-1 09:49:26 | 显示全部楼层
不错啊!!i
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

已领礼包: 70个

财富等级: 招财进宝

发表于 2017-6-1 12:42:29 | 显示全部楼层
有点儿意思。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 79个

财富等级: 招财进宝

发表于 2021-1-14 08:34:15 | 显示全部楼层
思路比较好!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-9-28 06:20 , Processed in 0.169463 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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