MfcControl.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.Core.Common.DeviceData;
  15. using Aitex.Core.UI.ControlDataContext;
  16. using MECF.Framework.Common.OperationCenter;
  17. namespace Aitex.Core.UI.Control
  18. {
  19. /// <summary>
  20. /// MfcControl.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MfcControl : UserControl
  23. {
  24. public MfcControl()
  25. {
  26. InitializeComponent();
  27. }
  28. // define dependency properties
  29. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  30. "Command", typeof(ICommand), typeof(MfcControl),
  31. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  32. public static readonly DependencyProperty DeviceDataProperty = DependencyProperty.Register(
  33. "DeviceData", typeof(AITMfcData), typeof(MfcControl),
  34. new FrameworkPropertyMetadata(new AITMfcData(), FrameworkPropertyMetadataOptions.AffectsRender));
  35. public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register(
  36. "BackColor", typeof(Brush), typeof(MfcControl),
  37. new FrameworkPropertyMetadata(Brushes.Green, FrameworkPropertyMetadataOptions.AffectsRender));
  38. public static readonly DependencyProperty IsAutoModeProperty = DependencyProperty.Register(
  39. "IsAutoMode", typeof(bool), typeof(MfcControl),
  40. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  41. public bool IsAutoMode
  42. {
  43. get
  44. {
  45. return (bool)this.GetValue(IsAutoModeProperty);
  46. }
  47. set
  48. {
  49. this.SetValue(IsAutoModeProperty, value);
  50. }
  51. }
  52. /// <summary>
  53. /// 输入值是否百分比,默认否
  54. /// </summary>
  55. public bool IsPercent { get; set; }
  56. public ICommand Command
  57. {
  58. get
  59. {
  60. return (ICommand)this.GetValue(CommandProperty);
  61. }
  62. set
  63. {
  64. this.SetValue(CommandProperty, value);
  65. }
  66. }
  67. /// <summary>
  68. /// set, get current progress value AnalogDeviceData
  69. /// </summary>
  70. public AITMfcData DeviceData
  71. {
  72. get
  73. {
  74. return (AITMfcData)this.GetValue(DeviceDataProperty);
  75. }
  76. set
  77. {
  78. this.SetValue(DeviceDataProperty, value);
  79. }
  80. }
  81. public Brush BackColor
  82. {
  83. get
  84. {
  85. return (Brush)this.GetValue(BackColorProperty);
  86. }
  87. set
  88. {
  89. this.SetValue(BackColorProperty, value);
  90. }
  91. }
  92. protected override void OnRender(DrawingContext drawingContext)
  93. {
  94. base.OnRender(drawingContext);
  95. //draw background color
  96. rectBkground.Fill = BackColor;
  97. if (DeviceData != null)
  98. {
  99. //draw red board if mfc meets a warning
  100. rectBkground.Stroke = DeviceData.IsWarning ? Brushes.Red : Brushes.Gray;
  101. labelValue.Foreground = DeviceData.IsWarning ? Brushes.Pink : Brushes.LightYellow;
  102. rectBkground.StrokeThickness = DeviceData.IsWarning ? 2 : 1;
  103. if (dialogBox != null)
  104. {
  105. if (IsPercent)
  106. dialogBox.RealValue = (DeviceData.FeedBack * 100).ToString("F1") + "%";
  107. else
  108. dialogBox.RealValue = DeviceData.FeedBack.ToString("F1");
  109. }
  110. }
  111. }
  112. private InputDialogBox dialogBox;
  113. public Window AnalogOwner { get; set; }
  114. private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  115. {
  116. if (IsAutoMode) return;
  117. if (DeviceData == null)
  118. return;
  119. dialogBox = new InputDialogBox
  120. {
  121. CommandDelegate = Execute,
  122. DeviceName = string.Format("{0}: {1}", DeviceData.Type, DeviceData.DisplayName),
  123. DeviceId = DeviceData.DeviceSchematicId,
  124. DefaultValue = DeviceData.DefaultValue,
  125. RealValue = DeviceData.FeedBack.ToString("F1"),
  126. SetPoint = Math.Round(DeviceData.SetPoint, 1),
  127. MaxValue = DeviceData.Scale,
  128. Unit = DeviceData.Unit,
  129. };
  130. dialogBox.IsPercent = IsPercent;
  131. if (IsPercent)
  132. dialogBox.SetPoint = Math.Round(DeviceData.SetPoint * 100.0, 1);
  133. if (AnalogOwner != null)
  134. dialogBox.Owner = AnalogOwner;
  135. dialogBox.Topmost = true;
  136. dialogBox.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  137. dialogBox.FocasAll();
  138. dialogBox.ShowDialog();
  139. dialogBox = null;
  140. }
  141. private void Execute(double value)
  142. {
  143. if (Command == null)
  144. {
  145. InvokeClient.Instance.Service.DoOperation($"{DeviceData.UniqueName}.{AITMfcOperation.Ramp}", value, 0);
  146. return;
  147. }
  148. Command.Execute(new object[] { DeviceData.DeviceName, AITMfcOperation.Ramp, value });
  149. }
  150. private void Grid_MouseEnter(object sender, MouseEventArgs e)
  151. {
  152. if (DeviceData != null)
  153. {
  154. string tooltipValue =
  155. string.Format("{0}:{1}\r\n\r\nID:{2}\r\nScale:{3} {4}\r\nSetPoint:{5} {4}\r\nFeedback:{6} {4}\r\nTolerance:{7}\r\nStatus:{8}",
  156. DeviceData.Type,
  157. DeviceData.DisplayName,
  158. DeviceData.DeviceSchematicId,
  159. DeviceData.Scale,
  160. DeviceData.Unit,
  161. IsPercent ? (DeviceData.SetPoint * 100).ToString("F1") + "%" : DeviceData.SetPoint.ToString("F1"),
  162. IsPercent ? (DeviceData.FeedBack * 100).ToString("F1") + "%" : DeviceData.FeedBack.ToString("F1"),
  163. DeviceData.Scale > 0 ? ((DeviceData.FeedBack - DeviceData.SetPoint) / DeviceData.Scale * 100).ToString("F1") : "0",
  164. DeviceData.IsWarning ? "Tolerance Warning" : "Normal");
  165. ToolTip = tooltipValue;
  166. }
  167. }
  168. }
  169. }