using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Windows; using Autodesk.Revit.DB; using EPPlus.Core.Extensions; using EPPlus.Core.Extensions.Attributes; using Nice3point.Revit.Toolkit.External.Handlers; using OfficeOpenXml; namespace Szmedi.RvKits.InfoManager.EAMTools { public partial class EAMCodeCheckViewModel : ObservableObject { private readonly ActionEventHandler handler; [ObservableProperty] private List currentFacilities; [ObservableProperty] private List instances; [ObservableProperty] private string selectedStation; [ObservableProperty] private List facilitySystems; [ObservableProperty] private string selectedSystem; public EAMCodeCheckViewModel(Document doc) { handler = new ActionEventHandler(); FileInfo fi = new(Path.Combine(GlobalVariables.DirAssembly, "Data", "EAM数据.xlsx")); using ExcelPackage package = new(fi); //ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve; try { var list = doc.OfClass() .Where(e => e.Category.AllowsBoundParameters && !Enum.GetName(typeof(BuiltInCategory), e.Category.Id.IntegerValue)!.Contains("Fitting") && e.Category.CategoryType == CategoryType.Model && e.Category.Id.IntegerValue != -2008013) //风道末端 .Cast() .OrderBy(ins => ins.Name) .Select(ins => new InstanceFacility(ins)); Instances = [.. list]; Facilities = package.ToList(1, configuration => configuration.SkipCastingErrors()); var group = Facilities.GroupBy(fa => fa.StationName).OrderBy(g => g.Key); Stations = [.. group.Select(g => g.Key)]; var regex = new Regex(@"-(\D+站|所|段|场)-"); var result = regex.Matches(doc.PathName).Cast().Select(m => m.Groups[1].Value).FirstOrDefault(); if (result != null && Stations.Contains(result)) { SelectedStation = result; } } catch (EPPlus.Core.Extensions.Exceptions.ExcelValidationException) { MessageBox.Show("列名不存在或不匹配,请检查表头是否存在换行。"); } finally { AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomainOnAssemblyResolve; } } [RelayCommand] private void Check() { if (CurrentFacilities?.Count == 0 || Instances?.Count == 0) { return; } foreach (var EAMData in CurrentFacilities) { //var b = Instances.Any(ins => ins.Number.Contains(EAMData.EAMCode)); foreach (var ins in Instances) { if (!string.IsNullOrEmpty(ins.Number) && ins.Number.Contains(EAMData.EAMCode)) { EAMData.IsMapped = true; ins.IsMapped = true; break; } } } CurrentFacilities = [.. Enumerable.OrderBy(CurrentFacilities, fa => fa.IsMapped)]; Instances = [.. Enumerable.OrderBy(Instances, ins => ins.IsMapped)]; } [RelayCommand] private void ShowFacility(object obj) { if (obj is not IFacility model) { return; } handler.Raise(uiapp => { var uidoc = uiapp.ActiveUIDocument; var doc = uidoc.Document; if (!model.Instance.IsValidObject) { return; } //if (model.Instance.IsHidden(uidoc.ActiveView)) //{ // return; //} //if (uidoc.ActiveView is not View3D view3d) //{ // view3d = doc.QueryElementsByType().FirstElement() as View3D; //} var b = IsVisible(doc.ActiveView.Id, model.Instance); if (!b) { MessageBox.Show("提示", "该构件在当前视图不可见"); return; } var box = model.Instance.get_BoundingBox(uidoc.ActiveGraphicalView); var uiView = uidoc.GetOpenUIViews().FirstOrDefault(v => v.ViewId == uidoc.ActiveGraphicalView.Id); uiView!.ZoomAndCenterRectangle(box.Min, box.Max); uidoc.Selection.SetElementIds([model.Instance.Id]); }); } private static bool IsVisible(ElementId viewId, Element elem) { if (FilteredElementCollector.IsViewValidForElementIteration(elem.Document, viewId)) // 某类视图不能使用 FilteredElementCollector { var fec = new FilteredElementCollector(elem.Document, viewId).ToElementIds(); return fec.Any(id => id == elem.Id); } return false; } private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) { if (!args.Name.Contains("ComponentModel.Annotations")) { return null; } var assemblyFile = Path.Combine(GlobalVariables.DirAssembly, "System.ComponentModel.Annotations.dll"); return File.Exists(assemblyFile) ? Assembly.LoadFrom(assemblyFile) : null; } partial void OnSelectedStationChanged(string oldValue, string newValue) { if (!string.IsNullOrEmpty(newValue)) { CurrentFacilities = Facilities.Where(fa => fa.StationName == newValue).ToList(); var group = Enumerable.GroupBy(CurrentFacilities, fa => fa.System); FacilitySystems = [.. group.Select(g => g.Key)]; FacilitySystems.Insert(0, "<请选择系统>"); } } partial void OnSelectedSystemChanged(string oldValue, string newValue) { if (newValue == "<请选择系统>") { CurrentFacilities = Facilities.Where(fa => fa.System == newValue).ToList(); return; } if (!string.IsNullOrEmpty(newValue)) { CurrentFacilities = Enumerable.Where(CurrentFacilities, fa => fa.System == newValue).ToList(); } } public List Facilities { get; } public List Stations { get; } } public class EAMData : ObservableObject { private bool? isMapped = false; [ExcelTableColumn("是否匹配", true)] public bool? IsMapped { get => isMapped; set => SetProperty(ref isMapped, value); } /// /// 线路名称 /// [ExcelTableColumn("LINE_NAME")] public string LineName { get; set; } /// /// EAM编码 /// [ExcelTableColumn("OBJ_CODE")] public string EAMCode { get; set; } /// /// 设备名称 /// [ExcelTableColumn("NAME")] public string Name { get; set; } /// /// 站点名称 /// [ExcelTableColumn("WP_NAME", true)] public string StationName { get; set; } /// /// 系统 /// [ExcelTableColumn("SYSTEM", true)] public string System { get; set; } } }