diff --git a/Pores/Bootstrap.cs b/Pores/Bootstrap.cs index 691e013..2d3e3ab 100644 --- a/Pores/Bootstrap.cs +++ b/Pores/Bootstrap.cs @@ -38,7 +38,7 @@ namespace Pores .AsSelf(); build - .RegisterType() + .RegisterType() .AsSelf(); build diff --git a/Pores/Controllers/BaseSaveController.cs b/Pores/Controllers/BaseSaveController.cs deleted file mode 100644 index 45b89a1..0000000 --- a/Pores/Controllers/BaseSaveController.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Pores.Controllers -{ - public abstract class BaseSaveController - { - public abstract void Save(TData data); - public abstract TData Load(); - } -} diff --git a/Pores/Controllers/BaseSaveLoadController.cs b/Pores/Controllers/BaseSaveLoadController.cs new file mode 100644 index 0000000..4bc02c6 --- /dev/null +++ b/Pores/Controllers/BaseSaveLoadController.cs @@ -0,0 +1,8 @@ +namespace Pores.Controllers +{ + public abstract class BaseSaveLoadController + { + public abstract void Save(DataType data, string fileName); + public abstract DataType Load(); + } +} diff --git a/Pores/Controllers/BatchGenerator.cs b/Pores/Controllers/BatchGenerator.cs index 14834ab..854a94b 100644 --- a/Pores/Controllers/BatchGenerator.cs +++ b/Pores/Controllers/BatchGenerator.cs @@ -10,10 +10,10 @@ namespace Pores.Controllers { public class BatchGenerator { - public PoresToFileSaver BatchSaver { get; set; } + public ReportGenerator BatchSaver { get; set; } public Material Material { get; set; } - public BatchGenerator(Material material, PoresToFileSaver batchSaver) + public BatchGenerator(Material material, ReportGenerator batchSaver) { Material = material; BatchSaver = batchSaver; @@ -33,9 +33,9 @@ namespace Pores.Controllers Y = bp.Center.Y + bp.Width / 2, Z = bp.Center.Z }; - BatchSaver.FileName = $"batch_${index.ToString()}_${DateTime.Now.ToString("yyyyMMdd_HHmmss")}.log"; - BatchSaver.FileName = string.Format("batch_{0}_{1}.log", index.ToString(), DateTime.Now.ToString("yyyyMMdd_HHmmss")); - BatchSaver.Save(pores.Where(p => IsPointInsideOfCube(p.Point, p1, p2))); + BatchSaver.Save( + pores.Where(p => IsPointInsideOfCube(p.Point, p1, p2)), + BatchSaver.FileNamer(string.Format("batch_{0}", index))); } public bool IsPointInsideOfCube(Point3D p, Point3D vertex1, Point3D vertex2) diff --git a/Pores/Controllers/PoreGenerator.cs b/Pores/Controllers/PoreGenerator.cs index 4a7070d..2e2cc46 100644 --- a/Pores/Controllers/PoreGenerator.cs +++ b/Pores/Controllers/PoreGenerator.cs @@ -9,16 +9,15 @@ namespace Pores.Controllers { public IPoreLocalization PoresLocalization { get; set; } public Material CurrentMaterial { get; set; } - public IList Pores { get; set; } public PoreGenerator(Material currentMaterial) { CurrentMaterial = currentMaterial; } - public void Generate(IPoreLocalization localization) + public IList Generate(IPoreLocalization localization) { - Pores = localization.GetLocalization(); + return localization.GetLocalization(); } } } diff --git a/Pores/Controllers/PoresToFileSaver.cs b/Pores/Controllers/PoresToFileSaver.cs deleted file mode 100644 index 8d12b4d..0000000 --- a/Pores/Controllers/PoresToFileSaver.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Newtonsoft.Json; -using Pores.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Windows; - -namespace Pores.Controllers -{ - public class PoresToFileSaver : BaseSaveController> - { - public string FileName { get; set; } - - public override IEnumerable Load() - { - try - { - var data = File.ReadAllText(FileName); - var output = JsonConvert.DeserializeObject>(data); - return output; - } - catch (Exception ex) - { - MessageBox.Show(ex.Message); - return null; - } - } - - public override void Save(IEnumerable data) - { - var output = JsonConvert.SerializeObject(data); - try - { - File.WriteAllText(FileName, output); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message); - } - } - - public PoresToFileSaver(string fileName = "not_defined") - { - FileName = fileName; - } - } -} diff --git a/Pores/Controllers/ReportGenerator.cs b/Pores/Controllers/ReportGenerator.cs new file mode 100644 index 0000000..3298c7d --- /dev/null +++ b/Pores/Controllers/ReportGenerator.cs @@ -0,0 +1,56 @@ +using Microsoft.Win32; +using Newtonsoft.Json; +using Pores.Interfaces; +using Pores.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows; + +namespace Pores.Controllers +{ + public class ReportGenerator : BaseSaveLoadController + { + public string FileNamer(string fileName) + { + return + ".\\results\\" + + fileName + "_" + + DateTime.Now.ToString("yyyyMMdd_HHmmss") + + ".mdl"; + } + + public override DataType Load() + { + try + { + var dialog = new OpenFileDialog(); + if (dialog.ShowDialog() != true) + return default(DataType); + var data = File.ReadAllText(dialog.FileName); + var output = JsonConvert.DeserializeObject(data); + return output; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + return default(DataType); + } + } + + public override void Save(DataType data, string fileName) + { + var output = JsonConvert.SerializeObject(data); + try + { + if (!Directory.Exists(".\\results\\")) Directory.CreateDirectory(".\\results\\"); + File.WriteAllText(fileName, output); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } + } +} diff --git a/Pores/Interfaces/IPoreGenerator.cs b/Pores/Interfaces/IPoreGenerator.cs index e060534..c3accc7 100644 --- a/Pores/Interfaces/IPoreGenerator.cs +++ b/Pores/Interfaces/IPoreGenerator.cs @@ -5,7 +5,6 @@ namespace Pores.Interfaces { public interface IPoreGenerator { - IList Pores { get; set; } - void Generate(IPoreLocalization localization); + IList Generate(IPoreLocalization localization); } } diff --git a/Pores/MainWindow.xaml b/Pores/MainWindow.xaml index edda30d..569526b 100644 --- a/Pores/MainWindow.xaml +++ b/Pores/MainWindow.xaml @@ -6,21 +6,27 @@ xmlns:localizations="clr-namespace:Pores.Models.Localizations" xmlns:local="clr-namespace:Pores" mc:Ignorable="d" - Title="Pores" Height="450" Width="800"> - + Title="Pores" Height="500" Width="1000"> + + - - - + + + + + + + + @@ -33,19 +39,19 @@ - + - + - + + - @@ -56,26 +62,44 @@ - - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Pores/MainWindowViewModel.cs b/Pores/MainWindowViewModel.cs index fc15f29..ef6b3f1 100644 --- a/Pores/MainWindowViewModel.cs +++ b/Pores/MainWindowViewModel.cs @@ -5,65 +5,107 @@ using Pores.Helpers; using System.Windows.Input; using System.Collections.Generic; using System; +using System.Threading.Tasks; +using System.Threading; +using System.Collections.ObjectModel; namespace Pores { public class MainWindowViewModel : PropertyChangedClass { public Material CurrentMaterial { get; set; } + public ObservableCollection Pores { get; set; } = new ObservableCollection(); public IEnumerable Localizations { get; set; } public IPoreLocalization SelectedLocalization { get; set; } public IPoreGenerator PoreGeneratorInstance { get; set; } public BatchGenerator BatchGenerator { get; } - private PoresToFileSaver PoreSaver; + private ReportGenerator ReportSaver; + + public bool GenerateBatchForEachModelFlag { get; set; } = true; + public bool EnableControlsFlag { get; set; } = true; public int NumberOfPores { get; set; } + public int ModelAndBatchNumber { get; set; } = 1; + public ICommand GeneratePoresCommand { get; set; } public ICommand SaveToFileCommand { get; set; } public ICommand GenAndSaveBatchCommand { get; set; } + public ICommand GenerateAllCommand { get; set; } public BatchPoint BatchPoint { get; set; } = new BatchPoint(); + public Thread AllSavingThread { get; set; } + public MainWindowViewModel( Material material, IEnumerable localizations, IPoreGenerator poreGenerator, - PoresToFileSaver poreSaver, + ReportGenerator reportSaver, BatchGenerator batchGenerator) { CurrentMaterial = material; Localizations = localizations; PoreGeneratorInstance = poreGenerator; - PoreSaver = poreSaver; + ReportSaver = reportSaver; BatchGenerator = batchGenerator; - GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null); - SaveToFileCommand = new RelayCommand(Save, () => PoreGeneratorInstance?.Pores?.Count > 0); - GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => PoreGeneratorInstance?.Pores?.Count > 0); - } - - public void StartGeneration() - { - PoreGeneratorInstance.Generate(SelectedLocalization); + + GeneratePoresCommand = new RelayCommand(GeneratePoresToForm, () => SelectedLocalization != null); + SaveToFileCommand = new RelayCommand(Save, () => Pores?.Count > 0); + GenAndSaveBatchCommand = new RelayCommand(SaveBatch, () => Pores?.Count > 0); + GenerateAllCommand = new RelayCommand(c => GenAndSaveAll((int)c), () => + { + if (SelectedLocalization != null) + if (AllSavingThread?.IsAlive != true) + return true; + return false; + }); } - public void Save() + public void GeneratePoresToForm() + => Pores = new ObservableCollection(PoreGeneratorInstance.Generate(SelectedLocalization)); + + public IList GeneratePores() + => PoreGeneratorInstance.Generate(SelectedLocalization); + + public void Save() + => Save(Pores); + + public void Save(IList pores, int i = 0) { - PoreSaver.FileName = string.Format( - "model_w{0}_d{1}_h{2}_n{3}_{4}.mdl", + var n = string.Format( + "model_{0}_w{1}_d{2}_h{3}_n{4}", + i, CurrentMaterial.Width, CurrentMaterial.Depth, - CurrentMaterial.Height, - CurrentMaterial.NumberOfPores, - DateTime.Now.ToString("yyyyMMdd_HHmmss")); + CurrentMaterial.Height, + pores.Count); - PoreSaver.Save(PoreGeneratorInstance.Pores); + ReportSaver.Save(pores, ReportSaver.FileNamer(n)); } - public void CreateBatch() + public void SaveBatch() + => SaveBatch(Pores); + + public void SaveBatch(IList pores, int i = 0) + => BatchGenerator.GenerateBatch(BatchPoint, pores, i); + + public void GenAndSaveAll(int num) { - BatchGenerator.GenerateBatch(BatchPoint, PoreGeneratorInstance.Pores); + AllSavingThread = new Thread(() => + { + EnableControlsFlag = false; + for (int i = 0; i < num; i++) + { + var list = GeneratePores(); + Save(list, i); + if (GenerateBatchForEachModelFlag) + SaveBatch(list, i); + } + EnableControlsFlag = true; + }); + AllSavingThread.Start(); } } } diff --git a/Pores/Pores.csproj b/Pores/Pores.csproj index c653925..ddbdaee 100644 --- a/Pores/Pores.csproj +++ b/Pores/Pores.csproj @@ -80,9 +80,9 @@ Code - + - +