From b2b90f4f524682b1fdab37e8d2f09051ddd734e5 Mon Sep 17 00:00:00 2001 From: Nikita Romanenko Date: Wed, 12 Dec 2018 17:16:26 +0300 Subject: [PATCH] Components init replaced to the Bootstrap.cs as an autofac container builder. And there is a lot of other edits. for better flexability. --- Pores/App.xaml.cs | 11 +--- Pores/Bootstrap.cs | 53 ++++++++++++++++++ Pores/Controllers/BatchGenerator.cs | 31 +++++++---- .../Controllers/ModelGenerationController.cs | 24 -------- Pores/Controllers/PoreGenerator.cs | 24 ++++++++ Pores/Controllers/PoresToFileSaver.cs | 2 +- Pores/Interfaces/IPoreGenerator.cs | 11 ++++ ...esLocalization.cs => IPoreLocalization.cs} | 0 Pores/MainWindow.xaml | 20 +++---- Pores/MainWindow.xaml.cs | 9 ++- Pores/MainWindowViewModel.cs | 55 ++++++++++++------- Pores/Models/BatchPoint.cs | 11 ++++ Pores/Models/Material.cs | 1 - Pores/Pores.csproj | 10 +++- Pores/packages.config | 1 + 15 files changed, 178 insertions(+), 85 deletions(-) create mode 100644 Pores/Bootstrap.cs delete mode 100644 Pores/Controllers/ModelGenerationController.cs create mode 100644 Pores/Controllers/PoreGenerator.cs create mode 100644 Pores/Interfaces/IPoreGenerator.cs rename Pores/Interfaces/{IPoresLocalization.cs => IPoreLocalization.cs} (100%) create mode 100644 Pores/Models/BatchPoint.cs diff --git a/Pores/App.xaml.cs b/Pores/App.xaml.cs index f8e65c4..7968fb7 100644 --- a/Pores/App.xaml.cs +++ b/Pores/App.xaml.cs @@ -1,16 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; namespace Pores { - /// - /// Interaction logic for App.xaml - /// public partial class App : Application { } diff --git a/Pores/Bootstrap.cs b/Pores/Bootstrap.cs new file mode 100644 index 0000000..691e013 --- /dev/null +++ b/Pores/Bootstrap.cs @@ -0,0 +1,53 @@ +using Autofac; +using Pores.Controllers; +using Pores.Interfaces; +using Pores.Models; +using Pores.Models.Localizations; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Pores +{ + public class Bootstrap + { + public IContainer CompileContainer() + { + ContainerBuilder build = new ContainerBuilder(); + + build + .Register(c => new Material()) + .As() + .SingleInstance(); + + build + .Register(c => new ObservableCollection() { new NormalLocalization(c.Resolve()) }) + .As>() + .SingleInstance(); + + build + .RegisterType() + .As() + .SingleInstance(); + + build + .RegisterType() + .AsSelf(); + + build + .RegisterType() + .AsSelf(); + + build + .RegisterType() + .AsSelf() + .SingleInstance(); + + + return build.Build(); + } + } +} diff --git a/Pores/Controllers/BatchGenerator.cs b/Pores/Controllers/BatchGenerator.cs index a2a3512..14834ab 100644 --- a/Pores/Controllers/BatchGenerator.cs +++ b/Pores/Controllers/BatchGenerator.cs @@ -10,23 +10,32 @@ namespace Pores.Controllers { public class BatchGenerator { - public PoresToFileSaver batchSaver = new PoresToFileSaver("batch.log"); + public PoresToFileSaver BatchSaver { get; set; } + public Material Material { get; set; } - public Material Material { get; } - public IList Pores { get; } - - public BatchGenerator(Material material, IList pores) + public BatchGenerator(Material material, PoresToFileSaver batchSaver) { Material = material; - Pores = pores; + BatchSaver = batchSaver; } - public void GenerateBatch(Point3D center, double width, double depth) + public void GenerateBatch(BatchPoint bp, IList pores, int index = 0) { - var p1 = new Point3D() { X = center.X - width / 2, Y = center.Y - width / 2, Z = Math.Abs(center.Z - depth) }; - var p2 = new Point3D() { X = center.X + width / 2, Y = center.Y + width / 2, Z = center.Z }; - batchSaver.FileName = "batch_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log"; - batchSaver.Save(Pores.Where(p => IsPointInsideOfCube(p.Point, p1, p2))); + var p1 = new Point3D() + { + X = bp.Center.X - bp.Width / 2, + Y = bp.Center.Y - bp.Width / 2, + Z = Math.Abs(bp.Center.Z - bp.Depth) + }; + var p2 = new Point3D() + { + X = bp.Center.X + bp.Width / 2, + 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))); } public bool IsPointInsideOfCube(Point3D p, Point3D vertex1, Point3D vertex2) diff --git a/Pores/Controllers/ModelGenerationController.cs b/Pores/Controllers/ModelGenerationController.cs deleted file mode 100644 index 7370d89..0000000 --- a/Pores/Controllers/ModelGenerationController.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Pores.Helpers; -using Pores.Interfaces; -using Pores.Models; -using System.Collections.Generic; - -namespace Pores.Controllers -{ - public class ModelGenerationController : PropertyChangedClass - { - public IPoreLocalization PoresLocalization { get; set; } - public Material MaterialInstance { get; set; } - public IList PoreCoordinates { get; set; } - - public ModelGenerationController(IPoreLocalization poresLocalization) - { - PoresLocalization = poresLocalization; - } - - public void GeneratePores() - { - PoreCoordinates = PoresLocalization.GetLocalization(); - } - } -} diff --git a/Pores/Controllers/PoreGenerator.cs b/Pores/Controllers/PoreGenerator.cs new file mode 100644 index 0000000..4a7070d --- /dev/null +++ b/Pores/Controllers/PoreGenerator.cs @@ -0,0 +1,24 @@ +using Pores.Helpers; +using Pores.Interfaces; +using Pores.Models; +using System.Collections.Generic; + +namespace Pores.Controllers +{ + public class PoreGenerator : PropertyChangedClass, IPoreGenerator + { + 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) + { + Pores = localization.GetLocalization(); + } + } +} diff --git a/Pores/Controllers/PoresToFileSaver.cs b/Pores/Controllers/PoresToFileSaver.cs index b963458..8d12b4d 100644 --- a/Pores/Controllers/PoresToFileSaver.cs +++ b/Pores/Controllers/PoresToFileSaver.cs @@ -39,7 +39,7 @@ namespace Pores.Controllers } } - public PoresToFileSaver(string fileName) + public PoresToFileSaver(string fileName = "not_defined") { FileName = fileName; } diff --git a/Pores/Interfaces/IPoreGenerator.cs b/Pores/Interfaces/IPoreGenerator.cs new file mode 100644 index 0000000..e060534 --- /dev/null +++ b/Pores/Interfaces/IPoreGenerator.cs @@ -0,0 +1,11 @@ +using Pores.Models; +using System.Collections.Generic; + +namespace Pores.Interfaces +{ + public interface IPoreGenerator + { + IList Pores { get; set; } + void Generate(IPoreLocalization localization); + } +} diff --git a/Pores/Interfaces/IPoresLocalization.cs b/Pores/Interfaces/IPoreLocalization.cs similarity index 100% rename from Pores/Interfaces/IPoresLocalization.cs rename to Pores/Interfaces/IPoreLocalization.cs diff --git a/Pores/MainWindow.xaml b/Pores/MainWindow.xaml index a27ee6d..edda30d 100644 --- a/Pores/MainWindow.xaml +++ b/Pores/MainWindow.xaml @@ -23,20 +23,20 @@ - + - + - + - + - + @@ -66,15 +66,15 @@ - + - + - + - + - + diff --git a/Pores/MainWindow.xaml.cs b/Pores/MainWindow.xaml.cs index 58eb07d..fa6c4fc 100644 --- a/Pores/MainWindow.xaml.cs +++ b/Pores/MainWindow.xaml.cs @@ -1,7 +1,5 @@ -using Pores.Controllers; -using Pores.Interfaces; -using Pores.Models; -using Pores.Models.Localizations; + +using Autofac; using System.Windows; namespace Pores @@ -10,7 +8,8 @@ namespace Pores { public MainWindow() { - DataContext = new MainWindowViewModel(); + var c = new Bootstrap().CompileContainer(); + DataContext = c.Resolve(); InitializeComponent(); } } diff --git a/Pores/MainWindowViewModel.cs b/Pores/MainWindowViewModel.cs index a051107..fc15f29 100644 --- a/Pores/MainWindowViewModel.cs +++ b/Pores/MainWindowViewModel.cs @@ -1,56 +1,69 @@ using Pores.Controllers; using Pores.Interfaces; using Pores.Models; -using Pores.Models.Localizations; using Pores.Helpers; using System.Windows.Input; -using System.Collections.ObjectModel; +using System.Collections.Generic; +using System; namespace Pores { public class MainWindowViewModel : PropertyChangedClass { - public Material Material { get; set; } = new Material(); + public Material CurrentMaterial { get; set; } + public IEnumerable Localizations { get; set; } + public IPoreLocalization SelectedLocalization { get; set; } - public ModelGenerationController GenController { get; set; } - public ObservableCollection Localizations { get; set; } - public PoresToFileSaver saveController = new PoresToFileSaver("output.log"); + public IPoreGenerator PoreGeneratorInstance { get; set; } + public BatchGenerator BatchGenerator { get; } + + private PoresToFileSaver PoreSaver; public int NumberOfPores { get; set; } public ICommand GeneratePoresCommand { get; set; } public ICommand SaveToFileCommand { get; set; } public ICommand GenAndSaveBatchCommand { get; set; } - public double BatchWidth { get; set; } - public double BatchDepth { get; set; } - public Point3D BatchCenter { get; set; } = new Point3D(); + public BatchPoint BatchPoint { get; set; } = new BatchPoint(); - public MainWindowViewModel() + public MainWindowViewModel( + Material material, + IEnumerable localizations, + IPoreGenerator poreGenerator, + PoresToFileSaver poreSaver, + BatchGenerator batchGenerator) { - Localizations = new ObservableCollection - { - new NormalLocalization(Material), - }; + CurrentMaterial = material; + Localizations = localizations; + PoreGeneratorInstance = poreGenerator; + PoreSaver = poreSaver; + BatchGenerator = batchGenerator; GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null); - SaveToFileCommand = new RelayCommand(Save, () => GenController?.PoreCoordinates?.Count > 0); - GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => GenController?.PoreCoordinates?.Count > 0); + SaveToFileCommand = new RelayCommand(Save, () => PoreGeneratorInstance?.Pores?.Count > 0); + GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => PoreGeneratorInstance?.Pores?.Count > 0); } public void StartGeneration() { - GenController = new ModelGenerationController(SelectedLocalization); - GenController.GeneratePores(); + PoreGeneratorInstance.Generate(SelectedLocalization); } public void Save() { - saveController.Save(GenController.PoreCoordinates); + PoreSaver.FileName = string.Format( + "model_w{0}_d{1}_h{2}_n{3}_{4}.mdl", + CurrentMaterial.Width, + CurrentMaterial.Depth, + CurrentMaterial.Height, + CurrentMaterial.NumberOfPores, + DateTime.Now.ToString("yyyyMMdd_HHmmss")); + + PoreSaver.Save(PoreGeneratorInstance.Pores); } public void CreateBatch() { - var batchGenerator = new BatchGenerator(Material, GenController.PoreCoordinates); - batchGenerator.GenerateBatch(BatchCenter, BatchWidth, BatchDepth); + BatchGenerator.GenerateBatch(BatchPoint, PoreGeneratorInstance.Pores); } } } diff --git a/Pores/Models/BatchPoint.cs b/Pores/Models/BatchPoint.cs new file mode 100644 index 0000000..e7550ca --- /dev/null +++ b/Pores/Models/BatchPoint.cs @@ -0,0 +1,11 @@ +using Pores.Helpers; + +namespace Pores.Models +{ + public class BatchPoint : PropertyChangedClass + { + public Point3D Center { get; set; } = new Point3D(); + public double Width { get; set; } + public double Depth { get; set; } + } +} diff --git a/Pores/Models/Material.cs b/Pores/Models/Material.cs index 82f15f4..62fedbb 100644 --- a/Pores/Models/Material.cs +++ b/Pores/Models/Material.cs @@ -4,7 +4,6 @@ namespace Pores.Models { public class Material : PropertyChangedClass { - public Point3D GetCoordinate { get; set; } public double Height { get; set; } public double Width { get; set; } public double Depth { get; set; } diff --git a/Pores/Pores.csproj b/Pores/Pores.csproj index 05334af..c653925 100644 --- a/Pores/Pores.csproj +++ b/Pores/Pores.csproj @@ -39,6 +39,9 @@ 4 + + ..\packages\Autofac.4.8.1\lib\net45\Autofac.dll + ..\packages\Costura.Fody.3.2.1\lib\net40\Costura.dll @@ -76,14 +79,17 @@ App.xaml Code + - + - + + + diff --git a/Pores/packages.config b/Pores/packages.config index 10bfa91..3575338 100644 --- a/Pores/packages.config +++ b/Pores/packages.config @@ -1,5 +1,6 @@  +