using GalaSoft.MvvmLight; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows; using GalaSoft.MvvmLight.Command; using System.Collections.ObjectModel; using System.Windows.Shapes; using System.Runtime.Serialization; using TProduct.ProcessDrawing.Components; using System.Reflection; namespace TProduct.ProcessDrawing.Models { public class DeviceItem4Display : ObservableObject { public DeviceItem4Display(TProduct.ProcessDrawing.Entities.DeviceEntity entity) { this.NO = entity.NO; this.Name = entity.Name; this.X = double.Parse(entity.X); this.Y = double.Parse(entity.Y); this.Z = int.Parse(entity.Z); this.Width = double.Parse(entity.W); this.Height = double.Parse(entity.H); this.ComponentName = entity.ComponentName; this.IsFixPosi = entity.IsFixPosi; this.IsFixSize = entity.IsFixSize; this.Dia = entity.Dia; this.FlowDirection = entity.FlowDirection; this.Rotate = entity.Rotate; //手动添加属性 // PropList.Add(new DevicePropModel() { PropName = "Protocol", PropValue = "ModbusRtu" }); //AddVariableCommand = new RelayCommand(() => //{ // VariableList.Add(new VariableModel() { VarNum = DateTime.Now.ToString("yyyyMMddHHmmssFFF") }); //}); //DeleteVariableCommand = new RelayCommand(model => VariableList.Remove(model)); this.UiComponent = BuildUiComponent(entity.ComponentName); if (this.UiComponent != null ) { InitialPipeIndexList(entity.PipeIndexList); } this.PropList = new ObservableCollection(); if (entity.Props != null) { foreach (var dp in entity.Props) { var v_p = new DevicePropModel { PropName = dp.PropName, PropValue = dp.PropValue, PropTag = dp.PropTag, PropDispStyle = dp.PropDispStyle, PropOptions = DeviceItemHelper.GetPropOptions(dp.PropTag) }; //v_p.OnChangePropValue = this.DoPropValueChanged; this.PropList.Add(v_p); } } } public ComponentBase BuildUiComponent(string component_name) { var assembly = Assembly.Load("TProduct.ProcessDrawing.Components"); Type t = assembly.GetType("TProduct.ProcessDrawing.Components." + component_name); if (t == null) return null; var obj = Activator.CreateInstance(t) as UserControl; if (obj == null) return null; // 组件生成 var c = (ComponentBase)obj; c.SetConfigStatus(false ); System.Windows.Data.Binding binding = null; binding = new System.Windows.Data.Binding(); binding.Path = new System.Windows.PropertyPath("IsSelected"); c.SetBinding(ComponentBase.IsSelectedProperty, binding); binding = new System.Windows.Data.Binding(); binding.Path = new System.Windows.PropertyPath("Rotate"); c.SetBinding(ComponentBase.RotateAngleProperty, binding); c.RotateAngle = this.Rotate; binding = new System.Windows.Data.Binding(); binding.Path = new System.Windows.PropertyPath("WaterFlowDirection"); c.SetBinding(ComponentBase.WaterFlowDirectionProperty, binding); c.WaterFlowDirection = this.FlowDirection; return c; } private void InitialPipeIndexList(List sel_ids) { if (sel_ids == null) return; var pi_list = TProduct.Common.PipelineHelper.GetAll(); if (pi_list == null) return; PipeIndexList = new List(); for (int i = 0; i < pi_list.Count; i++) { var pi = pi_list[i]; var value = i + 1; if (sel_ids.Contains(value)) { PipeIndexList.Add(i + 1); } } } public List PipeIndexList { get; set; } = null; public bool IsHavePipeIndex(int pipeIndex) { if (PipeIndexList == null) return false; if(PipeIndexList.Contains(pipeIndex)) return true; return false ; } public Func> OnGetDevices { get; set; } //编号 public string NO { get; set; } //头部 public string Name { get; set; } private bool _isSelected; public bool IsSelected { get { return _isSelected; } set { Set(ref _isSelected, value); if (value) { z_temp = this.Z; this.Z = 999; } else { this.Z = z_temp; } } } private bool _isVisible = true; public bool IsVisible { get { return _isVisible; } set { Set(ref _isVisible, value); } } private int _dia; public int Dia { get { return _dia; } set { Set(ref _dia, value); if (this.UiComponent != null) { var disp_size = DeviceSettingHelper.GetPipeDiaDispSize(value); this.UiComponent.SetDia(disp_size); } } } private double x; public double X { get { return x; } set { Set(ref x, value); } } private double y; public double Y { get { return y; } set { Set(ref y, value); } } private int _z = 0; public int Z { get { return _z; } set { Set(ref _z, value);} } int z_temp = 0; public int RealZ { get { return z_temp; } set { Set(ref z_temp, value); } } private double _width; public double Width { get { return _width; } set { Set(ref _width, value); if (this.UiComponent != null && this.UiComponent is ComponentBase) (this.UiComponent as ComponentBase).Width = value; } } private double _height; public double Height { get { return _height; } set { Set(ref _height, value); if (this.UiComponent != null && this.UiComponent is ComponentBase) (this.UiComponent as ComponentBase).Height = value; } } private int _rotate; public int Rotate { get { return _rotate; } set { Set(ref _rotate, value); if(this.UiComponent!=null && this.UiComponent is ComponentBase) (this.UiComponent as ComponentBase).RotateAngle = value; } } private int _flowDirection = 1; public int FlowDirection { get { return _flowDirection; } set { Set(ref _flowDirection, value); if (this.UiComponent != null && this.UiComponent is ComponentBase) (this.UiComponent as ComponentBase).WaterFlowDirection = value; } } private bool _isFixPosi = false; public bool IsFixPosi { get { return _isFixPosi; } set { Set(ref _isFixPosi, value); } } private bool _isFixSize = false; public bool IsFixSize { get { return _isFixSize; } set { Set(ref _isFixSize, value); } } // 根据这个名称动态创建一个组件实例 public string ComponentName { get; set; } public ComponentBase UiComponent { get; set; } //属性 public ObservableCollection PropList { get; set; } = new ObservableCollection(); #region 处理组件移动逻辑 bool isMoving = false; Point startP = new Point(0, 0); public void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 获取光标对于拖动对象的坐标值 startP = e.GetPosition((System.Windows.IInputElement)sender); if( !_isFixPosi) isMoving = true; //var all_devices = OnGetDevices(); // 鼠标光标捕获 Mouse.Capture((IInputElement)sender); e.Handled = true; } public void OnMouseMove(object sender, MouseEventArgs e) { // 移动 是在按下 之后 if (isMoving && !_isFixPosi ) { } } public void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isMoving = false; // 释放光标 Mouse.Capture(null); } private Canvas GetParent(FrameworkElement d) { dynamic obj = VisualTreeHelper.GetParent(d); if (obj != null && obj is Canvas) return obj; return GetParent(obj); } #endregion #region 初始化右键菜单 public List ContextMenus { get; set; } public void InitContextMenu(string ComponentType) { ContextMenus = new List(); if(new string[] { "HorizontalPipeline", "VerticalPipeline" }.Contains(this.ComponentName)) { ContextMenus.Add(new MenuItem { Header = "改变流向", Command = new RelayCommand(() => { if (this.FlowDirection == 1) this.FlowDirection = -1; else this.FlowDirection = 1; } ), Visibility = Visibility.Visible }); } else { ContextMenus.Add(new MenuItem { Header = "顺时针旋转", Command = new RelayCommand(() => this.Rotate += 90), Visibility = Visibility.Visible }); ContextMenus.Add(new MenuItem { Header = "逆时针旋转", Command = new RelayCommand(() => this.Rotate -= 90), Visibility = Visibility.Visible }); } ContextMenus.Add(new Separator()); ContextMenus.Add(new MenuItem { Header = this._isFixPosi? "解除位置锁定" : "锁定位置", Tag= "FixPosi", Command = new RelayCommand(() => { foreach(var menu in ContextMenus) { if(!(menu is MenuItem)) { continue; } var menu2 = menu as MenuItem; if(menu2.Tag != null && menu2.Tag.ToString() == "FixPosi") { menu2.Header = this._isFixPosi ? "解除位置锁定" : "锁定位置"; } } this.IsFixPosi = !this._isFixPosi; }) }); ContextMenus.Add(new MenuItem { Header = this._isFixSize ? "解除尺寸锁定" : "锁定尺寸", Tag = "FixSize", Command = new RelayCommand(() => { foreach (var menu in ContextMenus) { if (!(menu is MenuItem)) { continue; } var menu2 = menu as MenuItem; if (menu2.Tag != null && menu2.Tag.ToString() == "FixSize") { menu2.Header = this._isFixSize ? "解除尺寸锁定" : "锁定尺寸"; } } this.IsFixSize = !this._isFixSize; }) }); ContextMenus.Add(new Separator()); ContextMenus.Add(new MenuItem { Header = "向上一层", Command = new RelayCommand(() => { if (this._isSelected) { this.z_temp++; } else { this.Z++; } } ) }); ContextMenus.Add(new MenuItem { Header = "向下一层", Command = new RelayCommand(() => { if (this._isSelected) { this.z_temp--; if (this.z_temp < 0) this.z_temp = 0; } else { this._z--; if (this._z < 0) this._z = 0; } }) }); ContextMenus.Add(new Separator { }); var cms = ContextMenus.Where(cm => cm.Visibility == Visibility.Visible).ToList(); foreach (var item in cms) { if (item is Separator) item.Visibility = Visibility.Collapsed; else break; } } #endregion } }