|
- #include <dbdict.h>
- #include <dbblocktablerecord.h>
- #include <dbidmap.h>
- // 计算各类块数量
- std::map<std::string, int> CountBlockTypes(AcDbDatabase* pDb)
- {
- std::map<std::string, int> blockCount;
- blockCount["普通块"] = 0;
- blockCount["动态块"] = 0;
- blockCount["匿名块"] = 0;
- AcDbBlockTable* pBlockTable;
- if (Acad::eOk == pDb->getBlockTable(pBlockTable, AcDb::kForRead))
- {
- for (AcDbBlockTableIterator it(*pBlockTable); !it.done(); it.next())
- {
- AcDbBlockTableRecordPtr pBlockRec(it.item());
- if (!pBlockRec.openStatus())
- continue;
- // 判断是否为动态块
- if (pBlockRec->isKindOf(AcDbDynBlockReference::desc()))
- {
- blockCount["动态块"]++;
- }
- else if (/* 判断是否为匿名块 */)
- {
- blockCount["匿名块"]++;
- }
- else
- {
- blockCount["普通块"]++;
- }
- pBlockRec.close();
- }
- pBlockTable->close();
- }
- return blockCount;
- }
- // 将统计数据导出至Excel
- void ExportToExcel(const std::map<std::string, int>& blockCounts, const wchar采用t* excelFilePath)
- {
- // 这里假设已有一个适配器或库可以与Excel交互
- ExcelAdapter adapter;
- // 打开或创建Excel文件
- adapter.OpenOrCreateWorkbook(excelFilePath);
- // 添加新工作表
- adapter.AddWorksheet("块统计");
- // 写入表头
- adapter.WriteCell(1, 1, L"块类型");
- adapter.WriteCell(1, 2, L"数量");
- // 写入统计结果
- int row = 2;
- for (const auto& entry : blockCounts)
- {
- adapter.WriteCell(row++, 1, entry.first.c采用str());
- adapter.WriteCell(row++, 2, entry.second);
- }
- // 保存并关闭Excel文件
- adapter.SaveAndClose();
- }
- // 使用示例
- void MainProcess(AcDbDatabase* pDb)
- {
- auto counts = CountBlockTypes(pDb);
- std::wstring excelPath(L"统计结果.xlsx"); // 替换为实际要保存的Excel文件路径
- ExportToExcel(counts, excelPath);
- }
复制代码 |
|