关于表格的代码,共享给大家
论坛里面表格相关的代码太少了,我共享下using Autodesk..ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
namespace TableCreation
{
public class Commands
{
static public void CreateTable1()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectId appDictId = ObjectId.Null;
//DBDictionary appDict = new DBDictionary();
TableStyle myTableStyle = new TableStyle();
PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
if (pr.Status == PromptStatus.OK)
{
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
Table tb = new Table();
DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
{
DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
if (!compDict.Contains("Standard"))
{
compDict.UpgradeOpen();
appDictId = compDict.SetAt("Standard", myTableStyle);
tr.AddNewlyCreatedDBObject(myTableStyle, true);
}
else
{
appDictId = compDict.GetAt("Standard");
myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard"), OpenMode.ForRead);
}
myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
myTableStyle.IsHeaderSuppressed = false;
//myTableStyle.DowngradeOpen();
}
tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
tb.IsTitleSuppressed = false;
tb.IsHeaderSuppressed = false;
tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
//FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
tb.NumRows = 7;
// Added an additional column for the block image
// and one for the "is dynamic" flag
tb.NumColumns = 9;
tb.SetRowHeight(3);
tb.SetColumnWidth(15);
tb.Rows.Height = 10;
tb.Rows.Height = 20;
tb.Rows.Height = 30;
tb.Columns.Width = 30;
tb.Columns.Width = 40;
tb.Columns.Width = 60;
tb.Position = pr.Value;
// Create a 2-dimensional array
// of our table contents
string[,] str = new string;
str = "Part No.";
str = "Name";
str = "Material ";
str = "1876-1";
str = "Flange";
str = "Perspex";
str = "0985-4";
str = "Bolt";
str = "Steel";
str = "3476-K";
str = "Tile";
str = "Ceramic";
str = "8734-3";
str = "Kean";
str = "Mostly water";
// Use a nested loop to add and format each cell
for (int i = 0; i %).IsDynamicBlock \\f \"%bl2\">%");
tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
}
}
//非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
tb.GenerateLayout();
//tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
tr.Commit();
}
}
}
static public void CreateTable()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectId appDictId = ObjectId.Null;
//DBDictionary appDict = new DBDictionary();
TableStyle myTableStyle = new TableStyle();
PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
if (pr.Status == PromptStatus.OK)
{
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
Table tb = new Table();
DBDictionary nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);//获取数据库object,并转换为DBDictionary
if (nod.Contains("ACAD_TABLESTYLE"))//包含这个字典
{
DBDictionary compDict = (DBDictionary)tr.GetObject(nod.GetAt("ACAD_TABLESTYLE"), OpenMode.ForRead);
if (!compDict.Contains("Standard1"))
{
compDict.UpgradeOpen();
appDictId = compDict.SetAt("Standard1", myTableStyle);
tr.AddNewlyCreatedDBObject(myTableStyle, true);
}
else
{
appDictId = compDict.GetAt("Standard1");
myTableStyle = (TableStyle)tr.GetObject(compDict.GetAt("Standard1"), OpenMode.ForRead);
}
myTableStyle.UpgradeOpen();//这个是必须的,否则会有问题的
myTableStyle.FlowDirection = FlowDirection.NotSet;//LeftToRight从下往上,NotSet从上往下,如果下面表格再设置就会不按照样式来
myTableStyle.IsTitleSuppressed = false;// tb.TableStyle = myTableStyle.ObjectIdb必须要有这个语句才起作用
myTableStyle.IsHeaderSuppressed = false;
//myTableStyle.DowngradeOpen();
}
tb.TableStyle = myTableStyle.ObjectId;//这个语句很关键,如果没有下面tb的很多设置不起作用
tb.IsTitleSuppressed = true;
tb.IsHeaderSuppressed = true;
tb.FlowDirection = FlowDirection.LeftToRight;//如果没有tb.TableStyle = myTableStyle.ObjectId,则这个无论怎么设置似乎都不起作用
//FlowDirection myFlowDirection = FlowDirection.BottomToTop;//BottomToTop,不存在的,似乎桌子公司错了
tb.SetSize(10, 5);//设置行列数目 //旧的方式 tb.NumRows = 7 //tb.NumColumns = 7;
tb.SetRowHeight(5);//设置行高
tb.SetColumnWidth(15);// 设置列宽
//tb.Columns.Width = 20; // 设置第一列宽度为20
tb.Position = pr.Value;
// Create a 2-dimensional array
// of our table contents
string[,] str = new string;
str = "Part No.";
str = "Name";
str = "Material ";
str = "1876-1";
str = "Flange";
str = "Perspex";
str = "0985-4";
str = "Bolt";
str = "Steel";
str = "3476-K";
str = "Tile";
str = "Ceramic";
str = "8734-3";
str = "Kean";
str = "Mostly water";
// Use a nested loop to add and format each cell
for (int i = 0; i %).IsDynamicBlock \\f \"%bl2\">%";
//tb.SetTextString(i, 4, "%%).IsDynamicBlock \\f \"%bl2\">%");
tb.SetAlignment(i, 4, CellAlignment.MiddleCenter);
}
}
//非常重要,根据当前样式更新表格,不加此句,会导致AutoCAD崩溃
tb.GenerateLayout();
//tb.FlowDirection = Autodesk.AutoCAD.DatabaseServices.FlowDirection.TopToBottom;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
tr.Commit();
}
}
}
public static void t8()//选择table,读取属性
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Table myTable = new Table();
PromptEntityOptions opts = new PromptEntityOptions("\n选择一个table:");
opts.SetRejectMessage("错误的选择!");
opts.AddAllowedClass(typeof(Table), false);
PromptEntityResult res = ed.GetEntity(opts);
if (res.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
myTable = tr.GetObject(res.ObjectId, OpenMode.ForRead) as Table;
myTable.FlowDirection = FlowDirection.LeftToRight;
tr.Commit();
}
}
}
} 注意要先在自己的图纸中定义块,否则将块加入表格会出错
分享精神赞一个
不过确实如此,因为CAD自带表格有点……鸡肋,所以大家都不怎么喜欢CAD自带的表格,还是excel表格好用,计算修改都很方便,需要CAD表格,直接导进来。
表示不知道怎么用
页:
[1]