using Aitex.Core.Common; using Aitex.Core.UI.MVVM; using Aitex.Sorter.UI.Controls.Common; using MECF.Framework.Common.Equipment; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; namespace Aitex.Sorter.UI.Controls { /// /// AtmRobotMultiLP.xaml 的交互逻辑 /// public partial class AtmRobotMultiLP : UserControl, INotifyPropertyChanged { protected readonly int MoveTime = 300; private const int AnimationTimeout = 3000; // seconds public int RotateAngle { get { return (int)GetValue(RotateAngleProperty); } set { SetValue(RotateAngleProperty, value); } } // Using a DependencyProperty as the backing store for RotateAngel. This enables animation, styling, binding, etc... public static readonly DependencyProperty RotateAngleProperty = DependencyProperty.Register("RotateAngel", typeof(int), typeof(AtmRobotMultiLP), new PropertyMetadata(0)); public ModuleName Station { get { return (ModuleName)GetValue(StationProperty); } set { SetValue(StationProperty, value); } } // Using a DependencyProperty as the backing store for Station. This enables animation, styling, binding, etc... public static readonly DependencyProperty StationProperty = DependencyProperty.Register("Station", typeof(ModuleName), typeof(AtmRobotMultiLP), new PropertyMetadata(ModuleName.Robot)); public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc... public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(AtmRobotMultiLP), new PropertyMetadata(null)); public string RobotBladeTarget { get { return (string)GetValue(RobotBladeTargetProperty); } set { SetValue(RobotBladeTargetProperty, value); } } public static readonly DependencyProperty RobotBladeTargetProperty = DependencyProperty.Register("RobotBladeTarget", typeof(string), typeof(AtmRobotMultiLP), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); public Dictionary StationPosition { get { return (Dictionary)GetValue(StationPositionProperty); } set { SetValue(StationPositionProperty, value); } } // Using a DependencyProperty as the backing store for StationPosition. This enables animation, styling, binding, etc... public static readonly DependencyProperty StationPositionProperty = DependencyProperty.Register("StationPosition", typeof(Dictionary), typeof(AtmRobotMultiLP), new PropertyMetadata(null, StationPositionChangedCallback)); public bool ShowDock { get { return (bool)GetValue(ShowDockProperty); } set { SetValue(ShowDockProperty, value); } } // Using a DependencyProperty as the backing store for ShowDock. This enables animation, styling, binding, etc... public static readonly DependencyProperty ShowDockProperty = DependencyProperty.Register("ShowDock", typeof(bool), typeof(AtmRobotMultiLP), new PropertyMetadata(false)); public WaferInfo[] RobotWafers { get { return (WaferInfo[])GetValue(RobotWafersProperty); } set { SetValue(RobotWafersProperty, value); } } // Using a DependencyProperty as the backing store for RobotWafers. This enables animation, styling, binding, etc... public static readonly DependencyProperty RobotWafersProperty = DependencyProperty.Register("RobotWafers", typeof(WaferInfo[]), typeof(AtmRobotMultiLP), new PropertyMetadata(null)); private string CurrentPosition { get; set; } private List menu; public List Menu { get { return menu; } set { menu = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Menu")); } } public ICommand MoveCommand { get { return new DelegateCommand(MoveTo); } } private AnimationQueue queue; public event PropertyChangedEventHandler PropertyChanged; static void StationPositionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { var self = (AtmRobotMultiLP)d; var positions = (Dictionary)e.NewValue; self.Menu = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = x.Key }).ToList(); } public AtmRobotMultiLP() { #if DEBUG System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level = System.Diagnostics.SourceLevels.Critical; #endif InitializeComponent(); root.DataContext = this; queue = new AnimationQueue("Robot Animation"); queue.StatusUpdated += Queue_StatusUpdated; canvas1.Rotate(90); canvas2.Rotate(180); canvas3.Rotate(180); CurrentPosition = ModuleName.System.ToString(); } protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); if (DesignerProperties.GetIsInDesignMode(this)) { return; } if (RobotBladeTarget != null) { queue.EnqueueStatus(new AnimationParameter() { Target = RobotBladeTarget }); } } private void Queue_StatusUpdated(object sender, StatusUpdateArgs e) { LogMsg(string.Format("Target: {0} - {1}", CurrentPosition, e.Parameter.Target)); var nextEvent = new AutoResetEvent(false); var next = new Action(() => { nextEvent.Set(); }); var wait = new Action(() => { if (!nextEvent.WaitOne(AnimationTimeout)) { LogMsg("Aniamtion time out!"); } }); var done = new Action(() => { CurrentPosition = e.Parameter.Target; e.Event.Set(); }); var needMove = CurrentPosition != e.Parameter.Target; if (needMove) { Invoke(() => MoveRobot(e.Parameter.Target, next)); wait(); } done(); } private void MoveRobot(string target, Action onComplete = null) { MoveToStart(CurrentPosition, () => TranslateTo(target, () => RetractArm(() => MoveToStart(target, () => MoveToEnd(target, onComplete))))); } private void RetractArm(Action onComplete = null) { canvas2.Rotate(180, true, 50, 0, 0, onComplete); canvas3.Rotate(180, true); } private void MoveToStart(string station, Action onComplete = null) { var position = StationPosition[station]; canvas1.Rotate(position.StartPosition.Root, true, MoveTime, 0, 0, onComplete); canvas2.Rotate(position.StartPosition.Arm, true, MoveTime); canvas3.Rotate(position.StartPosition.Hand, true, MoveTime); CurrentPosition = station; } private void MoveToEnd(string station, Action onComplete = null) { var position = StationPosition[station]; canvas1.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete); canvas2.Rotate(position.EndPosition.Arm, true, MoveTime); canvas3.Rotate(position.EndPosition.Hand, true, MoveTime); CurrentPosition = station; } private void TranslateTo(string station, Action onComplete = null) { var position = StationPosition[station]; if (position.StartPosition.X.HasValue) { Translate(position.StartPosition.X.Value, onComplete); } else { onComplete?.Invoke(); } } private void Translate(int x, Action onComplete = null) { var storyBoard = new Storyboard(); var oldX = translate.X; var animation = new DoubleAnimation(oldX, x, TimeSpan.FromMilliseconds(MoveTime)); storyBoard.Children.Add(animation); Storyboard.SetTarget(animation, root); var propertyChain = new DependencyProperty[] { Canvas.RenderTransformProperty, TransformGroup.ChildrenProperty, TranslateTransform.XProperty }; string thePath = "(0).(1)[1].(2)"; PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); Storyboard.SetTargetProperty(animation, myPropertyPath); storyBoard.Completed += (s, e) => { //InvalidateVisual(); if (onComplete != null) { onComplete(); } }; storyBoard.Begin(); } private void MoveTo(string target) { MoveRobot(target); } private void Invoke(Action action) { Dispatcher.Invoke(action); } private void LogMsg(string msg) { var source = "Robot"; Console.WriteLine("{0} {1}", source, msg); } } public enum BladeStatusMultiLP { Extend, Retract } public class RobotPosition { public int? X; public int Root; public int Arm; public int Hand; } public class StationPosition { public RobotPosition StartPosition; public RobotPosition EndPosition; } }