diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels')
2 files changed, 1314 insertions, 2 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorParametersVewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorParametersVewVM.cs new file mode 100644 index 000000000..b2e677339 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorParametersVewVM.cs @@ -0,0 +1,788 @@ +using OxyPlot; +using OxyPlot.Wpf; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.ActionLogs; +using Tango.BL.Builders; +using Tango.BL.DTO; +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 System.Diagnostics; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorParametersVewVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + private ObservablesContext _active_context; + private ObservablesContext _machineDbContext; + // private ColorProcessParameterDTO _hwBeforeSave; + + #region Properties + + private ColorProcessParameter _selectedColorProcessparameter; + /// <summary> + /// Gets or sets the selected RML. + /// </summary> + public ColorProcessParameter SelectedColorProcessParameter + { + get { return _selectedColorProcessparameter; } + set + { + _selectedColorProcessparameter = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessData> _magentaProcessData; + public ObservableCollection<ColorProcessData> MagentaProcessData + { + get + { + return _magentaProcessData; + } + set + { + _magentaProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessData> _cyanProcessData; + public ObservableCollection<ColorProcessData> CyanProcessData + { + get + { + return _cyanProcessData; + } + set + { + _cyanProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessData> _yellowProcessData; + public ObservableCollection<ColorProcessData> YellowProcessData + { + get + { + return _yellowProcessData; + } + set + { + _yellowProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessData> _blackProcessData; + public ObservableCollection<ColorProcessData> BlackProcessData + { + get + { + return _blackProcessData; + } + set + { + _blackProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessFactor> _factor100ProcessData; + public ObservableCollection<ColorProcessFactor> Factor100ProcessData + { + get + { + return _factor100ProcessData; + } + set + { + _factor100ProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessFactor> _factor200ProcessData; + public ObservableCollection<ColorProcessFactor> Factor200ProcessData + { + get + { + return _factor200ProcessData; + } + set + { + _factor200ProcessData = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessFactor> _minInkUptake; + public ObservableCollection<ColorProcessFactor> MinInkUptake + { + get + { + return _minInkUptake; + } + set + { + _minInkUptake = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorProcessFactor> _maxInkUptake; + public ObservableCollection<ColorProcessFactor> MaxInkUptake + { + get + { + return _maxInkUptake; + } + set + { + _maxInkUptake = value; + RaisePropertyChangedAuto(); + } + } + + + private PlotProperties _cyanPlot; + + public PlotProperties CyanPlot + { + get { return _cyanPlot; } + set { + _cyanPlot = value; + RaisePropertyChangedAuto(); + } + } + + private PlotProperties _magentaPlot; + + public PlotProperties MagentaPlot + { + get { return _magentaPlot; } + set { _magentaPlot = value; } + } + + private PlotProperties _yellowPlot; + + public PlotProperties YellowPlot + { + get { return _yellowPlot; } + set { _yellowPlot = value; } + } + + private PlotProperties _blackPlot; + + public PlotProperties BlackPlot + { + get { return _blackPlot; } + set { _blackPlot = value; } + } + /// <summary> + /// Gets or sets the machines providers. + /// </summary> + public ISuggestionProvider MachinesProvider { get; set; } + + protected Machine _selectedMachine; + /// <summary> + /// Gets or sets the selected machine. + /// </summary> + public Machine SelectedMachine + { + get { return _selectedMachine; } + set + { + if (value != null && _selectedMachine != value) + { + _selectedMachine = value; + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + } + } + } + + private bool _isViewLoaded; + /// <summary> + /// Gets or sets a value indicating whether this instance is view loaded. Used to update charts. + /// </summary> + /// <value> + /// <c>true</c> if this instance is view loaded; otherwise, <c>false</c>. + /// </value> + public bool IsViewLoaded + { + get { return _isViewLoaded; } + set { + if(_isViewLoaded != value) + { + _isViewLoaded = value; + } + } + } + + private Dictionary<string, ColorProcessData> _removedColorProcessDataBeforeSave; + + public Dictionary<string, ColorProcessData> RemovedColorProcessDataBeforeSave + { + get { return _removedColorProcessDataBeforeSave; } + set { _removedColorProcessDataBeforeSave = value; } + } + + #endregion + #region commands + + public RelayCommand ImportCyanDataCommand { get; set; } + public RelayCommand ImportMagentaDataCommand { get; set; } + public RelayCommand ImportYellowDataCommand { get; set; } + public RelayCommand ImportBlackDataCommand { get; set; } + + private void ImportMagentaData(object obj) + { + List<ColorDataExcelModel> items; + if (LoadColorDataFromExcel(out items) && items != null) + { + MagentaProcessData.Clear(); + items.ForEach(x => MagentaProcessData.Add(new ColorProcessData() { InkNlCm = x.NlCm, L = x.L, A = x.A, B = x.B, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid})); + MagentaPlot.CreateGraph(MagentaProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("MAGENTA"); + } + } + + private void ImportYellowData(object obj) + { + List<ColorDataExcelModel> items; + if (LoadColorDataFromExcel(out items) && items != null) + { + YellowProcessData.Clear(); + items.ForEach(x => YellowProcessData.Add(new ColorProcessData() { InkNlCm = x.NlCm, L = x.L, A = x.A, B = x.B, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid})); + YellowPlot.CreateGraph(YellowProcessData.ToList(), false); + UpdateFactorsOnChangeProcessData("YELLOW"); + } + } + + private void ImportCyanData(object obj) + { + List<ColorDataExcelModel> items; + if (LoadColorDataFromExcel(out items) && items != null) + { + CyanProcessData.Clear(); + items.ForEach(x => CyanProcessData.Add(new ColorProcessData() { InkNlCm = x.NlCm, L = x.L, A = x.A, B = x.B, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid })); + CyanPlot.CreateGraph(CyanProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("CYAN"); + } + } + + private void ImportBlackData(object obj) + { + List<ColorDataExcelModel> items; + if (LoadColorDataFromExcel(out items) && items != null) + { + BlackProcessData.Clear(); + items.ForEach(x => BlackProcessData.Add(new ColorProcessData() { InkNlCm = x.NlCm, L = x.L, A = x.A, B = x.B, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid })); + BlackPlot.CreateGraph(BlackProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("BLACK"); + } + } + + private bool LoadColorDataFromExcel(out List<ColorDataExcelModel> items) + { + OpenFileDialog dlg = new OpenFileDialog(); + items = null; + try + { + dlg.Title = $"Import excel file with data"; + dlg.Filter = "Excel Files|*.xlsx"; + if (dlg.ShowDialog().Value) + { + string error = ""; + ColorDataExcelModel.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 false; + } + return true; + } + } + 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."); + } + return false; + } + + #endregion + + public ColorParametersVewVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + _isViewLoaded = false; + RemovedColorProcessDataBeforeSave = new Dictionary<string, ColorProcessData>(); + + MachinesProvider = new SuggestionProvider((filter) => + { + try + { + return _machineDbContext.Machines.Where(x => x.SerialNumber.StartsWith(filter)).ToList(); + } + catch + { + return null; + } + }); + + CyanProcessData = new ObservableCollection<ColorProcessData>(); + CyanProcessData.CollectionChanged += OnCyanCollectionChanged; + + MagentaProcessData = new ObservableCollection<ColorProcessData>(); + MagentaProcessData.CollectionChanged += OnMagentaCollectionChanged; + + YellowProcessData = new ObservableCollection<ColorProcessData>(); + YellowProcessData.CollectionChanged += OnYellowCollectionChanged; + + BlackProcessData = new ObservableCollection<ColorProcessData>(); + BlackProcessData.CollectionChanged += OnBlackCollectionChanged; + + CyanPlot = new PlotProperties("CYAN"); + YellowPlot = new PlotProperties("YELLOW"); + MagentaPlot = new PlotProperties("MAGENTA"); + BlackPlot = new PlotProperties("BLACK"); + + ImportCyanDataCommand = new RelayCommand(ImportCyanData); + ImportMagentaDataCommand = new RelayCommand(ImportMagentaData); + ImportYellowDataCommand = new RelayCommand(ImportYellowData); + ImportBlackDataCommand = new RelayCommand(ImportBlackData); + } + + #region Loading + + public async void LoadColorParameters(string RMLExtemtionGUID) + { + IsFree = false; + if (_active_context != null) + { + _active_context.Dispose(); + } + if (_machineDbContext != null) + { + _machineDbContext.Dispose(); + } + + + LogManager.Log("Initializing machine Db context..."); + + _machineDbContext = ObservablesContext.CreateDefault(); + + + _active_context = ObservablesContext.CreateDefault(); + + await Task.Factory.StartNew(() => + { + using (_notification.PushTaskItem("Loading Color Process Parameters ...")) + { + + var currentcolorProcessParameter = _active_context.ColorProcessParameters.Where(x => x.RmlsExtensionsGuid == RMLExtemtionGUID).FirstOrDefault(); + if(currentcolorProcessParameter != null) + { + SelectedColorProcessParameter = new ColorProcessParametersBuilder(_active_context).Set(currentcolorProcessParameter.Guid).WithColorProcessData().WithColorProcessFactor(). Build(); + } + + if (SelectedColorProcessParameter == null) + { + SelectedColorProcessParameter = new ColorProcessParameter() { RmlsExtensionsGuid = RMLExtemtionGUID }; + SelectedColorProcessParameter.WhitePointL = 0.0; + SelectedColorProcessParameter.WhitePointA = 0.0; + SelectedColorProcessParameter.WhitePointB = 0.0; + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "CYAN", FactorPercent = 100, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "MAGENTA", FactorPercent = 100, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "YELLOW", FactorPercent = 100, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "BLACK", FactorPercent = 100, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "CYAN", FactorPercent = 200, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "MAGENTA", FactorPercent = 200, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "YELLOW", FactorPercent = 200, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "BLACK", FactorPercent = 200, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "CYAN", FactorPercent = 1, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "MAGENTA", FactorPercent = 1, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "YELLOW", FactorPercent = 1, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "BLACK", FactorPercent = 1, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "CYAN", FactorPercent = 1111, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "MAGENTA", FactorPercent = 1111, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "YELLOW", FactorPercent = 1111, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + SelectedColorProcessParameter.ColorProcessFactor.Add(new ColorProcessFactor() { ColorName = "BLACK", FactorPercent = 1111, ColorProcessParametersGuid = SelectedColorProcessParameter.Guid }); + _active_context.ColorProcessParameters.Add(SelectedColorProcessParameter); + _active_context.SaveChangesAsync(); + } + + } + }); + + LoadParameters(); + IsFree = true; + } + + private void LoadParameters() + { + Factor100ProcessData = SelectedColorProcessParameter.ColorProcessFactor.Where(x => x.FactorPercent == 100).ToObservableCollection(); + + Factor200ProcessData = SelectedColorProcessParameter.ColorProcessFactor.Where(x => x.FactorPercent == 200).ToObservableCollection(); + + MinInkUptake = SelectedColorProcessParameter.ColorProcessFactor.Where(x => x.FactorPercent == 1).ToObservableCollection(); + + MaxInkUptake = SelectedColorProcessParameter.ColorProcessFactor.Where(x => x.FactorPercent == 1111).ToObservableCollection(); + + RemovedColorProcessDataBeforeSave.Clear(); + + var cyanDataList = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "CYAN").ToList().OrderBy(x => x.InkNlCm ).ToList(); + CyanProcessData.Clear(); + cyanDataList.ForEach( y => CyanProcessData.Add(y)); + + var magentaDatalist = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "MAGENTA").ToList().OrderBy(x => x.InkNlCm).ToList(); ; + MagentaProcessData.Clear(); + magentaDatalist.ForEach(y => MagentaProcessData.Add(y)); + + var yellowDatalist = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "YELLOW").ToList().OrderBy(x => x.InkNlCm).ToList(); ; + YellowProcessData.Clear(); + yellowDatalist.ForEach(y => YellowProcessData.Add(y)); + + var blackDatalist = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "BLACK").ToList().OrderBy(x => x.InkNlCm).ToList(); ; + BlackProcessData.Clear(); + blackDatalist.ForEach(y => BlackProcessData.Add(y)); + + UpdatePlots(); + SelectedColorProcessParameter.ColorProcessFactor.ToList().ForEach(x => UpdateFactorsOnChangeProcessData(x.ColorName)); + } + + #endregion + + #region Update Plot + + public void UpdatePlots() + { + if (IsViewLoaded) + { + CyanPlot.CreateGraph(CyanProcessData.ToList(), true); + MagentaPlot.CreateGraph(MagentaProcessData.ToList(), true); + YellowPlot.CreateGraph(YellowProcessData.ToList(), false); + BlackPlot.CreateGraph(BlackProcessData.ToList(), true); + } + } + + private void OnCyanCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.Action == NotifyCollectionChangedAction.Reset) + { + var cyanProcessData = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "CYAN").ToObservableCollection(); + foreach (ColorProcessData item in cyanProcessData) + { + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + item.PropertyChanged -= CyanMeasurementModelPropertyChanged; + } + // UpdateFactorsOnChangeProcessData("Cyan"); + } + else if (e.Action == NotifyCollectionChangedAction.Remove) + { + foreach (ColorProcessData item in e.OldItems) + { + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + item.PropertyChanged -= CyanMeasurementModelPropertyChanged; + } + UpdateFactorsOnChangeProcessData("CYAN"); + } + else if (e.Action == NotifyCollectionChangedAction.Add) + { + foreach (ColorProcessData item in e.NewItems) + { + item.ColorName = "CYAN"; + item.ColorProcessParametersGuid = SelectedColorProcessParameter.Guid; + SelectedColorProcessParameter.ColorProcessData.Add(item); + RemovedColorProcessDataBeforeSave.Remove(item.Guid); + item.PropertyChanged += CyanMeasurementModelPropertyChanged; + } + } + } + + private void CyanMeasurementModelPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if(IsFree) + { + CyanPlot.CreateGraph(CyanProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("CYAN"); + } + } + + private void OnMagentaCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.Action == NotifyCollectionChangedAction.Reset) + { + var magentaProcessData = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "MAGENTA").ToObservableCollection(); + foreach (ColorProcessData item in magentaProcessData) + { + item.PropertyChanged -= MagentaMeasurementModelPropertyChanged; + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + } + // UpdateFactorsOnChangeProcessData("Magenta"); + } + else if (e.Action == NotifyCollectionChangedAction.Remove) + { + foreach (ColorProcessData item in e.OldItems) + { + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + item.PropertyChanged -= MagentaMeasurementModelPropertyChanged; + } + UpdateFactorsOnChangeProcessData("MAGENTA"); + } + else if (e.Action == NotifyCollectionChangedAction.Add) + { + foreach (ColorProcessData item in e.NewItems) + { + item.ColorName = "MAGENTA"; + item.ColorProcessParametersGuid = SelectedColorProcessParameter.Guid; + SelectedColorProcessParameter.ColorProcessData.Add(item); + RemovedColorProcessDataBeforeSave.Remove(item.Guid); + item.PropertyChanged += MagentaMeasurementModelPropertyChanged; + } + } + } + + private void MagentaMeasurementModelPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (IsFree) + { + MagentaPlot.CreateGraph(MagentaProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("MAGENTA"); + } + } + + private void OnYellowCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.Action == NotifyCollectionChangedAction.Reset) + { + var yellowProcessData = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "YELLOW").ToObservableCollection(); + foreach (ColorProcessData item in yellowProcessData) + { + item.PropertyChanged -= YellowMeasurementModelPropertyChanged; + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + } + //UpdateFactorsOnChangeProcessData("Yellow"); + } + else if (e.Action == NotifyCollectionChangedAction.Remove) + { + foreach (ColorProcessData item in e.OldItems) + { + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + item.PropertyChanged -= YellowMeasurementModelPropertyChanged; + } + UpdateFactorsOnChangeProcessData("YELLOW"); + } + else if (e.Action == NotifyCollectionChangedAction.Add) + { + foreach (ColorProcessData item in e.NewItems) + { + item.ColorName = "YELLOW"; + item.ColorProcessParametersGuid = SelectedColorProcessParameter.Guid; + SelectedColorProcessParameter.ColorProcessData.Add(item); + RemovedColorProcessDataBeforeSave.Remove(item.Guid); + item.PropertyChanged += YellowMeasurementModelPropertyChanged; + } + } + } + + private void YellowMeasurementModelPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (IsFree) + { + YellowPlot.CreateGraph(YellowProcessData.ToList(), false); + UpdateFactorsOnChangeProcessData("YELLOW"); + } + } + + private void OnBlackCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if(e.Action == NotifyCollectionChangedAction.Reset) + { + var blackProcessData = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName == "BLACK").ToObservableCollection(); + foreach (ColorProcessData item in blackProcessData) + { + item.PropertyChanged -= BlackMeasurementModelPropertyChanged; + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + } + //UpdateFactorsOnChangeProcessData("Black"); + } + else if (e.Action == NotifyCollectionChangedAction.Remove) + { + foreach (ColorProcessData item in e.OldItems) + { + SelectedColorProcessParameter.ColorProcessData.Remove(item); + RemovedColorProcessDataBeforeSave[item.Guid] = item; + item.PropertyChanged -= BlackMeasurementModelPropertyChanged; + } + UpdateFactorsOnChangeProcessData("BLACK"); + } + else if (e.Action == NotifyCollectionChangedAction.Add) + { + foreach (ColorProcessData item in e.NewItems) + { + item.ColorName = "BLACK"; + item.ColorProcessParametersGuid = SelectedColorProcessParameter.Guid; + SelectedColorProcessParameter.ColorProcessData.Add(item); + RemovedColorProcessDataBeforeSave.Remove(item.Guid); + item.PropertyChanged += BlackMeasurementModelPropertyChanged; + } + + } + } + + private void BlackMeasurementModelPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (IsFree) + { + BlackPlot.CreateGraph(BlackProcessData.ToList(), true); + UpdateFactorsOnChangeProcessData("BLACK"); + } + } + + #endregion + + #region update factors + + /// <summary> + /// Updates the ColorProcessFactor from ColorProcessData item. + /// </summary> + /// <param name="factor">The factor.</param> + /// <param name="item">The item.</param> + private void UpdateFactor(ColorProcessFactor factor, ColorProcessData item) + { + if (factor == null) + return; + if (item == null) + { + factor.InkNlCm = 0; + factor.L = factor.A = factor.B = 0.0; + return; + } + factor.InkNlCm = item.InkNlCm; + factor.L = item.L; + factor.A = item.A; + factor.B = item.B; + } + + /// <summary> + /// Calculate and Update the factors on change process data. + /// </summary> + /// <param name="color">The color.</param> + private void UpdateFactorsOnChangeProcessData(string color) + { + bool isLtype = color.ToLower() == "yellow" ? false : true; + var processData = SelectedColorProcessParameter.ColorProcessData.Where(x => x.ColorName.ToLower() == color.ToLower()).ToObservableCollection(); + double target100 = FactorTarget.GetFactor100(color); + double target200 = FactorTarget.GetFactor200(color); + ColorProcessData closest100 = null; + ColorProcessData closest200 = null; + ColorProcessData minInk = null; + ColorProcessData maxInk = null; + + var minDifference100 = double.MaxValue; + var minDifference200 = double.MaxValue; + + if (processData.Count == 0) + return; + + foreach (var item in processData) + { + var colorvalue = isLtype ? item.L : item.B; + var difference100 = Math.Abs(colorvalue - target100); + if (minDifference100 > difference100) + { + minDifference100 = (int)difference100; + closest100 = item; + } + var difference200 = Math.Abs(colorvalue - target200); + if (minDifference200 > difference200) + { + minDifference200 = (int)difference200; + closest200 = item; + } + if (minInk == null || minInk.InkNlCm > item.InkNlCm) + { + minInk = item; + } + if (maxInk == null || maxInk.InkNlCm < item.InkNlCm) + { + maxInk = item; + } + } + UpdateFactor(Factor100ProcessData.FirstOrDefault(x => x.ColorName == color), closest100); + UpdateFactor(Factor200ProcessData.FirstOrDefault(x => x.ColorName == color), closest200); + UpdateFactor(MinInkUptake.FirstOrDefault(x => x.ColorName == color), minInk); + UpdateFactor(MaxInkUptake.FirstOrDefault(x => x.ColorName == color), maxInk); + } + + #endregion + + #region save + + public async void Save() + { + try + { + IsFree = false; + await Task.Factory.StartNew(() => + { + SelectedColorProcessParameter.LastUpdated = DateTime.UtcNow; + + var colorProcessParameterAfterChange = ColorProcessParameterDTO.FromObservable(SelectedColorProcessParameter); + + foreach (KeyValuePair<string, ColorProcessData> item in RemovedColorProcessDataBeforeSave) + { + var existingColorProcessData = _active_context.ColorProcessData.FirstOrDefault(y => y.Guid == item.Value.Guid); + if (existingColorProcessData != null) + { + _active_context.ColorProcessData.Remove(existingColorProcessData); + } + } + _active_context.SaveChanges(); + + }); + LoadColorParameters(SelectedColorProcessParameter.RmlsExtensionsGuid); + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not update color parameters."); + _notification.ShowError($"An error occurred while trying to save color parameters.\n{ex.Message}"); + } + finally + { + IsFree = true; + } + } + + #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 607670461..3fde7abbe 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 @@ -1,18 +1,542 @@ - +using Microsoft.Win32; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Data; +using Tango.BL; +using Tango.BL.Builders; +using Tango.BL.Calibration; +using Tango.BL.Entities; +using Tango.Core.Commands; using Tango.MachineStudio.Common; +using Tango.MachineStudio.Common.Notifications; + + +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; +using Tango.MachineStudio.ThreadExtensions.Contracts; +using Tango.MachineStudio.ThreadExtensions.Views; namespace Tango.MachineStudio.ThreadExtensions.ViewModels { - public class MainViewVM : StudioViewModel + public class MainViewVM : StudioViewModel<IMainView> { + private INotificationProvider _notification; + private IAuthenticationProvider _authentication; + private IActionLogManager _actionLogManager; + private RmlsExtensionDTO _rmlExtensionBeforeSave; + + private ObservablesContext _rmlExtentions_context; + private ObservablesContext _active_context; + + #region properties + private ObservableCollection<RmlsExtension> _rmlsExtension; + public ObservableCollection<RmlsExtension> RmlsExtensions + { + get { return _rmlsExtension; } + set { _rmlsExtension = value; + RaisePropertyChangedAuto(); } + } + + + private RmlsExtension _activeRMLExtention; + public RmlsExtension ActiveRMLExtention + { + get { return _activeRMLExtention; } + set { _activeRMLExtention = value; + RaisePropertyChangedAuto(); } + } + + private RmlsExtension _selectedRMLExtension; + public RmlsExtension SelectedRMLExtension + { + get { return _selectedRMLExtension; } + set { _selectedRMLExtension = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private ICollectionView _rmlExtCollectionView; + /// <summary> + /// Gets or sets the RML collection view. + /// </summary> + public ICollectionView RmlExtCollectionView + { + get { return _rmlExtCollectionView; } + set + { + _rmlExtCollectionView = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<YarnApplication> _applications; + public ObservableCollection<YarnApplication> Applications + { + get { return _applications; } + set { _applications = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnBrand> _brands; + public ObservableCollection<YarnBrand> Brands + { + get { return _brands; } + set { _brands = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnColor> _yarnColor; + public ObservableCollection<YarnColor> YarnColor + { + get { return _yarnColor; } + set { _yarnColor = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnEndUse> _enduse; + public ObservableCollection<YarnEndUse> EndUse + { + get { return _enduse; } + set { _enduse = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnFamily> _family; + public ObservableCollection<YarnFamily> Family + { + get { return _family; } + set { _family = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnGeometry> _geometry; + public ObservableCollection<YarnGeometry> Geometry + { + get { return _geometry; } + set { _geometry = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnGlossLevel> _glosslevel; + public ObservableCollection<YarnGlossLevel> GlossLevel + { + get { return _glosslevel; } + set { _glosslevel = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnGroup> _group; + public ObservableCollection<YarnGroup> Group + { + get { return _group; } + set { _group = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnManufacturer> _manufacturer; + public ObservableCollection<YarnManufacturer> Manufacturer + { + get { return _manufacturer; } + set { _manufacturer = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnMaterial> _materials; + public ObservableCollection<YarnMaterial> Materials + { + get { return _materials; } + set { _materials = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnSubFamily> _subFamilies; + public ObservableCollection<YarnSubFamily> SubFamilies + { + get { return _subFamilies; } + set { _subFamilies = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnTexturing> _texturing; + public ObservableCollection<YarnTexturing> Texturing + { + get { return _texturing; } + set { _texturing = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnType> _yarnTypes; + public ObservableCollection<YarnType> YarnTypes + { + get { return _yarnTypes; } + set { _yarnTypes = value; RaisePropertyChangedAuto(); } + } + + private ObservableCollection<YarnIndustrysector> _industrySector; + public ObservableCollection<YarnIndustrysector> IndustrySector + { + get { return _industrySector; } + set { _industrySector = value; RaisePropertyChangedAuto(); } + } + + private String _Filter; + /// <summary> + /// Gets or sets the search filter. + /// </summary> + public String Filter + { + get { return _Filter; } + set { _Filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); } + } + + private void OnFilterChanged() + { + RmlExtCollectionView.Refresh(); + } + + private ColorParametersVewVM _colorParametersVewVM; + public ColorParametersVewVM ColorParametersVewVM + { + get { return _colorParametersVewVM; } + set { _colorParametersVewVM = value; RaisePropertyChangedAuto(); } + } + #endregion + + #region commands + + public RelayCommand SaveCommand { get; set; } + + public RelayCommand ManageRmlExtensionCommand { get; set; } + + public RelayCommand AddRmlExtCommand { get; set; } + + public RelayCommand RemoveRmlExtensionCommand { get; set; } + + public RelayCommand CloneRmlExtensionCommand { get; set; } + + public RelayCommand BackToThreadExtensionViewsCommand { get; set; } + + + private void BackToThreadExtensionViews(object obj) + { + View.NavigateTo(ThreadExtensionNavigationView.ThreadExtensionsView); + LoadRmlExtentions(); + } + + private async void CloneSelectedRmlExtension(object obj) + { + using (_notification.PushTaskItem("Cloning thread...")) + { + try + { + IsFree = false; + + using (var context = ObservablesContext.CreateDefault()) + { + RmlsExtension rml_extention = await new RmlExtensionsBuilder(_rmlExtentions_context).Set(SelectedRMLExtension.Guid).WithUser().BuildAsync(); + + RmlsExtension cloned = new RmlsExtension(); + rml_extention.MapPropertiesTo(cloned, MappingFlags.NoReferenceTypes); + + cloned.Guid = Guid.NewGuid().ToString(); + cloned.ID = 0; + + + context.RmlsExtensions.Add(cloned); + await context.SaveChangesAsync(); + + //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, cloned.Name, cloned, "RML cloned from Machine Studio."); + } + + LoadRmlExtentions(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error cloning thread."); + _notification.ShowError($"An error occurred while trying to clone the selected thread\n{ex.Message}"); + } + finally + { + IsFree = true; + } + } + } + + private async void RemoveRmlExtension(object obj) + { + if (_notification.ShowQuestion(" Are you sure you want to delete the selected RML Extension?")) + { + using (_notification.PushTaskItem("Removing RML Extension...")) + { + try + { + IsFree = false; + + await SelectedRMLExtension.DeleteCascadeAsync(_rmlExtentions_context); + //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlDeleted, _authentication.CurrentUser, SelectedRML.Name, SelectedRML, "RML deleted from Machine Studio."); + + LoadRmlExtentions(); + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error removing selected RML {SelectedRMLExtension?.Name}."); + _notification.ShowError($"An error occurred while trying to remove the selected RML Extension.\n{ex.FlattenMessage()}"); + LoadRmlExtentions(); + } + finally + { + IsFree = true; + } + } + } + } + #endregion + + public MainViewVM(INotificationProvider notificationProvider, IAuthenticationProvider authentication, IActionLogManager actionLogManager) + { + _notification = notificationProvider; + _authentication = authentication; + _actionLogManager = actionLogManager; + + BackToThreadExtensionViewsCommand = new RelayCommand(BackToThreadExtensionViews, () => IsFree); + SaveCommand = new RelayCommand(Save, () => IsFree); + ManageRmlExtensionCommand = new RelayCommand(() => LoadActiveRMLExtention(SelectedRMLExtension.Guid), () => SelectedRMLExtension != null); + RemoveRmlExtensionCommand = new RelayCommand(RemoveRmlExtension, () => SelectedRMLExtension != null); + CloneRmlExtensionCommand = new RelayCommand(CloneSelectedRmlExtension, () => SelectedRMLExtension != null); + AddRmlExtCommand = new RelayCommand(AddNewRmlExtention); + } + public override void OnApplicationReady() { + LoadRmlExtentions(); + } + + #region Loading + + private async void LoadRmlExtentions() + { + try + { + IsFree = false; + + using (_notification.PushTaskItem("Loading RmlExtentions...")) + { + if (_rmlExtentions_context != null) _rmlExtentions_context.Dispose(); + + _rmlExtentions_context = ObservablesContext.CreateDefault(); + RmlsExtensions = await new RMLExtentionsCollectionBuilder(_rmlExtentions_context).SetAll().WithUser().WithYarnProperties().BuildAsync(); + + + RmlExtCollectionView = CollectionViewSource.GetDefaultView(RmlsExtensions); + RmlExtCollectionView.SortDescriptions.Add(new SortDescription(nameof(Rml.LastUpdated), ListSortDirection.Descending)); + + RmlExtCollectionView.Filter = (rml) => + { + RmlsExtension r = rml as RmlsExtension; + return String.IsNullOrWhiteSpace(Filter) + || r.Name.ToLower().Contains(Filter.ToLower()); + }; + + } + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading RMLExtensions.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + + private void LoadRmlProperties() + { + Applications = _active_context.YarnApplications.ToObservableCollection(); + Brands = _active_context.YarnBrand.ToObservableCollection(); + YarnColor = _active_context.YarnColor.ToObservableCollection(); + EndUse = _active_context.YarnEndUse.ToObservableCollection(); + Family = _active_context.YarnFamily.ToObservableCollection(); + Geometry = _active_context.YarnGeometry.ToObservableCollection(); + GlossLevel = _active_context.YarnGlossLevel.ToObservableCollection(); + Group = _active_context.YarnGroup.ToObservableCollection(); + Manufacturer = _active_context.YarnManufacturer.ToObservableCollection(); + + Materials = _active_context.YarnMaterials.ToObservableCollection(); + SubFamilies = _active_context.YarnSubFamily.ToObservableCollection(); + Texturing = _active_context.YarnTexturing.ToObservableCollection(); + YarnTypes = _active_context.YarnType.ToObservableCollection(); + IndustrySector = _active_context.YarnIndustrysector.ToObservableCollection(); + } + + private async void AddNewRmlExtention(object obj) + { + using (_notification.PushTaskItem("Creating new RML Extension...")) + { + IsFree = false; + + if (_active_context != null) + { + _active_context.Dispose(); + } + + _active_context = ObservablesContext.CreateDefault(); + + LoadRmlProperties(); + + RmlsExtension rml_extention = new RmlsExtension(); + rml_extention.Created = DateTime.UtcNow; + rml_extention.UserGuid = _authentication.CurrentUser.Guid; + rml_extention.YarnManufacturer = Manufacturer.FirstOrDefault(); + rml_extention.YarnBrand = Brands.FirstOrDefault(); + rml_extention.Country = null; + rml_extention.YarnEndUse = EndUse.FirstOrDefault(); + rml_extention.YarnApplications = Applications.FirstOrDefault(); + rml_extention.YarnIndustrysector = IndustrySector.FirstOrDefault(); + + rml_extention.YarnMaterial = Materials.FirstOrDefault(); + rml_extention.YarnType = YarnTypes.FirstOrDefault(); + rml_extention.YarnSubFamily = SubFamilies.FirstOrDefault(); + rml_extention.YarnFamily = Family.FirstOrDefault(); + rml_extention.YarnGroup = Group.FirstOrDefault(); + rml_extention.YarnTexturing = Texturing.FirstOrDefault(); + rml_extention.YarnGeometry = Geometry.FirstOrDefault(); + rml_extention.YarnColor = YarnColor.FirstOrDefault(); + rml_extention.YarnGlossLevel = GlossLevel.FirstOrDefault(); + rml_extention.LinearDensity = 0; + rml_extention.YarnUnit = YarnUnits.DTEX; + rml_extention.YarnPlies = Plies.P1; + rml_extention.FilamentCount = 0; + rml_extention.TwistTpm = 0; + rml_extention.YarnTwistDirections = TwistDirections.S; + rml_extention.MinElongation = 0.0; + rml_extention.MaxElongation = 100.0; + rml_extention.MinMaxForceN = 0.0; + rml_extention.MaxMaxForceN = 100.0; + rml_extention.MinElasticity = 0.0; + rml_extention.MaxElasticity = 100.0; + rml_extention.MinTenacity = 0.0; + rml_extention.MaxTenacity = 100.0; + rml_extention.Finishing = "Lubrication"; + + _active_context.RmlsExtensions.Add(rml_extention); + await _active_context.SaveChangesAsync(); + + //_actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlCreated, _authentication.CurrentUser, rml.Name, rml, "Rml created using Machine Studio."); + ColorParametersVewVM = new ColorParametersVewVM(_notification, _actionLogManager); + LoadActiveRMLExtention(rml_extention.Guid); + + IsFree = true; + } + } + + private async void LoadActiveRMLExtention(String guid) + { + using (_notification.PushTaskItem("Loading RML Extension...")) + { + try + { + IsFree = false; + + if (_active_context != null) + { + _active_context.Dispose(); + } + + _active_context = ObservablesContext.CreateDefault(); + + LoadRmlProperties(); + + ActiveRMLExtention = await new RmlExtensionsBuilder(_active_context) + .Set(guid) + .WithUser() + .BuildAsync(); + + ColorParametersVewVM = new ColorParametersVewVM(_notification, _actionLogManager); + ColorParametersVewVM.LoadColorParameters(guid); + View.NavigateTo(ThreadExtensionNavigationView.ThreadExtentionView); + + 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; + } + } + } + + private async void RefreshView(String guid) + { + try + { + IsFree = false; + + //if (_active_context != null) + //{ + // _active_context.Dispose(); + //} + + //_active_context = ObservablesContext.CreateDefault(); + + LoadRmlProperties(); + ActiveRMLExtention = await new RmlExtensionsBuilder(_active_context) + .Set(guid) + .WithUser() + .BuildAsync(); + + InvalidateRelayCommands(); + + IsFree = true; + } + catch (Exception ex) + { + //LogManager.Log($"Error loading RML '{ActiveRML.Name}'..."); + _notification.ShowError($"Error refresh the selected thread.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + + #endregion + + private async void Save() + { + IsFree = false; + + try + { + using (_notification.PushTaskItem("Saving RML Extension...")) + { + + ActiveRMLExtention.LastUpdated = DateTime.UtcNow; + + ColorParametersVewVM.Save(); + + var rmlExtensionAfter = RmlsExtensionDTO.FromObservable(ActiveRMLExtention); + + await _active_context.SaveChangesAsync(); + + //ColorParametersVewVM.LoadColorParameters(ActiveRMLExtention.Guid); + // _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.RmlSaved, _authentication.CurrentUser, _rmlBeforeSave.Name, _rmlBeforeSave, rmlAfter, "RML saved using Machine Studio."); + + _rmlExtensionBeforeSave = rmlExtensionAfter; + RefreshView(SelectedRMLExtension.Guid); + } + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error saving RML Extension {ActiveRMLExtention.Name}"); + _notification.ShowError($"An error occurred while trying to save the current RML Extension.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } } } } |
