| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 | using Aitex.Core.Common;using Aitex.Core.UI.MVVM;using Aitex.Sorter.UI.Controls;using Aitex.Sorter.UI.Controls.Common;using MECF.Framework.Common.CommonData;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 EfemUI.Controls{    /// <summary>    /// AtmRobot.xaml 的交互逻辑    /// </summary>    public partial class AtmRobot : UserControl, INotifyPropertyChanged    {        protected readonly int MoveTime = 300;        private const int AnimationTimeout = 3000; // seconds        public bool IsEnableTextMenu        {            get { return (bool)GetValue(IsEnableTextMenuProperty); }            set { SetValue(IsEnableTextMenuProperty, value); }        }        // Using a DependencyProperty as the backing store for IsEnableTextMenu.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty IsEnableTextMenuProperty =            DependencyProperty.Register("IsEnableTextMenu", typeof(bool), typeof(AtmRobot), new PropertyMetadata(true));        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(AtmRobot), 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(AtmRobot), 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(AtmRobot), 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(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public string RobotBlade2Target        {            get { return (string)GetValue(RobotBlade2TargetProperty); }            set { SetValue(RobotBlade2TargetProperty, value); }        }        public static readonly DependencyProperty RobotBlade2TargetProperty =            DependencyProperty.Register("RobotBlade2Target", typeof(string), typeof(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public Dictionary<string, StationPosition> StationPosition        {            get { return (Dictionary<string, StationPosition>)GetValue(StationPositionProperty); }            set { SetValue(StationPositionProperty, value); }        }        // Using a DependencyProperty as the backing store for StationPosition2.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty StationPositionProperty =            DependencyProperty.Register("StationPosition", typeof(Dictionary<string, StationPosition>), typeof(AtmRobot), 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(AtmRobot), new PropertyMetadata(false));        public WaferInfo Wafer1        {            get { return (WaferInfo)GetValue(Wafer1Property); }            set { SetValue(Wafer1Property, value); }        }        // Using a DependencyProperty as the backing store for Wafer1.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty Wafer1Property =            DependencyProperty.Register("Wafer1", typeof(WaferInfo), typeof(AtmRobot), new PropertyMetadata(null));        public RobotMoveInfo MoveInfo        {            get { return (RobotMoveInfo)GetValue(MoveInfoProperty); }            set { SetValue(MoveInfoProperty, value); }        }        public static readonly DependencyProperty MoveInfoProperty =            DependencyProperty.Register("MoveInfo", typeof(RobotMoveInfo), typeof(AtmRobot), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public WaferInfo Wafer2        {            get { return (WaferInfo)GetValue(Wafer2Property); }            set { SetValue(Wafer2Property, value); }        }        // Using a DependencyProperty as the backing store for Wafer2.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty Wafer2Property =            DependencyProperty.Register("Wafer2", typeof(WaferInfo), typeof(AtmRobot), new PropertyMetadata(null));        private string CurrentPosition        {            get; set;        }        private List<MenuItem> menu;        public List<MenuItem> Menu        {            get            {                return menu;            }            set            {                menu = value;                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Menu"));            }        }        public ICommand MoveCommand        {            get            {                return new DelegateCommand<string>(MoveTo);            }        }        private AnimationQueue queue;        public event PropertyChangedEventHandler PropertyChanged;        static void StationPositionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            var self = (AtmRobot)d;            var positions = (Dictionary<string, StationPosition>)e.NewValue;            self.Menu = positions.Select(x => new MenuItem() { Header = x.Key, Command = self.MoveCommand, CommandParameter = x.Key }).ToList();        }        public AtmRobot()        {#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;            CurrentPosition = "ArmA.System";            canvas1.Rotate(155);            canvas2.Rotate(240);            canvas3.Rotate(236);            canvas21.Rotate(25);            canvas22.Rotate(120);            canvas23.Rotate(484);        }        protected override void OnRender(DrawingContext drawingContext)        {            base.OnRender(drawingContext);            if (DesignerProperties.GetIsInDesignMode(this))            {                return;            }            //if (RobotBladeTarget != null)            //{            //    queue.EnqueueStatus(new AnimationParameter() { Target = RobotBladeTarget });            //}            //if (RobotBlade2Target != null)            //{            //    queue.EnqueueStatus(new AnimationParameter() { Target = RobotBlade2Target });            //}            if (MoveInfo != null)            {                queue.EnqueueStatus(new AnimationParameter() { Target = MoveInfo.BladeTarget});// Arm = MoveInfo.ArmTarget.ToString();            }        }        private void Queue_StatusUpdated(object sender, StatusUpdateArgs e)        {            //string TargetPosition = CurrentPosition.Split('.')[0] + "." + e.Parameter.Target;            string TargetPosition = e.Parameter.Target;            LogMsg(string.Format("Target: {0} - {1}", CurrentPosition, TargetPosition));            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 = TargetPosition;                e.Event.Set();            });            var needMove = CurrentPosition != TargetPosition;            if (needMove)            {                Invoke(() => MoveRobot(TargetPosition, 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);            canvas2.Rotate(180, true);            canvas3.Rotate(180, true);        }        private void MoveToStart(string station, Action onComplete = null)        {            //var position = StationPosition[station];            StationPosition positionA = station.Contains("ArmA") ? StationPosition[station] : StationPosition[station.Replace("ArmB", "ArmA")];            StationPosition positionB = station.Contains("ArmB") ? StationPosition[station] : StationPosition[station.Replace("ArmA", "ArmB")];            canvas1.Rotate(positionA.StartPosition.Root, true, MoveTime, 0, 0, onComplete);            canvas2.Rotate(positionA.StartPosition.Arm, true, MoveTime);            canvas3.Rotate(positionA.StartPosition.Hand, true, MoveTime);            canvas21.Rotate(positionB.StartPosition.Root, true, MoveTime);            canvas22.Rotate(positionB.StartPosition.Arm, true, MoveTime);            canvas23.Rotate(positionB.StartPosition.Hand, true, MoveTime);            CurrentPosition = station;        }        private void MoveToEnd(string station, Action onComplete = null)        {            var position = StationPosition[station];            if (station.Contains("ArmA"))            {                canvas1.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);                canvas2.Rotate(position.EndPosition.Arm, true, MoveTime);                canvas3.Rotate(position.EndPosition.Hand, true, MoveTime);            }            if (station.Contains("ArmB"))            {                canvas21.Rotate(position.EndPosition.Root, true, MoveTime, 0, 0, onComplete);                canvas22.Rotate(position.EndPosition.Arm, true, MoveTime);                canvas23.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 BladeStatusLP    //{    //	Extend,    //	Retract    //}    //public class Robot2Position    //{    //	public int? X;    //	public int Root;    //	public int Arm;    //	public int Hand;    //}    //public class StationPosition2    //{    //	public Robot2Position StartPosition;    //	public Robot2Position EndPosition;    //}}
 |