From 384ccdc90a77d1b099ace3245163f24883bd60ee Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Mon, 20 Jul 2020 16:48:12 +0300 Subject: New Linearization graph. Related Work Items: #2957 --- .../ViewModels/ColorCalibrationViewVM.cs | 143 ++++++++++++++++----- 1 file changed, 110 insertions(+), 33 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs index f118ce77a..d065b58c5 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -17,12 +17,14 @@ using Tango.PMR.ColorLab; using Tango.Logging; using Tango.MachineStudio.Common.Notifications; using System.Text.RegularExpressions; +using Microsoft.Win32; namespace Tango.MachineStudio.RML.ViewModels { public class ColorCalibrationViewVM : ExtendedObject { private IColorCalibrator _calibrator; + private ILinearizationMeasurements _linearizationMeasurementscalibrator; private INotificationProvider _notification; #region Properties @@ -41,15 +43,7 @@ namespace Tango.MachineStudio.RML.ViewModels get { return _liquidType; } set { _liquidType = value; RaisePropertyChangedAuto(); } } - - private BL.Entities.LiquidType _selectedLinearizationliquidType; - - public BL.Entities.LiquidType SelectedLinearizationLiquidType - { - get { return _selectedLinearizationliquidType; } - set { _selectedLinearizationliquidType = value; RaisePropertyChangedAuto(); } - } - + private List _liquidTypes; public List LiquidTypes @@ -98,18 +92,14 @@ namespace Tango.MachineStudio.RML.ViewModels public RelayCommand CreateGraphCommand { get; set; } public RelayCommand CreateLinearizationGraphCommand { get; set; } + public RelayCommand ExportGraphCommand { get; set; } - public string CalibrationLiquidTypeName + public string LiquidTypeName { get { return LiquidType == null ?"" : LiquidType.Name; } } - - public string LinearizationLiquidTypeName - { - get { return SelectedLinearizationLiquidType == null ? "" : SelectedLinearizationLiquidType.Name; } - } - + public Plot PlotControl { get; set; } private IList _points; /// @@ -201,14 +191,17 @@ namespace Tango.MachineStudio.RML.ViewModels HasError = false; CreateGraphCommand = new RelayCommand(CreateGraph); CreateLinearizationGraphCommand = new RelayCommand(CreateLinearizationGraph); + ExportGraphCommand = new RelayCommand(ExportGraph); this.Points = new List(); TargetPoints = new List(); + LinearizationPoints = new List(); } public void Loading() { LiquidType = LiquidTypes.Count > 0 ? LiquidTypes[0] : null; _calibrator = new DefaultColorCalibrator(); + _linearizationMeasurementscalibrator = new DefaultColorCalibrator(); } @@ -281,33 +274,117 @@ namespace Tango.MachineStudio.RML.ViewModels #endregion #region CreateLinearizationGraph - private void CreateLinearizationGraph(object obj) + private async void CreateLinearizationGraph(object obj) { - if (_selectedLinearizationliquidType == null) + if (_liquidType == null ) return; - string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_selectedLinearizationliquidType.Type]; + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; ColorLinearizationModel model = new ColorLinearizationModel(); - string fileName = @"C:\Test\Test Input Lineration.xlsx"; - model.GetDataFromFile(fileName); - //await Task.Factory.StartNew(() => - //{ - // Factor = GetLiquidFactor(); - //}); + string fileName = GetInkDataFileOpen();// @"C:\Test\Test Input Lineration.xlsx";//dialog to open file + if (fileName == null) + return; - LinearizationPoints.Clear(); - - Measurements.ToList().ForEach(x => { - LinearizationPoints.Add(new DataPoint(x.Ink, x.L)); + List items; + model.GetDataFromFile(fileName, out items); + if (items.Count == 0) + return; + + List outputPoints = new List(); + await Task.Factory.StartNew(() => + { + outputPoints = GetLinearizationMeasurements(items); }); - RaisePropertyChanged("To"); - RaisePropertyChanged("From"); - XStep = (int)(Points.Count / 6); + LinearizationPoints.Clear(); + + if (outputPoints == null || outputPoints.Count != items.Count) + return; + + foreach (var nw in items.Zip(outputPoints, Tuple.Create)) + { + LinearizationPoints.Add(new DataPoint(nw.Item1.InkPercentage, nw.Item2)); + } + LinearizationPlotControl.InvalidatePlot(true); } + private String GetInkDataFileOpen() + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Select Ink data file"; + dlg.Filter = "Excel |*.xlsx"; + if (dlg.ShowDialogCenter()) + { + return dlg.FileName; + } + + return null; + } + private List GetLinearizationMeasurements(List items) + { + try + { + LinearizationInput linearizationInput = new LinearizationInput(); + items.ForEach(x => linearizationInput.Measurements.Add(new LinearizationMeasurement { L = x.L, A = x.A, B = x.B, InkPercentage = x.InkPercentage })); + linearizationInput.LiquidType = PMR.ColorLab.LiquidType.TryParse(_liquidType.Type.ToString(), out PMR.ColorLab.LiquidType outValue) ? outValue : PMR.ColorLab.LiquidType.Black; + + LAB lab; + if (ColorCalibrationExt.TargetLiquidTypeToLAB.TryGetValue(_liquidType.Type, out lab)) + { + linearizationInput.TargetL = lab.L; + linearizationInput.TargetA = lab.A; + linearizationInput.TargetB = lab.B; + } + LinearizationOutput result = _linearizationMeasurementscalibrator.GetLinearizationMeasurements(linearizationInput); + + ErrorMessage = Regex.Replace(result.ErrorMessage, "[^A-Za-z0-9_., ]+", ""); + + HasError = false == String.IsNullOrEmpty(ErrorMessage); + return result.InkPercentage.ToList(); + } + catch (Exception ex) + { + HasError = true; + ErrorMessage = "Error occurred while trying to call GetLiquidFactor."; + LogManager.Log(ex, "Error occurred while trying to call GetLiquidFactor."); + } + return null; + } + #endregion + + #region Export graph + + protected void ExportGraph() + { + SaveFileDialog dlg = new SaveFileDialog(); + try + { + dlg.Title = $"Export excel calibration file for {LiquidType.Name}"; + dlg.Filter = "Excel Files|*.xlsx"; + dlg.DefaultExt = ".xlsx"; + dlg.FileName = $"CData_{LiquidType.Name}_v{LiquidType.Version}.xlsx"; + if (dlg.ShowDialog().Value) + { + List points = new List(); + LinearizationPoints.ToList().ForEach(x=> points.Add(ToCalibrationPoint(x))); + BL.Calibration.CalibrationHelper.ExportCalibrationDataToExcel(points, dlg.FileName); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error exporting excel file " + dlg.FileName); + _notification.ShowError("An error occurred while trying to export the calibration data."); + } + } + public CalibrationPoint ToCalibrationPoint(DataPoint point) + { + return new CalibrationPoint() + { + X = point.X, + Y = point.Y, + }; + } #endregion - } } -- cgit v1.3.1