using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web.UI.WebControls; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Media3D; using Bricscad.EditorInput; using ImTools; using Newtonsoft.Json; using Prism.Services.Dialogs; using SWS.CAD.Base; using SWS.Commons; using SWS.Electrical.Models; using SWS.Model; using SWS.Service; using SWS.Share; using SWS.WPF.ViewModels; using Teigha.DatabaseServices; using Teigha.Geometry; using Teigha.GraphicsSystem; using Telerik.Windows.Controls; using Unity; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; //using static ImTools.Union; using Visibility = System.Windows.Visibility; namespace SWS.Electrical.ViewModels { public class DialogRefreshAnnotationViewModel : DialogBase, IDialogAware { private ObservableCollection _listAnnotation = new ObservableCollection(); /// /// 正常标注列表 /// public ObservableCollection listAnnotation { get { return this._listAnnotation; } set { if (value != this._listAnnotation) { this._listAnnotation = value; RaisePropertyChanged(nameof(listAnnotation)); } } } private ObservableCollection _listErrAnnotation = new ObservableCollection(); /// /// 异常标注列表 /// public ObservableCollection listErrAnnotation { get { return this._listErrAnnotation; } set { if (value != this._listErrAnnotation) { this._listErrAnnotation = value; RaisePropertyChanged(nameof(listErrAnnotation)); } } } private ObservableCollection _listMsg = new ObservableCollection(); /// /// 信息列表 /// public ObservableCollection listMsg { get { return this._listMsg; } set { if (value != this._listMsg) { this._listMsg = value; RaisePropertyChanged(nameof(listMsg)); } } } private string _NormalTagCount = "正常标注"; /// /// 正常标注 /// public string NormalTagCount { get { return _NormalTagCount; } set { _NormalTagCount = value; OnPropertyChanged(nameof(NormalTagCount)); } } private string _ErrTagCount = "异常标注"; /// /// 异常标注 /// public string ErrTagCount { get { return _ErrTagCount; } set { _ErrTagCount = value; OnPropertyChanged(nameof(ErrTagCount)); } } private bool _IsEnableGetAttribute = true; /// /// 获取元件属性值 是否可用 /// public bool IsEnableGetAttribute { get { return _IsEnableGetAttribute; } set { _IsEnableGetAttribute = value; OnPropertyChanged(nameof(IsEnableGetAttribute)); } } private bool _IsEnableRefreshAnnotation = true; /// /// 刷新标注 是否可用 /// public bool IsEnableRefreshAnnotation { get { return _IsEnableRefreshAnnotation; } set { _IsEnableRefreshAnnotation = value; OnPropertyChanged(nameof(IsEnableRefreshAnnotation)); } } /// /// 命令事件 获取元件属性值 /// public ICommand Command_GetAttribute { get; set; } /// /// 命令事件 刷新标注 /// public ICommand Command_RefreshAnnotation { get; set; } DrawingServce _ServiceDrawing; DrawingCatalogueService _ServiceDrawingCatalogue; ObjectTypeService _ServiceObjectType; private TreeModel dwgNode; int time = 10; public DialogRefreshAnnotationViewModel() { Command_GetAttribute = new DelegateCommand(onGetAttribute); Command_RefreshAnnotation = new DelegateCommand(onRefreshAnnotation); title = "刷新标注"; _ServiceDrawing = GlobalObject.container.Resolve(); _ServiceObjectType = GlobalObject.container.Resolve(); _ServiceDrawingCatalogue = GlobalObject.container.Resolve(); } public string Title => ""; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { try { dwgNode = parameters.GetValue(GlobalObject.dialogPar.para1.ToString()); title += " - " + dwgNode.Text; IsBusy = true; BusyContent = "数据加载中..."; AddMsg($"数据加载中..."); var listMtext = General.GetAllMText(); var listTag = General.GetAllAnnotation(); List listSuccess = new List(); List listFailed = new List(); foreach (var item in listTag) { DtoAnnotation dto = new DtoAnnotation(); dto.TagHandid = item.TagHandId; dto.AttributeName = item.Name; dto.AnnotationHandid = item.LabelHandId; dto.Status = "未刷新"; var mtext = listMtext.FirstOrDefault(a => a.HandId == dto.AnnotationHandid); if (mtext != null) { dto.AnnotationValue = mtext.Text; listSuccess.Add(dto); } else { dto.Status = "未匹配"; listFailed.Add(dto); } } listAnnotation = new ObservableCollection(listSuccess); listErrAnnotation = new ObservableCollection(listFailed); NormalTagCount = $"正常标注({listSuccess.Count}个)"; ErrTagCount = $"异常标注({listFailed.Count}个)"; AddMsg($"数据加载完成"); } catch (Exception ex) { MessageBox.Show("DialogOpened异常:" + ex.Message); } finally { IsBusy = false; } } /// /// 获取元件属性值 /// /// public async void onGetAttribute(object o) { //if (isRefresh) //{ // MessageBox.Show("正在刷新标注,请勿操作..."); // return; //} //if (isGetAttribute) //{ // MessageBox.Show("正在获取属性值,请勿操作..."); // return; //} try { IsEnableGetAttribute = IsEnableRefreshAnnotation = false; listMsg.Clear(); AddMsg("开始查询元件属性值信息..."); List listTagHandid = new List(); listTagHandid = listAnnotation.Select(a => a.TagHandid).Distinct().ToList(); foreach (string handid in listTagHandid) { AddMsg($"查询元件句柄[{handid}]属性值......"); var res = await _ServiceObjectType.GetTagInfosByPixels(dwgNode.ID, handid); if (res == null || !res.Any()) { AddMsg($"元件句柄[{handid}]获取属性值失败!", false); continue; } if (res.Count == 1) { var objectTypeName = res[0].ObjectTypeName; var tagNumber = res[0].tags[0].TagNumber; var props = res[0].tags[0].EngineDataProperty; var listAnno = listAnnotation.Where(a => a.TagHandid == handid).ToList(); foreach (var anno in listAnno) { var pro = props.FirstOrDefault(a => a.PropertyName == anno.AttributeName); if (pro != null) { anno.ObjectTypeName = objectTypeName; anno.TagNumber = tagNumber; anno.AttributeValue = pro.PropertyValue.ToString(); AddMsg($"获取属性值成功,类型:{objectTypeName},位号:{tagNumber},属性名:{anno.AttributeName},属性值:{anno.AttributeValue}"); } } } else { AddMsg($"元件句柄({handid})获取属性值异常!", false); continue; } } AddMsg("元件属性值信息查询完成!"); } catch (Exception ex) { AddMsg("基点元件信息查询异常:" + ex.Message); } finally { IsEnableGetAttribute = IsEnableRefreshAnnotation = true; } } /// /// 刷新标注 /// /// public async void onRefreshAnnotation(object o) { //if (isRefresh) //{ // MessageBox.Show("正在刷新标注,请勿操作..."); // return; //} var msg = string.Empty; try { IsEnableGetAttribute = IsEnableRefreshAnnotation = false; listMsg.Clear(); if (!listAnnotation.Any()) { AddMsg($"没有正常匹配的标注数据,无需刷新!", false); return; } var progress = new Progress(UpdateProgress); await DoWorkAsync(progress, new CancellationTokenSource()); } catch (Exception ex) { MessageBox.Show("异常:" + ex.Message); } finally { IsEnableGetAttribute = IsEnableRefreshAnnotation = true; } } #region dowork private void UpdateProgress(MessageModel dto) { System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => { AddMsg(dto.Message, dto.IsSuccess); })); } private async Task DoWorkAsync(IProgress progress, CancellationTokenSource cts) { try { List listValue = new List(); var msg = string.Empty; foreach (var anno in listAnnotation) { if (anno.Status == "已刷新") { AddMsg($"当前标注[{anno.AnnotationHandid}]的已刷新,跳至下一个"); continue; } if (anno.AttributeValue == anno.AnnotationValue) { AddMsg($"当前标注[{anno.AnnotationHandid}]的属性值和标注值一致,无需刷新,跳至下一个"); anno.Status = "无需刷新"; continue; } listValue.Add(new KeyValueModel() { Key = anno.AnnotationHandid, Value = anno.AttributeValue }); AddMsg($"当前标注句柄:{anno.AnnotationHandid},属性值:{anno.AttributeValue},已加入刷新列表"); anno.Status = "等待刷新"; await Task.Delay(100); } if (!listValue.Any()) { AddMsg($"所有标注值与属性值一致,无需刷新!", false); return; } msg = General.RefreshMTextInfo(listValue); if (string.IsNullOrWhiteSpace(msg)) { foreach (var anno in listValue) { var dto = listAnnotation.FirstOrDefault(a => a.AnnotationHandid == anno.Key); if (dto != null) { dto.Status = "已刷新"; } } AddMsg("操作已完成!"); } else { AddMsg($"刷新标注列表异常:{msg}",false); } } catch (Exception ex) { MessageBox.Show("异常:" + ex.Message); } } #endregion #region 添加提示信息 /// /// 添加提示信息 /// /// 信息 /// 是否成功 private void AddMsg(string msg, bool isSucc = true) { try { TextBlock tb = new TextBlock(); tb.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "==>> " + msg; tb.Foreground = isSucc ? Brushes.LightSeaGreen : Brushes.Red; listMsg.Add(tb); } catch (Exception ex) { MessageBox.Show("添加提示信息异常:" + ex.Message); } } #endregion } }