diff --git a/Pores/Controllers/BaseSaveController.cs b/Pores/Controllers/BaseSaveController.cs new file mode 100644 index 0000000..9bfe999 --- /dev/null +++ b/Pores/Controllers/BaseSaveController.cs @@ -0,0 +1,9 @@ +namespace Pores.Controllers +{ + public abstract class BaseSaveController + { + public virtual TData Data { get; set; } = default(TData); + public abstract void Save(); + public abstract void Load(); + } +} diff --git a/Pores/Controllers/PoresToFileController.cs b/Pores/Controllers/PoresToFileController.cs new file mode 100644 index 0000000..d5229be --- /dev/null +++ b/Pores/Controllers/PoresToFileController.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using Pores.Models; +using System.Collections.Generic; +using System.IO; + +namespace Pores.Controllers +{ + public class PoresToFileController : BaseSaveController> + { + public string FileName { get; } + + public override void Load() + { + } + + public override void Save() + { + var output = JsonConvert.SerializeObject(Data); + File.WriteAllText(FileName, output); + } + + public PoresToFileController(string fileName) + { + FileName = fileName; + } + } +} diff --git a/Pores/MainWindow.xaml b/Pores/MainWindow.xaml index a4ebcf8..238b716 100644 --- a/Pores/MainWindow.xaml +++ b/Pores/MainWindow.xaml @@ -36,7 +36,7 @@ - + diff --git a/Pores/MainWindowViewModel.cs b/Pores/MainWindowViewModel.cs index e5b6368..5d638d2 100644 --- a/Pores/MainWindowViewModel.cs +++ b/Pores/MainWindowViewModel.cs @@ -5,6 +5,7 @@ using Pores.Models.Localizations; using Pores.Helpers; using System.Windows.Input; using System.Collections.ObjectModel; +using System.Collections.Generic; namespace Pores { @@ -14,6 +15,8 @@ namespace Pores public IPoreLocalization SelectedLocalization { get; set; } public ModelGenerationController GenController { get; set; } public ObservableCollection Localizations { get; set; } + public PoresToFileController saveController = new PoresToFileController("output.log"); + public ICommand SaveToFileCommand { get; set; } public int NumberOfPores { get; set; } public ICommand GeneratePoresCommand { get; set; } @@ -24,7 +27,8 @@ namespace Pores { new NormalLocalization(Material), }; - GeneratePoresCommand = new RelayCommand(StartGeneration, () => true); + GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null); + SaveToFileCommand = new RelayCommand(Save, () => GenController?.PoreCoordinates?.Count > 0); } public void StartGeneration() @@ -32,5 +36,11 @@ namespace Pores GenController = new ModelGenerationController(SelectedLocalization); GenController.GeneratePores(); } + + public void Save() + { + saveController.Data = GenController.PoreCoordinates; + saveController.Save(); + } } } diff --git a/Pores/Models/Coordinate.cs b/Pores/Models/Coordinate.cs index c6ad906..1c509aa 100644 --- a/Pores/Models/Coordinate.cs +++ b/Pores/Models/Coordinate.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Pores.Models { + [Serializable] public class Coordinate { public double X { get; set; } diff --git a/Pores/Models/Localizations/NormalLocalization.cs b/Pores/Models/Localizations/NormalLocalization.cs index 2f4bac1..dc07058 100644 --- a/Pores/Models/Localizations/NormalLocalization.cs +++ b/Pores/Models/Localizations/NormalLocalization.cs @@ -9,7 +9,7 @@ namespace Pores.Models.Localizations { public double mu { get; set; } public double sigma { get; set; } - private double maxR; + public double maxR { get; set; } private Material Material { get; } public double GaussFunction (double x, double sigma, double mu) @@ -27,7 +27,7 @@ namespace Pores.Models.Localizations public override ObservableCollection GetLocalization() { var r = new Random(); - + maxR = Math.Sqrt(Material.NumberOfPores / (Material.Height * Material.Width * Material.Depth) / 3.14); var result = new ObservableCollection(); for (int i = 0; i <= Material.NumberOfPores; i++) for (int j = 0; j <= Material.NumberOfPores; j++) @@ -42,7 +42,7 @@ namespace Pores.Models.Localizations var randomDouble = r.NextDouble(); var gaussResult = GaussFunctionByHeight(c); if (randomDouble < gaussResult) - result.Add(new Pore() { GetCoordinate = c, Radius = maxR }); + result.Add(new Pore() { Point = c, Radius = maxR }); } return result; } @@ -52,7 +52,7 @@ namespace Pores.Models.Localizations Material = material; sigma = 4; mu = Material.Height - Material.Height / 4; - maxR = Math.Sqrt(Material.NumberOfPores / (Material.Height * Material.Width * Material.Depth) / 3.14); + } } } diff --git a/Pores/Models/Pore.cs b/Pores/Models/Pore.cs index ca53dde..d44ec13 100644 --- a/Pores/Models/Pore.cs +++ b/Pores/Models/Pore.cs @@ -1,8 +1,11 @@ -namespace Pores.Models +using System; + +namespace Pores.Models { + [Serializable] public class Pore { - public Coordinate GetCoordinate { get; set; } + public Coordinate Point { get; set; } public double Radius { get; set; } } } diff --git a/Pores/Pores.csproj b/Pores/Pores.csproj index 2558c12..1d1efe8 100644 --- a/Pores/Pores.csproj +++ b/Pores/Pores.csproj @@ -42,6 +42,9 @@ ..\packages\Costura.Fody.3.2.1\lib\net40\Costura.dll + + ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + ..\packages\PropertyChanged.Fody.2.6.0\lib\net452\PropertyChanged.dll @@ -73,6 +76,8 @@ App.xaml Code + + diff --git a/Pores/packages.config b/Pores/packages.config index 6d24498..10bfa91 100644 --- a/Pores/packages.config +++ b/Pores/packages.config @@ -2,5 +2,6 @@ + \ No newline at end of file