终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

278 líneas
11 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace BPASmartClient.SCADAControl
  18. {
  19. /// <summary>
  20. /// NewConveyorBelt.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class NewConveyorBelt : UserControl, IExecutable
  23. {
  24. Path Path_mp = null;
  25. Path Path_cb = null;
  26. Storyboard storyboard = new Storyboard();
  27. Storyboard storyboard1 = new Storyboard();
  28. public NewConveyorBelt()
  29. {
  30. InitializeComponent();
  31. Width = 1200;
  32. Height = 400;
  33. this.SizeChanged += ConveyorBelt_SizeChanged;
  34. ConveyorBeltWidth = 70;
  35. Direction = 0;
  36. StrokeBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  37. StrokeDashArray = new DoubleCollection { 1.5, 1.5 };
  38. StrokeFillBrush = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#00BEFA"));
  39. StrokeThickness = 2;
  40. }
  41. public string ControlType => "滚动线";
  42. private bool isExecuteState;
  43. public bool IsExecuteState
  44. {
  45. get { return isExecuteState; }
  46. set
  47. {
  48. isExecuteState = value;
  49. if (IsExecuteState)
  50. {
  51. IsEnabled = true;
  52. Register();
  53. Style = null;
  54. }
  55. }
  56. }
  57. public void Register()
  58. {
  59. }
  60. public void VisualStateManagerData()
  61. {
  62. storyboard.RepeatBehavior = RepeatBehavior.Forever;
  63. DoubleAnimation dbAscending = new DoubleAnimation
  64. {
  65. From = 0,
  66. To = 100,
  67. Duration = new Duration(new TimeSpan(0, 0, 20)),
  68. };
  69. storyboard.Children.Add(dbAscending);
  70. Storyboard.SetTarget(dbAscending, Path_mp);
  71. Storyboard.SetTargetProperty(dbAscending, new PropertyPath("StrokeDashOffset"));
  72. storyboard1.RepeatBehavior = RepeatBehavior.Forever;
  73. DoubleAnimation dbAscending1 = new DoubleAnimation
  74. {
  75. From = 0,
  76. To = -100,
  77. Duration = new Duration(new TimeSpan(0, 0, 20)),
  78. };
  79. storyboard1.Children.Add(dbAscending1);
  80. Storyboard.SetTarget(dbAscending1, Path_mp);
  81. Storyboard.SetTargetProperty(dbAscending1, new PropertyPath("StrokeDashOffset"));
  82. Refursh();
  83. }
  84. private void ConveyorBelt_SizeChanged(object sender, SizeChangedEventArgs e)
  85. {
  86. //查找
  87. if (!(Path_cb != null && Path_mp != null))
  88. {
  89. foreach (Path tb in FindVisualChildren<Path>(this))
  90. {
  91. // do something with tb here
  92. if (tb.Tag != null)
  93. {
  94. if (tb.Tag.ToString() == "cb")
  95. {
  96. Path_cb = tb;
  97. }
  98. else if (tb.Tag.ToString() == "mp")
  99. {
  100. Path_mp = tb;
  101. }
  102. }
  103. }
  104. VisualStateManagerData();
  105. }
  106. //传送带外边框绘制
  107. PathGeometry geometry = new PathGeometry();
  108. PathFigure pathFigure = new PathFigure();
  109. pathFigure.StartPoint = new Point(this.Height / 2, 0);
  110. 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));
  111. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height), true));
  112. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth), true));
  113. double innerCircle = (this.Height - (ConveyorBeltWidth * 2)) / 2;//内圆半径
  114. pathFigure.Segments.Add(new LineSegment(new Point(ConveyorBeltWidth + innerCircle, this.Height - ConveyorBeltWidth), true));
  115. pathFigure.Segments.Add(new ArcSegment(new Point(ConveyorBeltWidth + innerCircle, ConveyorBeltWidth), new Size(innerCircle, innerCircle), 0, false, SweepDirection.Clockwise, true));
  116. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, ConveyorBeltWidth), true));
  117. pathFigure.Segments.Add(new LineSegment(new Point(this.Width, 0), true));
  118. pathFigure.Segments.Add(new LineSegment(new Point(this.Height / 2, 0), true));
  119. geometry.Figures.Add(pathFigure);
  120. Path_cb.Data = geometry;
  121. //传送带内部皮带绘制
  122. PathGeometry geometry1 = new PathGeometry();
  123. PathFigure pathFigure1 = new PathFigure();
  124. pathFigure1.StartPoint = new Point(this.Width, ConveyorBeltWidth / 2);
  125. double innerCircle1 = (this.Height - ConveyorBeltWidth) / 2;//内圆半径
  126. pathFigure1.Segments.Add(new LineSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, ConveyorBeltWidth / 2), true));
  127. pathFigure1.Segments.Add(new ArcSegment(new Point(innerCircle1 + ConveyorBeltWidth / 2, this.Height - ConveyorBeltWidth / 2), new Size(innerCircle1, innerCircle1), 0, false, SweepDirection.Counterclockwise, true));
  128. pathFigure1.Segments.Add(new LineSegment(new Point(this.Width, this.Height - ConveyorBeltWidth / 2), true));
  129. geometry1.Figures.Add(pathFigure1);
  130. Path_mp.Data = geometry1;
  131. }
  132. /// <summary>
  133. /// 搜索指定名称的子元素
  134. /// </summary>
  135. /// <typeparam name="T"></typeparam>
  136. /// <param name="obj"></param>
  137. /// <param name="name"></param>
  138. /// <returns></returns>
  139. public static T FindChild<T>(DependencyObject obj, string name) where T : FrameworkElement
  140. {
  141. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  142. {
  143. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  144. if (child != null && child is T && (child as T).Name.Equals(name))
  145. return (T)child;
  146. else
  147. {
  148. T childOfChild = FindChild<T>(child, name);
  149. if (childOfChild != null)
  150. return childOfChild;
  151. }
  152. }
  153. return null;
  154. }
  155. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  156. {
  157. if (depObj != null)
  158. {
  159. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  160. {
  161. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  162. if (child != null && child is T)
  163. {
  164. yield return (T)child;
  165. }
  166. foreach (T childOfChild in FindVisualChildren<T>(child))
  167. {
  168. yield return childOfChild;
  169. }
  170. }
  171. }
  172. }
  173. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  174. {
  175. (d as NewConveyorBelt)?.Refursh();
  176. }
  177. private void Refursh()
  178. {
  179. if (Direction == 1)
  180. {
  181. storyboard.Begin();
  182. storyboard1.Stop();
  183. // VisualStateManager.GoToState(this, "Left", false);
  184. }
  185. else if (Direction == 2)
  186. {
  187. storyboard.Stop();
  188. storyboard1.Begin();
  189. //VisualStateManager.GoToState(this, "Right", false);
  190. }
  191. else
  192. {
  193. storyboard.Stop();
  194. storyboard1.Stop();
  195. //VisualStateManager.GoToState(this, "Stop", false);
  196. }
  197. }
  198. public DoubleCollection StrokeDashArray
  199. {
  200. get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); }
  201. set { SetValue(StrokeDashArrayProperty, value); }
  202. }
  203. public static readonly DependencyProperty StrokeDashArrayProperty =
  204. DependencyProperty.Register("StrokeDashArray", typeof(DoubleCollection), typeof(NewConveyorBelt),
  205. new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
  206. public double ConveyorBeltWidth
  207. {
  208. get { return (double)GetValue(ConveyorBeltWidthProperty); }
  209. set { SetValue(ConveyorBeltWidthProperty, value); }
  210. }
  211. public static readonly DependencyProperty ConveyorBeltWidthProperty =
  212. DependencyProperty.Register("ConveyorBeltWidth", typeof(double), typeof(NewConveyorBelt),
  213. new PropertyMetadata(50.0, new PropertyChangedCallback(OnPropertyChanged)));
  214. public Brush StrokeFillBrush
  215. {
  216. get { return (Brush)GetValue(StrokeFillBrushProperty); }
  217. set { SetValue(StrokeFillBrushProperty, value); }
  218. }
  219. public static readonly DependencyProperty StrokeFillBrushProperty =
  220. DependencyProperty.Register("StrokeFillBrush", typeof(Brush), typeof(NewConveyorBelt),
  221. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  222. public Brush StrokeBrush
  223. {
  224. get { return (Brush)GetValue(StrokeBrushProperty); }
  225. set { SetValue(StrokeBrushProperty, value); }
  226. }
  227. public static readonly DependencyProperty StrokeBrushProperty =
  228. DependencyProperty.Register("StrokeBrush", typeof(Brush), typeof(NewConveyorBelt),
  229. new PropertyMetadata(Brushes.LightBlue, new PropertyChangedCallback(OnPropertyChanged)));
  230. public double StrokeThickness
  231. {
  232. get { return (double)GetValue(StrokeThicknessProperty); }
  233. set { SetValue(StrokeThicknessProperty, value); }
  234. }
  235. public static readonly DependencyProperty StrokeThicknessProperty =
  236. DependencyProperty.Register("StrokeThickness", typeof(double), typeof(NewConveyorBelt),
  237. new PropertyMetadata(1.0, new PropertyChangedCallback(OnPropertyChanged)));
  238. [Category("值设定")]
  239. public int Direction
  240. {
  241. get { return (int)GetValue(DirectionProperty); }
  242. set { SetValue(DirectionProperty, value); }
  243. }
  244. public static readonly DependencyProperty DirectionProperty =
  245. DependencyProperty.Register("Direction", typeof(int), typeof(NewConveyorBelt),
  246. new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
  247. }
  248. }