- UID
- 38377
- 积分
- 135
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2003-3-25
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
需要知道的是表格样式对象存储在一个特别的字典中,可通过Database的TableStyleDictionaryId属性进行访问。可以通过这个ObjectId属性来访问字典来查询存在的表格样式或添加一个新的样式。这样,指定表格样式的颜色和格式就变得简单了。下面的c#代码定义了一个新的表格样式 "Garish Table Style":一个红色的表头,黄色的数据区以及品红色的文字(kean有点太。。。不过他也提示了 运行本文程序的时候 最好戴上太阳镜):-)
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Colors;
- //trans by yiweimi.更多文章:http://hi.baidu.com/netarx qq:68006907 转载注明出处
- namespace TableAndStyleCreation
- {
- public class Commands
- {
- [CommandMethod("CTWS")]
- static public void CreateTableWithStyle()
- {
- 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)
- {
- Transaction tr =
- doc.TransactionManager.StartTransaction();
- using (tr)
- {
- // 如果表格样式不存在 则创建
- // First let us create our custom style,
- // if it doesn't exist
- const string styleName = "Garish Table Style";
- ObjectId tsId = ObjectId.Null;
- DBDictionary sd =
- (DBDictionary)tr.GetObject(
- db.TableStyleDictionaryId,
- OpenMode.ForRead
- );
- // 如果已经存在则使用 Use the style if it already exists
- if (sd.Contains(styleName))
- {
- tsId = sd.GetAt(styleName);
- }
- else
- {
- // 不存在,进行创建 Otherwise we have to create it
- TableStyle ts = new TableStyle();
- // 表头设为红色 Make the header area red
- ts.SetBackgroundColor(
- Color.FromColorIndex(ColorMethod.ByAci, 1),
- (int)(RowType.HeaderRow | RowType.TitleRow)
- );
- // 数据区域黄色 And the data area yellow
- ts.SetBackgroundColor(
- Color.FromColorIndex(ColorMethod.ByAci, 2),
- (int)RowType.DataRow
- );
- // 文字品红色 With magenta text everywhere (yeuch :-)
- ts.SetColor(
- Color.FromColorIndex(ColorMethod.ByAci, 6),
- (int)(RowType.HeaderRow |
- RowType.TitleRow |
- RowType.DataRow)
- );
- // 添加样式到字典
- // Add our table style to the dictionary
- // and to the transaction
- sd.UpgradeOpen();
- tsId = sd.SetAt(styleName, ts);
- tr.AddNewlyCreatedDBObject(ts, true);
- sd.DowngradeOpen();
- }
- BlockTable bt =
- (BlockTable)tr.GetObject(
- doc.Database.BlockTableId,
- OpenMode.ForRead
- );
- Table tb = new Table();
- // 使用表格样式 Use our table style
- if (tsId == ObjectId.Null)
- // 这个不可能发生 除非以上逻辑发生了变化
- // This should not happen, unless the
- // above logic changes
- tb.TableStyle = db.Tablestyle;
- else
- tb.TableStyle = tsId;
- tb.NumRows = 5;
- tb.NumColumns = 3;
- tb.SetRowHeight(3);
- tb.SetColumnWidth(15);
- tb.Position = pr.Value;
- // 创建一个包含表格内容的2维数组
- // Create a 2-dimensional array
- // of our table contents
- string[,] str = new string[5, 4];
- 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();
- BlockTableRecord btr =
- (BlockTableRecord)tr.GetObject(
- bt[BlockTableRecord.ModelSpace],
- OpenMode.ForWrite
- );
- btr.AppendEntity(tb);
- tr.AddNewlyCreatedDBObject(tb, true);
- tr.Commit();
- }
- }
- }
- }
- }
如下为加载NET模块、运行CTWS命令、选取放置点后获得的"garish"表:
并且如果我们运行TABLESTYLE命令,我们可以在列表中看到自定义的表格样式:
|
|