Browse Source

Components init replaced to the Bootstrap.cs as an autofac container builder. And there is a lot of other edits. for better flexability.

master
Nikita Romanenko 6 years ago
parent
commit
b2b90f4f52
  1. 11
      Pores/App.xaml.cs
  2. 53
      Pores/Bootstrap.cs
  3. 31
      Pores/Controllers/BatchGenerator.cs
  4. 24
      Pores/Controllers/ModelGenerationController.cs
  5. 24
      Pores/Controllers/PoreGenerator.cs
  6. 2
      Pores/Controllers/PoresToFileSaver.cs
  7. 11
      Pores/Interfaces/IPoreGenerator.cs
  8. 0
      Pores/Interfaces/IPoreLocalization.cs
  9. 20
      Pores/MainWindow.xaml
  10. 9
      Pores/MainWindow.xaml.cs
  11. 55
      Pores/MainWindowViewModel.cs
  12. 11
      Pores/Models/BatchPoint.cs
  13. 1
      Pores/Models/Material.cs
  14. 10
      Pores/Pores.csproj
  15. 1
      Pores/packages.config

11
Pores/App.xaml.cs

@ -1,16 +1,7 @@
using System; using System.Windows;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Pores namespace Pores
{ {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application public partial class App : Application
{ {
} }

53
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<Material>()
.SingleInstance();
build
.Register(c => new ObservableCollection<IPoreLocalization>() { new NormalLocalization(c.Resolve<Material>()) })
.As<IEnumerable<IPoreLocalization>>()
.SingleInstance();
build
.RegisterType<PoreGenerator>()
.As<IPoreGenerator>()
.SingleInstance();
build
.RegisterType<BatchGenerator>()
.AsSelf();
build
.RegisterType<PoresToFileSaver>()
.AsSelf();
build
.RegisterType<MainWindowViewModel>()
.AsSelf()
.SingleInstance();
return build.Build();
}
}
}

31
Pores/Controllers/BatchGenerator.cs

@ -10,23 +10,32 @@ namespace Pores.Controllers
{ {
public class BatchGenerator 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 BatchGenerator(Material material, PoresToFileSaver batchSaver)
public IList<Pore> Pores { get; }
public BatchGenerator(Material material, IList<Pore> pores)
{ {
Material = material; Material = material;
Pores = pores; BatchSaver = batchSaver;
} }
public void GenerateBatch(Point3D center, double width, double depth) public void GenerateBatch(BatchPoint bp, IList<Pore> pores, int index = 0)
{
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()
{ {
var p1 = new Point3D() { X = center.X - width / 2, Y = center.Y - width / 2, Z = Math.Abs(center.Z - depth) }; X = bp.Center.X + bp.Width / 2,
var p2 = new Point3D() { X = center.X + width / 2, Y = center.Y + width / 2, Z = center.Z }; Y = bp.Center.Y + bp.Width / 2,
batchSaver.FileName = "batch_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log"; Z = bp.Center.Z
batchSaver.Save(Pores.Where(p => IsPointInsideOfCube(p.Point, p1, p2))); };
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) public bool IsPointInsideOfCube(Point3D p, Point3D vertex1, Point3D vertex2)

24
Pores/Controllers/ModelGenerationController.cs

@ -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<Pore> PoreCoordinates { get; set; }
public ModelGenerationController(IPoreLocalization poresLocalization)
{
PoresLocalization = poresLocalization;
}
public void GeneratePores()
{
PoreCoordinates = PoresLocalization.GetLocalization();
}
}
}

24
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<Pore> Pores { get; set; }
public PoreGenerator(Material currentMaterial)
{
CurrentMaterial = currentMaterial;
}
public void Generate(IPoreLocalization localization)
{
Pores = localization.GetLocalization();
}
}
}

2
Pores/Controllers/PoresToFileSaver.cs

@ -39,7 +39,7 @@ namespace Pores.Controllers
} }
} }
public PoresToFileSaver(string fileName) public PoresToFileSaver(string fileName = "not_defined")
{ {
FileName = fileName; FileName = fileName;
} }

11
Pores/Interfaces/IPoreGenerator.cs

@ -0,0 +1,11 @@
using Pores.Models;
using System.Collections.Generic;
namespace Pores.Interfaces
{
public interface IPoreGenerator
{
IList<Pore> Pores { get; set; }
void Generate(IPoreLocalization localization);
}
}

0
Pores/Interfaces/IPoresLocalization.cs → Pores/Interfaces/IPoreLocalization.cs

20
Pores/MainWindow.xaml

