c# cad 二次开发 类库 CAD表格的操作,给CAD添加一个表格

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;
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DK业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值