From ebd971a77a5fdabdf4040f07ac0e63e687abb8aa Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Wed, 2 Oct 2019 16:59:05 +0300 Subject: Another fix for SetLiquidVolumes. Changed JobFile PMR. Implemented new JobFile with Liquid Volume awareness. Implemented Import/Export jobs on research module. --- .../ViewModels/MainViewVM.cs | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs index 2d04866d8..27d43bd20 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs @@ -48,6 +48,8 @@ using Tango.MachineStudio.Common.Navigation; using System.Diagnostics; using Tango.Core.ExtensionMethods; using Tango.ColorConversion; +using Tango.PMR.Exports; +using Microsoft.WindowsAPICodePack.Dialogs; namespace Tango.MachineStudio.Developer.ViewModels { @@ -725,6 +727,16 @@ namespace Tango.MachineStudio.Developer.ViewModels /// public RelayCommand ResetProcessParametersCommand { get; set; } + /// + /// Gets or sets the import job file command. + /// + public RelayCommand ImportJobFileCommand { get; set; } + + /// + /// Gets or sets the export job file command. + /// + public RelayCommand ExportJobFileCommand { get; set; } + #endregion #region Constructors @@ -786,6 +798,8 @@ namespace Tango.MachineStudio.Developer.ViewModels DisplayJobEmbroideryFileCommand = new RelayCommand(DisplayJobEmbroideryFile, () => CanWork); ReloadMachinesCommand = new RelayCommand(() => LoadMachine(), () => CanWork && SelectedMachine != null); ResetProcessParametersCommand = new RelayCommand(ResetProcessParameters, () => CanWork && MachineOperator != null); + ImportJobFileCommand = new RelayCommand(ImportJobFile, () => SelectedMachine != null && CanWork); + ExportJobFileCommand = new RelayCommand(ExportJobFile, () => SelectedMachine != null && SelectedMachineJob != null && CanWork); ApplicationManager.ConnectedMachineChanged += ApplicationManager_ConnectedMachineChanged; @@ -2488,6 +2502,112 @@ namespace Tango.MachineStudio.Developer.ViewModels #endregion + #region Job Import/Export + + private async void ExportJobFile() + { + if (SelectedJobs != null && SelectedJobs.Count > 1) + { + CommonOpenFileDialog dlg = new CommonOpenFileDialog(); + dlg.Title = "Select a folder to place all job files."; + dlg.IsFolderPicker = true; + + if (dlg.ShowDialog() == CommonFileDialogResult.Ok) + { + foreach (var job in SelectedJobs) + { + using (_notification.PushTaskItem($"Exporting job '{job.Name}'...")) + { + try + { + LogManager.Log($"Exporting job file {job.Name}"); + + var jobFile = await job.ToJobFile(); + File.WriteAllBytes(Path.Combine(dlg.FileName, job.Name + ".job"), jobFile.ToBytes()); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error exporting job file."); + _notification.ShowError($"An error occurred while trying to export job '{job.Name}'.\n{ex.FlattenMessage()}"); + } + } + } + } + } + else + { + SaveFileDialog dlg = new SaveFileDialog(); + dlg.Title = "Export Job File"; + dlg.Filter = "Twine Job Files|*.job"; + dlg.DefaultExt = ".job"; + dlg.FileName = SelectedMachineJob.Name; + if (dlg.ShowDialog().Value) + { + using (_notification.PushTaskItem($"Exporting job '{SelectedMachineJob.Name}'...")) + { + try + { + LogManager.Log($"Exporting job file {SelectedMachineJob.Name}"); + + var jobFile = await SelectedMachineJob.ToJobFile(); + File.WriteAllBytes(dlg.FileName, jobFile.ToBytes()); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error exporting job file."); + _notification.ShowError($"An error occurred while trying to export job '{SelectedMachineJob.Name}'.\n{ex.FlattenMessage()}"); + } + } + } + } + } + + private async void ImportJobFile() + { + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Import Job File"; + dlg.Filter = "Twine Job Files|*.job"; + dlg.Multiselect = true; + if (dlg.ShowDialog().Value) + { + using (_notification.PushTaskItem($"Importing job files...")) + { + try + { + IsFree = false; + + LogManager.Log($"Importing job files..."); + + foreach (var file in dlg.FileNames) + { + var bytes = File.ReadAllBytes(file); + var jobFile = JobFile.Parser.ParseFrom(bytes); + var job = await Job.FromJobFile(jobFile, SelectedMachine.Guid, AuthenticationProvider.CurrentUser.Guid); + + _machineDbContext.Jobs.Add(job); + } + + await _machineDbContext.SaveChangesAsync(); + + IsFree = true; + + _notification.ShowInfo($"Jobs imported successfully."); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error importing job file."); + _notification.ShowError($"An error occurred while trying to import the selected job file.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + } + } + + #endregion + #region Override Methods protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null) -- cgit v1.3.1