Browse Source

New visual control logic. Updates in saving logic: now base save class get more extended functionality. Main window view model got new save logic too, and users can create a bunch of models and batches by pressing one button.

master
Nikita Romanenko 6 years ago
parent
commit
8437cff1e1
  1. 2
      Pores/Bootstrap.cs
  2. 8
      Pores/Controllers/BaseSaveController.cs
  3. 8
      Pores/Controllers/BaseSaveLoadController.cs
  4. 10
      Pores/Controllers/BatchGenerator.cs
  5. 5
      Pores/Controllers/PoreGenerator.cs
  6. 47
      Pores/Controllers/PoresToFileSaver.cs
  7. 56
      Pores/Controllers/ReportGenerator.cs
  8. 3
      Pores/Interfaces/IPoreGenerator.cs
  9. 78
      Pores/MainWindow.xaml
  10. 76
      Pores/MainWindowViewModel.cs
  11. 4
      Pores/Pores.csproj

2
Pores/Bootstrap.cs

@ -38,7 +38,7 @@ namespace Pores
.AsSelf(); .AsSelf();
build build
.RegisterType<PoresToFileSaver>() .RegisterType<ReportGenerator>()
.AsSelf(); .AsSelf();
build build

8
Pores/Controllers/BaseSaveController.cs

@ -1,8 +0,0 @@
namespace Pores.Controllers
{
public abstract class BaseSaveController<TData>
{
public abstract void Save(TData data);
public abstract TData Load();
}
}

8
Pores/Controllers/BaseSaveLoadController.cs

@ -0,0 +1,8 @@
namespace Pores.Controllers
{
public abstract class BaseSaveLoadController
{
public abstract void Save<DataType>(DataType data, string fileName);
public abstract DataType Load<DataType>();
}
}

10
Pores/Controllers/BatchGenerator.cs

@ -10,10 +10,10 @@ namespace Pores.Controllers
{ {
public class BatchGenerator public class BatchGenerator
{ {
public PoresToFileSaver BatchSaver { get; set; } public ReportGenerator BatchSaver { get; set; }
public Material Material { get; set; } public Material Material { get; set; }
public BatchGenerator(Material material, PoresToFileSaver batchSaver) public BatchGenerator(Material material, ReportGenerator batchSaver)
{ {
Material = material; Material = material;
BatchSaver = batchSaver; BatchSaver = batchSaver;
@ -33,9 +33,9 @@ namespace Pores.Controllers
Y = bp.Center.Y + bp.Width / 2, Y = bp.Center.Y + bp.Width / 2,
Z = bp.Center.Z Z = bp.Center.Z
}; };
BatchSaver.FileName = $"batch_${index.ToString()}_${DateTime.Now.ToString("yyyyMMdd_HHmmss")}.log"; BatchSaver.Save(
BatchSaver.FileName = string.Format("batch_{0}_{1}.log", index.ToString(), DateTime.Now.ToString("yyyyMMdd_HHmmss")); 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) public bool IsPointInsideOfCube(Point3D p, Point3D vertex1, Point3D vertex2)

5
Pores/Controllers/PoreGenerator.cs

@ -9,16 +9,15 @@ namespace Pores.Controllers
{ {
public IPoreLocalization PoresLocalization { get; set; } public IPoreLocalization PoresLocalization { get; set; }
public Material CurrentMaterial { get; set; } public Material CurrentMaterial { get; set; }
public IList<Pore> Pores { get; set; }
public PoreGenerator(Material currentMaterial) public PoreGenerator(Material currentMaterial)
{ {
CurrentMaterial = currentMaterial; CurrentMaterial = currentMaterial;
} }
public void Generate(IPoreLocalization localization) public IList<Pore> Generate(IPoreLocalization localization)
{ {
Pores = localization.GetLocalization(); return localization.GetLocalization();
} }
} }
} }

47
Pores/Controllers/PoresToFileSaver.cs

@ -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<IEnumerable<Pore>>
{
public string FileName { get; set; }
public override IEnumerable<Pore> Load()
{
try
{
var data = File.ReadAllText(FileName);
var output = JsonConvert.DeserializeObject<IList<Pore>>(data);
return output;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
public override void Save(IEnumerable<Pore> 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;
}
}
}

56
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<DataType>()
{
try
{
var dialog = new OpenFileDialog();
if (dialog.ShowDialog() != true)
return default(DataType);
var data = File.ReadAllText(dialog.FileName);
var output = JsonConvert.DeserializeObject<DataType>(data);
return output;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return default(DataType);
}
}
public override void Save<DataType>(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);
}
}
}
}

