c# cad 二次开发 类库 CAD表格的操作,给CAD添加一个表格
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15表格操作
{
public class Class1
{
public struct BlockData
{
public string blockName;
public string layerName;
public string X;
public string Y;
public string Z;
public string ZS;
public string XS;
}
[CommandMethod(“TableDemo”)]
public void TableDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
Table table = new Table();
table.SetSize(10, 5);
table.SetRowHeight(10);
table.SetColumnWidth(50);
table.Columns[0].Width = 20;
table.Position = new Point3d(100, 100, 0);
//table.SetTextString(0, 0, “材料统计表”);早前版本的设置方法
table.Cells[0, 0].TextString = “材料统计表”;
table.Cells[0, 0].TextHeight = 6;
Color color = Color.FromColorIndex(ColorMethod.ByAci,3);
table.Cells[0, 0].BackgroundColor = color;
color = Color.FromColorIndex(ColorMethod.ByAci, 1);
table.Cells[0, 0].ContentColor = color;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(table);
trans.AddNewlyCreatedDBObject(table, true);
trans.Commit();
}
}
[CommandMethod(“DataToTableDemo”)]
public void DataToTableDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] values = new TypedValue[]
{
new TypedValue((int)DxfCode.Start,“INSERT”),
new TypedValue((int)DxfCode.LayerName,“ssd”)
};
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.OK)
{
ObjectId[] ids = psr.Value.GetObjectIds();
PromptPointResult ppr = ed.GetPoint(“选择表格的插入点:”);
if (ppr.Status == PromptStatus.OK)
{
Point3d point = ppr.Value;
BlockData[] data = this.GetBlockRefData(db, ids);
this.SetDataToTable(db, data, point);
}
}
}
///
/// 将数据以表格的形式插入图形
///
///
///
///
private void SetDataToTable(Database db, BlockData[] data, Point3d position)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//Table table = new Table();
TableExt table = new TableExt(data.Length, 7,position,data,“块数据统计表”);
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
btr.AppendEntity(table);
trans.AddNewlyCreatedDBObject(table, true);
trans.Commit();
}
}
///
/// 获取块参照的信息
///
///
///
///
private BlockData[] GetBlockRefData(Database db, ObjectId[] ids)
{
BlockData[] data = new BlockData[ids.Length];
using (Transaction trasn = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
// 块名 图层 X Y Z ZS XS
BlockReference br = (BlockReference)ids[i].GetObject(OpenMode.ForRead);
data[i].blockName = br.Name;
data[i].layerName = br.Layer;
data[i].X = br.Position.X.ToString();
data[i].Y = br.Position.Y.ToString();
data[i].Z = br.Position.Z.ToString();
foreach (ObjectId item in br.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)item.GetObject(OpenMode.ForRead);
if (attRef.Tag.ToString() == “ZS”)
{
data[i].ZS = attRef.TextString;
}
else if (attRef.Tag.ToString() == “XS”)
{
data[i].XS = attRef.TextString;
}
}
}
}
return data;
}
}
}