PgGridVm.cs 12.2 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using CommunityToolkit.Mvvm.Input;
潘栩锋's avatar
潘栩锋 committed
2 3 4 5 6 7 8 9 10 11 12 13
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Media;

潘栩锋's avatar
潘栩锋 committed
14
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
15 16 17 18 19 20
{
    class PgGridVm : IPgGridVm
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region IPgGridVm Property
21 22 23 24 25 26 27
        /// <summary>
        /// 调试速度 单位:脉冲/秒
        /// </summary>
        public UInt32 VJog { get; set; }
        /// <summary>
        /// 调试速度 单位:m/min
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
28
        public double VJogMm => VJog * Mmpp * Speed1Scale / 1000 * 60;
29

潘栩锋's avatar
潘栩锋 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        /// <summary>
        /// 目标位置 单位:脉冲
        /// </summary>
        public int TargetPos { get; set; }
        /// <summary>
        /// 目标位置 单位:mm
        /// </summary>
        public double TargetMm => TargetPos * Mmpp;
        /// <summary>
        /// AD值模式
        /// </summary>
        public bool IsADMode { get; set; }

        /// <summary>
        /// 最多6条曲线
        /// </summary>
        public ObservableCollection<SeriesInfo> SeriesInfos { get; } = new ObservableCollection<SeriesInfo>();

        #region 曲线
        public Func<double, string> XFormatter { get; private set; }
        public Func<double, string> YFormatter { get; private set; }

        public double XUpper { get; set; } = 1024;
        public double XLower { get; set; } = 0;

        public double YUpper { get; set; } = double.NaN;
        public double YLower { get; set; } = double.NaN;

        public double XMax { get; private set; } = 1024;
        public double XMin { get; private set; } = 0;

        public double YMax { get; private set; } = double.NaN;
        public double YMin { get; private set; } = double.NaN;

        public double SectionValue => MeasureValuePos / PosOfGrid;
        public double SectionWidth => MeasureWidthPos / PosOfGrid;
        public SeriesCollection Series { get; } = new SeriesCollection();
        #endregion

        #region 测量
        /// <summary>
        /// 测量开始位置 单位:脉冲
        /// </summary>
        public int MeasureValuePos { get; set; }
        /// <summary>
        /// 测量开始位置 单位:mm
        /// </summary>
        public double MeasureValueMm => MeasureValuePos * Mmpp;
        /// <summary>
        /// 测量宽度 单位:脉冲
        /// </summary>
        public int MeasureWidthPos { get; set; }
        /// <summary>
        /// 测量宽度 单位:mm
        /// </summary>
        public double MeasureWidthMm => MeasureWidthPos * Mmpp;
        #endregion

        #endregion

        #region IPgGridVm Command
        public RelayCommand RunToCmd { get; private set; }
        public RelayCommand GetFDataCmd { get; private set; }
        public RelayCommand GetBDataCmd { get; private set; }
        public RelayCommand ClearCmd { get; private set; }
        public RelayCommand MeasureCmd { get; private set; }

        public RelayCommand SaveCmd { get; private set; }

        public RelayCommand<SeriesInfo> SelectCmd { get; private set; }
        #endregion

        public int PosOfGrid { get; set; } = 10;
        public double Mmpp { get; set; } = 0.1;
104
        public double Speed1Scale { get; set; } = 4;
105 106
        ITDGageService gageService;
        IFlyADService flyAdService;
107
        IInitParamService initParam;
潘栩锋's avatar
潘栩锋 committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        public PgGridVm()
        {
            #region 与数据无关界面参数

            XFormatter = (x) =>
            {
                int index = (int)x;
                int pos = index * PosOfGrid;
                int mm = (int)(pos * Mmpp);
                return $"{pos}\n{mm}mm";
            };
            YFormatter = (y) =>
            {
                if (IsADMode)
                {
                    return $"{y}";
                }
                else
                {
                    return $"{y:F1}";
                }
            };
潘栩锋's avatar
潘栩锋 committed
130

潘栩锋's avatar
潘栩锋 committed
131 132 133 134 135
            for (int i = 0; i < 6; i++)
            {
                SeriesInfo info = new SeriesInfo()
                {
                    Title = $"{i}",
潘栩锋's avatar
潘栩锋 committed
136
                    Fill = FLY.ControlLibrary.Themes.Styles.GetForeground(i),
潘栩锋's avatar
潘栩锋 committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                };
                LineSeries lineSeries = new LineSeries
                {
                    StrokeThickness = 2,
                    Fill = new SolidColorBrush(System.Windows.Media.Colors.Transparent),
                    PointGeometry = null
                };
                lineSeries.SetBinding(LineSeries.StrokeProperty, "Fill");
                lineSeries.DataContext = info;
                Series.Add(lineSeries);
                info.Series = lineSeries;
                SeriesInfos.Add(info);
            }

            SeriesInfos[0].IsSelected = true;
            UpdateADMode();
            #endregion

            InitCmd();

            this.PropertyChanged += PgGridVm_PropertyChanged;
        }

160 161 162 163 164 165
        public void Init(
            IInitParamService initParam,
            ITDGageService gageService,
            IFlyADService flyADService
            )
        {
166
            this.initParam = initParam;
167 168
            this.gageService = gageService;
            this.flyAdService = flyADService;
169 170
            Misc.BindingOperations.SetBinding(this.initParam, nameof(initParam.Encoder1_mmpp), this, nameof(Mmpp));
            Misc.BindingOperations.SetBinding(this.initParam, nameof(initParam.PosOfGrid), this, nameof(PosOfGrid));
171

172 173 174
            Misc.BindingOperations.SetBinding(this.initParam, nameof(initParam.Speed1Scale), this, nameof(Speed1Scale));

            Misc.BindingOperations.SetBinding(this.initParam, nameof(initParam.VJOG), this, nameof(VJog));
175 176 177


        }
潘栩锋's avatar
潘栩锋 committed
178 179
        private void PgGridVm_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
180
            if (e.PropertyName == nameof(IsADMode))
潘栩锋's avatar
潘栩锋 committed
181 182 183 184 185 186 187 188 189 190
            {
                UpdateADMode();
                UpdateRange(true);
            }
        }

        void InitCmd()
        {
            RunToCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
191 192
                if (VJog != initParam.VJOG)
                {
193 194 195
                    initParam.VJOG = VJog;
                    initParam.Apply();
                }
196
                gageService.StartP2(STARTP2_MODE.RUNTO, TargetPos);
潘栩锋's avatar
潘栩锋 committed
197 198 199 200
            });

            GetFDataCmd = new RelayCommand(() =>
            {
201
                flyAdService.GetGrid(Misc.DIRECTION.FORWARD, GetGridCB, null);
潘栩锋's avatar
潘栩锋 committed
202 203 204 205
            });

            GetBDataCmd = new RelayCommand(() =>
            {
206
                flyAdService.GetGrid(Misc.DIRECTION.BACKWARD, GetGridCB, null);
潘栩锋's avatar
潘栩锋 committed
207 208
            });

