using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Input; using System.Windows.Media; using Bricscad.Windows; using CAD.Extend.Model; using Prism.Commands; using Prism.Dialogs; using SWS.CAD.Base; using Teigha.Colors; using Teigha.DatabaseServices; using Brush = System.Windows.Media.Brush; using Color = System.Windows.Media.Color; namespace CAD.Extend.ViewModels { public class DialogConvertBaseMapViewModel : DialogBase, IDialogAware { #region binding private ObservableCollection _listLayerName = new ObservableCollection(); /// /// 底图名列表 /// public ObservableCollection listLayerName { get { return _listLayerName; } set { _listLayerName = value; RaisePropertyChanged(nameof(listLayerName)); } } private KeyValueModel _SelectedLayerName; /// /// 选中的底图名 /// public KeyValueModel SelectedLayerName { get { return _SelectedLayerName; } set { _SelectedLayerName = value; RaisePropertyChanged(nameof(SelectedLayerName)); } } private bool _IsToBlock; /// /// 是否转为图块 /// public bool IsToBlock { get { return _IsToBlock; } set { _IsToBlock = value; RaisePropertyChanged(nameof(IsToBlock)); } } #endregion private string _BlockName = "EI-Elec专用底图"; /// /// 块名 /// public string BlockName { get { return _BlockName; } set { _BlockName = value; RaisePropertyChanged(nameof(BlockName)); } } private Brush _BlockColor; /// /// 图层颜色 /// public Brush BlockColor { get { return _BlockColor; } set { _BlockColor = value; RaisePropertyChanged(nameof(BlockColor)); } } /// /// 随机块名 /// public ICommand Command_BlockName { get; set; } /// /// 颜色 /// public ICommand Command_Color { get; set; } /// /// 图层颜色 默认灰色 /// Teigha.Colors.Color layerColor = Teigha.Colors.Color.FromColorIndex(ColorMethod.ByAci, 8); List listObjectId = new List(); string layerName = "EI-Elec专用底图图层"; public DialogConvertBaseMapViewModel() { title = "背景底图设置"; Command_BlockName = new DelegateCommand(onBlockName); Command_Color = new DelegateCommand(onColor); System.Drawing.Color winColor = layerColor.ColorValue; var c = Color.FromArgb(winColor.A, winColor.R, winColor.G, winColor.B); BlockColor = new SolidColorBrush(c); } /// /// 颜色选择 /// public void onColor() { var list=General.GetTopLevelDictionaryNames(); ColorDialog dlg = new ColorDialog(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { layerColor = dlg.Color; System.Drawing.Color winColor = dlg.Color.ColorValue; var b = Color.FromArgb(winColor.A, winColor.R, winColor.G, winColor.B); BlockColor = new SolidColorBrush(b); } } /// /// 随机块名 /// public void onBlockName() { var a = GetRndABC(); Thread.Sleep(10); var b = GetRndABC(); Thread.Sleep(10); var c = GetRndABC(); BlockName = "EI-Elec图块" + a + b + c; } public string GetRndABC() { // a-z ASCII值 97-122 // A-Z ASCII值 65-90 int i = new Random().Next(65, 90); char c = (char)i; return c.ToString(); } #region Dialog public DialogCloseListener RequestClose { get; } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } #endregion public void OnDialogOpened(IDialogParameters parameters) { listObjectId = parameters.GetValue>(GlobalObject.dialogPar.info.ToString()); var list = General.GetAllLayerNames(); foreach (var layerName in list) { if (layerName.Equals(layerName)) { listLayerName.Insert(0, new KeyValueModel() { Key = layerName, Value = layerName }); } else { listLayerName.Add(new KeyValueModel() { Key = layerName, Value = layerName }); } } if (!listLayerName.Where(a => a.Key == layerName).Any()) { listLayerName.Insert(0, new KeyValueModel() { Key = layerName, Value = layerName }); } SelectedLayerName = listLayerName[0]; } public override void ExecuteOKCommandAsync(object para) { if (IsToBlock) { if (string.IsNullOrEmpty(BlockName)) { MessageBox.Show("块名不能为空!"); return; } if (General.CheckHasBlockName(BlockName)) { MessageBox.Show("块名已存在,请更换块名!"); return; } General.SetEntityToLayer(listObjectId, SelectedLayerName.Value, layerColor, BlockName); } else { General.SetEntityToLayer(listObjectId, SelectedLayerName.Value, layerColor); } MessageBox.Show("底图设置成功!"); //Prism.Dialogs.DialogParameters resPara = new Prism.Dialogs.DialogParameters(); //RequestClose.Invoke(resPara, ButtonResult.Yes); } public override void ExecuteCloseCommand(object parameter) { if (parameter as string == "ClickNo") { RequestClose.Invoke(ButtonResult.No); } else { RequestClose.Invoke(ButtonResult.Cancel); } this.Dispose(); } } }