You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoCAD C# 块属性统计:按DK、TCD属性生成统计表格问题咨询

代码问题定位

你现有代码的核心错误如下:

  • 条件判断逻辑不成立:attref.Tag=="DK" && attref.Tag=="TCD" 永远为假,单个属性标签不可能同时匹配两个不同值
  • 缺失跳过第一个选中块的计数逻辑
  • 没有存储DK、TCD属性的配对关系,也缺少表格生成的相关逻辑

修正后完整代码

[CommandMethod("Createstatistics")]
public static void Createstatistics()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database data = doc.Database;
    Editor ed = doc.Editor;

    // 存储DK和TCD的配对集合
    List<(string DKValue, string TCDValue)> statList = new List<(string, string)>();
    // 选中块计数器,用于跳过第一个
    int blockCount = 0;

    using (Transaction trans = data.TransactionManager.StartTransaction())
    {
        try
        {
            PromptSelectionOptions opts = new PromptSelectionOptions();
            opts.MessageForAdding = "选择要统计的块";
            PromptSelectionResult opResult = ed.GetSelection(opts);
            
            if (opResult.Status != PromptStatus.OK || opResult.Value.Count == 0)
            {
                ed.WriteMessage("\n未选中任何块,命令退出");
                return;
            }

            SelectionSet slSet = opResult.Value;
            foreach (ObjectId obj in slSet.GetObjectIds())
            {
                blockCount++;
                // 跳过第一个块
                if (blockCount == 1) continue;

                BlockReference blR = trans.GetObject(obj, OpenMode.ForRead) as BlockReference;
                if (blR == null) continue;

                AttributeCollection attCol = blR.AttributeCollection;
                string dkVal = string.Empty;
                string tcdVal = string.Empty;

                foreach (ObjectId obId in attCol)
                {
                    AttributeReference attref = trans.GetObject(obId, OpenMode.ForRead) as AttributeReference;
                    if (attref == null) continue;

                    // 分别匹配两个属性标签,存储对应值
                    if (attref.Tag == "DK")
                    {
                        dkVal = attref.TextString;
                    }
                    else if (attref.Tag == "TCD")
                    {
                        tcdVal = attref.TextString;
                    }
                }

                // 两个属性都非空才加入统计列表
                if (!string.IsNullOrEmpty(dkVal) && !string.IsNullOrEmpty(tcdVal))
                {
                    statList.Add((dkVal, tcdVal));
                }
            }

            // 生成CAD表格插入到当前图纸
            if (statList.Count > 0)
            {
                PromptPointResult ppr = ed.GetPoint("\n指定表格插入点");
                if (ppr.Status != PromptStatus.OK) return;
                Point3d insertPoint = ppr.Value;

                Table table = new Table();
                table.TableStyle = data.TableStyle;
                table.SetSize(statList.Count + 1, 2); // 行数=数据量+表头,列数2
                table.SetRowHeight(3);
                table.SetColumnWidth(30);
                table.Position = insertPoint;

                // 表头
                table.Cells[0, 0].TextString = "DK";
                table.Cells[0, 1].TextString = "TCD";

                // 填充数据
                for (int i = 0; i < statList.Count; i++)
                {
                    table.Cells[i + 1, 0].TextString = statList[i].DKValue;
                    table.Cells[i + 1, 1].TextString = statList[i].TCDValue;
                }

                table.GenerateLayout();
                BlockTable bt = trans.GetObject(data.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                btr.AppendEntity(table);
                trans.AddNewlyCreatedDBObject(table, true);
                ed.WriteMessage($"\n统计完成,共生成{statList.Count}条记录");
            }
            else
            {
                ed.WriteMessage("\n未找到匹配的DK、TCD属性块");
            }

            trans.Commit();
        }
        catch (System.Exception e)
        {
            ed.WriteMessage($"\n执行出错:{e.ToString()}");
            trans.Abort();
        }
    }
}

代码说明

  • 新增了计数器跳过第一个选中的块,符合你的排除需求
  • 分别匹配DK和TCD属性存储对应值,确保两个属性配对
  • 最后会在你指定的插入点生成两列的统计表格,表头分别为DK、TCD
  • 增加了空值判断和异常分支的友好提示

内容的提问来源于stack exchange,提问作者PhilipZone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 05:15:03