diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels')
8 files changed, 1809 insertions, 32 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/CalibrationDataVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/CalibrationDataVM.cs new file mode 100644 index 000000000..b8fab210c --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/CalibrationDataVM.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Core; +using Tango.PMR.ColorLab; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class CalibrationDataPointVM : ExtendedObject + { + private double _x; + + public double X + { + get { return _x; } + set { _x = value; RaisePropertyChangedAuto(); } + } + + private double _y; + + public double Y + { + get { return _y; } + set { _y = value; RaisePropertyChangedAuto(); } + } + + private int _index; + + public int Index + { + get { return _index; } + set { _index = value; RaisePropertyChangedAuto(); } + } + + + public CalibrationDataPointVM() + { + + } + + public CalibrationDataPointVM(double x, double y) + { + X = x; + Y = y; + } + + public CalibrationDataPointVM(CalibrationPoint calibrationPoint) : this(calibrationPoint.X, calibrationPoint.Y) + { + + } + + public CalibrationPoint ToPMR() + { + return new CalibrationPoint() + { + X = X, + Y = Y, + }; + } + } + + public class CalibrationDataVM : ExtendedObject + { + private BL.Entities.LiquidType _liquidType; + + public BL.Entities.LiquidType LiquidType + { + get { return _liquidType; } + set { _liquidType = value; RaisePropertyChangedAuto(); } + } + + private IdsPack _idsPack; + + public IdsPack IdsPack + { + get { return _idsPack; } + set { _idsPack = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<CalibrationDataPointVM> _calibrationPoints; + + public ObservableCollection<CalibrationDataPointVM> CalibrationPoints + { + get { return _calibrationPoints; } + set { _calibrationPoints = value; RaisePropertyChangedAuto(); OnCalibrationPointsChanged(); } + } + + private void OnCalibrationPointsChanged() + { + if (CalibrationPoints != null) + { + CalibrationPoints.CollectionChanged -= CalibrationPoints_CollectionChanged; + CalibrationPoints.CollectionChanged += CalibrationPoints_CollectionChanged; + + SetIndices(); + } + } + + private void CalibrationPoints_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + SetIndices(); + } + + private void SetIndices() + { + if (CalibrationPoints != null) + { + int index = 1; + foreach (var p in CalibrationPoints) + { + p.Index = index++; + } + } + } + + private CalibrationDataVM() + { + CalibrationPoints = new ObservableCollection<CalibrationDataPointVM>(); + } + + public CalibrationDataVM(IdsPack pack) : this() + { + IdsPack = pack; + LiquidType = pack.LiquidType; + } + + public CalibrationDataVM(BL.Entities.LiquidType liquidType) : this() + { + LiquidType = liquidType; + CalibrationPoints = new ObservableCollection<CalibrationDataPointVM>(); + //CalibrationPoints.Add(new CalibrationDataPointVM() { Index = 1, X = 2, Y = 22 }); + } + } +}
\ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs new file mode 100644 index 000000000..a2a119e29 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs @@ -0,0 +1,424 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.SharedUI; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.BL.Enumerations; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.MachineStudio.Common.Notifications; +using Tango.PMR.ColorLab; +using Tango.ColorCalibration; +using Tango.BL.ActionLogs; +using Tango.BL.Entities; +using Tango.Core; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorCalibrationTabVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + private ILinearizationMeasurements _linearizationMeasurementscalibrator; + private List<BL.Entities.LiquidType> _liquids; + + #region Properties + + private string _name; + /// <summary> + /// Gets or sets the name of the thread. Using in print + /// </summary> + public string Name + { + get { return _name; } + set + { + _name = value; + if(RmlExtensionColorCalibrationsTest != null) + RmlExtensionColorCalibrationsTest.Name = _name; + RaisePropertyChangedAuto(); + } + } + + private int _tabIndex; + + public int TabIndex + { + get { return _tabIndex; } + set { _tabIndex = value; + RaisePropertyChangedAuto(); + } + } + + + private bool _isSelected; + /// <summary> + /// Gets or sets a value indicating whether this instance is selected. + /// </summary> + public bool IsSelected + { + get { return _isSelected; } + set { _isSelected = value; RaisePropertyChangedAuto(); } + } + + public CalibrationPlotModel CyanPlot { get; set; } + + public CalibrationPlotModel MagentaPlot { get; set; } + + public CalibrationPlotModel YellowPlot { get; set; } + + public CalibrationPlotModel BlackPlot { get; set; } + + public CalibrationDataVM CyanCalibrationData { get; set; } + + public CalibrationDataVM MagentaCalibrationData { get; set; } + + public CalibrationDataVM YellowCalibrationData { get; set; } + + public CalibrationDataVM BlackCalibrationData { get; set; } + + private RmlExtensionColorCalibrationsTest _rmlExtensionColorCalibrationsTest; + + public RmlExtensionColorCalibrationsTest RmlExtensionColorCalibrationsTest + { + get { return _rmlExtensionColorCalibrationsTest; } + set { _rmlExtensionColorCalibrationsTest = value; + OnRmlExtensionColorCalibrationsTestChanged(); + } + } + + /// <summary> + /// Gets or sets last points before save. Used in Save to remove points before save new. + /// </summary> + /// <value> + /// The removed points. + /// </value> + public Dictionary<LiquidTypes, List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>> RemovedPoints { get; set; } + + public bool IsTabLoaded { get; set; } + + + #endregion + + #region Commands + + public RelayCommand ImportCyanDataCommand { get; set; } + public RelayCommand ImportMagentaDataCommand { get; set; } + public RelayCommand ImportYellowDataCommand { get; set; } + public RelayCommand ImportBlackDataCommand { get; set; } + + #endregion + + public ColorCalibrationTabVM(INotificationProvider notification, IActionLogManager actionLogManager, List<BL.Entities.LiquidType> liquids) + { + _notification = notification; + _actionLogManager = actionLogManager; + _linearizationMeasurementscalibrator = new DefaultColorCalibrator(); + _liquids = liquids; + RemovedPoints = new Dictionary<LiquidTypes, List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>>(); + IsTabLoaded = false; + + CyanPlot = new CalibrationPlotModel(FactorColors.CYAN, "Cyan"); + YellowPlot = new CalibrationPlotModel(FactorColors.YELLOW, "Yellow"); + MagentaPlot = new CalibrationPlotModel(FactorColors.MAGENTA, "Magenta"); + BlackPlot = new CalibrationPlotModel(FactorColors.BLACK, "Black"); + + CyanCalibrationData = new CalibrationDataVM(new Tango.BL.Entities.LiquidType() { Code = (int)LiquidTypes.Cyan }); + MagentaCalibrationData = new CalibrationDataVM(new Tango.BL.Entities.LiquidType() { Code = (int)LiquidTypes.Magenta }); + YellowCalibrationData = new CalibrationDataVM(new Tango.BL.Entities.LiquidType() { Code = (int)LiquidTypes.Yellow }); + BlackCalibrationData = new CalibrationDataVM(new Tango.BL.Entities.LiquidType() { Code = (int)LiquidTypes.Black }); + + ImportCyanDataCommand = new RelayCommand(ImportCyanData); + ImportMagentaDataCommand = new RelayCommand(ImportMagentaData); + ImportYellowDataCommand = new RelayCommand(ImportYellowData); + ImportBlackDataCommand = new RelayCommand(ImportBlackData); + + } + + #region Methods + private async void ImportCyanData(object obj) + { + List<ColorLinearizationModel.LinearizationDataItem> items; + if (ImportDataFromFile(out items) && items.Count > 0) + { + List<double> calibrationPoints = new List<double>(); + await Task.Factory.StartNew(() => + { + calibrationPoints = GetLinearizationMeasurements(items, PMR.ColorLab.LiquidType.Cyan); + }); + UpdatePlots(CyanPlot, items, calibrationPoints); + + CyanCalibrationData.CalibrationPoints.Clear(); + var index = 1; + foreach (var nw in items.Zip(calibrationPoints, Tuple.Create)) + { + CyanCalibrationData.CalibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = nw.Item1.InkPercentage, Y = nw.Item2 }); + } + + SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData> data = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData; + var liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Cyan).FirstOrDefault(); + var cyanliquidData = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData.Where(x => x.LiquidTypeGuid == liquidtype.Guid).FirstOrDefault(); + if (cyanliquidData != null) + { + if (!RemovedPoints.ContainsKey(LiquidTypes.Cyan)) + { + RemovedPoints[LiquidTypes.Cyan] = new List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>(cyanliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.ToList()); + } + cyanliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Clear(); + items.ForEach(x => cyanliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Add(new RmlExtensionColorCalibrationsTestsLiquidDataPoint() { Ink = x.InkPercentage, L = x.L, A = x.A, B = x.B, CalculatedPoint = CyanCalibrationData.CalibrationPoints.Where(y => y.X == x.InkPercentage).Select(z => z.Y).FirstOrDefault() })); + } + } + + } + + private async void ImportMagentaData(object obj) + { + List<ColorLinearizationModel.LinearizationDataItem> items; + if (ImportDataFromFile(out items) && items.Count > 0) + { + List<double> calibrationPoints = new List<double>(); + await Task.Factory.StartNew(() => + { + calibrationPoints = GetLinearizationMeasurements(items, PMR.ColorLab.LiquidType.Magenta); + }); + UpdatePlots(MagentaPlot, items, calibrationPoints); + + MagentaCalibrationData.CalibrationPoints.Clear(); + var index = 1; + foreach (var nw in items.Zip(calibrationPoints, Tuple.Create)) + { + MagentaCalibrationData.CalibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = nw.Item1.InkPercentage, Y = nw.Item2 }); + } + + SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData> data = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData; + var liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Magenta).FirstOrDefault(); + var magentaliquidData = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData.Where(x => x.LiquidTypeGuid == liquidtype.Guid).FirstOrDefault(); + if (magentaliquidData != null) + { + if (!RemovedPoints.ContainsKey(LiquidTypes.Magenta)) + { + RemovedPoints[LiquidTypes.Magenta] = new List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>(magentaliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.ToList()); + } + magentaliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Clear(); + items.ForEach(x => magentaliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Add(new RmlExtensionColorCalibrationsTestsLiquidDataPoint() { Ink = x.InkPercentage, L = x.L, A = x.A, B = x.B, CalculatedPoint = MagentaCalibrationData.CalibrationPoints.Where(y => y.X == x.InkPercentage).Select(z => z.Y).FirstOrDefault() })); + } + } + } + + private async void ImportYellowData(object obj) + { + List<ColorLinearizationModel.LinearizationDataItem> items; + if (ImportDataFromFile(out items) && items.Count > 0) + { + List<double> calibrationPoints = new List<double>(); + await Task.Factory.StartNew(() => + { + calibrationPoints = GetLinearizationMeasurements(items, PMR.ColorLab.LiquidType.Yellow); + }); + UpdatePlots(YellowPlot, items, calibrationPoints); + + YellowCalibrationData.CalibrationPoints.Clear(); + var index = 1; + foreach (var nw in items.Zip(calibrationPoints, Tuple.Create)) + { + YellowCalibrationData.CalibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = nw.Item1.InkPercentage, Y = nw.Item2 }); + } + + SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData> data = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData; + var liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Yellow).FirstOrDefault(); + var yellowliquidData = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData.Where(x => x.LiquidTypeGuid == liquidtype.Guid).FirstOrDefault(); + if (yellowliquidData != null) + { + if (!RemovedPoints.ContainsKey(LiquidTypes.Yellow)) + { + RemovedPoints[LiquidTypes.Yellow] = new List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>(yellowliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.ToList()); + } + yellowliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Clear(); + items.ForEach(x => yellowliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Add(new RmlExtensionColorCalibrationsTestsLiquidDataPoint() { Ink = x.InkPercentage, L = x.L, A = x.A, B = x.B, CalculatedPoint = YellowCalibrationData.CalibrationPoints.Where(y => y.X == x.InkPercentage).Select(z => z.Y).FirstOrDefault() })); + } + } + } + + private async void ImportBlackData(object obj) + { + List<ColorLinearizationModel.LinearizationDataItem> items; + if (ImportDataFromFile(out items) && items.Count > 0) + { + List<double> calibrationPoints = new List<double>(); + await Task.Factory.StartNew(() => + { + calibrationPoints = GetLinearizationMeasurements(items, PMR.ColorLab.LiquidType.Black); + }); + UpdatePlots(BlackPlot, items, calibrationPoints); + + BlackCalibrationData.CalibrationPoints.Clear(); + var index = 1; + foreach (var nw in items.Zip(calibrationPoints, Tuple.Create)) + { + BlackCalibrationData.CalibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = nw.Item1.InkPercentage, Y = nw.Item2 }); + } + + SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData> data = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData; + var liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Black).FirstOrDefault(); + var blackliquidData = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData.Where(x => x.LiquidTypeGuid == liquidtype.Guid).FirstOrDefault(); + if (blackliquidData != null) + { + if (!RemovedPoints.ContainsKey(LiquidTypes.Black)) + { + RemovedPoints[LiquidTypes.Black] = new List<RmlExtensionColorCalibrationsTestsLiquidDataPoint>(blackliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.ToList()); + } + blackliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Clear(); + items.ForEach(x => blackliquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.Add(new RmlExtensionColorCalibrationsTestsLiquidDataPoint() { Ink = x.InkPercentage, L = x.L, A = x.A, B = x.B, CalculatedPoint = BlackCalibrationData.CalibrationPoints.Where(y => y.X == x.InkPercentage).Select(z => z.Y).FirstOrDefault() })); + } + } + + } + + /// <summary> + /// Open file dialog and get name of file + /// </summary> + /// <returns></returns> + private bool ImportDataFromFile(out List<ColorLinearizationModel.LinearizationDataItem> items) + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Select data file"; + dlg.Filter = "Excel |*.xlsx"; + items = null; + if (dlg.ShowDialogCenter() && dlg.FileName.IsNotNullOrEmpty()) + { + ColorLinearizationModel model = new ColorLinearizationModel(); + string errors = ""; + model.GetDataFromFile(dlg.FileName, out items, ref errors); + if (false == String.IsNullOrEmpty(errors) || items == null || items.Count == 0) + { + _notification.ShowError("An error occurred while trying to import data form the selected excel file. Please check the file format if valid and is available to read."); + return false; + } + return true; + } + + return false; + } + + public void InitData() + { + if(RmlExtensionColorCalibrationsTest != null) + { + SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData> data = RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData; + foreach( var liquidData in data) + { + var liquidGUID = liquidData.LiquidTypeGuid; + var liquidType = _liquids.Where(l => l.Guid == liquidGUID).FirstOrDefault(); + + var points = liquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.OrderBy(y => y.Ink).ToList(); + LoadDataPlots(points, GetPlot(liquidType), GetCalibrationDataVM(liquidType)); + } + } + } + + private CalibrationPlotModel GetPlot(BL.Entities.LiquidType liquidType) + { + switch(liquidType.Type) + { + case LiquidTypes.Cyan: + return CyanPlot; + case LiquidTypes.Magenta: + return MagentaPlot; + case LiquidTypes.Yellow: + return YellowPlot; + case LiquidTypes.Black: + return BlackPlot; + default: + return null; + } + } + private CalibrationDataVM GetCalibrationDataVM(BL.Entities.LiquidType liquidType) + { + switch (liquidType.Type) + { + case LiquidTypes.Cyan: + return CyanCalibrationData; + case LiquidTypes.Magenta: + return MagentaCalibrationData; + case LiquidTypes.Yellow: + return YellowCalibrationData; + case LiquidTypes.Black: + return BlackCalibrationData; + default: + return null; + } + } + private PMR.ColorLab.LiquidType ConvertLiquidTypeToPMR(BL.Entities.LiquidType liquidType) + { + if (liquidType.Type == LiquidTypes.Cyan) + return PMR.ColorLab.LiquidType.Cyan; + if (liquidType.Type == LiquidTypes.Yellow) + return PMR.ColorLab.LiquidType.Yellow; + if (liquidType.Type == LiquidTypes.Magenta) + return PMR.ColorLab.LiquidType.Magenta; + return PMR.ColorLab.LiquidType.Black; + } + + private void UpdatePlots( CalibrationPlotModel plot, List<ColorLinearizationModel.LinearizationDataItem> items, List<double> calibrationPoints) + { + plot.InitDataGraph(items); + plot.InitLinearizationGraph(items, calibrationPoints); + } + + private void LoadDataPlots( List<RmlExtensionColorCalibrationsTestsLiquidDataPoint> points, CalibrationPlotModel plot, CalibrationDataVM calibrationTable) + { + List<ColorLinearizationModel.LinearizationDataItem> items = new List<ColorLinearizationModel.LinearizationDataItem>(); + points.ForEach(x => items.Add(new ColorLinearizationModel.LinearizationDataItem() { InkPercentage = x.Ink, L = x.L, A = x.A, B = x.B })); + + List<double> calibrationPoints = new List<double>(); + + calibrationTable.CalibrationPoints.Clear(); + var index = 1; + points.ForEach(x => { + calibrationTable.CalibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = x.Ink, Y = x.CalculatedPoint }); + calibrationPoints.Add(x.CalculatedPoint); + }); + UpdatePlots(plot, items, calibrationPoints); + } + + private void OnRmlExtensionColorCalibrationsTestChanged() + { + + } + #endregion + + #region CreateLinearizationGraph + + private List<double> GetLinearizationMeasurements(List<ColorLinearizationModel.LinearizationDataItem> items, PMR.ColorLab.LiquidType liquidType) + { + 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 = liquidType; + + LinearizationOutput result = _linearizationMeasurementscalibrator.GetLinearizationMeasurements(linearizationInput); + + if (!String.IsNullOrEmpty(result.ErrorMessage)) + { + LogManager.Log(result.ErrorMessage, "GetLinearizationMeasurements returns error." + result.ErrorMessage); + InvokeUI(() => + { + _notification.ShowError("Linearizion process failed. " + result.ErrorMessage); + }); + } + + return result.InkPercentage.ToList(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error occurred while trying to call GetLinearizationMeasurements."); + } + return null; + } + + #endregion + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationViewVM.cs new file mode 100644 index 000000000..3f4d8a415 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationViewVM.cs @@ -0,0 +1,530 @@ + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.ActionLogs; +using Tango.BL.Builders; +using Tango.BL.Entities; +using Tango.Core; +using Tango.MachineStudio.Common.Notifications; +using Tango.SharedUI; +using Tango.AutoComplete.Editors; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.Logging; +using Tango.BL.Enumerations; +using Tango.PMR.ColorLab; +using Tango.BL.Calibration; + + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorCalibrationViewVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + private ObservablesContext _active_context; + private List<BL.Entities.LiquidType> _liquids; + + public event EventHandler SaveColorCalibration; + + + #region Properties + + private ObservableCollection<ColorCalibrationTabVM> _calibrationTabs; + + public ObservableCollection<ColorCalibrationTabVM> CalibrationTabs + { + get { return _calibrationTabs; } + set { _calibrationTabs = value; } + } + + private ColorCalibrationTabVM _selectedTab; + + public ColorCalibrationTabVM SelectedTab + { + get { return _selectedTab; } + set + { + _selectedTab = value; + RaisePropertyChangedAuto(); + + foreach (var tab in _calibrationTabs.Where(x => x != _selectedTab)) + { + tab.IsSelected = false; + } + + if (_selectedTab != null) + { + _selectedTab.IsSelected = true; + } + } + } + + private string _RMLExtentionGUID; + + public string RMLExtentionGUID + { + get { return _RMLExtentionGUID; } + set + { + _RMLExtentionGUID = value; + OnRMLExtensionGUIDChanged(); + } + } + + private string _RMLGUID; + + public string RMLGUID + { + get { return _RMLGUID; } + set + { + _RMLGUID = value; + OnRMLExtensionGUIDChanged(); + } + } + + private Rml _activeRML; + public Rml ActiveRML + { + get { return _activeRML; } + set + { + _activeRML = value; + RaisePropertyChangedAuto(); + } + } + + private MachineModel _machine; + public MachineModel Machine + { + get { return _machine; } + set { _machine = value; + SelectedMachineChanged(); + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + } + } + + + private bool _isViewLoaded; + /// <summary> + /// Gets or sets a value indicating whether this instance is view loaded. Used to update charts. + /// </summary> + public bool IsViewLoaded + { + get { return _isViewLoaded; } + set + { + if (_isViewLoaded != value) + { + _isViewLoaded = value; + } + } + } + + private RmlExtensionColorCalibration _rmlExtensionColorCalibration; + + public RmlExtensionColorCalibration RmlExtensionColorCalibration + { + get { return _rmlExtensionColorCalibration; } + set { _rmlExtensionColorCalibration = value; } + } + + //private ObservableCollection<RmlExtensionColorCalibrationsTest> _rmlExtensionColorCalibrationsTests; + + //public ObservableCollection<RmlExtensionColorCalibrationsTest> RmlExtensionColorCalibrationsTests + //{ + // get { return _rmlExtensionColorCalibrationsTests; } + // set { _rmlExtensionColorCalibrationsTests = value; } + //} + + + #endregion + + #region commands + public RelayCommand CreateColorDataImportExcelTemplateCommand { get; set; } + public RelayCommand ApplyToRMLCommand { get; set; } + public RelayCommand ApplyToMachineCalibrationCommand { get; set; } + + public RelayCommand SaveCommand { get; set; } + + public RelayCommand AddTabCommand { get; set; } + + public RelayCommand<ColorCalibrationTabVM> RemoveTabCommand { get; set; } + + public RelayCommand RenameTabCommand { get; set; } + + #endregion + + public ColorCalibrationViewVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + CalibrationTabs = new ObservableCollection<ColorCalibrationTabVM>(); + + ApplyToRMLCommand = new RelayCommand(ApplyToRML); + ApplyToMachineCalibrationCommand = new RelayCommand(ApplyToMachineCalibration); + SaveCommand = new RelayCommand(Save, () => IsFree); + + AddTabCommand = new RelayCommand(() => AddNewTab()); + RemoveTabCommand = new RelayCommand<ColorCalibrationTabVM>(RemoveTab); + RenameTabCommand = new RelayCommand(RenameTab); + + CreateColorDataImportExcelTemplateCommand = new RelayCommand(CreateColorDataImportExcelTemplate); + } + + #region Methods + + private void OnRMLExtensionGUIDChanged() + { + } + + private void SelectedMachineChanged() + { + SelectedTab = null; + LoadColorCalibrations(); + } + + private void CreateColorDataImportExcelTemplate() + { + SaveFileDialog dlg = new SaveFileDialog(); + try + { + dlg.Title = $"Create excel template file"; + dlg.Filter = "Excel Files|*.xlsx"; + dlg.DefaultExt = ".xlsx"; + dlg.FileName = "Color Data File Template"; + if (dlg.ShowDialog().Value) + { + CalibrationHelper.CreateColorDataInputExcelTemplate(dlg.FileName); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error generating excel calibration template file " + dlg.FileName); + _notification.ShowError("An error occurred while trying to generate the calibration file."); + } + } + /// <summary> + /// Applies calibration points to RML. + /// </summary> + private void ApplyToRML(object obj) + { + if(SelectedTab != null && ActiveRML != null) + { + var liquidTypesRmls = ActiveRML.LiquidTypesRmls; + var calibrationDataVM = SelectedTab.CyanCalibrationData; + ApplayCalibrationDataToliquidTypesRml(liquidTypesRmls, LiquidTypes.Cyan, SelectedTab.CyanCalibrationData); + ApplayCalibrationDataToliquidTypesRml(liquidTypesRmls, LiquidTypes.Magenta, SelectedTab.MagentaCalibrationData); + ApplayCalibrationDataToliquidTypesRml(liquidTypesRmls, LiquidTypes.Yellow, SelectedTab.YellowCalibrationData); + ApplayCalibrationDataToliquidTypesRml(liquidTypesRmls, LiquidTypes.Black, SelectedTab.BlackCalibrationData); + Save(); + } + } + + private void ApplayCalibrationDataToliquidTypesRml(SynchronizedObservableCollection<LiquidTypesRml> liquidTypesRmls, LiquidTypes type, CalibrationDataVM calibrationDataVM) + { + if (calibrationDataVM.CalibrationPoints.Count == 0) + return; + + var liquidTypeRml = liquidTypesRmls.SingleOrDefault(x => x.LiquidType.Type == type); + CalibrationData calData = new CalibrationData(); + calData.LiquidType = (PMR.ColorLab.LiquidType)liquidTypeRml.LiquidType.Code; + calData.CalibrationPoints.Clear(); + calData.CalibrationPoints.AddRange(calibrationDataVM.CalibrationPoints.Select(x => new CalibrationPoint() { X = x.X, Y = x.Y })); + liquidTypeRml.PutCalibrationData(calData); + } + /// <summary> + /// Applies calibration points to machine calibration. + /// </summary> + private void ApplyToMachineCalibration(object obj) + { + if (SelectedTab != null) + { + var idsPack = Machine.IdsPacks. + Where(x => ActiveRML.LiquidTypesRmls.ToList().Exists(y => x.LiquidType != null && y.LiquidType.Guid == x.LiquidType.Guid)) + .OrderBy(x => x.PackIndex).ToList(); + + ApplayCalibrationDataToToMachineRml(idsPack, LiquidTypes.Cyan, SelectedTab.CyanCalibrationData); + ApplayCalibrationDataToToMachineRml(idsPack, LiquidTypes.Magenta, SelectedTab.MagentaCalibrationData); + ApplayCalibrationDataToToMachineRml(idsPack, LiquidTypes.Yellow, SelectedTab.YellowCalibrationData); + ApplayCalibrationDataToToMachineRml(idsPack, LiquidTypes.Black, SelectedTab.BlackCalibrationData); + Save(); + } + } + + private void ApplayCalibrationDataToToMachineRml(List<IdsPack> idsPacks, LiquidTypes type, CalibrationDataVM calibrationDataVM) + { + if (calibrationDataVM.CalibrationPoints.Count == 0) + return; + + var idsPack = idsPacks.FirstOrDefault(x => x.LiquidType.Type == type); + + if (idsPack != null) + { + var cat = idsPack.LiquidType.Cats.FirstOrDefault(x => x.MachineGuid == Machine.Guid && x.RmlGuid == ActiveRML.Guid); ; + + if (cat == null) + { + cat = new Cat(); + cat.Name = "untitled"; + _active_context.Cats.Add(cat); + } + + cat.RmlGuid = ActiveRML.Guid; + cat.MachineGuid = Machine.Guid; + cat.LiquidType = idsPack.LiquidType; + + CalibrationData calData = new CalibrationData(); + calData.LiquidType = (PMR.ColorLab.LiquidType)idsPack.LiquidType.Code; + calData.CalibrationPoints.AddRange(calibrationDataVM.CalibrationPoints.Select(x => new CalibrationPoint() { X = x.X, Y = x.Y })); + + cat.PutCalibrationData(calData); + } + } + + #endregion + + #region Loading + + public async void LoadColorCalibrations() + { + if (Machine == null) + { + _notification.ShowWarning(LogManager.Log($"Please, select machine.", LogCategory.Warning)); + IsFree = false; + return; + } + if (ActiveRML == null) + { + IsFree = false; + return; + } + try + { + IsFree = false; + if (_active_context != null) + { + _active_context.Dispose(); + } + + _active_context = ObservablesContext.CreateDefault(); + if (_liquids == null) + { + _liquids = _active_context.LiquidTypes.ToList(); + } + CalibrationTabs.Clear(); + LogManager.Log("Loading color calibration tests ..."); + + using (_notification.PushTaskItem("Loading Color Calibrations Tests ...")) + { + var testResults = await new RMLExtensionColorCalibrationBuilder(_active_context).SetAll().ForRMLExtension(RMLExtentionGUID).ForMachine(Machine.Guid).WithTests().BuildAsync(); + RmlExtensionColorCalibration = testResults.OrderBy(x => x.ID).ToList().FirstOrDefault(); + if (RmlExtensionColorCalibration == null) + { + RmlExtensionColorCalibration = new RmlExtensionColorCalibration() { RmlsExtensionsGuid = RMLExtentionGUID, MachineGuid = Machine.Guid }; + _active_context.RmlExtensionColorCalibrations.Add(RmlExtensionColorCalibration); + _active_context.SaveChanges(); + } + + foreach (var test in RmlExtensionColorCalibration.RmlExtensionColorCalibrationsTests) + { + CalibrationTabs.Add(new ColorCalibrationTabVM(_notification, _actionLogManager, _liquids) { RmlExtensionColorCalibrationsTest = test, Name = test.Name }); + if (CalibrationTabs.Count == 1) + SelectedTab = CalibrationTabs[0]; + } + if (CalibrationTabs.Count == 0) + { + SelectedTab = CreateNewColorCalibrationTabVM("Untitled", 1); + CalibrationTabs.Add(SelectedTab); + _active_context.SaveChanges(); + } + if (IsViewLoaded) + { + CalibrationTabs.ToList().ForEach(x => x.InitData()); + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading Color Calibration tests.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + + private ColorCalibrationTabVM CreateNewColorCalibrationTabVM(string name, int index) + { + ColorCalibrationTabVM newtab = new ColorCalibrationTabVM(_notification, _actionLogManager, _liquids) { Name = name, TabIndex = index }; + + newtab.RmlExtensionColorCalibrationsTest = new RmlExtensionColorCalibrationsTest() + { + RmlExtensionColorCalibrationGuid = RmlExtensionColorCalibration.Guid, + Name = name + }; + var liquidDataCollection = new SynchronizedObservableCollection<RmlExtensionColorCalibrationsTestsLiquidData>(); + var liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Cyan).FirstOrDefault(); + var l_guid = liquidtype.Guid; + liquidDataCollection.Add(new RmlExtensionColorCalibrationsTestsLiquidData() { RmlExtensionColorCalibrationsTestGuid = newtab.RmlExtensionColorCalibrationsTest.Guid, LiquidTypeGuid = l_guid }); + liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Magenta).FirstOrDefault(); + l_guid = liquidtype.Guid; + liquidDataCollection.Add(new RmlExtensionColorCalibrationsTestsLiquidData() { RmlExtensionColorCalibrationsTestGuid = newtab.RmlExtensionColorCalibrationsTest.Guid, LiquidTypeGuid = l_guid}); + liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Yellow).FirstOrDefault(); + l_guid = liquidtype.Guid; + liquidDataCollection.Add(new RmlExtensionColorCalibrationsTestsLiquidData() { RmlExtensionColorCalibrationsTestGuid = newtab.RmlExtensionColorCalibrationsTest.Guid, LiquidTypeGuid = l_guid }); + liquidtype = _liquids.Where(l => l.Type == LiquidTypes.Black).FirstOrDefault(); + l_guid = liquidtype.Guid; + liquidDataCollection.Add(new RmlExtensionColorCalibrationsTestsLiquidData() { RmlExtensionColorCalibrationsTestGuid = newtab.RmlExtensionColorCalibrationsTest.Guid, LiquidTypeGuid = l_guid }); + newtab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData = liquidDataCollection; + if(RmlExtensionColorCalibration != null) + { + RmlExtensionColorCalibration.RmlExtensionColorCalibrationsTests.Add(newtab.RmlExtensionColorCalibrationsTest); + // _active_context.RmlExtensionColorCalibrations.Add(RmlExtensionColorCalibration); + } + + return newtab; + } + + #endregion + + #region Tabs modification + + /// <summary> + /// Removes the specified tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RemoveTab(ColorCalibrationTabVM tab) + { + if (CalibrationTabs.Count == 1) + return; + if (_notification.ShowQuestion("Are you sure you want to delete the selected tab?")) + { + foreach( var liquidData in tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData) + { + + _active_context.RmlExtensionColorCalibrationsTestsLiquidDataPoints.RemoveRange(liquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints); + } + _active_context.RmlExtensionColorCalibrationsTestsLiquidData.RemoveRange(tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData); + tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData = null; + + RmlExtensionColorCalibration.RmlExtensionColorCalibrationsTests.Remove(tab.RmlExtensionColorCalibrationsTest); + _active_context.RmlExtensionColorCalibrationsTests.Remove(tab.RmlExtensionColorCalibrationsTest); + + CalibrationTabs.Remove(tab); + SelectedTab = CalibrationTabs.LastOrDefault(); + _active_context.SaveChanges(); + } + } + + /// <summary> + /// Adds a new tab. + /// </summary> + private bool AddNewTab(String name = null) + { + if (CalibrationTabs.Count > 7) + { + //_notification.ShowError("Cannot exceed the maximum number of 8 tabs. You can remove a tab, or create a new project."); + return false; + } + + if (name == null) + { + name = _notification.ShowTextInput("Enter tab name", "Tab Name", "Test"); + } + + if (!String.IsNullOrWhiteSpace(name)) + { + var tab = CreateNewColorCalibrationTabVM(name, (CalibrationTabs.Count + 1)); + _active_context.SaveChanges(); + CalibrationTabs.Add(tab); + SelectedTab = tab; + return true; + } + else + { + return false; + } + } + + /// <summary> + /// Renames the tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RenameTab() + { + if (SelectedTab != null) + { + var name = _notification.ShowTextInput("Enter tab name", "Tab Name", SelectedTab.Name); + + if (!String.IsNullOrWhiteSpace(name)) + { + SelectedTab.Name = name; + } + } + } + + #endregion + + #region Save + + public async void Save() + { + if (Machine == null) + { + _notification.ShowWarning(LogManager.Log($"Could not save Color Calibrations. Please, select machine before save.", LogCategory.Warning)); + return; + } + + try + { + IsFree = false; + DateTime lastUpdated = DateTime.UtcNow; + RmlExtensionColorCalibration.LastUpdated = lastUpdated; + + foreach (var tab in CalibrationTabs) + { + tab.RmlExtensionColorCalibrationsTest.LastUpdated = lastUpdated; + var removedPoints = tab.RemovedPoints; + foreach (var liquidData in tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData) + { + var liquidtype = _liquids.Where(l => l.Guid == liquidData.LiquidTypeGuid).FirstOrDefault(); + if (liquidtype != null && removedPoints.ContainsKey(liquidtype.Type)) + { + _active_context.RmlExtensionColorCalibrationsTestsLiquidDataPoints.RemoveRange(removedPoints[liquidtype.Type]); + } + liquidData.LastUpdated = lastUpdated; + } + tab.RemovedPoints.Clear(); + } + await _active_context.SaveChangesAsync(); + + // LoadColorParameters(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not update color calibrations."); + _notification.ShowError($"An error occurred while trying to save color calibrations.\n{ex.Message}"); + } + finally + { + IsFree = true; + EventHandler handler = SaveColorCalibration; + handler?.Invoke(this, new EventArgs()); + } + } + + #endregion + + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs new file mode 100644 index 000000000..eaaf7dc96 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.SharedUI; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.BL.Enumerations; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.MachineStudio.Common.Notifications; +using Tango.BL.ActionLogs; +using Tango.BL.Entities; +using Tango.Core; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorShadeTabVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + #region Properties + + private string _name; + /// <summary> + /// Gets or sets the name of the thread. Using in print + /// </summary> + public string Name + { + get { return _name; } + set + { + _name = value; + if (RmlExtensionColorShadesTest != null) + RmlExtensionColorShadesTest.Name = _name; + RaisePropertyChangedAuto(); + } + } + + private int _tabIndex; + + public int TabIndex + { + get { return _tabIndex; } + set + { + _tabIndex = value; + RaisePropertyChangedAuto(); + } + } + + + private bool _isSelected; + /// <summary> + /// Gets or sets a value indicating whether this instance is selected. + /// </summary> + public bool IsSelected + { + get { return _isSelected; } + set { _isSelected = value; RaisePropertyChangedAuto(); } + } + + private RmlExtensionColorShadesTest _rmlExtensionColorShadesTest; + + public RmlExtensionColorShadesTest RmlExtensionColorShadesTest + { + get { return _rmlExtensionColorShadesTest; } + set + { + _rmlExtensionColorShadesTest = value; + RaisePropertyChangedAuto(); + } + } + + /// <summary> + /// Gets or sets last points before save. Used in Save to remove points before save new. + /// </summary> + /// <value> + /// The removed points. + /// </value> + public List<RmlExtensionColorShadesTestsData> RemovedDataList { get; set; } + + #endregion + + public RelayCommand ImportColorsCommand { get; set; } + + public ColorShadeTabVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + ImportColorsCommand = new RelayCommand(ImportColors); + RemovedDataList = new List<RmlExtensionColorShadesTestsData>(); + } + + private void ImportColors(object obj) + { + List<ColorShadesModel.ColorShadesDataItem> items; + + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Select data file"; + dlg.Filter = "Excel |*.xlsx"; + items = null; + if (dlg.ShowDialogCenter() && dlg.FileName.IsNotNullOrEmpty()) + { + ColorShadesModel model = new ColorShadesModel(); + string errors = ""; + model.GetDataFromFile(dlg.FileName, out items, ref errors); + if (false == String.IsNullOrEmpty(errors) || items == null || items.Count == 0) + { + _notification.ShowError("An error occurred while trying to import data form the selected excel file. Please check the file format if valid and is available to read."); + + } + else + { + if (RemovedDataList.Count == 0 && RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.Count > 0) + { + RemovedDataList = new List<RmlExtensionColorShadesTestsData>(RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.ToList()); + } + RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.Clear(); + items.ForEach(x => RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.Add(new RmlExtensionColorShadesTestsData() { + ColorNum = x.ColorNumber, L = x.L, A = x.A, B = x.B, C = x.C, M = x.M, Y = x.Y, K = x.K, Ti = x.TI, LRes = x.L2, ARes = x.A2, BRes = x.B2, DeltaE = x.DelteE })); + + } + + } + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs new file mode 100644 index 000000000..f305d5139 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs @@ -0,0 +1,400 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.ActionLogs; +using Tango.BL.Builders; +using Tango.BL.Entities; +using Tango.Core; +using Tango.MachineStudio.Common.Notifications; +using Tango.SharedUI; +using Tango.AutoComplete.Editors; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.Logging; +using Tango.MachineStudio.ThreadExtensions.ViewModels; +using System.Collections.ObjectModel; +using Tango.BL.Calibration; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorShadeViewVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + private ObservablesContext _active_context; + public event EventHandler SaveColorShadesEvent; + + #region Properties + + + private RmlExtensionColorShade _colorShade; + + public RmlExtensionColorShade RmlExtensionColorShade + { + get { return _colorShade; } + set { _colorShade = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorShadeTabVM> _colorShadeTabs; + + public ObservableCollection<ColorShadeTabVM> ColorShadeTabs + { + get { return _colorShadeTabs; } + set { _colorShadeTabs = value; } + } + + private ColorShadeTabVM _selectedTab; + + public ColorShadeTabVM SelectedTab + { + get { return _selectedTab; } + set + { + _selectedTab = value; + RaisePropertyChangedAuto(); + + foreach (var tab in _colorShadeTabs.Where(x => x != _selectedTab)) + { + tab.IsSelected = false; + } + + if (_selectedTab != null) + { + _selectedTab.IsSelected = true; + } + } + } + + private string _RMLExtentionGUID; + + public string RMLExtentionGUID + { + get { return _RMLExtentionGUID; } + set + { + _RMLExtentionGUID = value; + } + } + + private string _RMLGUID; + + public string RMLGUID + { + get { return _RMLGUID; } + set + { + _RMLGUID = value; + } + } + + protected string _selectedMachineGuid; + /// <summary> + /// Gets or sets the selected machine. + /// </summary> + public String SelectedMachineGUID + { + get { return _selectedMachineGuid; } + set + { + if (value != null && _selectedMachineGuid != value) + { + _selectedMachineGuid = value; + SelectedMachineChanged(); + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + } + } + } + + + private bool _isViewLoaded; + /// <summary> + /// Gets or sets a value indicating whether this instance is view loaded. Used to update charts. + /// </summary> + public bool IsViewLoaded + { + get { return _isViewLoaded; } + set + { + if (_isViewLoaded != value) + { + _isViewLoaded = value; + } + } + } + + #endregion + + #region commands + public RelayCommand CreateColorDataImportExcelTemplateCommand { get; set; } + + public RelayCommand SaveCommand { get; set; } + + public RelayCommand AddTabCommand { get; set; } + + public RelayCommand<ColorShadeTabVM> RemoveTabCommand { get; set; } + + public RelayCommand RenameTabCommand { get; set; } + + #endregion + + public ColorShadeViewVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + ColorShadeTabs = new ObservableCollection<ColorShadeTabVM>(); + + SaveCommand = new RelayCommand(Save, () => IsFree); + + AddTabCommand = new RelayCommand(() => AddNewTab()); + RemoveTabCommand = new RelayCommand<ColorShadeTabVM>(RemoveTab); + RenameTabCommand = new RelayCommand(RenameTab); + + CreateColorDataImportExcelTemplateCommand = new RelayCommand(CreateColorDataImportExcelTemplate); + } + + #region Loading + public async void LoadColorShades() + { + if (SelectedMachineGUID == null) + { + _notification.ShowWarning(LogManager.Log($"Please, select machine.", LogCategory.Warning)); + IsFree = false; + return; + } + if (RMLGUID == null) + { + IsFree = false; + return; + } + try + { + IsFree = false; + if (_active_context != null) + { + _active_context.Dispose(); + } + + _active_context = ObservablesContext.CreateDefault(); + + ColorShadeTabs.Clear(); + LogManager.Log("Loading color shade view ..."); + using (_notification.PushTaskItem("Loading Color Shade View ...")) + { + var testResults = await new RMLExtensionColorShadeBuilder(_active_context).SetAll().ForRMLExtension(RMLExtentionGUID).ForMachine(SelectedMachineGUID).WithTests().BuildAsync(); + RmlExtensionColorShade = testResults.OrderBy(x => x.ID).ToList().FirstOrDefault(); + if (RmlExtensionColorShade == null) + { + RmlExtensionColorShade = new RmlExtensionColorShade() { RmlsExtensionsGuid = RMLExtentionGUID, MachineGuid = SelectedMachineGUID }; + _active_context.RmlExtensionColorShades.Add(RmlExtensionColorShade); + _active_context.SaveChanges(); + } + + foreach (var test in RmlExtensionColorShade.RmlExtensionColorShadesTests) + { + ColorShadeTabs.Add(new ColorShadeTabVM(_notification, _actionLogManager) { RmlExtensionColorShadesTest = test, Name = test.Name }); + if (ColorShadeTabs.Count == 1) + SelectedTab = ColorShadeTabs[0]; + } + if (ColorShadeTabs.Count == 0) + { + SelectedTab = CreateNewColorShadeTabVM("Untitled", 1); + ColorShadeTabs.Add(SelectedTab); + _active_context.SaveChanges(); + } + if (IsViewLoaded) + { + // ColorShadeTabs.ToList().ForEach(x => x.InitData()); + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading Color Calibration tests.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + + private ColorShadeTabVM CreateNewColorShadeTabVM(string name, int index) + { + ColorShadeTabVM newtab = new ColorShadeTabVM(_notification, _actionLogManager) { Name = name, TabIndex = index }; + + newtab.RmlExtensionColorShadesTest = new RmlExtensionColorShadesTest() + { + RmlExtensionColorShadesGuid = RmlExtensionColorShade.Guid, + Name = name + }; + if (RmlExtensionColorShade != null) + { + RmlExtensionColorShade.RmlExtensionColorShadesTests.Add(newtab.RmlExtensionColorShadesTest); + } + + return newtab; + } + #endregion + + #region Methods + + private void OnRMLExtensionGUIDChanged() + { + } + + private void SelectedMachineChanged() + { + SelectedTab = null; + LoadColorShades(); + } + + private void CreateColorDataImportExcelTemplate() + { + SaveFileDialog dlg = new SaveFileDialog(); + try + { + dlg.Title = $"Create excel template file"; + dlg.Filter = "Excel Files|*.xlsx"; + dlg.DefaultExt = ".xlsx"; + dlg.FileName = "Color Shades File Template"; + if (dlg.ShowDialog().Value) + { + CalibrationHelper.CreateColorShadesInputExcelTemplate(dlg.FileName); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error generating excel color shades template file " + dlg.FileName); + _notification.ShowError("An error occurred while trying to generate the color shades file."); + } + } + + #endregion + + #region Tabs modification + + /// <summary> + /// Removes the specified tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RemoveTab(ColorShadeTabVM tab) + { + if (ColorShadeTabs.Count == 1) + return; + if (_notification.ShowQuestion("Are you sure you want to delete the selected tab?")) + { + _active_context.RmlExtensionColorShadesTestsData.RemoveRange(tab.RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.ToList()); + + //_active_context.RmlExtensionColorCalibrationsTestsLiquidData.RemoveRange(tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData); + tab.RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData = null; + + RmlExtensionColorShade.RmlExtensionColorShadesTests.Remove(tab.RmlExtensionColorShadesTest); + _active_context.RmlExtensionColorShadesTests.Remove(tab.RmlExtensionColorShadesTest); + + ColorShadeTabs.Remove(tab); + SelectedTab = ColorShadeTabs.LastOrDefault(); + _active_context.SaveChanges(); + } + } + + /// <summary> + /// Adds a new tab. + /// </summary> + private bool AddNewTab(String name = null) + { + if (ColorShadeTabs.Count > 7) + { + //_notification.ShowError("Cannot exceed the maximum number of 8 tabs. You can remove a tab, or create a new project."); + return false; + } + + if (name == null) + { + name = _notification.ShowTextInput("Enter tab name", "Tab Name", "Test"); + } + + if (!String.IsNullOrWhiteSpace(name)) + { + var tab = CreateNewColorShadeTabVM(name, (ColorShadeTabs.Count + 1)); + _active_context.SaveChanges(); + ColorShadeTabs.Add(tab); + SelectedTab = tab; + return true; + } + else + { + return false; + } + } + + /// <summary> + /// Renames the tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RenameTab() + { + if (SelectedTab != null) + { + var name = _notification.ShowTextInput("Enter tab name", "Tab Name", SelectedTab.Name); + + if (!String.IsNullOrWhiteSpace(name)) + { + SelectedTab.Name = name; + } + } + } + + #endregion + + #region Save + + public async void Save() + { + if (SelectedMachineGUID == null) + { + _notification.ShowWarning(LogManager.Log($"Could not save Color Shades. Please, select machine before save.", LogCategory.Warning)); + return; + } + + try + { + IsFree = false; + DateTime lastUpdated = DateTime.UtcNow; + RmlExtensionColorShade.LastUpdated = lastUpdated; + + foreach (var tab in ColorShadeTabs) + { + tab.RmlExtensionColorShadesTest.LastUpdated = lastUpdated; + var removedPoints = tab.RemovedDataList; + if (removedPoints.Count > 0) + { + _active_context.RmlExtensionColorShadesTestsData.RemoveRange(removedPoints); + } + tab.RemovedDataList.Clear(); + } + await _active_context.SaveChangesAsync(); + + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not update color shades."); + _notification.ShowError($"An error occurred while trying to save color shades.\n{ex.Message}"); + } + finally + { + IsFree = true; + EventHandler handler = SaveColorShadesEvent; + handler?.Invoke(this, new EventArgs()); + } + } + + #endregion + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs index 4802e0af1..47965deb4 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs @@ -257,6 +257,21 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _testResultsViewVM = value; RaisePropertyChangedAuto(); } } + private ColorCalibrationViewVM _colorCalibrationViewVM; + public ColorCalibrationViewVM ColorCalibrationViewVM + { + get { return _colorCalibrationViewVM; } + set { _colorCalibrationViewVM = value; RaisePropertyChangedAuto(); } + } + + private ColorShadeViewVM _solorShadeViewVM; + public ColorShadeViewVM ColorShadeViewVM + { + get { return _solorShadeViewVM; } + set { _solorShadeViewVM = value; RaisePropertyChangedAuto(); } + } + + protected MachineModel _selectedMachine; /// <summary> /// Gets or sets the selected machine. @@ -294,20 +309,20 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels } - private MachineTestResultsTabs PreviosSelectedTab { get; set; } + //private MachineTestResultsTabs PreviosSelectedTab { get; set; } - private MachineTestResultsTabs _selectedTab; + //private MachineTestResultsTabs _selectedTab; - public MachineTestResultsTabs SelectedTab - { - get { return _selectedTab; } - set { - PreviosSelectedTab = _selectedTab; - _selectedTab = value; - OnSelectedMachineTestResultsTabChanged(); - RaisePropertyChangedAuto(); - } - } + //public MachineTestResultsTabs SelectedTab + //{ + // get { return _selectedTab; } + // set { + // PreviosSelectedTab = _selectedTab; + // _selectedTab = value; + // OnSelectedMachineTestResultsTabChanged(); + // RaisePropertyChangedAuto(); + // } + //} #endregion @@ -922,7 +937,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels { Guid = x.Guid, Name = x.Name, - SerialNumber = x.SerialNumber + SerialNumber = x.SerialNumber, + IdsPacks = x.Configuration.IdsPacks.Where(z => !z.IsEmpty) }).ToObservableCollection(); } @@ -994,9 +1010,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels .WithUser() .BuildAsync(); - ActiveRML = new RmlBuilder(_active_context) - .Set(SelectedRMLExtension.RMLGuid) - .Build(); + ActiveRML = new RmlBuilder(_active_context).Set(SelectedRMLExtension.RMLGuid).WithLiquidFactors().Build(); if (!String.IsNullOrEmpty(ActiveRML.Manufacturer) && false == Manufacturers.Any(x => x == ActiveRML.Manufacturer)) { @@ -1040,12 +1054,27 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels TestResultsViewVM.SelectedMachineGUID = SelectedMachine != null ? SelectedMachine.Guid : null; TestResultsViewVM.ThreadName = ActiveRML.Manufacturer; + ColorCalibrationViewVM = new ColorCalibrationViewVM(_notification, _actionLogManager); + ColorCalibrationViewVM.RMLExtentionGUID = guid; + ColorCalibrationViewVM.ActiveRML = ActiveRML; + ColorCalibrationViewVM.RMLGUID = ActiveRML.Guid; + ColorCalibrationViewVM.Machine = SelectedMachine; + + ColorShadeViewVM = new ColorShadeViewVM(_notification, _actionLogManager); + ColorShadeViewVM.RMLExtentionGUID = guid; + ColorShadeViewVM.RMLGUID = ActiveRML.Guid; + ColorShadeViewVM.SelectedMachineGUID = SelectedMachine != null ? SelectedMachine.Guid : null; ; + if (ActiveRMLExtension.RMLStatus == RMLExtensionStatus.New) { ColorParametersVewVM.SaveColorParameters -= UpdateStatus; ColorParametersVewVM.SaveColorParameters += UpdateStatus; TestResultsViewVM.SaveTestResults -= UpdateStatus; TestResultsViewVM.SaveTestResults += UpdateStatus; + ColorCalibrationViewVM.SaveColorCalibration -= UpdateStatus; + ColorCalibrationViewVM.SaveColorCalibration += UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent -= UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent += UpdateStatus; } View.NavigateTo(RMLExtensionNavigationView.RMLExtentionView); @@ -1108,6 +1137,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels { ColorParametersVewVM.SaveColorParameters -= UpdateStatus; TestResultsViewVM.SaveTestResults -= UpdateStatus; + ColorCalibrationViewVM.SaveColorCalibration -= UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent -= UpdateStatus; ActiveRMLExtension.RMLStatus = RMLExtensionStatus.InProgress; ActiveRMLExtension.LastUpdated = DateTime.UtcNow; @@ -1134,6 +1165,14 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels { TestResultsViewVM.SelectedMachineGUID = SelectedMachine.Guid; } + if(ColorCalibrationViewVM != null) + { + ColorCalibrationViewVM.Machine = SelectedMachine; + } + if(ColorShadeViewVM != null) + { + ColorShadeViewVM.SelectedMachineGUID = SelectedMachine.Guid; + } } #endregion @@ -1183,10 +1222,10 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels #region selections - private void OnSelectedMachineTestResultsTabChanged() - { - if(SelectedTab == MachineTestResultsTabs.ColorParameters) - { + //private void OnSelectedMachineTestResultsTabChanged() + // { + //if(SelectedTab == MachineTestResultsTabs.ColorParameters) + // { //if(PreviosSelectedTab == MachineTestResultsTabs.TestResults && TestResultsViewVM != null) //{ // TestResultsViewVM.Save(); @@ -1194,15 +1233,15 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels //save //if (_notification.ShowQuestion("Are you sure you want to exit this page without saving changes?")) - } - else if(SelectedTab == MachineTestResultsTabs.TestResults) - { + // } + // else if(SelectedTab == MachineTestResultsTabs.TestResults) + // { //if (PreviosSelectedTab == MachineTestResultsTabs.ColorParameters && ColorParametersVewVM != null) //{ // ColorParametersVewVM.Save(); //} - } - } + //} + // } #endregion @@ -1304,6 +1343,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels colorParametrsExcelList.Add(colorParametrsExcelModel); TestResultsViewVM.LoadTestResultsExcel(testResultsExcelModelList, machine.Guid, machine.SerialNumber); + //ColorCalibrationViewVM.WritetoExcel } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultViewVM.cs index 41be789ed..68a886f99 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultViewVM.cs @@ -1,8 +1,11 @@ using Microsoft.Win32; +using Microsoft.WindowsAPICodePack.Dialogs; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Data.Entity; +using System.Diagnostics; using System.Text; using System.Threading.Tasks; using Tango.BL; @@ -10,6 +13,7 @@ using Tango.BL.ActionLogs; using Tango.BL.DTO; using Tango.BL.Entities; using Tango.BL.Enumerations; +using Tango.BL.ValueObjects; using Tango.Core.Commands; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.ThreadExtensions.Models; @@ -21,9 +25,9 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels { private INotificationProvider _notification; private IActionLogManager _actionLogManager; - + #region Properties - + private string _threadName; /// <summary> /// Gets or sets the name of the thread. Using in print @@ -51,7 +55,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels get { return _isSelected; } set { _isSelected = value; RaisePropertyChangedAuto(); } } - + private RmlExtensionTestResult _testResult; public RmlExtensionTestResult TestResult @@ -59,19 +63,124 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels get { return _testResult; } set { _testResult = value; RaisePropertyChangedAuto(); + RaisePropertyChanged(nameof(TestResultsFiles)); } } - - #endregion + public List<RmlExtensionTestResultsFile> TestResultsFiles + { + get + { + return TestResult.RmlExtensionTestResultsFiles.ToList(); + } + } + public RelayCommand<RmlExtensionTestResultsFile> DeleteCommand { get; set; } + public RelayCommand<RmlExtensionTestResultsFile> DownLoadFileCommand { get; set; } + public RelayCommand UploadCommand { get; set; } + public RelayCommand DownLoadAllCommand { get; set; } + #endregion + public TestResultViewVM(INotificationProvider notification, IActionLogManager actionLogManager) { _notification = notification; _actionLogManager = actionLogManager; + + UploadCommand = new RelayCommand(UploadFiles); + DownLoadFileCommand = new RelayCommand<RmlExtensionTestResultsFile>(DownLoadFile); + DeleteCommand = new RelayCommand<RmlExtensionTestResultsFile>(DeleteFile); + DownLoadAllCommand = new RelayCommand(DownLoadAllFiles); } + + #region TestResultsFiles + private async void UploadFiles(object obj) + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Select data file"; + dlg.Filter = "CSV Files|*.csv"; + dlg.Multiselect = true; + if (dlg.ShowDialog().Value) + { + try + { + var files = dlg.FileNames.ToList(); + + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + var testResult = await db.RmlExtensionTestResults.Where(x => x.Guid == TestResult.Guid).Include(t1 => t1.RmlExtensionTestResultsFiles).FirstOrDefaultAsync(); + foreach (var strpath in files) + { + var testResultfile = new RmlExtensionTestResultsFile(); + testResultfile.FileName = Path.GetFileName(strpath); + //temporary!!! + testResultfile.FilePath = strpath; + + // TestResult.RmlExtensionTestResultsFiles.Add(testResultfile); + testResult.RmlExtensionTestResultsFiles.Add(testResultfile); + } + if (testResult != null) + { + await db.SaveChangesAsync(); + } + TestResult.RmlExtensionTestResultsFiles = testResult.RmlExtensionTestResultsFiles;///????? + RaisePropertyChanged(nameof(TestResultsFiles)); + } + _notification.ShowInfo("File successfully loaded."); + } + catch (Exception ex) + { + _notification.ShowError($"An error occurred while trying to import the file.\n{ex.FlattenMessage()}"); + } + } + + } + + private void DownLoadFile(RmlExtensionTestResultsFile file) + { + SaveFileDialog dlg = new SaveFileDialog(); + dlg.Title = "Save the csv file"; + dlg.Filter = "CSV Files|*.csv"; + dlg.DefaultExt = ".csv"; + dlg.FileName = file.FileName; + if (dlg.ShowDialog().Value) + { + /// + } + } + + private void DownLoadAllFiles() + { + CommonOpenFileDialog dlg = new CommonOpenFileDialog(); + dlg.Title = "Select folder."; + dlg.IsFolderPicker = true; + if (dlg.ShowDialog() == CommonFileDialogResult.Ok) + { + var filesPath = TestResult.RmlExtensionTestResultsFiles.Select( x=>x.FilePath).ToList(); + ///// + } + } + + private async void DeleteFile(RmlExtensionTestResultsFile file) + { + if (_notification.ShowQuestion("Are you sure you want to delete the selected file?")) + { + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + var deletefile = db.RmlExtensionTestResultsFiles.FirstOrDefault(x => x.Guid == file.Guid); + if(deletefile != null) + { + db.RmlExtensionTestResultsFiles.Remove(deletefile); + await db.SaveChangesAsync(); + } + } + TestResult.RmlExtensionTestResultsFiles.Remove(file); + RaisePropertyChanged(nameof(TestResultsFiles)); + } + } + #endregion + } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultsViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultsViewVM.cs index c1fb4497f..f639eb6e7 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultsViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/TestResultsViewVM.cs @@ -1,6 +1,8 @@ -using System; +using Microsoft.Win32; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -271,7 +273,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels using (_notification.PushTaskItem("Loading Test Results Parameters ...")) { - var testResults = await new RMLExtensionTestResultsCollectionBuilder(_active_context).SetAll().ForRMLExtension(RMLExtemtionGUID).ForMachine(SelectedMachineGUID).WithRubbingAndTensileResults().BuildAsync(); + var testResults = await new RMLExtensionTestResultsCollectionBuilder(_active_context).SetAll().ForRMLExtension(RMLExtemtionGUID).ForMachine(SelectedMachineGUID).WithRubbingAndTensileResults().WithTestResultsFiles().BuildAsync(); SelectedTestResults = testResults.OrderBy(x => x.ResultIndex).ToSynchronizedObservableCollection(); foreach (var result in SelectedTestResults) { @@ -403,6 +405,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels #endregion + #region Excel + public async void LoadTestResultsExcel(List<TestResultsExcelModel> testResultsExcelModelList, string machineGUID, string machineSerialNumber) { try @@ -556,5 +560,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels IsFree = true; } } + + #endregion + } } |
