From d33c19b3ac6803de4b5c8d475832efef131c1a45 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Wed, 30 Dec 2020 15:11:34 +0000 Subject: Revert "Hope it is fine" --- .../ViewModels/CalibrationDataViewVM.cs | 10 + .../ViewModels/ColorCalibrationViewVM.cs | 551 +++++++++++++++++++++ .../ViewModels/ColorConversionViewVM.cs | 16 +- .../ViewModels/LiquidVolumeVM.cs | 13 + .../ViewModels/MainViewVM.cs | 420 ++++++++++++---- .../ViewModels/RmlDeleteDialogViewVM.cs | 95 ++++ 6 files changed, 1004 insertions(+), 101 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RmlDeleteDialogViewVM.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/CalibrationDataViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/CalibrationDataViewVM.cs index b34ef83bc..fd69fef35 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/CalibrationDataViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/CalibrationDataViewVM.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.BL.Calibration; +using Tango.BL.Entities; using Tango.Core.Commands; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Notifications; @@ -106,5 +107,14 @@ namespace Tango.MachineStudio.RML.ViewModels _notification.ShowError("An error occurred while trying to export the calibration data."); } } + + public void ApplyCalibrationData(LiquidType liquidType, List newpoints) + { + CalibrationDataVM vm = LiquidsCalibrationData.FirstOrDefault(x => x.LiquidType == liquidType); + if(vm != null) + { + newpoints.ForEach(x => vm.CalibrationPoints.Add(x)); + } + } } } 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 new file mode 100644 index 000000000..f04eccd90 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -0,0 +1,551 @@ +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.Core.Commands; +using Tango.MachineStudio.RML.Models; +using Tango.SharedUI; +using OxyPlot; +using OxyPlot.Wpf; +using OxyPlot.Annotations; +using Tango.ColorCalibration; +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 + + private Rml _rml; + public Rml RML + { + get { return _rml; } + set { _rml = value; RaisePropertyChangedAuto(); } + } + + private BL.Entities.LiquidType _liquidType; + + public BL.Entities.LiquidType LiquidType + { + get { return _liquidType; } + set { _liquidType = value; RaisePropertyChangedAuto(); RaisePropertyChanged("LiquidTypeName"); } + } + + private List _liquidTypes; + + public List LiquidTypes + { + get { return _liquidTypes; } + set { _liquidTypes = value; } + } + + + private ObservableCollection _measurements; + public ObservableCollection Measurements + { + get + { + return _measurements; + } + set + { + _measurements = value; RaisePropertyChangedAuto(); + } + } + + + private double _factor; + + public double Factor + { + get { return _factor; } + set { _factor = value; RaisePropertyChangedAuto(); } + } + + private string _errorMessage; + + public string ErrorMessage + { + get { return _errorMessage; } + set { _errorMessage = value; RaisePropertyChangedAuto(); } + } + private bool _hasError; + + public bool HasError + { + get { return _hasError; } + set { _hasError = value; RaisePropertyChangedAuto(); } + } + + public RelayCommand ImportDataCommand { get; set; } + public RelayCommand CreateGraphCommand { get; set; } + public RelayCommand CreateLinearizationGraphCommand { get; set; } + public RelayCommand ExportGraphCommand { get; set; } + public RelayCommand ApplyCalibrationDataCommand { get; set; } + + + public string LiquidTypeName + { + get { return LiquidType == null ?"" : LiquidType.Name; } + } + + public Plot PlotControl { get; set; } + private IList _points; + /// + /// Binding to ItemsSource of line chart. + /// + public IList Points + { + get { return _points; } + set + { + _points = value; + RaisePropertyChangedAuto(); + } + } + private IList _targetPoints; + /// + /// Binding to ItemsSource of line chart. + /// + public IList TargetPoints + { + get { return _targetPoints; } + set + { + _targetPoints = value; + RaisePropertyChangedAuto(); + } + } + private int _step; + public int XStep + { + get { return _step; } + set { _step = value; RaisePropertyChangedAuto(); } + } + + private double _from; + /// + /// From use to binding to bottom axis min value + /// + public double From + { + get { return _from; } + set + { + _from = value; RaisePropertyChangedAuto(); + } + } + + private double _to; + /// + /// To use to binding to bottom axis max value + /// + public double To + { + get { return _to; } + set + { + _to = value; RaisePropertyChangedAuto(); + } + } + + private double _LabMinVal; + + public double LabMinVal + { + get { return _LabMinVal; } + set { _LabMinVal = value; RaisePropertyChangedAuto(); } + } + + private double _LabMaxVal; + + public double LabMaxVal + { + get { return _LabMaxVal; } + set { _LabMaxVal = value; RaisePropertyChangedAuto(); } + } + + public Plot LABLinearizationPlotControl { get; set; } + public Plot LinearizationPlotControl { get; set; } + private IList _linearizationPoints; + /// + /// Binding to ItemsSource of line chart. + /// + public IList LinearizationPoints + { + get { return _linearizationPoints; } + set + { + _linearizationPoints = value; + RaisePropertyChangedAuto(); + } + } + + private IList _LPoints; + + public IList LPoints + { + get { return _LPoints; } + set { _LPoints = value; } + } + + private IList _APoints; + + public IList APoints + { + get { return _APoints; } + set { _APoints = value; } + } + + private IList _BPoints; + + public IList BPoints + { + get { return _BPoints; } + set { _BPoints = value; } + } + + + #endregion + + public ColorCalibrationViewVM(INotificationProvider notification) + { + _notification = notification; + Measurements = new ObservableCollection() + { + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + }; + Factor = 0; + HasError = false; + ImportDataCommand = new RelayCommand(ImportDataForCalcFactorExcel); + CreateGraphCommand = new RelayCommand(CreateGraph); + CreateLinearizationGraphCommand = new RelayCommand(CreateLinearizationGraph); + ExportGraphCommand = new RelayCommand(ExportGraph); + ApplyCalibrationDataCommand = new RelayCommand(ApplyCalibrationData); + this.Points = new List(); + TargetPoints = new List(); + LinearizationPoints = new List(); + LPoints = new List(); + APoints = new List(); + BPoints = new List(); + } + + public void Loading() + { + LiquidType = LiquidTypes.Count > 0 ? LiquidTypes[0] : null; + _calibrator = new DefaultColorCalibrator(); + _linearizationMeasurementscalibrator = new DefaultColorCalibrator(); + + } + + public void ClearResults() + { + Points.Clear(); + TargetPoints.Clear(); + ErrorMessage = ""; + HasError = false; + Factor = 0.0; + } + + private double GetLiquidFactor() + { + try + { + CalibrationInput conversionInput = new CalibrationInput(); + Measurements.ToList().ForEach(x => conversionInput.Measurements.Add(new CalibrationMeasurement { L = x.L, A = x.A, B = x.B, NanoliterPerCentimeter = x.Ink })); + conversionInput.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)) + { + conversionInput.TargetL = lab.L; + conversionInput.TargetA = lab.A; + conversionInput.TargetB = lab.B; + } + CalibrationOutput result = _calibrator.GetLiquidFactor(conversionInput); + + ErrorMessage = result.ErrorMessage; + HasError = false == String.IsNullOrEmpty(ErrorMessage); + + return result.LiquidFactor; + } + catch (Exception ex ) + { + HasError = true; + ErrorMessage = "Error occurred while trying to call GetLiquidFactor."; + LogManager.Log(ex, "Error occurred while trying to call GetLiquidFactor."); + } + return 0.0; + } + + private void ImportDataForCalcFactorExcel() + { + OpenFileDialog dlg = new OpenFileDialog(); + + try + { + dlg.Title = $"Import excel file for calculate Factor"; + dlg.Filter = "Excel Files|*.xlsx"; + if (dlg.ShowDialog().Value) + { + ClearResults(); + + List items;//List items + ColorLinearizationModel model = new ColorLinearizationModel(); + string error = ""; + model.GetDataFromFile(dlg.FileName, out items, ref error); + if (false == String.IsNullOrEmpty(error) || 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; + } + Measurements.Clear(); + items.ForEach(x => Measurements.Add(new CalibrationMeasurementModel(x))); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error importing excel file " + dlg.FileName); + _notification.ShowError("An error occurred while trying to import the selected excel file. Please check the file format if valid and is available to read."); + } + } + #region CreateGraph + + private async void CreateGraph(object obj) + { + if (_liquidType == null) + return; + ClearResults(); + PlotControl.InvalidatePlot(true); + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + await Task.Factory.StartNew(() => + { + Factor = GetLiquidFactor( ); + }); + + DataPoint targetPoint = GetTargetPoint(); + + To = 0; + From = 0; + + Measurements.ToList().ForEach(x =>{ Points.Add(new DataPoint(x.Ink, labType == "L" ? x.L : x.B)); + TargetPoints.Add(new DataPoint(x.Ink, targetPoint.Y)); }); + + _to = labType == "L"? 100 : 128; + _from = labType == "L" ? 0 : -127; + + RaisePropertyChanged("To"); + RaisePropertyChanged("From"); + XStep = (int)(Points.Count / 6); + RaisePropertyChanged("CalibrationType"); + PlotControl.InvalidatePlot(true); + + } + + private DataPoint GetTargetPoint() + { + var listValues = Measurements.ToList(); + if (listValues.Count == 0 || Factor <=0) + return new DataPoint(0, 0); + + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + + var left = listValues.Where(y => y.Ink == listValues.Min(x => x.Ink)).ToList().First(); + var right = listValues.Where(y => y.Ink == listValues.Max(x => x.Ink)).ToList().First(); + // Normalize start/end to left right to make the offset calc simpler. + DataPoint leftPoint = new DataPoint(left.Ink, labType == "L" ? left.L : left.B); + DataPoint rightPoint = new DataPoint(right.Ink, labType == "L" ? right.L : right.B); + + double deltaX = rightPoint.X - leftPoint.X; + double deltaY = rightPoint.Y - leftPoint.Y; + + // prevents division by zero exceptions. + if (deltaX == 0 ) + return new DataPoint(0, 0); + + double slope = deltaY / deltaX; + double offset = leftPoint.Y - leftPoint.X * slope; + double calculatedY = Factor * slope + offset; + + return new DataPoint(Factor, calculatedY); ; + } + + #endregion + #region CreateLinearizationGraph + + private async void CreateLinearizationGraph(object obj) + { + if (_liquidType == null ) + return; + + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + + ColorLinearizationModel model = new ColorLinearizationModel(); + string fileName = GetInkDataFileOpen();// @"C:\Test\Test Input Lineration.xlsx";//dialog to open file + if (fileName == null) + return; + + LinearizationPoints.Clear(); + LinearizationPlotControl.InvalidatePlot(true); + LPoints.Clear(); + APoints.Clear(); + BPoints.Clear(); + LabMinVal = LabMaxVal = 0; + LABLinearizationPlotControl.InvalidatePlot(true); + + List items; + string errors = ""; + model.GetDataFromFile(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; + } + + List outputPoints = new List(); + + await Task.Factory.StartNew(() => + { + outputPoints = GetLinearizationMeasurements(items); + }); + + LabMinVal = items.Min(x => Math.Min(x.L, Math.Min(x.A, x.B))); + LabMaxVal = items.Max(x => Math.Max(x.L, Math.Max(x.A, x.B))); + foreach (var labItem in items) + { + LPoints.Add(new DataPoint(labItem.InkPercentage, labItem.L)); + APoints.Add(new DataPoint(labItem.InkPercentage, labItem.A)); + BPoints.Add(new DataPoint(labItem.InkPercentage, labItem.B)); + } + LABLinearizationPlotControl.InvalidatePlot(true); + + 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); + } + + /// + /// Open file dialog and get name of file + /// + /// + 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; + + LinearizationOutput result = _linearizationMeasurementscalibrator.GetLinearizationMeasurements(linearizationInput); + + if(!String.IsNullOrEmpty(result.ErrorMessage)) + { + LogManager.Log(result.ErrorMessage, "GetLinearizationMeasurements returns error." + result.ErrorMessage); + InvokeUI(() => + { + _notification.ShowError("Linearization failed. " + result.ErrorMessage); + }); + } + + LAB lab; + if (ColorCalibrationExt.TargetLiquidTypeToLAB.TryGetValue(_liquidType.Type, out lab)) + { + linearizationInput.TargetL = lab.L; + linearizationInput.TargetA = lab.A; + linearizationInput.TargetB = lab.B; + } + return result.InkPercentage.ToList(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error occurred while trying to call GetLinearizationMeasurements."); + } + return null; + } + #endregion + + #region Export graph + /// + /// Exports the graph point to Excel file. + /// + 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, + }; + } + + /// + /// Applies the calibration data. + /// + protected void ApplyCalibrationData() + { + if (LinearizationPoints.Count == 0) + return; + + List points = new List(); + LinearizationPoints.ToList().ForEach(x => points.Add(new CalibrationDataPointVM(x.X, x.Y))); + ViewModelLocator.MainViewVM.CalibrationDataViewVM.ApplyCalibrationData(LiquidType, points); + } + #endregion + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorConversionViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorConversionViewVM.cs index b68239b4b..95de19f42 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorConversionViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorConversionViewVM.cs @@ -111,6 +111,7 @@ namespace Tango.MachineStudio.RML.ViewModels Color = x.LiquidType.Color, Name = x.LiquidType.Name, LiquidType = x.LiquidType, + MaxNanoliterPerCentimeter = x.MaxNlPerCm, }).ToObservableCollection(); } @@ -309,6 +310,19 @@ namespace Tango.MachineStudio.RML.ViewModels } } + private double GetTotalMaximumLiquidNlPerCMLimit() + { + try + { + var tables = RML.GetActiveProcessGroup().ProcessParametersTables.OrderBy(x => x.TableIndex).ToList(); + return tables.Max(x => x.MaxInkUptake); + } + catch + { + return BrushStop.MAX_INK_UPTAKE; + } + } + private void OnLiquidVolumeChanged() { try @@ -316,7 +330,7 @@ namespace Tango.MachineStudio.RML.ViewModels if (LiquidsCalibrationData == null || _prevent_inverse_conversion) return; //TODO: This is temporary because of out of range volumes. - if (LiquidVolumes.Where(x => x.LiquidType.HasPigment).Sum(x => x.Volume) > 200) + if (LiquidVolumes.Where(x => x.LiquidType.HasPigment).Sum(x => x.NanoliterPerCentimeter) > GetTotalMaximumLiquidNlPerCMLimit()) { IsVolumesOutOfRange = true; return; diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/LiquidVolumeVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/LiquidVolumeVM.cs index 7399d62af..27b811bb6 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/LiquidVolumeVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/LiquidVolumeVM.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL.Dispensing; using Tango.BL.Entities; using Tango.Core; using Tango.SharedUI; @@ -47,5 +48,17 @@ namespace Tango.MachineStudio.RML.ViewModels get { return _liquidType; } set { _liquidType = value; RaisePropertyChangedAuto(); } } + + public double MaxNanoliterPerCentimeter { get; set; } + + public double NanoliterPerCentimeter + { + get + { + StandardColorDispensingCalc calc = new StandardColorDispensingCalc(); + return calc.CalculateNanoliterPerCentimeter(Volume, MaxNanoliterPerCentimeter); + } + } + } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs index ee21b9ac3..cadd1fb95 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs @@ -21,12 +21,19 @@ using Tango.MachineStudio.RML.Views; using Tango.PMR.ColorLab; using System.Data.Entity; using Tango.Core.ExtensionMethods; +using Tango.MachineStudio.Common.Authentication; +using Tango.BL.ActionLogs; +using Tango.BL.DTO; +using Tango.BL.Enumerations; namespace Tango.MachineStudio.RML.ViewModels { public class MainViewVM : StudioViewModel { private INotificationProvider _notification; + private IAuthenticationProvider _authentication; + private IActionLogManager _actionLogManager; + private RmlDTO _rmlBeforeSave; private ObservablesContext _rmls_context; private ObservablesContext _active_context; @@ -38,6 +45,20 @@ namespace Tango.MachineStudio.RML.ViewModels set { _rmls = value; RaisePropertyChangedAuto(); } } + private ICollectionView _rmlssCollectionView; + /// + /// Gets or sets the RML collection view. + /// + public ICollectionView RmlsCollectionView + { + get { return _rmlssCollectionView; } + set + { + _rmlssCollectionView = value; + RaisePropertyChangedAuto(); + } + } + private ObservableCollection _materials; public ObservableCollection Materials { @@ -80,6 +101,13 @@ namespace Tango.MachineStudio.RML.ViewModels set { _fiberSynths = value; RaisePropertyChangedAuto(); } } + private ObservableCollection _spoolTypes; + public ObservableCollection SpoolTypes + { + get { return _spoolTypes; } + set { _spoolTypes = value; RaisePropertyChangedAuto(); } + } + private Rml _selectedRML; public Rml SelectedRML { @@ -143,6 +171,30 @@ namespace Tango.MachineStudio.RML.ViewModels set { _colorConversionViewVM = value; RaisePropertyChangedAuto(); } } + private RmlsSpool _selectedSpool; + public RmlsSpool SelectedSpool + { + get { return _selectedSpool; } + set { _selectedSpool = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private ColorCalibrationViewVM _colorCalibrationVM; + public ColorCalibrationViewVM ColorCalibrationVM + { + get { return _colorCalibrationVM; } + set { _colorCalibrationVM = value; RaisePropertyChangedAuto(); } + } + + private String _RMLFilter; + /// + /// Gets or sets the job filter. + /// + public String RMLFilter + { + get { return _RMLFilter; } + set { _RMLFilter = value; RaisePropertyChangedAuto(); OnRMLFilterChanged(); } + } + /// /// Gets or sets the manage RML command. /// @@ -185,9 +237,21 @@ namespace Tango.MachineStudio.RML.ViewModels public RelayCommand ImportRMLFileCommand { get; set; } - public MainViewVM(INotificationProvider notificationProvider) + /// + /// Gets or sets the add spool command. + /// + public RelayCommand AddSpoolCommand { get; set; } + + /// + /// Gets or sets the remove spool command. + /// + public RelayCommand RemoveSpoolCommand { get; set; } + + public MainViewVM(INotificationProvider notificationProvider, IAuthenticationProvider authentication, IActionLogManager actionLogManager) { _notification = notificationProvider; + _authentication = authentication; + _actionLogManager = actionLogManager; ManageRmlCommand = new RelayCommand(() => LoadActiveRML(SelectedRML.Guid), () => SelectedRML != null); RemoveRmlCommand = new RelayCommand(RemoveSelectedRml, () => SelectedRML != null); CloneRmlCommand = new RelayCommand(CloneSelectedRml, () => SelectedRML != null); @@ -206,6 +270,9 @@ namespace Tango.MachineStudio.RML.ViewModels ExportRMLFileCommand = new RelayCommand(ExportRmlFile, () => SelectedRML != null && IsFree); ImportRMLFileCommand = new RelayCommand(ImportRmlFile, () => IsFree); + + AddSpoolCommand = new RelayCommand(AddNewSpool); + RemoveSpoolCommand = new RelayCommand(RemoveSpool, () => SelectedSpool != null); } public override void OnApplicationReady() @@ -215,130 +282,177 @@ namespace Tango.MachineStudio.RML.ViewModels private async void LoadRmls() { - if (_rmls_context != null) _rmls_context.Dispose(); + try + { + IsFree = false; - _rmls_context = ObservablesContext.CreateDefault(); - Rmls = await new RmlsCollectionBuilder(_rmls_context).SetAll().WithLiquidFactors().WithMediaProperties().BuildAsync(); + using (_notification.PushTaskItem("Loading Rmls...")) + { + if (_rmls_context != null) _rmls_context.Dispose(); - //Load CCT file names... - var ccts = await _rmls_context.Ccts.Select(x => new - { - x.Guid, - x.FileName - }).ToListAsync(); + _rmls_context = ObservablesContext.CreateDefault(); + Rmls = await new RmlsCollectionBuilder(_rmls_context).SetAll().WithLiquidFactors().WithMediaProperties().BuildAsync(); + //Load CCT file names... + var ccts = await _rmls_context.Ccts.Select(x => new + { + x.Guid, + x.FileName + }).ToListAsync(); - foreach (var rml in Rmls) - { - var cct = ccts.SingleOrDefault(x => x.Guid == rml.CctGuid); + foreach (var rml in Rmls) + { + var cct = ccts.SingleOrDefault(x => x.Guid == rml.CctGuid); - if (cct != null) - { - rml.Cct = new Cct() + if (cct != null) + { + rml.Cct = new Cct() + { + Guid = cct.Guid, + FileName = cct.FileName, + }; + } + } + RmlsCollectionView = CollectionViewSource.GetDefaultView(Rmls); + RmlsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Rml.LastUpdated), ListSortDirection.Descending)); + //RmlsCollectionView.Filter = new Predicate(FilterCollection); + + RmlsCollectionView.Filter = (rml) => { - Guid = cct.Guid, - FileName = cct.FileName, + Rml r = rml as Rml; + return String.IsNullOrWhiteSpace(RMLFilter) + || r.Name.ToLower().Contains(RMLFilter.ToLower()); }; + } } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading RMLS.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } } private async void LoadActiveRML(String guid) { using (_notification.PushTaskItem("Loading RML...")) { - IsFree = false; - - if (_active_context != null) + try { - _active_context.Dispose(); - } - - _active_context = ObservablesContext.CreateDefault(); + IsFree = false; - CCTS = _active_context.Ccts - .Select(x => new CctModel() + if (_active_context != null) { - Guid = x.Guid, - FileName = x.FileName, + _active_context.Dispose(); + } - }).ToObservableCollection(); + _active_context = ObservablesContext.CreateDefault(); - CCTS.Where(x => String.IsNullOrWhiteSpace(x.FileName)).ToList().ForEach(x => x.FileName = x.Guid); + CCTS = _active_context.Ccts + .Select(x => new CctModel() + { + Guid = x.Guid, + FileName = x.FileName, - LoadRmlProperties(); + }).ToObservableCollection(); - ActiveRML = await new RmlBuilder(_active_context) - .Set(guid) - .WithActiveParametersGroup() - .WithLiquidFactors() - .WithCCT() - .BuildAsync(); + CCTS.Where(x => String.IsNullOrWhiteSpace(x.FileName)).ToList().ForEach(x => x.FileName = x.Guid); - if (ActiveRML.Cct != null) - { - SelectedCCT = CCTS.SingleOrDefault(x => x.Guid == ActiveRML.Cct.Guid); - } + LoadRmlProperties(); - if (ActiveRML.ProcessParametersTablesGroups.ToList().Count == 0) - { - if (!_notification.ShowQuestion("Could not find any process group for the selected RML. Would you like to create one?")) + ActiveRML = await new RmlBuilder(_active_context) + .Set(guid) + .WithActiveParametersGroup() + .WithLiquidFactors() + .WithCCT() + .WithSpools() + .BuildAsync(); + + if (ActiveRML.Cct != null) { - _notification.ShowError("Cannot load an RML with no process group."); - IsFree = true; - return; + SelectedCCT = CCTS.SingleOrDefault(x => x.Guid == ActiveRML.Cct.Guid); } - else + + if (ActiveRML.ProcessParametersTablesGroups.ToList().Count == 0) { - ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); - group.Name = "Active Group"; - group.Active = true; - group.ProcessParametersTables.Add(new ProcessParametersTable() + if (!_notification.ShowQuestion("Could not find any process group for the selected RML. Would you like to create one?")) + { + _notification.ShowError("Cannot load an RML with no process group."); + IsFree = true; + return; + } + else { - Name = "Process Table 1", - }); + ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); + group.Name = "Active Group"; + group.Active = true; + group.ProcessParametersTables.Add(new ProcessParametersTable() + { + Name = "Process Table 1", + }); - group.Rml = ActiveRML; + group.Rml = ActiveRML; - _active_context.ProcessParametersTablesGroups.Add(group); - _active_context.ProcessParametersTables.Add(group.ProcessParametersTables[0]); - await _active_context.SaveChangesAsync(); - LoadActiveRML(ActiveRML.Guid); - return; + _active_context.ProcessParametersTablesGroups.Add(group); + _active_context.ProcessParametersTables.Add(group.ProcessParametersTables[0]); + await _active_context.SaveChangesAsync(); + LoadActiveRML(ActiveRML.Guid); + return; + } } - } - ActiveProcessParametersGroup = ActiveRML.ProcessParametersTablesGroups.ToList().FirstOrDefault(); - ActiveProcessParametersTableView = CollectionViewSource.GetDefaultView(ActiveProcessParametersGroup.ProcessParametersTables); - ActiveProcessParametersTableView.SortDescriptions.Add(new SortDescription(nameof(ProcessParametersTable.TableIndex), ListSortDirection.Ascending)); + ActiveProcessParametersGroup = ActiveRML.ProcessParametersTablesGroups.ToList().FirstOrDefault(); + ActiveProcessParametersTableView = CollectionViewSource.GetDefaultView(ActiveProcessParametersGroup.ProcessParametersTables); + ActiveProcessParametersTableView.SortDescriptions.Add(new SortDescription(nameof(ProcessParametersTable.TableIndex), ListSortDirection.Ascending)); - CalibrationDataViewVM = new CalibrationDataViewVM(_notification); - LiquidTypesRmls = ActiveRML.LiquidTypesRmls; + CalibrationDataViewVM = new CalibrationDataViewVM(_notification); + LiquidTypesRmls = ActiveRML.LiquidTypesRmls; - foreach (var liquidTypeRml in LiquidTypesRmls) - { - CalibrationDataVM catVM = new CalibrationDataVM(liquidTypeRml.LiquidType); - - if (liquidTypeRml.DefaultCatData != null) + foreach (var liquidTypeRml in LiquidTypesRmls) { - catVM.CalibrationPoints = liquidTypeRml.GetCalibrationData().CalibrationPoints.Select(x => new CalibrationDataPointVM(x.X, x.Y)).ToObservableCollection(); + CalibrationDataVM catVM = new CalibrationDataVM(liquidTypeRml.LiquidType); + + if (liquidTypeRml.DefaultCatData != null) + { + catVM.CalibrationPoints = liquidTypeRml.GetCalibrationData().CalibrationPoints.Select(x => new CalibrationDataPointVM(x.X, x.Y)).ToObservableCollection(); + } + + CalibrationDataViewVM.LiquidsCalibrationData.Add(catVM); } - CalibrationDataViewVM.LiquidsCalibrationData.Add(catVM); - } + ColorConversionViewVM = new ColorConversionViewVM(_notification) + { + RML = ActiveRML, + CCT = SelectedCCT, + LiquidsCalibrationData = CalibrationDataViewVM.LiquidsCalibrationData, + LiquidTypesRmls = LiquidTypesRmls, + }; - ColorConversionViewVM = new ColorConversionViewVM(_notification) - { - RML = ActiveRML, - CCT = SelectedCCT, - LiquidsCalibrationData = CalibrationDataViewVM.LiquidsCalibrationData, - LiquidTypesRmls = LiquidTypesRmls, - }; + ColorCalibrationVM = new ColorCalibrationViewVM(_notification) + { + RML = ActiveRML, + LiquidTypes = LiquidTypesRmls.Where(x => x.LiquidType.HasPigment).ToList().Select(y => y.LiquidType).ToList(), + }; - View.NavigateTo(RmlNavigationView.RmlView); + _rmlBeforeSave = RmlDTO.FromObservable(ActiveRML); - InvalidateRelayCommands(); + View.NavigateTo(RmlNavigationView.RmlView); - IsFree = true; + InvalidateRelayCommands(); + + IsFree = true; + } + catch (Exception ex) + { + LogManager.Log($"Error loading RML '{ActiveRML.Name}'..."); + _notification.ShowError($"Error loading the selected thread.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } } } @@ -371,6 +485,7 @@ namespace Tango.MachineStudio.RML.ViewModels LinearMassDensityUnits = _active_context.LinearMassDensityUnits.ToObservableCollection(); FiberShapes = _active_context.FiberShapes.ToObservableCollection(); FiberSynths = _active_context.FiberSynths.ToObservableCollection(); + SpoolTypes = _active_context.SpoolTypes.ToObservableCollection(); } private async void AddNewRml() @@ -400,8 +515,10 @@ namespace Tango.MachineStudio.RML.ViewModels Rml rml = new Rml(); rml.Name = name; + rml.DisplayName = name; + rml.QualificationDate = DateTime.UtcNow; rml.Manufacturer = "Twine"; - rml.Code = Rmls.Max(x => x.Code) + 1; + rml.Code = Rmls.Count > 0 ? Rmls.Max(x => x.Code) + 1 : 1; rml.MediaMaterial = Materials.FirstOrDefault(); rml.MediaPurpose = Purposes.FirstOrDefault(); rml.MediaCondition = Conditions.FirstOrDefault(); @@ -423,6 +540,9 @@ namespace Tango.MachineStudio.RML.ViewModels _active_context.ProcessParametersTables.Add(group.ProcessParametersTables[0]); _active_context.Rmls.Add(rml); await _active_context.SaveChangesAsync(); + + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, rml.Name, rml, "Rml created using Machine Studio."); + LoadActiveRML(rml.Guid); IsFree = true; @@ -432,25 +552,64 @@ namespace Tango.MachineStudio.RML.ViewModels private async void RemoveSelectedRml() { - if (_notification.ShowQuestion("Removing the selected RML will result in the loss of all related process parameters and default calibration data. Are you sure you want to delete the selected RML?")) + if (_notification.ShowQuestion("Removing the selected thread will result in the loss of all related process parameters and default calibration data. Are you sure you want to delete the selected RML?")) { - IsFree = false; - using (_notification.PushTaskItem("Removing RML...")) { try { + IsFree = false; + + var rml_jobs = await _rmls_context.Jobs.Where(x => x.RmlGuid == SelectedRML.Guid).Include(x => x.Machine).OrderBy(x => x.Machine.SerialNumber).ToListAsync(); + + if (rml_jobs.Count > 0) + { + var vm = new RmlDeleteDialogViewVM(SelectedRML, Rmls.ToList(), rml_jobs.ToList()); + _notification.ShowModalDialog(vm, (x) => { }, () => { }); + + if (!vm.DialogResult) + { + return; + } + + //Perform actions... + using (var db = ObservablesContext.CreateDefault()) + { + foreach (var jobAction in vm.JobsActions) + { + if (jobAction.Action == RmlDeleteDialogViewVM.RmlDeleteJobAction.Delete) + { + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.JobDeleted, _authentication.CurrentUser.Guid, jobAction.Job.Name, jobAction.Job.Guid, $"Job deleted due to RML '{SelectedRML.Name}' being removed."); + await jobAction.Job.DeleteCascadeAsync(db); + } + else if (jobAction.Action == RmlDeleteDialogViewVM.RmlDeleteJobAction.Change) + { + var job = await db.Jobs.SingleOrDefaultAsync(x => x.Guid == jobAction.Job.Guid); + job.RmlGuid = jobAction.TargetRml.Guid; + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.JobSaved, _authentication.CurrentUser.Guid, jobAction.Job.Name, jobAction.Job.Guid, $"Job RML changed to '{jobAction.TargetRml.Name}' due to RML '{SelectedRML.Name}' being removed."); + } + } + + await db.SaveChangesAsync(); + } + } + await SelectedRML.DeleteCascadeAsync(_rmls_context); + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlDeleted, _authentication.CurrentUser, SelectedRML.Name, SelectedRML, "RML deleted from Machine Studio."); + LoadRmls(); } catch (Exception ex) { - LogManager.Log(ex, $"Error removing selected RML {SelectedRML.Name}."); - _notification.ShowError($"An error occurred while trying to remove the selected RML.\n{ex.Message}"); + LogManager.Log(ex, $"Error removing selected RML {SelectedRML?.Name}."); + _notification.ShowError($"An error occurred while trying to remove the selected RML.\n{ex.FlattenMessage()}"); + LoadRmls(); + } + finally + { + IsFree = true; } } - - IsFree = true; } } @@ -468,15 +627,18 @@ namespace Tango.MachineStudio.RML.ViewModels using (var context = ObservablesContext.CreateDefault()) { - var rml = await new RmlBuilder(context).Set(SelectedRML.Guid).WithActiveParametersGroup().WithLiquidFactors().BuildAsync(); + var rml = await new RmlBuilder(context).Set(SelectedRML.Guid).WithActiveParametersGroup().WithLiquidFactors().WithSpools().BuildAsync(); Rml cloned = new Rml(); - rml.MapPrimitivesWithStrings(cloned); + rml.MapPropertiesTo(cloned, MappingFlags.NoReferenceTypes); cloned.Code = Rmls.Max(x => x.Code) + 1; cloned.Guid = Guid.NewGuid().ToString(); cloned.ID = 0; cloned.Name = name; + cloned.DisplayName = name; + cloned.RmlQualificationLevel = RmlQualifications.Provisional; + cloned.QualificationDate = DateTime.UtcNow; ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); group.Name = rml.GetActiveProcessGroup().Name; @@ -502,8 +664,19 @@ namespace Tango.MachineStudio.RML.ViewModels cloned.LiquidTypesRmls.Add(l); } + foreach (var spool in rml.RmlsSpools) + { + RmlsSpool s = new RmlsSpool(); + spool.MapPropertiesTo(s, MappingFlags.ValueTypesOnly); + s.RmlGuid = cloned.Guid; + s.SpoolTypeGuid = spool.SpoolTypeGuid; + cloned.RmlsSpools.Add(s); + } + context.Rmls.Add(cloned); await context.SaveChangesAsync(); + + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, cloned.Name, cloned, "RML cloned from Machine Studio."); } LoadRmls(); @@ -558,6 +731,11 @@ namespace Tango.MachineStudio.RML.ViewModels ActiveProcessParametersTableView.Refresh(); } + private void OnRMLFilterChanged() + { + RmlsCollectionView.Refresh(); + } + private void RemoveLiquidFactor(LiquidTypesRml liquidFactor) { if (_notification.ShowQuestion("Removing this liquid factor will remove the liquid type association with the RML and will drop the calibration data. Are you sure?")) @@ -648,6 +826,11 @@ namespace Tango.MachineStudio.RML.ViewModels ActiveRML.LastUpdated = DateTime.UtcNow; + if (_rmlBeforeSave.QualificationLevel != ActiveRML.QualificationLevel) + { + ActiveRML.QualificationDate = DateTime.UtcNow; + } + if (SelectedCCT != null) { if (SelectedCCT.IsNew) @@ -665,18 +848,26 @@ namespace Tango.MachineStudio.RML.ViewModels } } + var rmlAfter = RmlDTO.FromObservable(ActiveRML); + await _active_context.SaveChangesAsync(); + + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlSaved, _authentication.CurrentUser, _rmlBeforeSave.Name, _rmlBeforeSave, rmlAfter, "RML saved using Machine Studio."); + + _rmlBeforeSave = rmlAfter; + + LoadActiveRML(ActiveRML.Guid); } } catch (Exception ex) { LogManager.Log(ex, $"Error saving RML {ActiveRML.Name}"); - _notification.ShowError($"An error occurred while trying to save the current RML.\n{ex.Message}"); + _notification.ShowError($"An error occurred while trying to save the current RML.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; } - - LoadActiveRML(ActiveRML.Guid); - - IsFree = true; } private void BackToRmls() @@ -692,6 +883,12 @@ namespace Tango.MachineStudio.RML.ViewModels String file = GetCCTFileOpen(); if (file != null) { + if (CCTS.ToList().Exists(x => x.FileName == Path.GetFileName(file))) + { + _notification.ShowError("The selected CCT file already exists on the database. Please select the CCT file from the dropdown box."); + return; + } + CctModel cctModel = new CctModel(); cctModel.Guid = Guid.NewGuid().ToString(); cctModel.IsNew = true; @@ -804,6 +1001,8 @@ namespace Tango.MachineStudio.RML.ViewModels var rmlFile = await Rml.FromRmlFile(db, json); db.Rmls.Add(rmlFile); + + _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlImported, _authentication.CurrentUser, rmlFile.Name, rmlFile, "RML imported from Machine Studio."); } await db.SaveChangesAsync(); @@ -860,5 +1059,26 @@ namespace Tango.MachineStudio.RML.ViewModels } #endregion + + #region Spools + + private void AddNewSpool() + { + _active_context.RmlsSpools.Add(new RmlsSpool() + { + Rml = ActiveRML, + }); + } + + private void RemoveSpool() + { + if (SelectedSpool != null) + { + _active_context.RmlsSpools.Remove(SelectedSpool); + ActiveRML.RmlsSpools.Remove(SelectedSpool); + } + } + + #endregion } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RmlDeleteDialogViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RmlDeleteDialogViewVM.cs new file mode 100644 index 000000000..b4c6c6274 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RmlDeleteDialogViewVM.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Core; +using Tango.SharedUI; + +namespace Tango.MachineStudio.RML.ViewModels +{ + public class RmlDeleteDialogViewVM : DialogViewVM + { + public enum RmlDeleteJobAction + { + Delete, + Change + } + + public class RmlDeleteJob : ExtendedObject + { + public Job Job { get; set; } + public Machine Machine { get; set; } + private RmlDeleteJobAction _action; + + public RmlDeleteJobAction Action + { + get { return _action; } + set { _action = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(IsDelete)); } + } + + public bool IsDelete + { + get { return Action == RmlDeleteJobAction.Delete; } + } + + + public Rml TargetRml { get; set; } + + public override string ToString() + { + return $"{Machine.SerialNumber} => {Job.Name} => {Action} {(Action == RmlDeleteJobAction.Change ? $" => {TargetRml.Name}" : String.Empty)}"; + } + } + + private List _jobsToDelete; + + private List _jobsActions; + public List JobsActions + { + get { return _jobsActions; } + set { _jobsActions = value; RaisePropertyChangedAuto(); } + } + + public Rml Rml { get; set; } + + public List Rmls { get; set; } + + public List Actions { get; set; } + + public RmlDeleteDialogViewVM(Rml rml, List rmls, List jobsToDelete) + { + Rml = rml; + Rmls = rmls.Where(x => x.Guid != rml.Guid).ToList(); + _jobsToDelete = jobsToDelete; + JobsActions = new List(); + + Actions = new List() + { + RmlDeleteJobAction.Delete, + RmlDeleteJobAction.Change + }; + } + + public override void OnShow() + { + base.OnShow(); + + List list = new List(); + + foreach (var job in _jobsToDelete) + { + RmlDeleteJob deleteJob = new RmlDeleteJob(); + deleteJob.Job = job; + deleteJob.Action = RmlDeleteJobAction.Delete; + deleteJob.Machine = job.Machine; + deleteJob.TargetRml = Rmls.FirstOrDefault(x => x.Guid != Rml.Guid); + + list.Add(deleteJob); + } + + JobsActions = list; + } + } +} -- cgit v1.3.1