终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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