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