diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs | 420 |
1 files changed, 100 insertions, 320 deletions
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 cadd1fb95..ee21b9ac3 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,19 +21,12 @@ 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<IMainView> { private INotificationProvider _notification; - private IAuthenticationProvider _authentication; - private IActionLogManager _actionLogManager; - private RmlDTO _rmlBeforeSave; private ObservablesContext _rmls_context; private ObservablesContext _active_context; @@ -45,20 +38,6 @@ namespace Tango.MachineStudio.RML.ViewModels set { _rmls = value; RaisePropertyChangedAuto(); } } - private ICollectionView _rmlssCollectionView; - /// <summary> - /// Gets or sets the RML collection view. - /// </summary> - public ICollectionView RmlsCollectionView - { - get { return _rmlssCollectionView; } - set - { - _rmlssCollectionView = value; - RaisePropertyChangedAuto(); - } - } - private ObservableCollection<MediaMaterial> _materials; public ObservableCollection<MediaMaterial> Materials { @@ -101,13 +80,6 @@ namespace Tango.MachineStudio.RML.ViewModels set { _fiberSynths = value; RaisePropertyChangedAuto(); } } - private ObservableCollection<SpoolType> _spoolTypes; - public ObservableCollection<SpoolType> SpoolTypes - { - get { return _spoolTypes; } - set { _spoolTypes = value; RaisePropertyChangedAuto(); } - } - private Rml _selectedRML; public Rml SelectedRML { @@ -171,30 +143,6 @@ 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; - /// <summary> - /// Gets or sets the job filter. - /// </summary> - public String RMLFilter - { - get { return _RMLFilter; } - set { _RMLFilter = value; RaisePropertyChangedAuto(); OnRMLFilterChanged(); } - } - /// <summary> /// Gets or sets the manage RML command. /// </summary> @@ -237,21 +185,9 @@ namespace Tango.MachineStudio.RML.ViewModels public RelayCommand ImportRMLFileCommand { get; set; } - /// <summary> - /// Gets or sets the add spool command. - /// </summary> - public RelayCommand AddSpoolCommand { get; set; } - - /// <summary> - /// Gets or sets the remove spool command. - /// </summary> - public RelayCommand RemoveSpoolCommand { get; set; } - - public MainViewVM(INotificationProvider notificationProvider, IAuthenticationProvider authentication, IActionLogManager actionLogManager) + public MainViewVM(INotificationProvider notificationProvider) { _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); @@ -270,9 +206,6 @@ 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() @@ -282,177 +215,130 @@ namespace Tango.MachineStudio.RML.ViewModels private async void LoadRmls() { - try - { - IsFree = false; + if (_rmls_context != null) _rmls_context.Dispose(); - using (_notification.PushTaskItem("Loading Rmls...")) - { - if (_rmls_context != null) _rmls_context.Dispose(); + _rmls_context = ObservablesContext.CreateDefault(); + Rmls = await new RmlsCollectionBuilder(_rmls_context).SetAll().WithLiquidFactors().WithMediaProperties().BuildAsync(); - _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); + //Load CCT file names... + var ccts = await _rmls_context.Ccts.Select(x => new + { + x.Guid, + x.FileName + }).ToListAsync(); - 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<object>(FilterCollection); + foreach (var rml in Rmls) + { + var cct = ccts.SingleOrDefault(x => x.Guid == rml.CctGuid); - RmlsCollectionView.Filter = (rml) => + if (cct != null) + { + rml.Cct = new Cct() { - Rml r = rml as Rml; - return String.IsNullOrWhiteSpace(RMLFilter) - || r.Name.ToLower().Contains(RMLFilter.ToLower()); + Guid = cct.Guid, + FileName = cct.FileName, }; - } } - 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...")) { - try + IsFree = false; + + if (_active_context != null) { - IsFree = false; + _active_context.Dispose(); + } - if (_active_context != null) - { - _active_context.Dispose(); - } + _active_context = ObservablesContext.CreateDefault(); - _active_context = ObservablesContext.CreateDefault(); + CCTS = _active_context.Ccts + .Select(x => new CctModel() + { + Guid = x.Guid, + FileName = x.FileName, - CCTS = _active_context.Ccts - .Select(x => new CctModel() - { - Guid = x.Guid, - FileName = x.FileName, + }).ToObservableCollection(); - }).ToObservableCollection(); + CCTS.Where(x => String.IsNullOrWhiteSpace(x.FileName)).ToList().ForEach(x => x.FileName = x.Guid); - CCTS.Where(x => String.IsNullOrWhiteSpace(x.FileName)).ToList().ForEach(x => x.FileName = x.Guid); + LoadRmlProperties(); - LoadRmlProperties(); + ActiveRML = await new RmlBuilder(_active_context) + .Set(guid) + .WithActiveParametersGroup() + .WithLiquidFactors() + .WithCCT() + .BuildAsync(); - ActiveRML = await new RmlBuilder(_active_context) - .Set(guid) - .WithActiveParametersGroup() - .WithLiquidFactors() - .WithCCT() - .WithSpools() - .BuildAsync(); + if (ActiveRML.Cct != null) + { + SelectedCCT = CCTS.SingleOrDefault(x => x.Guid == ActiveRML.Cct.Guid); + } - if (ActiveRML.Cct != null) + 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?")) { - SelectedCCT = CCTS.SingleOrDefault(x => x.Guid == ActiveRML.Cct.Guid); + _notification.ShowError("Cannot load an RML with no process group."); + IsFree = true; + return; } - - if (ActiveRML.ProcessParametersTablesGroups.ToList().Count == 0) + else { - 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 + ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); + group.Name = "Active Group"; + group.Active = true; + group.ProcessParametersTables.Add(new ProcessParametersTable() { - ProcessParametersTablesGroup group = new ProcessParametersTablesGroup(); - group.Name = "Active Group"; - group.Active = true; - group.ProcessParametersTables.Add(new ProcessParametersTable() - { - Name = "Process Table 1", - }); + 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)); - - CalibrationDataViewVM = new CalibrationDataViewVM(_notification); - LiquidTypesRmls = ActiveRML.LiquidTypesRmls; - - foreach (var liquidTypeRml in LiquidTypesRmls) - { - CalibrationDataVM catVM = new CalibrationDataVM(liquidTypeRml.LiquidType); + ActiveProcessParametersGroup = ActiveRML.ProcessParametersTablesGroups.ToList().FirstOrDefault(); + ActiveProcessParametersTableView = CollectionViewSource.GetDefaultView(ActiveProcessParametersGroup.ProcessParametersTables); + ActiveProcessParametersTableView.SortDescriptions.Add(new SortDescription(nameof(ProcessParametersTable.TableIndex), ListSortDirection.Ascending)); - if (liquidTypeRml.DefaultCatData != null) - { - catVM.CalibrationPoints = liquidTypeRml.GetCalibrationData().CalibrationPoints.Select(x => new CalibrationDataPointVM(x.X, x.Y)).ToObservableCollection(); - } + CalibrationDataViewVM = new CalibrationDataViewVM(_notification); + LiquidTypesRmls = ActiveRML.LiquidTypesRmls; - CalibrationDataViewVM.LiquidsCalibrationData.Add(catVM); - } + foreach (var liquidTypeRml in LiquidTypesRmls) + { + CalibrationDataVM catVM = new CalibrationDataVM(liquidTypeRml.LiquidType); - ColorConversionViewVM = new ColorConversionViewVM(_notification) + if (liquidTypeRml.DefaultCatData != null) { - RML = ActiveRML, - CCT = SelectedCCT, - LiquidsCalibrationData = CalibrationDataViewVM.LiquidsCalibrationData, - LiquidTypesRmls = LiquidTypesRmls, - }; + catVM.CalibrationPoints = liquidTypeRml.GetCalibrationData().CalibrationPoints.Select(x => new CalibrationDataPointVM(x.X, x.Y)).ToObservableCollection(); + } - ColorCalibrationVM = new ColorCalibrationViewVM(_notification) - { - RML = ActiveRML, - LiquidTypes = LiquidTypesRmls.Where(x => x.LiquidType.HasPigment).ToList().Select(y => y.LiquidType).ToList(), - }; + CalibrationDataViewVM.LiquidsCalibrationData.Add(catVM); + } - _rmlBeforeSave = RmlDTO.FromObservable(ActiveRML); + ColorConversionViewVM = new ColorConversionViewVM(_notification) + { + RML = ActiveRML, + CCT = SelectedCCT, + LiquidsCalibrationData = CalibrationDataViewVM.LiquidsCalibrationData, + LiquidTypesRmls = LiquidTypesRmls, + }; - View.NavigateTo(RmlNavigationView.RmlView); + View.NavigateTo(RmlNavigationView.RmlView); - InvalidateRelayCommands(); + 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; - } + IsFree = true; } } @@ -485,7 +371,6 @@ 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() @@ -515,10 +400,8 @@ 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.Count > 0 ? Rmls.Max(x => x.Code) + 1 : 1; + rml.Code = Rmls.Max(x => x.Code) + 1; rml.MediaMaterial = Materials.FirstOrDefault(); rml.MediaPurpose = Purposes.FirstOrDefault(); rml.MediaCondition = Conditions.FirstOrDefault(); @@ -540,9 +423,6 @@ 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; @@ -552,64 +432,25 @@ namespace Tango.MachineStudio.RML.ViewModels private async void RemoveSelectedRml() { - 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?")) + 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?")) { + 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<RmlDeleteDialogViewVM, RmlDeleteDialogView>(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.FlattenMessage()}"); - LoadRmls(); - } - finally - { - IsFree = true; + LogManager.Log(ex, $"Error removing selected RML {SelectedRML.Name}."); + _notification.ShowError($"An error occurred while trying to remove the selected RML.\n{ex.Message}"); } } + + IsFree = true; } } @@ -627,18 +468,15 @@ namespace Tango.MachineStudio.RML.ViewModels using (var context = ObservablesContext.CreateDefault()) { - var rml = await new RmlBuilder(context).Set(SelectedRML.Guid).WithActiveParametersGroup().WithLiquidFactors().WithSpools().BuildAsync(); + var rml = await new RmlBuilder(context).Set(SelectedRML.Guid).WithActiveParametersGroup().WithLiquidFactors().BuildAsync(); Rml cloned = new Rml(); - rml.MapPropertiesTo(cloned, MappingFlags.NoReferenceTypes); + rml.MapPrimitivesWithStrings(cloned); 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; @@ -664,19 +502,8 @@ 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(); @@ -731,11 +558,6 @@ 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?")) @@ -826,11 +648,6 @@ namespace Tango.MachineStudio.RML.ViewModels ActiveRML.LastUpdated = DateTime.UtcNow; - if (_rmlBeforeSave.QualificationLevel != ActiveRML.QualificationLevel) - { - ActiveRML.QualificationDate = DateTime.UtcNow; - } - if (SelectedCCT != null) { if (SelectedCCT.IsNew) @@ -848,26 +665,18 @@ 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.FlattenMessage()}"); - } - finally - { - IsFree = true; + _notification.ShowError($"An error occurred while trying to save the current RML.\n{ex.Message}"); } + + LoadActiveRML(ActiveRML.Guid); + + IsFree = true; } private void BackToRmls() @@ -883,12 +692,6 @@ 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; @@ -1001,8 +804,6 @@ 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(); @@ -1059,26 +860,5 @@ 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 } } |
