AtmRobot.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using Aitex.Core.Common;
  2. using Aitex.Core.UI.MVVM;
  3. using Aitex.Sorter.UI.Controls;
  4. using Aitex.Sorter.UI.Controls.Common;
  5. using MECF.Framework.Common.CommonData;
  6. using MECF.Framework.Common.Equipment;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Animation;
  17. namespace EfemUI.Controls
  18. {
  19. /// <summary>
  20. /// AtmRobot.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class AtmRobot : UserControl, INotifyPropertyChanged
  23. {
  24. protected readonly int MoveTime = 300;
  25. private const int AnimationTimeout = 3000; // seconds
  26. public bool IsEnableTextMenu
  27. {
  28. get { return (bool)GetValue(IsEnableTextMenuProperty); }
  29. set { SetValue(IsEnableTextMenuProperty, value); }
  30. }
  31. // Using a DependencyProperty as the backing store for IsEnableTextMenu. This enables animation, styling, binding, etc...
  32. public static readonly DependencyProperty IsEnableTextMenuProperty =
  33. DependencyProperty.Register("IsEnableTextMenu", typeof(bool), typeof(AtmRobot), new PropertyMetadata(true));
  34. public int RotateAngle
  35. {
  36. get { return (int)GetValue(RotateAngleProperty); }
  37. set { SetValue(RotateAngleProperty, value); }
  38. }
  39. // Using a DependencyProperty as the backing store for RotateAngel. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty RotateAngleProperty =
  41. DependencyProperty.Register("RotateAngel", typeof(int), typeof(AtmRobot), new PropertyMetadata(0));
  42. public ModuleName Station
  43. {
  44. get { return (ModuleName)GetValue(StationProperty); }
  45. set { SetValue(StationProperty, value); }
  46. }
  47. // Using a DependencyProperty as the backing store for Station. This enables animation, styling, binding, etc...
  48. public static readonly DependencyProperty StationProperty =
  49. DependencyProperty.Register("Station", typeof(ModuleName), typeof(AtmRobot), new PropertyMetadata(ModuleName.Robot));
  50. public ICommand Command
  51. {
  52. get { return (ICommand)GetValue(CommandProperty); }
  53. set { SetValue(CommandProperty, value); }
  54. }
  55. // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc...
  56. public static readonly DependencyProperty CommandProperty =
  57. DependencyProperty.Register("Command", typeof(ICommand), typeof(AtmRobot), new PropertyMetadata(null));
  58. public string RobotBladeTarget
  59. {
  60. get { return (string)GetValue(RobotBladeTargetProperty); }
  61. set { SetValue(RobotBladeTargetProperty, value); }
  62. }
  63. public static readonly DependencyProperty RobotBladeTargetProperty =
  64. DependencyProperty.Register("RobotBladeTarget", typeof(string), typeof(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  65. public string RobotBlade2Target
  66. {
  67. get { return (string)GetValue(RobotBlade2TargetProperty); }
  68. set { SetValue(RobotBlade2TargetProperty, value); }
  69. }
  70. public static readonly DependencyProperty RobotBlade2TargetProperty =
  71. DependencyProperty.Register("RobotBlade2Target", typeof(string), typeof(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  72. public Dictionary<string, StationPosition> StationPosition
  73. {
  74. get { return (Dictionary<string, StationPosition>)GetValue(StationPositionProperty); }
  75. set { SetValue(StationPositionProperty, value); }
  76. }
  77. // Using a DependencyProperty as the backing store for StationPosition2. This enables animation, styling, binding, etc...
  78. public static readonly DependencyProperty StationPositionProperty =
  79. DependencyProperty.Register("StationPosition", typeof(Dictionary<string, StationPosition>), typeof(AtmRobot), new PropertyMetadata(null, StationPositionChangedCallback));
  80. public bool ShowDock
  81. {
  82. get { return (bool)GetValue(ShowDockProperty); }
  83. set { SetValue(ShowDockProperty, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for ShowDock. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty ShowDockProperty =
  87. DependencyProperty.Register("ShowDock", typeof(bool), typeof(AtmRobot), new PropertyMetadata(false));
  88. public WaferInfo Wafer1
  89. {
  90. get { return (WaferInfo)GetValue(Wafer1Property); }
  91. set { SetValue(Wafer1Property, value); }
  92. }
  93. // Using a DependencyProperty as the backing store for Wafer1. This enables animation, styling, binding, etc...
  94. public static readonly DependencyProperty Wafer1Property =
  95. DependencyProperty.Register("Wafer1", typeof(WaferInfo), typeof(AtmRobot), new PropertyMetadata(null));
  96. public RobotMoveInfo MoveInfo
  97. {
  98. get { return (RobotMoveInfo)GetValue(MoveInfoProperty); }
  99. set { SetValue(MoveInfoProperty, value); }
  100. }
  101. public static readonly DependencyProperty MoveInfoProperty =
  102. DependencyProperty.Register("MoveInfo", typeof(RobotMoveInfo), typeof(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
  103. public WaferInfo Wafer2
  104. {
  105. get { return (WaferInfo)GetValue(Wafer2Property); }
  106. set { SetValue(Wafer2Property, value); }
  107. }
  108. // Using a DependencyProperty as the backing store for Wafer2. This enables animation, styling, binding, etc...
  109. public static readonly DependencyProperty Wafer2Property =
  110. DependencyProperty.Register("Wafer2", typeof(WaferInfo), typeof(AtmRobot), new PropertyMetadata(null));
  111. private string CurrentPosition
  112. {
  113. get; set;
  114. }
  115. private List<MenuItem> menu;
  116. public List<MenuItem> Menu
  117. {
  118. get
  119. {
  120. return menu;
  121. }
  122. set
  123. {
  124. menu = value;
  125. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Menu"));
  126. }
  127. }
  128. public ICommand MoveCommand
  129. {
  130. get
  131. {
  132. return new DelegateCommand<string>(MoveTo);
  133. }
  134. }
  135. private AnimationQueue queue;
  136. public event PropertyChangedEventHandler PropertyChanged;
  137. static void StationPositionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  138. {
  139. var self = (AtmRobot)d;
  140. var positions = (Dictionary<string, StationPosition>)e.NewValue;
  141. self.Menu = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = x.Key }).ToList();
  142. }
  143. public AtmRobot()
  144. {
  145. #if DEBUG
  146. System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical;
  147. #endif
  148. InitializeComponent();
  149. root.DataContext = this;
  150. queue = new AnimationQueue("Robot Animation");
  151. queue.StatusUpdated += Queue_StatusUpdated;
  152. CurrentPosition = "ArmA.System";
  153. canvas1.Rotate(155);
  154. canvas2.Rotate(240);
  155. canvas3.Rotate(236);
  156. canvas21.Rotate(25);
  157. canvas22.Rotate(120);
  158. canvas23.Rotate(484);
  159. }
  160. protected override void OnRender(DrawingContext drawingContext)
  161. {
  162. base.OnRender(drawingContext);
  163. if (DesignerProperties.GetIsInDesignMode(this))
  164. {
  165. return;
  166. }
  167. //if (RobotBladeTarget != null)
  168. //{
  169. // queue.EnqueueStatus(new AnimationParameter() { Target = RobotBladeTarget });
  170. //}
  171. //if (RobotBlade2Target != null)
  172. //{
  173. // queue.EnqueueStatus(new AnimationParameter() { Target = RobotBlade2Target });
  174. //}
  175. if (MoveInfo != null)
  176. {
  177. queue.EnqueueStatus(new AnimationParameter() { Target = MoveInfo.BladeTarget});// Arm = MoveInfo.ArmTarget.ToString();
  178. }
  179. }
  180. private void Queue_StatusUpdated(object sender, StatusUpdateArgs e)
  181. {
  182. //string TargetPosition = CurrentPosition.Split('.')[0] + "." + e.Parameter.Target;
  183. string TargetPosition = e.Parameter.Target;
  184. LogMsg(string.Format("Target: {0} - {1}", CurrentPosition, TargetPosition));
  185. var nextEvent = new AutoResetEvent(false);
  186. var next = new Action(() =>
  187. {
  188. nextEvent.Set();
  189. });
  190. var wait = new Action(() =>
  191. {
  192. if (!nextEvent.WaitOne(AnimationTimeout))
  193. {
  194. LogMsg("Aniamtion time out!");
  195. }
  196. });
  197. var done = new Action(() =>
  198. {
  199. CurrentPosition = TargetPosition;
  200. e.Event.Set();
  201. });
  202. var needMove = CurrentPosition != TargetPosition;
  203. if (needMove)
  204. {
  205. Invoke(() => MoveRobot(TargetPosition, next));
  206. wait();
  207. }
  208. done();
  209. }
  210. private void MoveRobot(string target, Action onComplete = null)
  211. {
  212. MoveToStart(CurrentPosition, () => TranslateTo(target, () =>
  213. //RetractArm(() =>
  214. MoveToStart(target, () => MoveToEnd(target, onComplete))));
  215. }
  216. private void RetractArm(Action onComplete = null)
  217. {
  218. canvas2.Rotate(180, true, 50, 0, 0, onComplete);
  219. canvas3.Rotate(180, true);
  220. canvas2.Rotate(180, true);
  221. canvas3.Rotate(180, true);
  222. }
  223. private void MoveToStart(string station, Action onComplete = null)
  224. {
  225. //var position = StationPosition[station];
  226. StationPosition positionA = station.Contains("ArmA") ? StationPosition[station] : StationPosition[station.Replace("ArmB", "ArmA")];
  227. StationPosition positionB = station.Contains("ArmB") ? StationPosition[station] : StationPosition[station.Replace("ArmA", "ArmB")];
  228. canvas1.Rotate(positionA.StartPosition.Root, true, MoveTime, 0, 0, onComplete);
  229. canvas2.Rotate(positionA.StartPosition.Arm, true, MoveTime);
  230. canvas3.Rotate(positionA.StartPosition.Hand, true, MoveTime);
  231. canvas21.Rotate(positionB.StartPosition.Root, true, MoveTime);
  232. canvas22.Rotate(positionB.StartPosition.Arm, true, MoveTime);
  233. canvas23.Rotate(positionB.StartPosition.Hand, true, MoveTime);
  234. CurrentPosition = station;
  235. }
  236. private void MoveToEnd(string station, Action onComplete = null)
  237. {
  238. var position = StationPosition[station];
  239. if (station.Contains("ArmA"))
  240. {
  241. canvas1.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);
  242. canvas2.Rotate(position.EndPosition.Arm, true, MoveTime);
  243. canvas3.Rotate(position.EndPosition.Hand, true, MoveTime);
  244. }
  245. if (station.Contains("ArmB"))
  246. {
  247. canvas21.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);
  248. canvas22.Rotate(position.EndPosition.Arm, true, MoveTime);
  249. canvas23.Rotate(position.EndPosition.Hand, true, MoveTime);
  250. }
  251. CurrentPosition = station;
  252. }
  253. private void TranslateTo(string station, Action onComplete = null)
  254. {
  255. var position = StationPosition[station];
  256. if (position.StartPosition.X.HasValue)
  257. {
  258. Translate(position.StartPosition.X.Value, onComplete);
  259. }
  260. else
  261. {
  262. onComplete?.Invoke();
  263. }
  264. }
  265. private void Translate(int x, Action onComplete = null)
  266. {
  267. var storyBoard = new Storyboard();
  268. var oldX = translate.X;
  269. var animation = new DoubleAnimation(oldX, x, TimeSpan.FromMilliseconds(MoveTime));
  270. storyBoard.Children.Add(animation);
  271. Storyboard.SetTarget(animation, root);
  272. var propertyChain = new DependencyProperty[]
  273. {
  274. Canvas.RenderTransformProperty,
  275. TransformGroup.ChildrenProperty,
  276. TranslateTransform.XProperty
  277. };
  278. string thePath = "(0).(1)[1].(2)";
  279. PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain);
  280. Storyboard.SetTargetProperty(animation, myPropertyPath);
  281. storyBoard.Completed += (s, e) =>
  282. {
  283. //InvalidateVisual();
  284. if (onComplete != null)
  285. {
  286. onComplete();
  287. }
  288. };
  289. storyBoard.Begin();
  290. }
  291. private void MoveTo(string target)
  292. {
  293. MoveRobot(target);
  294. }
  295. private void Invoke(Action action)
  296. {
  297. Dispatcher.Invoke(action);
  298. }
  299. private void LogMsg(string msg)
  300. {
  301. var source = "Robot";
  302. Console.WriteLine("{0} {1}", source, msg);
  303. }
  304. }
  305. //public enum BladeStatusLP
  306. //{
  307. // Extend,
  308. // Retract
  309. //}
  310. //public class Robot2Position
  311. //{
  312. // public int? X;
  313. // public int Root;
  314. // public int Arm;
  315. // public int Hand;
  316. //}
  317. //public class StationPosition2
  318. //{
  319. // public Robot2Position StartPosition;
  320. // public Robot2Position EndPosition;
  321. //}
  322. }