| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 | using MECF.Framework.Common.Beckhoff.AxisProvider;using MECF.Framework.Common.Beckhoff.Station;using MECF.Framework.Common.DataCenter;using MECF.Framework.Common.OperationCenter;using MECF.Framework.Common.Utilities;using PunkHPX8_Core;using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Runtime.CompilerServices;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace PunkHPX8_Themes.UserControls{    /// <summary>    /// PufStationPositionControl.xaml 的交互逻辑    /// </summary>    public partial class PufStationPositionControl : UserControl, INotifyPropertyChanged    {        #region 内部变量        /// <summary>        /// 步进        /// </summary>        private double _incrementValue;        /// <summary>        /// 查询后台数据集合        /// </summary>        private List<string> _rtDataKeys = new List<string>();        /// <summary>        /// 查询后台的数据        /// </summary>        private Dictionary<string, object> _rtDataValues;        /// <summary>        /// axis        /// </summary>        BeckhoffStationAxis _axis;        /// <summary>        /// Stations        /// </summary>        private List<string> _moduleStations;        /// <summary>        /// 选择项        /// </summary>        private string _moduleSelectedItem;        /// <summary>        /// 已经保存的位置        /// </summary>        private double _savePosition;        /// <summary>        /// scaleFactor        /// </summary>        private double _scaleFactor;        #endregion        #region 属性        public static readonly DependencyProperty ModuleNameProperty = DependencyProperty.Register(           "ModuleName", typeof(string), typeof(PufStationPositionControl),           new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 模块名称        /// </summary>        public string ModuleName        {            get            {                return (string)this.GetValue(ModuleNameProperty);            }            set            {                this.SetValue(ModuleNameProperty, value);            }        }        public List<string> ModuleItemSource        {            get { return _moduleStations; }            set            {                _moduleStations = value;                InvokePropertyChanged(nameof(ModuleItemSource));            }        }        public static readonly DependencyProperty IsAtStationProperty = DependencyProperty.Register(          "IsAtStation", typeof(bool), typeof(PufStationPositionControl));        public bool IsAtStation        {            get             {                return (bool)this.GetValue(IsAtStationProperty);             }            set            {                SetValue(IsAtStationProperty, value);                InvokePropertyChanged(nameof(IsAtStation));            }        }        /// <summary>        /// 选项索引        /// </summary>        public string ModuleSelectedItem        {            get            {                return _moduleSelectedItem;            }            set            {                SavedPosition = GetStationPosition(_axis, value);                _moduleSelectedItem = value;                InvokePropertyChanged(nameof(ModuleSelectedItem));            }        }        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(           "OperationCommand", typeof(ICommand), typeof(PufStationPositionControl),           new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public ICommand OperationCommand        {            get            {                return (ICommand)this.GetValue(CommandProperty);            }            set            {                this.SetValue(CommandProperty, value);            }        }        public static readonly DependencyProperty ModuleTitleProperty = DependencyProperty.Register(            "ModuleTitle", typeof(string), typeof(PufStationPositionControl),            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 标题        /// </summary>        public string ModuleTitle        {            get            {                return (string)this.GetValue(ModuleTitleProperty);            }            set            {                this.SetValue(ModuleTitleProperty, value);            }        }        public static readonly DependencyProperty SavedPositionProperty = DependencyProperty.Register(           "SavedPosition", typeof(double), typeof(PufStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));        /// <summary>        /// 保存位置        /// </summary>        public double SavedPosition        {            get            {                return _savePosition;            }            set            {                _savePosition = value;                SetValue(SavedPositionProperty, value);                IsAtStation = JudgeAtStation(value,CurrentPosition,ToleranceDefault);                InvokePropertyChanged(nameof(SavedPosition));            }        }        public static readonly DependencyProperty ToleranceDefaultProperty = DependencyProperty.Register(           "ToleranceDefault", typeof(double), typeof(PufStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));        /// <summary>        /// 保存位置        /// </summary>        public double ToleranceDefault        {            get            {                if(_axis!=null)                {                    return _axis.ToleranceDefault;                }                else                {                    return 0;                }            }            set            {                SetValue(ToleranceDefaultProperty, _axis != null ? _axis.ToleranceDefault : 0);            }        }        public static readonly DependencyProperty CurrentPositionProperty = DependencyProperty.Register(           "CurrentPosition", typeof(double), typeof(PufStationPositionControl), new FrameworkPropertyMetadata(0.00, new PropertyChangedCallback(OnCurrentPositionChanged)));        /// <summary>        /// 当前位置        /// </summary>        public double CurrentPosition        {            get            {                return (double)this.GetValue(CurrentPositionProperty);            }            set            {                SetValue(CurrentPositionProperty, value);            }        }        private static void OnCurrentPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)        {            if (e.NewValue != null)            {                double tmpSavedPosition = (double)d.GetValue(SavedPositionProperty);                double toleranceDefault = (double)d.GetValue(ToleranceDefaultProperty);                bool result = JudgeAtStation(tmpSavedPosition, (double)e.NewValue, toleranceDefault);                d.SetValue(IsAtStationProperty, result);            }        }        public static readonly DependencyProperty IsMotorOnProperty = DependencyProperty.Register(          "IsMotorOn", typeof(bool), typeof(PufStationPositionControl),          new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// AtStation        /// </summary>        public bool IsMotorOn        {            get            {                return (bool)this.GetValue(IsMotorOnProperty);            }            set            {                this.SetValue(IsMotorOnProperty, value);            }        }        public static readonly DependencyProperty DegValueProperty = DependencyProperty.Register(         "DegValue", typeof(double), typeof(PufStationPositionControl), new FrameworkPropertyMetadata(0.00, FrameworkPropertyMetadataOptions.AffectsRender));        /// <summary>        /// 当前位置        /// </summary>        public double DegValue        {            get            {                return (double)this.GetValue(DegValueProperty);            }            set            {                this.SetValue(DegValueProperty, value);            }        }        public static readonly DependencyProperty CurrentStationProperty = DependencyProperty.Register("CurrentStation", typeof(string), typeof(PufStationPositionControl));        /// <summary>        /// 当前位置        /// </summary>        public string CurrentStation        {            get            {                return (string)this.GetValue(CurrentStationProperty);            }            set            {                this.SetValue(CurrentStationProperty, value);            }        }        /// <summary>        /// 当前位置        /// </summary>        public double IncrementValue        {            get { return _incrementValue; }            set            {                _incrementValue = value;                InvokePropertyChanged(nameof(IncrementValue));            }        }        public event PropertyChangedEventHandler PropertyChanged;        #endregion        /// <summary>        /// 构造函数        /// </summary>        public PufStationPositionControl()        {            InitializeComponent();        }        private void GotoSavedPos_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.GotoSavedPosition", "TargetPosition",ModuleSelectedItem);        }        private void MotorPos_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.Save", $"{ModuleSelectedItem}", Math.Round(CurrentPosition, 2));            SavedPosition = CurrentPosition;            foreach (Station item in _axis.Stations)            {                if (item.Name == ModuleSelectedItem)                {                    item.Position = CurrentPosition.ToString();                    break;                }            }        }        private void LeftCommand_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogDown", "TargetPosition",DegValue,CurrentPosition);        }        private void RightCommand_Click(object sender, RoutedEventArgs e)        {            InvokeClient.Instance.Service.DoOperation($"{ModuleName}.JogUp", "TargetPosition", DegValue,CurrentPosition);        }        /// <summary>        /// 获取工位集合        /// </summary>        /// <param name="stationAxis"></param>        /// <returns></returns>        private List<string> GetStationList(BeckhoffStationAxis stationAxis)        {            List<string> lst = new List<string>();            if (stationAxis == null)            {                return lst;            }            foreach (var item in stationAxis.Stations)            {                lst.Add(item.Name);            }            return lst;        }        /// <summary>        /// 获取工位位置        /// </summary>        /// <param name="stationAxis"></param>        /// <param name="station"></param>        /// <returns></returns>        private double GetStationPosition(BeckhoffStationAxis stationAxis, string station)        {            if (stationAxis == null)            {                return 0;            }            foreach (var item in stationAxis.Stations)            {                if (item.Name == station)                {                    double.TryParse(item.Position, out var position);                    return position;                }            }            return 0;        }        /// <summary>        /// 转换数值        /// </summary>        /// <param name="value"></param>        /// <returns></returns>        private int ConvertValue(double value)        {            if(_scaleFactor!=0)            {                return (int)Math.Round(value * _scaleFactor,0);            }            else            {                return (int)Math.Round(value,0);            }        }        private void InvokePropertyChanged(string propertyName)        {            if (PropertyChanged != null)            {                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }        /// <summary>        /// 判定AtStation        /// </summary>        private static bool JudgeAtStation(double saved,double current,double tolerance)        {            return Math.Abs(saved - current) <= tolerance;        }        private void IsControlVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)        {            if (e.NewValue != null && ((bool)e.NewValue) == true)            {                if (!string.IsNullOrEmpty(ModuleName))                {                    _rtDataKeys.Clear();                    IncrementValue = (double)QueryDataClient.Instance.Service.GetConfig("System.Increment");                    _rtDataKeys.Add($"Station.{ModuleName}");                    _rtDataKeys.Add($"{ModuleName}.AxisProvider");                    _rtDataValues = QueryDataClient.Instance.Service.PollData(_rtDataKeys);                    if (_rtDataValues != null)                    {                        _axis = CommonFunction.GetValue<BeckhoffStationAxis>(_rtDataValues, $"Station.{ModuleName}");                        BeckhoffProviderAxis beckhoffProviderAxis = CommonFunction.GetValue<BeckhoffProviderAxis>(_rtDataValues, $"{ModuleName}.AxisProvider");                        if (_axis != null)                        {                            ToleranceDefault = _axis.ToleranceDefault;                            ModuleItemSource = GetStationList(_axis);                            if (ModuleItemSource != null && ModuleItemSource.Count != 0)                            {                                ModuleSelectedItem = ModuleItemSource[0];                            }                        }                        if (beckhoffProviderAxis != null)                        {                            _scaleFactor = beckhoffProviderAxis.ScaleFactor;                        }                    }                }            }        }    }}
 |