潘栩锋's avatar
潘栩锋 committed
209 210
            ClearCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
211 212 213 214 215 216 217 218 219
                var series = SeriesInfos.First(s => s.IsSelected);
                series.Clear();
            });

            MeasureCmd = new RelayCommand(() =>
            {
                UpdateMeasure();
            });

潘栩锋's avatar
潘栩锋 committed
220 221
            SaveCmd = new RelayCommand(() =>
            {
潘栩锋's avatar
潘栩锋 committed
222 223 224 225 226 227 228 229 230 231
                Save();
            });
            SelectCmd = new RelayCommand<SeriesInfo>((info) =>
            {
                if (info.IsSelected)
                    return;
                foreach (var i in SeriesInfos)
                    i.IsSelected = (i == info);
            });
        }
潘栩锋's avatar
潘栩锋 committed
232
        void GetGridCB(object asyncState, object retData)
潘栩锋's avatar
潘栩锋 committed
233
        {
潘栩锋's avatar
潘栩锋 committed
234
            GridInfo gridinfo = retData as GridInfo;
潘栩锋's avatar
潘栩锋 committed
235 236
            var series = SeriesInfos.First(s => s.IsSelected);
            series.IsBackw = gridinfo.direction == Misc.DIRECTION.BACKWARD;
潘栩锋's avatar
潘栩锋 committed
237 238 239 240 241 242 243 244 245 246 247 248 249
            series.ADs.Clear();
            series.Thicks.Clear();
            var ads = gridinfo.data.Select(ad =>
            {
                if (Misc.MyBase.ISVALIDATA(ad))
                {
                    return ad;
                }
                else
                {
                    return double.NaN;
                }
            });
潘栩锋's avatar
潘栩锋 committed
250
            series.ADs.AddRange(ads);
潘栩锋's avatar
潘栩锋 committed
251 252
            series.Thicks.AddRange(gridinfo.thick);
            UpdateRange();
潘栩锋's avatar
潘栩锋 committed
253 254 255 256 257 258 259 260
        }


        void UpdateADMode()
        {
            if (IsADMode)
            {
                foreach (var info in SeriesInfos)
261
                    info.Series.SetBinding(LineSeries.ValuesProperty, nameof(SeriesInfo.ADs));
潘栩锋's avatar
潘栩锋 committed
262 263 264 265
            }
            else
            {
                foreach (var info in SeriesInfos)
266
                    info.Series.SetBinding(LineSeries.ValuesProperty, nameof(SeriesInfo.Thicks));
潘栩锋's avatar
潘栩锋 committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            }
        }
        void UpdateMeasure()
        {
            int begin_grid = MeasureValuePos / PosOfGrid;
            int end_grid = (MeasureValuePos + MeasureWidthPos) / PosOfGrid;

            foreach (var info in SeriesInfos)
            {
                if (IsADMode)
                {
                    double sum = 0;
                    int cnt = 0;
                    for (int i = begin_grid; i <= end_grid && i < info.ADs.Count() && i >= 0; i++)
                    {
                        sum += info.ADs[i];
                        cnt++;
                    }
                    if (cnt == 0)
                        info.Avg = double.NaN;
                    else
                        info.Avg = sum / cnt;
                }
                else
                {
                    double sum = 0;
                    int cnt = 0;
                    for (int i = begin_grid; i <= end_grid && i < info.Thicks.Count() && i >= 0; i++)
                    {
                        sum += info.Thicks[i];
                        cnt++;
                    }
                    if (cnt == 0)
                        info.Avg = double.NaN;
                    else
                        info.Avg = sum / cnt;
                }
            }
        }

        void UpdateRange()
        {
            UpdateRange(false);
        }
        void UpdateRange(bool isReset)
        {
            List<double> vlist = new List<double>();
            foreach (var info in SeriesInfos)
            {
                IEnumerable<double> values = (IsADMode) ? info.ADs : info.Thicks;

潘栩锋's avatar
潘栩锋 committed
318
                if (values.Count() == 0)
潘栩锋's avatar
潘栩锋 committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
                    continue;

                var values2 = values.Where(v => !double.IsNaN(v));
                if (values2.Count() == 0)
                    continue;

                vlist.AddRange(values2);
            }
            if (vlist.Count() == 0)
            {
                //没有任何数据
                return;
            }
            else
            {
                bool isYMaxAuto = (YMax == YUpper) || double.IsNaN(YUpper);
                bool isYMinAuto = (YMin == YLower) || double.IsNaN(YLower);

                if (IsADMode)
                {
                    YMax = 65535;
                    YMin = 0;
                }
                else
                {
                    double max = vlist.Max();
                    double min = vlist.Min();
                    double range = max - min;
                    if (range < 10)
                        range = 10;

                    YMax = Math.Round(max + range / 10);
                    YMin = Math.Round(min - range / 10);

                }

                if (isReset)
                {
                    YUpper = YMax;
                    YLower = YMin;
                }
                else
                {
                    if (isYMaxAuto)
                        YUpper = YMax;
                    if (isYMinAuto)
                        YLower = YMin;
                }
            }
        }

        void Save()
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            string filename = $"Grid数据_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "xlsx files (*.xlsx)|*.xlsx";
            sfd.InitialDirectory = strDesktopPath;
            sfd.FileName = filename;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                PgGridOutput ouput = new PgGridOutput();
                ouput.Init(this);
                ouput.SaveToXlsx(sfd.FileName);
            }

            ////完成
            //FLY.ControlLibrary.Window_Tip.Show("成功", "保存到路径 " + filepath, TimeSpan.FromSeconds(2));
            //return;
        }

    }
}