.net下自定义autocad表格样式 - 精华帖集合
www.dimcax.com
.net下自定义autocad表格样式
需要知道的是表格样式对象存储在一个特别的字典中,可通过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.更多文章: 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命令,我们可以在列表中看到自定义的表格样式:
学习,!
感谢一微米兄的精彩翻译!