@ -23,20 +23,20 @@
<StackPanel VerticalAlignment="Center" Grid.Column="2"> <StackPanel VerticalAlignment="Center" Grid.Column="2">
<TextBlock Text="Параметры модели"/> <TextBlock Text="Параметры модели"/>
<TextBlock Text="Ширина: "/> <TextBlock Text="Ширина: "/>
<TextBox Text="{Binding Material.Width}"/> <TextBox Text="{Binding CurrentMaterial.Width}"/>
<TextBlock Text="Глубина: "/> <TextBlock Text="Глубина: "/>
<TextBox Text="{Binding Material.Depth}"/> <TextBox Text="{Binding CurrentMaterial.Depth}"/>
<TextBlock Text="Высота: "/> <TextBlock Text="Высота: "/>
<TextBox Text="{Binding Material.Height}"/> <TextBox Text="{Binding CurrentMaterial.Height}"/>
<TextBlock Text="Разрешение каждого из трех измерений (общее количество точек = n^3): " TextWrapping="Wrap"/> <TextBlock Text="Разрешение каждого из трех измерений (общее количество точек = n^3): " TextWrapping="Wrap"/>
<TextBox Text="{Binding Material.NumberOfPores}"/> <TextBox Text="{Binding CurrentMaterial.NumberOfPores}"/>
</StackPanel> </StackPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Grid.RowSpan="2" Margin="20 0"> <StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Grid.RowSpan="2" Margin="20 0">
<TextBlock Text="Результаты работы"/> <TextBlock Text="Результаты работы"/>
<TextBlock Text="Количество пор: "/> <TextBlock Text="Количество пор: "/>
<TextBox Text="{Binding GenController.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/> <TextBox Text="{Binding PoreGeneratorInstance.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center"> <StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center">
@ -66,15 +66,15 @@
<StackPanel VerticalAlignment="Center" Grid.Column="1"> <StackPanel VerticalAlignment="Center" Grid.Column="1">
<TextBlock Text="Параметры единичной выборки: "/> <TextBlock Text="Параметры единичной выборки: "/>
<TextBlock Text="Координаты центра (Х): "/> <TextBlock Text="Координаты центра (Х): "/>
<TextBox Text="{Binding BatchCenter.X}"/> <TextBox Text="{Binding BatchPoint.Center.X}"/>
<TextBlock Text="Координаты центра (Y): "/> <TextBlock Text="Координаты центра (Y): "/>
<TextBox Text="{Binding BatchCenter.Y}"/> <TextBox Text="{Binding BatchPoint.Center.Y}"/>
<TextBlock Text="Координаты центра (Z): "/> <TextBlock Text="Координаты центра (Z): "/>
<TextBox Text="{Binding BatchCenter.Z}"/> <TextBox Text="{Binding BatchPoint.Center.Z}"/>
<TextBlock Text="Ширина: "/> <TextBlock Text="Ширина: "/>
<TextBox Text="{Binding BatchWidth}"/> <TextBox Text="{Binding BatchPoint.Width}"/>
<TextBlock Text="Глубина:"/> <TextBlock Text="Глубина:"/>
<TextBox Text="{Binding BatchDepth}"/> <TextBox Text="{Binding BatchPoint.Depth}"/>
</StackPanel> </StackPanel>

9
Pores/MainWindow.xaml.cs

@ -1,7 +1,5 @@
using Pores.Controllers; 
using Pores.Interfaces; using Autofac;
using Pores.Models;
using Pores.Models.Localizations;
using System.Windows; using System.Windows;
namespace Pores namespace Pores
@ -10,7 +8,8 @@ namespace Pores
{ {
public MainWindow() public MainWindow()
{ {
DataContext = new MainWindowViewModel(); var c = new Bootstrap().CompileContainer();
DataContext = c.Resolve<MainWindowViewModel>();
InitializeComponent(); InitializeComponent();
} }
} }

55
Pores/MainWindowViewModel.cs

@ -1,56 +1,69 @@
using Pores.Controllers; using Pores.Controllers;
using Pores.Interfaces; using Pores.Interfaces;
using Pores.Models; using Pores.Models;
using Pores.Models.Localizations;
using Pores.Helpers; using Pores.Helpers;
using System.Windows.Input; using System.Windows.Input;
using System.Collections.ObjectModel; using System.Collections.Generic;
using System;
namespace Pores namespace Pores
{ {
public class MainWindowViewModel : PropertyChangedClass public class MainWindowViewModel : PropertyChangedClass
{ {
public Material Material { get; set; } = new Material(); public Material CurrentMaterial { get; set; }
public IEnumerable<IPoreLocalization> Localizations { get; set; }
public IPoreLocalization SelectedLocalization { get; set; } public IPoreLocalization SelectedLocalization { get; set; }
public ModelGenerationController GenController { get; set; } public IPoreGenerator PoreGeneratorInstance { get; set; }
public ObservableCollection<IPoreLocalization> Localizations { get; set; } public BatchGenerator BatchGenerator { get; }
public PoresToFileSaver saveController = new PoresToFileSaver("output.log");
private PoresToFileSaver PoreSaver;
public int NumberOfPores { get; set; } public int NumberOfPores { get; set; }
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 double BatchWidth { get; set; } public BatchPoint BatchPoint { get; set; } = new BatchPoint();
public double BatchDepth { get; set; }
public Point3D BatchCenter { get; set; } = new Point3D();
public MainWindowViewModel() public MainWindowViewModel(
Material material,
IEnumerable<IPoreLocalization> localizations,
IPoreGenerator poreGenerator,
PoresToFileSaver poreSaver,
BatchGenerator batchGenerator)
{ {
Localizations = new ObservableCollection<IPoreLocalization> CurrentMaterial = material;
{ Localizations = localizations;
new NormalLocalization(Material), PoreGeneratorInstance = poreGenerator;
}; PoreSaver = poreSaver;
BatchGenerator = batchGenerator;
GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null); GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null);
SaveToFileCommand = new RelayCommand(Save, () => GenController?.PoreCoordinates?.Count > 0); SaveToFileCommand = new RelayCommand(Save, () => PoreGeneratorInstance?.Pores?.Count > 0);
GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => GenController?.PoreCoordinates?.Count > 0); GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => PoreGeneratorInstance?.Pores?.Count > 0);
} }
public void StartGeneration() public void StartGeneration()
{ {
GenController = new ModelGenerationController(SelectedLocalization); PoreGeneratorInstance.Generate(SelectedLocalization);
GenController.GeneratePores();
} }
public void Save() 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() public void CreateBatch()
{ {
var batchGenerator = new BatchGenerator(Material, GenController.PoreCoordinates); BatchGenerator.GenerateBatch(BatchPoint, PoreGeneratorInstance.Pores);
batchGenerator.GenerateBatch(BatchCenter, BatchWidth, BatchDepth);
} }
} }
} }

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

