修改命名空间

This commit is contained in:
GG Z
2026-02-21 16:31:24 +08:00
parent 97c0b18dc7
commit 2ad3d0fde0
188 changed files with 783 additions and 2710 deletions

View File

@@ -1,15 +1,24 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Data;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using Nice3point.Revit.Toolkit.External.Handlers;
using ShrlAlgoToolkit.RevitAddins.Assists;
using ShrlAlgoToolkit.RevitAddins.Windows;
namespace ShrlAlgoToolkit.RevitAddins.ModelManager;
@@ -356,75 +365,150 @@ public partial class ModelCheckViewModel : ObservableObject
try
{
EPPlusHelper.WriteExcel(
dialog.FileName,
m =>
{
var groups = Items.GroupBy(e => e.ErrorMessage);
foreach (var group in groups)
{
var sheet = m.Workbook.Worksheets.Add(group.Key);
//int columns = tdd.NumberOfColumns;
//int rows = group.Count() + 1;
for (var i = -1; i < group.Count(); i++)
{
if (i == -1)
{
sheet.Cells[1, 1].Value = "元素ID";
sheet.Cells[1, 2].Value = "族类别";
sheet.Cells[1, 3].Value = "类型或实例名称";
sheet.Cells[1, 4].Value = "参照标高";
sheet.Cells[1, 5].Value = "系统";
sheet.Cells[1, 6].Value = "主体";
sheet.Cells[1, 7].Value = "房间";
}
else
{
var elem = group.ElementAt(i).Element;
var config = new OpenXmlConfiguration()
{
TableStyles = TableStyles.Default,
AutoFilter = true,
};
Dictionary<string, MessageModel[]> sheets = Items.GroupBy(e => e.ErrorMessage).ToDictionary(g => g.Key, g => g.ToArray());
MiniExcel.SaveAs(dialog.FileName, sheets);
var type = elem.GetType();
var host = type.GetProperty("Host");
var level = type.GetProperty("ReferenceLevel");
var mepSystem = type.GetProperty("MEPSystem");
var room = type.GetProperty("Room");
sheet.Cells[i + 2, 1].Value = elem.Id.ToString();
sheet.Cells[i + 2, 2].Value = elem.Category.Name;
sheet.Cells[i + 2, 3].Value = elem.Name;
//EPPlusHelper.WriteExcel(
// dialog.FileName,
// m =>
// {
// IEnumerable<IGrouping<string, MessageModel>> groups = Items.GroupBy(e => e.ErrorMessage);
// foreach (var group in groups)
// {
// var sheet = m.Workbook.Worksheets.Add(group.Key);
// for (var i = -1; i < group.Count(); i++)
// {
// if (i == -1)
// {
// sheet.Cells[1, 1].Value = "元素ID";
// sheet.Cells[1, 2].Value = "族类别";
// sheet.Cells[1, 3].Value = "类型或实例名称";
// sheet.Cells[1, 4].Value = "参照标高";
// sheet.Cells[1, 5].Value = "系统";
// sheet.Cells[1, 6].Value = "主体";
// sheet.Cells[1, 7].Value = "房间";
// }
// else
// {
// var elem = group.ElementAt(i).Element;
if (level != null)
{
sheet.Cells[i + 2, 4].Value = (level.GetValue(elem, null) as Level)?.Name;
}
if (mepSystem != null)
{
sheet.Cells[i + 2, 5].Value = (mepSystem.GetValue(elem, null) as MEPSystem)?.Name;
}
if (host != null)
{
sheet.Cells[i + 2, 6].Value = (host.GetValue(elem, null) as Element)?.Name;
}
if (room != null)
{
sheet.Cells[i + 2, 7].Value = (room.GetValue(room, null) as Room)?.Name;
}
}
}
EPPlusHelper.SetTitle(sheet, 1, 1, 1, 7);
EPPlusHelper.SetStyle(sheet, 2, 1, group.Count() + 1, 7);
// var type = elem.GetType();
// var host = type.GetProperty("Host");
// var level = type.GetProperty("ReferenceLevel");
// var mepSystem = type.GetProperty("MEPSystem");
// var room = type.GetProperty("Room");
// sheet.Cells[i + 2, 1].Value = elem.Id.ToString();
// sheet.Cells[i + 2, 2].Value = elem.Category.Name;
// sheet.Cells[i + 2, 3].Value = elem.Name;
var range = sheet.Cells[1, 1, group.Count() + 1, 7];
range.AutoFitColumns();
}
}
);
WinDialogAssist.OpenFolderAndSelectFile(dialog.FileName);
// if (level != null)
// {
// sheet.Cells[i + 2, 4].Value = (level.GetValue(elem, null) as Level)?.Name;
// }
// if (mepSystem != null)
// {
// sheet.Cells[i + 2, 5].Value = (mepSystem.GetValue(elem, null) as MEPSystem)?.Name;
// }
// if (host != null)
// {
// sheet.Cells[i + 2, 6].Value = (host.GetValue(elem, null) as Element)?.Name;
// }
// if (room != null)
// {
// sheet.Cells[i + 2, 7].Value = (room.GetValue(room, null) as Room)?.Name;
// }
// }
// }
// EPPlusHelper.SetTitle(sheet, 1, 1, 1, 7);
// EPPlusHelper.SetStyle(sheet, 2, 1, group.Count() + 1, 7);
// var range = sheet.Cells[1, 1, group.Count() + 1, 7];
// range.AutoFitColumns();
// }
// }
//);
ExportErrorMessageToExcel(dialog.FileName);
Common.Assists.WinDialogAssist.OpenFolderAndSelectFile(dialog.FileName);
}
catch (Exception ex)
{
Assists.LogAssist.ToLog(ex.Message);
Common.Assists.LogAssist.ToLog(ex.Message);
}
}
private void ExportErrorMessageToExcel(string filePath)
{
// 1. 准备大字典,用于存放所有 Sheet 数据
var allSheetsData = new Dictionary<string, object>();
// 2. 分组逻辑保持不变
IEnumerable<IGrouping<string, MessageModel>> groups = Items.GroupBy(e => e.ErrorMessage);
foreach (var group in groups)
{
// 3. 构造当前 Sheet 的行数据列表
var sheetRows = new List<Dictionary<string, object>>();
foreach (var item in group)
{
var elem = item.Element;
if (elem == null) continue;
var type = elem.GetType();
// 创建一行数据
var row = new Dictionary<string, object>();
// 直接赋值固定属性
row["元素ID"] = elem.Id.ToString();
row["族类别"] = elem.Category?.Name ?? "";
row["类型或实例名称"] = elem.Name;
// 使用反射获取动态属性(保留你原有的逻辑)
var levelProp = type.GetProperty("ReferenceLevel");
var mepProp = type.GetProperty("MEPSystem");
var hostProp = type.GetProperty("Host");
var roomProp = type.GetProperty("Room");
row["参照标高"] = levelProp != null ? (levelProp.GetValue(elem, null) as Level)?.Name : "";
row["系统"] = mepProp != null ? (mepProp.GetValue(elem, null) as MEPSystem)?.Name : "";
row["主体"] = hostProp != null ? (hostProp.GetValue(elem, null) as Element)?.Name : "";
// 注意:原代码中 room.GetValue(room, null) 应该是 roomProp.GetValue(elem, null) 的笔误
row["房间"] = roomProp != null ? (roomProp.GetValue(elem, null) as Room)?.Name : "";
sheetRows.Add(row);
}
// 4. 处理 Sheet 名称Excel 限制 31 个字符且不能有特殊字符)
string sheetName = group.Key ?? "未命名";
char[] invalidChars = { '\\', '/', '?', '*', '[', ']', ':' };
foreach (var c in invalidChars) sheetName = sheetName.Replace(c, '_');
if (sheetName.Length > 31) sheetName = sheetName.Substring(0, 31);
// 防止同名 Sheet 冲突
if (allSheetsData.ContainsKey(sheetName))
sheetName = sheetName.Substring(0, Math.Min(sheetName.Length, 25)) + "_" + Guid.NewGuid().ToString().Substring(0, 4);
allSheetsData.Add(sheetName, sheetRows);
}
// 5. 保存文件
try
{
// MiniExcel 默认会将 Dictionary 的 Key 作为表头并加粗
MiniExcel.SaveAs(filePath, allSheetsData, overwriteFile: true);
}
catch (IOException)
{
// 提示文件被占用
MessageBox.Show("请先关闭已打开的 Excel 文件后再尝试保存。");
}
}
private void FindBasePoint()
{
var basePoints = uiapp.ActiveUIDocument.Document.OfClass<BasePoint>().OfType<BasePoint>();
@@ -544,7 +628,7 @@ public partial class ModelCheckViewModel : ObservableObject
{
return;
}
WinDialogAssist.ShowOrActivate<MessageWin, MessageViewModel>(uiapp.ActiveUIDocument, errorItems, "未解决错误");
Common.Assists.WinDialogAssist.ShowOrActivate<MessageWin, MessageViewModel>(uiapp.ActiveUIDocument, errorItems, "未解决错误");
});
}