3
Pores/Interfaces/IPoreGenerator.cs

@ -5,7 +5,6 @@ namespace Pores.Interfaces
{ {
public interface IPoreGenerator public interface IPoreGenerator
{ {
IList<Pore> Pores { get; set; } IList<Pore> Generate(IPoreLocalization localization);
void Generate(IPoreLocalization localization);
} }
} }

78
Pores/MainWindow.xaml

@ -6,21 +6,27 @@
xmlns:localizations="clr-namespace:Pores.Models.Localizations" xmlns:localizations="clr-namespace:Pores.Models.Localizations"
xmlns:local="clr-namespace:Pores" xmlns:local="clr-namespace:Pores"
mc:Ignorable="d" mc:Ignorable="d"
Title="Pores" Height="450" Width="800"> Title="Pores" Height="500" Width="1000">
<Grid> <Grid IsEnabled="{Binding EnableControlsFlag}">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/> <ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="3*"/>
<RowDefinition Height="2*"/> <RowDefinition Height="2*"/>
<RowDefinition Height="1*"/> <RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<DockPanel Grid.RowSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="DimGray">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Просмотр модели"/>
</StackPanel>
</DockPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="2"> <StackPanel VerticalAlignment="Center" Grid.Column="1" Margin="5 0">
<TextBlock Text="Параметры модели"/> <TextBlock Text="Параметры модели"/>
<TextBlock Text="Ширина: "/> <TextBlock Text="Ширина: "/>
<TextBox Text="{Binding CurrentMaterial.Width}"/> <TextBox Text="{Binding CurrentMaterial.Width}"/>
@ -33,19 +39,19 @@
</StackPanel> </StackPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Grid.RowSpan="2" Margin="20 0"> <StackPanel VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" Margin="5 0">
<TextBlock Text="Результаты работы"/> <TextBlock Text="Результаты работы"/>
<TextBlock Text="Количество пор: "/> <TextBlock Text="Количество пор: "/>
<TextBox Text="{Binding PoreGeneratorInstance.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/> <TextBox Text="{Binding Pores.Count, Mode=OneWay}" IsEnabled="False"/>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center"> <StackPanel Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="5 0">
<TextBlock Text="Параметры модели распределения:"/>
<ComboBox ItemsSource="{Binding Localizations}" SelectedItem="{Binding SelectedLocalization}" HorizontalAlignment="Left" Width="198"></ComboBox> <ComboBox ItemsSource="{Binding Localizations}" SelectedItem="{Binding SelectedLocalization}" HorizontalAlignment="Left" Width="198"></ComboBox>
<ContentControl Content="{Binding SelectedLocalization}"> <ContentControl Content="{Binding SelectedLocalization}">
<ContentControl.Resources> <ContentControl.Resources>
<DataTemplate DataType="{x:Type localizations:NormalLocalization}"> <DataTemplate DataType="{x:Type localizations:NormalLocalization}">
<StackPanel> <StackPanel>
<TextBlock Text="Параметры модели"/>
<TextBlock Text="Сигма: "/> <TextBlock Text="Сигма: "/>
<TextBox Text="{Binding sigma}"/> <TextBox Text="{Binding sigma}"/>
<TextBlock Text="Мю: "/> <TextBlock Text="Мю: "/>
@ -56,26 +62,44 @@
</ContentControl> </ContentControl>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" VerticalAlignment="Bottom"> <DockPanel Grid.Column="2" Grid.RowSpan="3" Margin="5 0">
<Button Content="Сгенерировать поры" Command="{Binding GeneratePoresCommand}" Grid.Row="2"/>
<Button Content="Выборка в файл" Command="{Binding GenAndSaveBatchCommand}"
Grid.Row="2" Grid.Column="1"/>
<Button Content="Сохранить в файл" Command="{Binding SaveToFileCommand}" Grid.Row="2" Grid.Column="1"/>
</StackPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="1"> <StackPanel DockPanel.Dock="Bottom" VerticalAlignment="Bottom">
<TextBlock Text="Параметры единичной выборки: "/> <Button Content="Сгенерировать модель" Command="{Binding GeneratePoresCommand}"/>
<TextBlock Text="Координаты центра (Х): "/> <Button Content="Единичная выборка в файл" Command="{Binding GenAndSaveBatchCommand}"/>
<TextBox Text="{Binding BatchPoint.Center.X}"/> <Button Content="Сохранить в файл" Command="{Binding SaveToFileCommand}"/>
<TextBlock Text="Координаты центра (Y): "/> <Button Command="{Binding GenerateAllCommand}" CommandParameter="{Binding ModelAndBatchNumber}">
<TextBox Text="{Binding BatchPoint.Center.Y}"/> <Button.Content>
<TextBlock Text="Координаты центра (Z): "/> <TextBlock>
<TextBox Text="{Binding BatchPoint.Center.Z}"/> <Run Text="Сгенерировать модели+выборки ["/>
<TextBlock Text="Ширина: "/> <Run><Binding Path="ModelAndBatchNumber"/></Run>
<TextBox Text="{Binding BatchPoint.Width}"/> <Run Text="]"/>
<TextBlock Text="Глубина:"/> </TextBlock>
<TextBox Text="{Binding BatchPoint.Depth}"/> </Button.Content>
</StackPanel> </Button>
</StackPanel>
<StackPanel DockPanel.Dock="Top">
<TextBlock Text="Настройки экспорта выборок" FontWeight="SemiBold" HorizontalAlignment="Center" Margin="0 5"/>
<TextBlock Text="Параметры единичного фрагмента:"/>
<TextBlock Text="Координаты центра (Х): "/>
<TextBox Text="{Binding BatchPoint.Center.X}"/>
<TextBlock Text="Координаты центра (Y): "/>
<TextBox Text="{Binding BatchPoint.Center.Y}"/>
<TextBlock Text="Координаты центра (Z): "/>
<TextBox Text="{Binding BatchPoint.Center.Z}"/>
<TextBlock Text="Ширина: "/>
<TextBox Text="{Binding BatchPoint.Width}"/>
<TextBlock Text="Глубина:"/>
<TextBox Text="{Binding BatchPoint.Depth}"/>
<TextBlock Text="Количество моделей/моделей и выборок генерируемое за один раз:" TextWrapping="Wrap"/>
<TextBox Text="{Binding ModelAndBatchNumber}"/>
<CheckBox Margin="0 5" IsChecked="{Binding GenerateBatchForEachModelFlag}">
<TextBlock TextWrapping="Wrap" Text="Генерировать для каждой модели по выборке"/>
</CheckBox>
</StackPanel>
</DockPanel>

