Browse Source

Добавлена Json сериализация данных в файл

master
defend 6 years ago
parent
commit
861c2112e9
  1. 9
      Pores/Controllers/BaseSaveController.cs
  2. 27
      Pores/Controllers/PoresToFileController.cs
  3. 2
      Pores/MainWindow.xaml
  4. 12
      Pores/MainWindowViewModel.cs
  5. 4
      Pores/Models/Coordinate.cs
  6. 8
      Pores/Models/Localizations/NormalLocalization.cs
  7. 7
      Pores/Models/Pore.cs
  8. 5
      Pores/Pores.csproj
  9. 1
      Pores/packages.config

9
Pores/Controllers/BaseSaveController.cs

@ -0,0 +1,9 @@
namespace Pores.Controllers
{
public abstract class BaseSaveController<TData>
{
public virtual TData Data { get; set; } = default(TData);
public abstract void Save();
public abstract void Load();
}
}

27
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<IList<Pore>>
{
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;
}
}
}

2
Pores/MainWindow.xaml

@ -36,7 +36,7 @@
<StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Margin="20 0">
<TextBlock Text="Результаты работы"/>
<TextBlock Text="Количество пор: "/>
<TextBox Text="{Binding GenController.PoreCoordinates.Count, Mode=OneWay}"/>
<TextBox Text="{Binding GenController.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center">

12
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<IPoreLocalization> 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();
}
}
}

4
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; }

8
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<Pore> GetLocalization()
{
var r = new Random();
maxR = Math.Sqrt(Material.NumberOfPores / (Material.Height * Material.Width * Material.Depth) / 3.14);
var result = new ObservableCollection<Pore>();
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);
}
}
}

7
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; }
}
}

5
Pores/Pores.csproj

@ -42,6 +42,9 @@
<Reference Include="Costura, Version=3.2.1.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
<HintPath>..\packages\Costura.Fody.3.2.1\lib\net40\Costura.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PropertyChanged, Version=2.6.0.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
<HintPath>..\packages\PropertyChanged.Fody.2.6.0\lib\net452\PropertyChanged.dll</HintPath>
</Reference>
@ -73,6 +76,8 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Controllers\BaseSaveController.cs" />
<Compile Include="Controllers\PoresToFileController.cs" />
<Compile Include="Controllers\ModelGenerationController.cs" />
<Compile Include="Helpers\PropertyChanged.cs" />
<Compile Include="Helpers\RelayCommand.cs" />

1
Pores/packages.config

@ -2,5 +2,6 @@
<packages>
<package id="Costura.Fody" version="3.2.1" targetFramework="net461" />
<package id="Fody" version="3.3.3" targetFramework="net461" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />
<package id="PropertyChanged.Fody" version="2.6.0" targetFramework="net461" />
</packages>
Loading…
Cancel
Save