终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

498 行
21 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.MessageCommunication;
  3. using BPASmartClient.MessageCommunication.MsgControl;
  4. using BPASmartClient.MessageName;
  5. using BPASmartClient.MessageName.EnumHelp;
  6. using BPASmartClient.MessageName.发送消息Model;
  7. using BPASmartClient.MessageName.接收消息Model;
  8. using BPASmartClient.MessageName.接收消息Model.滚动线;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.ComponentModel;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Data;
  20. using System.Windows.Documents;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Media.Animation;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Navigation;
  26. using System.Windows.Shapes;
  27. namespace BPASmartClient.SCADAControl.CustomerControls
  28. {
  29. /// <summary>
  30. /// 滚筒线
  31. /// NewConveyorBelt.xaml 的交互逻辑
  32. /// </summary>
  33. public partial class NewConveyorBelt : UserControl, IExecutable
  34. {
  35. #region 临时变量
  36. TextBlock textBlock = null;
  37. Path Path_mp = null;
  38. Path Path_cb = null;
  39. Storyboard storyboard = new Storyboard();
  40. Storyboard storyboard1 = new Storyboard();
  41. #endregion
  42. public NewConveyorBelt()
  43. {
  44. InitializeComponent();
  45. Width = 1200;
  46. Height = 400;
  47. this.SizeChanged += ConveyorBelt_SizeChanged;
  48. ConveyorBeltWidth = 70;
  49. Direction = 0;
  50. StrokeBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  51. StrokeDashArray = new DoubleCollection { 1.5, 1.5 };
  52. StrokeFillBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  53. StrokeThickness = 2;
  54. this.Loaded += NewConveyorBelt_Loaded; ;
  55. }
  56. private void NewConveyorBelt_Loaded(object sender, RoutedEventArgs e)
  57. {
  58. EventReceiveNameList.CollectionChanged += EventNameList_CollectionChanged;
  59. EventSendNameList.CollectionChanged += EventSendNameList_CollectionChanged;
  60. }
  61. private void EventSendNameList_CollectionChanged(object? sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  62. {
  63. if (EventSendNameList.Count > 0)
  64. {
  65. try
  66. {
  67. EventSendNameListStr = JsonConvert.SerializeObject(EventSendNameList);
  68. }
  69. catch (Exception ex)
  70. {
  71. }
  72. }
  73. }
  74. private void EventNameList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  75. {
  76. if (EventReceiveNameList.Count > 0)
  77. {
  78. try
  79. {
  80. EventReceiveNameListStr = JsonConvert.SerializeObject(EventReceiveNameList);
  81. }
  82. catch (Exception ex)
  83. {
  84. }
  85. }
  86. }
  87. public string ControlType => "滚动线";
  88. private bool isExecuteState;
  89. public bool IsExecuteState
  90. {
  91. get { return isExecuteState; }
  92. set
  93. {
  94. isExecuteState = value;
  95. if (IsExecuteState)
  96. {
  97. IsEnabled = true;
  98. Register();
  99. Style = null;
  100. }
  101. }
  102. }
  103. #region 内部函数
  104. public void VisualStateManagerData()
  105. {
  106. storyboard.RepeatBehavior = RepeatBehavior.Forever;
  107. DoubleAnimation dbAscending = new DoubleAnimation
  108. {
  109. From = 0,
  110. To = 100,
  111. Duration = new Duration(new TimeSpan(0, 0, 20)),
  112. };
  113. storyboard.Children.Add(dbAscending);
  114. Storyboard.SetTarget(dbAscending, Path_mp);
  115. Storyboard.SetTargetProperty(dbAscending, new PropertyPath("StrokeDashOffset"));
  116. storyboard1.RepeatBehavior = RepeatBehavior.Forever;
  117. DoubleAnimation dbAscending1 = new DoubleAnimation
  118. {
  119. From = 0,
  120. To = -100,
  121. Duration = new Duration(new TimeSpan(0, 0, 20)),
  122. };
  123. storyboard1.Children.Add(dbAscending1);
  124. Storyboard.SetTarget(dbAscending1, Path_mp);
  125. Storyboard.SetTargetProperty(dbAscending1, new PropertyPath("StrokeDashOffset"));
  126. Refursh();
  127. }
  128. private void ConveyorBelt_SizeChanged(object sender, SizeChangedEventArgs e)
  129. {
  130. //查找
  131. if (!(Path_cb != null && Path_mp != null))
  132. {
  133. foreach (Path tb in FindVisualChildren<Path>(this))
  134. {
  135. // do something with tb here
  136. if (tb.Tag != null)
  137. {
  138. if (tb.Tag.ToString() == "cb")
  139. {
  140. Path_cb = tb;
  141. }
  142. else if (tb.Tag.ToString() == "mp")
  143. {
  144. Path_mp = tb;
  145. }
  146. }
  147. }
  148. VisualStateManagerData();
  149. }
  150. if (textBlock == null)
  151. {
  152. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  153. {
  154. // do something with tb here
  155. if (tb.Tag != null)
  156. {
  157. if (tb.Tag.ToString() == "标题")
  158. {
  159. textBlock = tb;
  160. }
  161. }
  162. }
  163. }
  164. if (this.Height > 0 && this.Width > 0)
  165. {
  166. try
  167. {
  168. //传送带外边框绘制
  169. PathGeometry geometry = new PathGeometry();
  170. PathFigure pathFigure = new PathFigure();
  171. pathFigure.StartPoint = new Point(this.Height / 2, 0);
  172. pathFigure.Segments.Add(new ArcSegment(new Point(this.Height / 2, this.Height), new Size(this.Height / 2, this.Height / 2), 0, false, SweepDirection.Counterclockwise, true));
  173. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height), true));
  174. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth), true));
  175. double innerCircle = (this.Height - (ConveyorBeltWidth * 2)) / 2;//内圆半径
  176. pathFigure.Segments.Add(new LineSegment(new Point(ConveyorBeltWidth + innerCircle, this.Height - ConveyorBeltWidth), true));
  177. pathFigure.Segments.Add(new ArcSegment(new Point(ConveyorBeltWidth + innerCircle, ConveyorBeltWidth), new Size(innerCircle, innerCircle), 0, false, SweepDirection.Clockwise, true));
  178. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, ConveyorBeltWidth), true));
  179. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, 0), true));
  180. pathFigure.Segments.Add(new LineSegment(new Point(this.Height / 2, 0), true));
  181. geometry.Figures.Add(pathFigure);
  182. Path_cb.Data = geometry;
  183. //传送带内部皮带绘制
  184. PathGeometry geometry1 = new PathGeometry();
  185. PathFigure pathFigure1 = new PathFigure();
  186. pathFigure1.StartPoint = new Point(this.Width, ConveyorBeltWidth / 2);
  187. double innerCircle1 = (this.Height - ConveyorBeltWidth) / 2;//内圆半径
  188. pathFigure1.Segments.Add(new LineSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, ConveyorBeltWidth / 2), true));
  189. pathFigure1.Segments.Add(new ArcSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, this.Height - ConveyorBeltWidth / 2), new Size(innerCircle1, innerCircle1), 0, false, SweepDirection.Counterclockwise, true));
  190. pathFigure1.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth / 2), true));
  191. geometry1.Figures.Add(pathFigure1);
  192. Path_mp.Data = geometry1;
  193. }
  194. catch (Exception ex)
  195. {
  196. }
  197. }
  198. }
  199. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  200. {
  201. if (depObj != null)
  202. {
  203. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  204. {
  205. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  206. if (child != null && child is T)
  207. {
  208. yield return (T)child;
  209. }
  210. foreach (T childOfChild in FindVisualChildren<T>(child))
  211. {
  212. yield return childOfChild;
  213. }
  214. }
  215. }
  216. }
  217. #endregion
  218. #region 属性
  219. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  220. {
  221. (d as NewConveyorBelt)?.Refursh();
  222. }
  223. public DoubleCollection StrokeDashArray
  224. {
  225. get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); }
  226. set { SetValue(StrokeDashArrayProperty, value); }
  227. }
  228. public static readonly DependencyProperty StrokeDashArrayProperty =
  229. DependencyProperty.Register("StrokeDashArray", typeof(DoubleCollection), typeof(NewConveyorBelt),
  230. new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  231. public double ConveyorBeltWidth
  232. {
  233. get { return (double)GetValue(ConveyorBeltWidthProperty); }
  234. set { SetValue(ConveyorBeltWidthProperty, value); }
  235. }
  236. public static readonly DependencyProperty ConveyorBeltWidthProperty =
  237. DependencyProperty.Register("ConveyorBeltWidth", typeof(double), typeof(NewConveyorBelt),
  238. new PropertyMetadata(50.0, new PropertyChangedCallback(OnPropertyChanged)));
  239. public Brush StrokeFillBrush
  240. {
  241. get { return (Brush)GetValue(StrokeFillBrushProperty); }
  242. set { SetValue(StrokeFillBrushProperty, value); }
  243. }
  244. public static readonly DependencyProperty StrokeFillBrushProperty =
  245. DependencyProperty.Register("StrokeFillBrush", typeof(Brush), typeof(NewConveyorBelt),
  246. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  247. public Brush StrokeBrush
  248. {
  249. get { return (Brush)GetValue(StrokeBrushProperty); }
  250. set { SetValue(StrokeBrushProperty, value); }
  251. }
  252. public static readonly DependencyProperty StrokeBrushProperty =
  253. DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(NewConveyorBelt),
  254. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  255. public double StrokeThickness
  256. {
  257. get { return (double)GetValue(StrokeThicknessProperty); }
  258. set { SetValue(StrokeThicknessProperty, value); }
  259. }
  260. public static readonly DependencyProperty StrokeThicknessProperty =
  261. DependencyProperty.Register("StrokeThickness", typeof(double), typeof(NewConveyorBelt),
  262. new PropertyMetadata(1.0, new PropertyChangedCallback(OnPropertyChanged)));
  263. [Category("值设定")]
  264. public int Direction
  265. {
  266. get { return (int)GetValue(DirectionProperty); }
  267. set { SetValue(DirectionProperty, value); }
  268. }
  269. public static readonly DependencyProperty DirectionProperty =
  270. DependencyProperty.Register("Direction", typeof(int), typeof(NewConveyorBelt),
  271. new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
  272. [Category("值设定")]
  273. public string Text
  274. {
  275. get { return (string)GetValue(TextProperty); }
  276. set { SetValue(TextProperty, value); }
  277. }
  278. public static readonly DependencyProperty TextProperty =
  279. DependencyProperty.Register("Text", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(new PropertyChangedCallback(onTargetChanged)));
  280. private static void onTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.TargetRefresh();
  281. [Category("主题订阅")]
  282. public string ConveyorBeltLeft
  283. {
  284. get { return (string)GetValue(ConveyorBeltLeftProperty); }
  285. set { SetValue(ConveyorBeltLeftProperty, value); }
  286. }
  287. public static readonly DependencyProperty ConveyorBeltLeftProperty =
  288. DependencyProperty.Register("ConveyorBeltLeft", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  289. [Category("主题订阅")]
  290. public string ConveyorBeltRight
  291. {
  292. get { return (string)GetValue(ConveyorBeltRightProperty); }
  293. set { SetValue(ConveyorBeltRightProperty, value); }
  294. }
  295. public static readonly DependencyProperty ConveyorBeltRightProperty =
  296. DependencyProperty.Register("ConveyorBeltRight", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  297. [Category("主题订阅")]
  298. public string ConveyorBeltStop
  299. {
  300. get { return (string)GetValue(ConveyorBeltStopProperty); }
  301. set { SetValue(ConveyorBeltStopProperty, value); }
  302. }
  303. public static readonly DependencyProperty ConveyorBeltStopProperty =
  304. DependencyProperty.Register("ConveyorBeltStop", typeof(string), typeof(NewConveyorBelt), new PropertyMetadata(string.Empty));
  305. [Category("名称[自动生成]")]
  306. public string EventReceiveNameListStr
  307. {
  308. get { return (string)GetValue(EventReceiveNameListStrProperty); }
  309. set { SetValue(EventReceiveNameListStrProperty, value); }
  310. }
  311. public static readonly DependencyProperty EventReceiveNameListStrProperty =
  312. DependencyProperty.Register("EventReceiveNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onEventReceiveNameListStrChanged)));
  313. private static void onEventReceiveNameListStrChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.ReceiveNameRefresh();
  314. [Category("消息接收名称集合")]
  315. public ObservableCollection<EventReceiveMessage> EventReceiveNameList
  316. {
  317. get { return (ObservableCollection<EventReceiveMessage>)GetValue(EventReceiveNameListProperty); }
  318. set { SetValue(EventReceiveNameListProperty, value); }
  319. }
  320. public static readonly DependencyProperty EventReceiveNameListProperty =
  321. DependencyProperty.Register("EventReceiveNameList", typeof(ObservableCollection<EventReceiveMessage>), typeof(NewConveyorBelt), new PropertyMetadata(new ObservableCollection<EventReceiveMessage>(), onEventNameListChanged));
  322. private static void onEventNameListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as NewConveyorBelt)?.RunNameRefresh();
  323. [Category("名称[自动生成]")]
  324. private string EventSendNameListStr
  325. {
  326. get { return (string)GetValue(EventSendNameListStrProperty); }
  327. set { SetValue(EventSendNameListStrProperty,value); }
  328. }
  329. private static readonly DependencyProperty EventSendNameListStrProperty =
  330. DependencyProperty.Register("EventSendNameListStr",typeof(string),typeof(NewConveyorBelt),new PropertyMetadata(string.Empty));
  331. [Category("消息发送名称集合")]
  332. private ObservableCollection<EventSendMessage> EventSendNameList
  333. {
  334. get { return (ObservableCollection<EventSendMessage>)GetValue(EventSendNameListProperty); }
  335. set { SetValue(EventSendNameListProperty,value); }
  336. }
  337. private static readonly DependencyProperty EventSendNameListProperty =
  338. DependencyProperty.Register("EventSendNameList",typeof(ObservableCollection<EventSendMessage>),typeof(NewConveyorBelt),new PropertyMetadata(new ObservableCollection<EventSendMessage>(),new PropertyChangedCallback(onEventNameListChanged)));
  339. #endregion
  340. #region 属性变更事件
  341. List<string> MessageNameL = null;
  342. public void Register()
  343. {
  344. if (!string.IsNullOrEmpty(EventReceiveNameListStr))
  345. {
  346. try
  347. {
  348. EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
  349. }
  350. catch (Exception ex)
  351. {
  352. }
  353. }
  354. if (!string.IsNullOrEmpty(EventSendNameListStr))
  355. {
  356. try
  357. {
  358. EventSendNameList = JsonConvert.DeserializeObject<ObservableCollection<EventSendMessage>>(EventSendNameListStr);
  359. }
  360. catch (Exception ex)
  361. {
  362. }
  363. }
  364. List<string> MessageNameNew = EventReceiveNameList?.Select(o => o.MeaageName.ToString())?.Distinct()?.ToList();
  365. if (MessageNameL == null || !MessageNameNew.SequenceEqual(MessageNameL))
  366. {
  367. MessageNameL?.ForEach(o => Class_InnerMessageBus.GetInstance().RemoveMessage(o,"NewConveyorBeltEventHandler"));
  368. MessageNameNew?.ForEach(x => Class_InnerMessageBus.GetInstance().ListenMessage(this,x,"NewConveyorBeltEventHandler"));
  369. MessageNameL = MessageNameNew;
  370. }
  371. }
  372. public void NewConveyorBeltEventHandler(object sender, InnerMessageEventArgs e)
  373. {
  374. try
  375. {
  376. if (IsExecuteState = true)
  377. {
  378. if (e.obj_MessageObj is RunEnumModel)//接收到运行消息
  379. {
  380. RunEnumModel mode = (RunEnumModel)e.obj_MessageObj;
  381. var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  382. //必对消息号:
  383. if (msg != null)
  384. {
  385. Direction = mode.Run == RunEnum.Run ? 1 : ((mode.Run == RunEnum.Run_Left) ? 2 : 0);
  386. }
  387. }
  388. else if (e.obj_MessageObj is ConveyorBeltMessageModel)//接收到数据模型
  389. {
  390. ConveyorBeltMessageModel mode = (ConveyorBeltMessageModel)e.obj_MessageObj;
  391. var msg = EventReceiveNameList?.ToList().Find(par => par.MeaageName.ToString() == e.str_MessageStr && par.MeaageID == mode.MessageID);
  392. //必对消息号:
  393. if (msg != null)
  394. {
  395. Text = mode.Title;
  396. }
  397. }
  398. }
  399. }
  400. catch (Exception ex)
  401. {
  402. }
  403. }
  404. private void Refursh()
  405. {
  406. if (Direction == 1)
  407. {
  408. storyboard.Begin();
  409. storyboard1.Stop();
  410. }
  411. else if (Direction == 2)
  412. {
  413. storyboard.Stop();
  414. storyboard1.Begin();
  415. }
  416. else
  417. {
  418. storyboard.Stop();
  419. storyboard1.Stop();
  420. }
  421. }
  422. /// <summary>
  423. /// 目标属性刷新
  424. /// </summary>
  425. public void TargetRefresh()
  426. {
  427. if (textBlock != null )
  428. {
  429. textBlock.Text = Text;
  430. }
  431. }
  432. public void RunNameRefresh()
  433. {
  434. }
  435. /// <summary>
  436. /// 接收数据改变
  437. /// </summary>
  438. public void ReceiveNameRefresh()
  439. {
  440. if (!string.IsNullOrEmpty(EventReceiveNameListStr))
  441. {
  442. try
  443. {
  444. EventReceiveNameList = JsonConvert.DeserializeObject<ObservableCollection<EventReceiveMessage>>(EventReceiveNameListStr);
  445. }
  446. catch (Exception ex)
  447. {
  448. }
  449. }
  450. }
  451. #endregion
  452. }
  453. }