76
Pores/MainWindowViewModel.cs

@ -5,65 +5,107 @@ using Pores.Helpers;
using System.Windows.Input; using System.Windows.Input;
using System.Collections.Generic; using System.Collections.Generic;
using System; using System;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.ObjectModel;
namespace Pores namespace Pores
{ {
public class MainWindowViewModel : PropertyChangedClass public class MainWindowViewModel : PropertyChangedClass
{ {
public Material CurrentMaterial { get; set; } public Material CurrentMaterial { get; set; }
public ObservableCollection<Pore> Pores { get; set; } = new ObservableCollection<Pore>();
public IEnumerable<IPoreLocalization> Localizations { get; set; } public IEnumerable<IPoreLocalization> Localizations { get; set; }
public IPoreLocalization SelectedLocalization { get; set; } public IPoreLocalization SelectedLocalization { get; set; }
public IPoreGenerator PoreGeneratorInstance { get; set; } public IPoreGenerator PoreGeneratorInstance { get; set; }
public BatchGenerator BatchGenerator { get; } 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 NumberOfPores { get; set; }
public int ModelAndBatchNumber { get; set; } = 1;
public ICommand GeneratePoresCommand { get; set; } public ICommand GeneratePoresCommand { get; set; }
public ICommand SaveToFileCommand { get; set; } public ICommand SaveToFileCommand { get; set; }
public ICommand GenAndSaveBatchCommand { get; set; } public ICommand GenAndSaveBatchCommand { get; set; }
public ICommand GenerateAllCommand { get; set; }
public BatchPoint BatchPoint { get; set; } = new BatchPoint(); public BatchPoint BatchPoint { get; set; } = new BatchPoint();
public Thread AllSavingThread { get; set; }
public MainWindowViewModel( public MainWindowViewModel(
Material material, Material material,
IEnumerable<IPoreLocalization> localizations, IEnumerable<IPoreLocalization> localizations,
IPoreGenerator poreGenerator, IPoreGenerator poreGenerator,
PoresToFileSaver poreSaver, ReportGenerator reportSaver,
BatchGenerator batchGenerator) BatchGenerator batchGenerator)
{ {
CurrentMaterial = material; CurrentMaterial = material;
Localizations = localizations; Localizations = localizations;
PoreGeneratorInstance = poreGenerator; PoreGeneratorInstance = poreGenerator;
PoreSaver = poreSaver; ReportSaver = reportSaver;
BatchGenerator = batchGenerator; 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() GeneratePoresCommand = new RelayCommand(GeneratePoresToForm, () => SelectedLocalization != null);
{ SaveToFileCommand = new RelayCommand(Save, () => Pores?.Count > 0);
PoreGeneratorInstance.Generate(SelectedLocalization); 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 GeneratePoresToForm()
=> Pores = new ObservableCollection<Pore>(PoreGeneratorInstance.Generate(SelectedLocalization));
public IList<Pore> GeneratePores()
=> PoreGeneratorInstance.Generate(SelectedLocalization);
public void Save() public void Save()
=> Save(Pores);
public void Save(IList<Pore> pores, int i = 0)
{ {
PoreSaver.FileName = string.Format( var n = string.Format(
"model_w{0}_d{1}_h{2}_n{3}_{4}.mdl", "model_{0}_w{1}_d{2}_h{3}_n{4}",
i,
CurrentMaterial.Width, CurrentMaterial.Width,
CurrentMaterial.Depth, CurrentMaterial.Depth,
CurrentMaterial.Height, CurrentMaterial.Height,
CurrentMaterial.NumberOfPores, pores.Count);
DateTime.Now.ToString("yyyyMMdd_HHmmss"));
PoreSaver.Save(PoreGeneratorInstance.Pores); ReportSaver.Save(pores, ReportSaver.FileNamer(n));
} }
public void CreateBatch() public void SaveBatch()
=> SaveBatch(Pores);
public void SaveBatch(IList<Pore> 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();
} }
} }
} }

4
Pores/Pores.csproj

@ -80,9 +80,9 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Bootstrap.cs" /> <Compile Include="Bootstrap.cs" />
<Compile Include="Controllers\BaseSaveController.cs" /> <Compile Include="Controllers\BaseSaveLoadController.cs" />
<Compile Include="Controllers\BatchGenerator.cs" /> <Compile Include="Controllers\BatchGenerator.cs" />
<Compile Include="Controllers\PoresToFileSaver.cs" /> <Compile Include="Controllers\ReportGenerator.cs" />
<Compile Include="Controllers\PoreGenerator.cs" /> <Compile Include="Controllers\PoreGenerator.cs" />
<Compile Include="Helpers\PropertyChanged.cs" /> <Compile Include="Helpers\PropertyChanged.cs" />
<Compile Include="Helpers\RelayCommand.cs" /> <Compile Include="Helpers\RelayCommand.cs" />

Loading…
Cancel
Save