1
Pores/Models/Material.cs

@ -4,7 +4,6 @@ namespace Pores.Models
{ {
public class Material : PropertyChangedClass public class Material : PropertyChangedClass
{ {
public Point3D GetCoordinate { get; set; }
public double Height { get; set; } public double Height { get; set; }
public double Width { get; set; } public double Width { get; set; }
public double Depth { get; set; } public double Depth { get; set; }

10
Pores/Pores.csproj

@ -39,6 +39,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Autofac, Version=4.8.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.8.1\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="Costura, Version=3.2.1.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL"> <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> <HintPath>..\packages\Costura.Fody.3.2.1\lib\net40\Costura.dll</HintPath>
</Reference> </Reference>
@ -76,14 +79,17 @@
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Bootstrap.cs" />
<Compile Include="Controllers\BaseSaveController.cs" /> <Compile Include="Controllers\BaseSaveController.cs" />
<Compile Include="Controllers\BatchGenerator.cs" /> <Compile Include="Controllers\BatchGenerator.cs" />
<Compile Include="Controllers\PoresToFileSaver.cs" /> <Compile Include="Controllers\PoresToFileSaver.cs" />
<Compile Include="Controllers\ModelGenerationController.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" />
<Compile Include="Interfaces\IPoresLocalization.cs" /> <Compile Include="Interfaces\IPoreGenerator.cs" />
<Compile Include="Interfaces\IPoreLocalization.cs" />
<Compile Include="MainWindowViewModel.cs" /> <Compile Include="MainWindowViewModel.cs" />
<Compile Include="Models\BatchPoint.cs" />
<Compile Include="Models\Point.cs" /> <Compile Include="Models\Point.cs" />
<Compile Include="Models\Point2D.cs" /> <Compile Include="Models\Point2D.cs" />
<Compile Include="Models\Point3D.cs" /> <Compile Include="Models\Point3D.cs" />

1
Pores/packages.config

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Autofac" version="4.8.1" targetFramework="net461" />
<package id="Costura.Fody" version="3.2.1" targetFramework="net461" /> <package id="Costura.Fody" version="3.2.1" targetFramework="net461" />
<package id="Fody" version="3.3.3" targetFramework="net461" developmentDependency="true" /> <package id="Fody" version="3.3.3" targetFramework="net461" developmentDependency="true" />
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" /> <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net461" />

Loading…
Cancel
Save