| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846 | #pragma warning disable CA1822namespace OINA.Extender.WPF.Testharness{    using System;    using System.Collections.Generic;    using System.Collections.ObjectModel;    using System.Collections.Specialized;    using System.ComponentModel;    using System.Drawing;    using System.Drawing.Imaging;    using System.Globalization;    using System.IO;    using System.Linq;    using System.Reflection;    using System.Runtime.Serialization;    using System.Text;    using System.Threading;    using System.Windows;    using System.Windows.Controls;    using System.Windows.Resources;    using System.Windows.Threading;    using Microsoft.Win32;    using OINA.Extender.Acquisition;    using OINA.Extender.Acquisition.Ed;    using OINA.Extender.Controls;    using OINA.Extender.Controls.Spectrum;    using OINA.Extender.Data.Ed;    using OINA.Extender.Data.Image;    using OINA.Extender.MonitorControl;    using OINA.Extender.Processing;    using OINA.Extender.Processing.Quant;    using WindowsMedia = System.Windows.Media;    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window, INotifyPropertyChanged    {        /// <summary>        /// List of ElectronImage objects        /// </summary>        private readonly List<IElectronImage> electronImageList = new List<IElectronImage>();        /// <summary>        /// StringBuilder object        /// </summary>        private StringBuilder sb = new StringBuilder();        /// <summary>        /// The element dictionary        /// </summary>        private Dictionary<int, string> elementDictionary;        /// <summary>        /// The current spectrum acquisitions        /// </summary>        private Dictionary<IEdSpectrum, AcquisitionItem> currentSpectrumAcquisitions;        /// <summary>        /// Acquisition status timer        /// </summary>        private DispatcherTimer acquisitionStatusTimer;        /// <summary>        /// Identify elements timer        /// </summary>        private DispatcherTimer identifyElementsTimer;        /// <summary>        /// The compare spectra ids        /// </summary>        private ObservableCollection<IEdSpectrum> compareSpectra;        /// <summary>        /// The comparison spectra        /// </summary>        private List<IEdSpectrum> comparisonSpectra;        private int dataChangedCount;        private List<DetectorEnableModel> enabledDetectors;        private List<string> identifiedElements;        private string selectedIdentifiedElement;        private QuantAnalysisStatus status;        private string statusMessage;        /// <summary>        /// MainWindow Constructor        /// </summary>        public MainWindow()        {            Application.Current.DispatcherUnhandledException += this.OnCurrentOnDispatcherUnhandledException;            this.compareSpectra = new ObservableCollection<IEdSpectrum>();            this.currentSpectrumAcquisitions = new Dictionary<IEdSpectrum, AcquisitionItem>();            this.InitializeComponent();            this.DataContext = this;            this.LoadComparisonSpectra();            // Binding spectrum acquisition controller events            OIHelper.EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;            OIHelper.EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;            // Binding image acquisition controller events            OIHelper.ImageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;            OIHelper.ImageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;            // Initial UI contents            this.InitialSpectrumSettingsContent();            this.InitialSpectrumViewer();            this.InitialImageSettingsContent();            this.InitialTimers();            this.InitialProcessingSettingsContent();            this.InitialiseElementDictionary();            this.IncludedListBox.DataContext = this.PeriodicTable;            this.UnsetListBox.DataContext = this.PeriodicTable;            this.ExcludedListBox.DataContext = this.PeriodicTable;            this.TableBackColorCombo.DataContext = this.PeriodicTable;            var clearCompareSpectra = new MenuItem            {                Header = @"Clear Compare Spectra",                Visibility = Visibility.Visible,            };            clearCompareSpectra.Click += this.OnClearCompareSpectra;            this.spectrumViewer.ContextMenu.Items.Add(clearCompareSpectra);            this.spectrumViewer.ContextMenu.UpdateLayout();            this.compareSpectrumColor.SelectedColor = GetRandomColor();            this.SortListBox();            this.PeriodicTable.PropertyChanged += this.OnPropertyChanged;        }        /// <summary>        /// Gets the comparison spectra.        /// </summary>        /// <value>        /// The comparison spectra.        /// </value>        public IList<IEdSpectrum> ComparisonSpectra        {            get { return this.comparisonSpectra; }        }        /// <summary>        /// Gets the compare spectra ids.        /// </summary>        /// <value>        /// The compare spectra ids.        /// </value>        public ObservableCollection<IEdSpectrum> CompareSpectra        {            get { return this.compareSpectra; }        }        /// <summary>        /// Gets the element dictionary.        /// </summary>        /// <value>        /// The element dictionary.        /// </value>        public Dictionary<int, string> ElementDictionary        {            get { return this.elementDictionary; }        }        /// <summary>        /// Gets a value indicating whether this instance is acquiring spectrum.        /// </summary>        /// <value>        /// <c>true</c> if this instance is acquiring spectrum; otherwise, <c>false</c>.        /// </value>        public bool IsAcquiringSpectrum        {            get { return OIHelper.EdSpectrumAcquisitionController.IsAcquiring; }        }        /// <summary>        /// The currently selected Exclusion element        /// </summary>        private int currentExclusionElement;        /// <summary>        /// Gets or sets currently selected Exclusion element        /// </summary>        public int CurrentExclusionElement        {            get            {                return this.currentExclusionElement;            }            set            {                if (value == this.currentExclusionElement)                {                    return;                }                this.currentExclusionElement = value;                this.RaisePropertyChanged(nameof(this.CurrentExclusionElement));            }        }        /// <summary>        /// Enable or disable Markers on Spectrum        /// </summary>        private bool showMarkers;        public bool ShowMarkers        {            get            {                return this.showMarkers;            }            set            {                if (value == this.showMarkers)                {                    return;                }                this.showMarkers = value;                this.ShowHideMarker();                this.RaisePropertyChanged(nameof(this.ShowMarkers));            }        }        /// <summary>        /// Gets or sets currently selected Deconvolution element        /// </summary>        private int currentDeconvolutionElement;        /// <summary>        /// Gets or sets currently selected Deconvolution element        /// </summary>        public int CurrentDeconvolutionElement        {            get            {                return this.currentDeconvolutionElement;            }            set            {                if (value == this.currentDeconvolutionElement)                {                    return;                }                this.currentDeconvolutionElement = value;                this.RaisePropertyChanged(nameof(this.CurrentDeconvolutionElement));            }        }        public IEnumerable<IEdDeviceStatus> DeviceStatus        {            get            {                return OIHelper.MonitoringController.EdStatus.Values;            }        }        /// <summary>        /// Handles the DispatcherUnhandledException.        /// Specifically, here it handles the LicenseCheckExceptions which are wrapped in the DispatcherUnhandledException.        /// Shutdowns application when a invalid or missing licence has been found in LicenseCheck.cs.        /// </summary>        /// <param name="obj">Object</param>        /// <param name="args">Event</param>        private void OnCurrentOnDispatcherUnhandledException(object obj, DispatcherUnhandledExceptionEventArgs args)        {            LicenseCheckException ex = args.Exception.InnerException as LicenseCheckException;            if (ex != null)            {                MessageBox.Show(@"This application will shutdown, as a valid license is not available." + Environment.NewLine + ex.Message, @"Error Message", MessageBoxButton.OK, MessageBoxImage.Error);                args.Handled = true;                Application.Current.Shutdown();            }        }        /// <summary>        /// Gets the random color.        /// </summary>        /// <returns>A random color</returns>        private static WindowsMedia.Color GetRandomColor()        {            Random randomGen = new Random();            byte[] values = new byte[3];            randomGen.NextBytes(values);            PropertyInfo[] info = typeof(WindowsMedia.Colors).GetProperties();            PropertyInfo colorInfo = info[randomGen.Next(info.Length)];            WindowsMedia.Color randomColor = (WindowsMedia.Color)WindowsMedia.ColorConverter.ConvertFromString(colorInfo.Name);            return randomColor;        }        #region Event handlers        /// <summary>        /// Called when [clear compare spectra menu item is selected].        /// </summary>        /// <param name="sender">The sender.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void OnClearCompareSpectra(object sender, RoutedEventArgs e)        {            this.spectrumViewer.ClearComparisonSpectra();            this.compareSpectra.Clear();        }        /// <summary>        /// Acquisition status timer event handler        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void OnAcquisitionStatusTimer_Tick(object sender, EventArgs e)        {            if (OIHelper.EdSpectrumAcquisitionController.IsAcquiring || OIHelper.ImageAcquisitionController.IsAcquiring)            {                this.lbAcquisitionStatus.Visibility = System.Windows.Visibility.Visible;            }            else            {                this.lbAcquisitionStatus.Visibility = System.Windows.Visibility.Hidden;            }        }        /// <summary>        /// Indentify elements timer event handler        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void OnIdentifyElementsTimer_Tick(object sender, EventArgs e)        {            this.DoAutoID();        }        /// <summary>        /// Called when [ed spectrum experiment started].        /// </summary>        /// <param name="sender">The sender.</param>        /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>        private void OnEdSpectrumExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdSpectrum> e)        {            var edSpectrum = e.Value;            // execute on ui thread            this.Dispatcher.BeginInvoke(() =>            {                this.spectrumViewer.Spectrum = edSpectrum;                // Clear periodic table elements                this.PeriodicTable.ClearAll();                // Update Known Elements                this.PeriodicTable.IncludeElements(OIHelper.AutoIdSettings.KnownElements.ToArray());            });            this.identifyElementsTimer.Tick += this.OnIdentifyElementsTimer_Tick;            this.identifyElementsTimer.Start();            edSpectrum.PropertyChanged += this.OnPropertyChanged;            this.dataChangedCount = 0;            edSpectrum.DataChanged += this.OnEdSpectrumDataChanged;        }        /// <summary>        /// Called when [image experiment started].        /// </summary>        /// <param name="sender">The sender.</param>        /// <param name="e">The instance containing the event data.</param>        private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)        {            // execute on UI thread            this.Dispatcher.BeginInvoke(() =>            {                this.imageViewer.DisplayImage = e.Value[0];            });        }        /// <summary>        /// OnExperimentFinished        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>        private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)        {            e.Value.PropertyChanged -= this.OnPropertyChanged;            e.Value.DataChanged -= this.OnEdSpectrumDataChanged;            this.identifyElementsTimer.Tick -= this.OnIdentifyElementsTimer_Tick;            this.identifyElementsTimer.Stop();            // execute on UI thread            this.Dispatcher.BeginInvoke(() =>            {                this.DoAutoID();                this.UpdatePeriodicTable();                this.DoQuant();                if (this.currentSpectrumAcquisitions.ContainsKey(e.Value))                {                    this.AcquisitionQueueListBox.Items.Remove(this.currentSpectrumAcquisitions[e.Value]);                    this.currentSpectrumAcquisitions.Remove(e.Value);                }                // *** If this is the last experiment, call EndMultipleAcquisition on the Acquisition                // *** controller, to re-enable external scan switching                if (!this.currentSpectrumAcquisitions.Any())                {                    OIHelper.EdSpectrumAcquisitionController.EndMultipleAcquisition();                }            });        }        /// <summary>        /// Called when [image experiment finished].        /// </summary>        /// <param name="sender">The sender.</param>        /// <param name="e">The instance containing the event data.</param>        private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)        {            // execute on UI thread            this.Dispatcher.BeginInvoke(() =>            {                e.Value.ToList().ForEach(i =>                    {                        this.electronImageList.Remove(i);                        this.ImageAcquisitionQueueListBox.Items.Remove(i.Label);                    });            });        }        /// <summary>        /// Show or Hide Markers base on Selected Element in Periodic Table        /// </summary>        public void ShowHideMarker()        {            if (this.ShowMarkers)            {                this.spectrumViewer.MarkersElement = this.PeriodicTable.SelectedElement;            }            else            {                this.spectrumViewer.MarkersElement = 0;            }        }        /// <summary>        /// OnPropertyChanged        /// </summary>        /// <param name="sender">Event source</param>        /// <param name="e">Event arguments</param>        private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)        {            var edSpectrum = sender as IEdSpectrum;            if (e.PropertyName == nameof(edSpectrum.RealTimeSeconds))            {                this.Dispatcher.Invoke(() =>                {                    this.textRealTimeSeconds.Text = Math.Round(edSpectrum.RealTimeSeconds, 3).ToString(CultureInfo.InvariantCulture);                });            }            else if (e.PropertyName == nameof(edSpectrum.LiveTimeSeconds))            {                this.Dispatcher.Invoke(() =>                {                    this.textLiveTimeSeconds.Text = Math.Round(edSpectrum.LiveTimeSeconds, 3).ToString(CultureInfo.InvariantCulture);                });            }            else if (e.PropertyName == nameof(this.PeriodicTable.SelectedElement))            {                this.ShowHideMarker();            }        }        /// <summary>        /// Handles the CollectionChanged event of the IncludedElements.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>        private void IncludedElements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)        {            var spectrum = this.spectrumViewer.Spectrum;            // Update peak labels for included elements            if (spectrum != null)            {                switch (e.Action)                {                    case NotifyCollectionChangedAction.Add:                        foreach (int i in e.NewItems.Cast<int>())                        {                            spectrum.SetIdentifiedElement(i, true);                        }                        break;                    case NotifyCollectionChangedAction.Remove:                        foreach (int i in e.OldItems.Cast<int>())                        {                            spectrum.SetIdentifiedElement(i, false);                        }                        break;                    default:                        break;                }                this.UpdateSpectrumPeakLabels();            }        }        /// <summary>        /// Update current spectrum peak labels        /// </summary>        private void UpdateSpectrumPeakLabels()        {            try            {                OIHelper.EdSpectrumProcessing.UpdatePeakLabels(this.spectrumViewer.Spectrum, null, Processing.Ed.PeakLabelFormat.Symbol);            }            catch (ArgumentNullException)            {            }            catch (InvalidOperationException)            {            }        }        /// <summary>        /// Sort Listbox        /// </summary>        private void SortListBox()        {            this.IncludedListBox.Items.SortDescriptions.Add(new SortDescription(string.Empty, ListSortDirection.Ascending));            this.ExcludedListBox.Items.SortDescriptions.Add(new SortDescription(string.Empty, ListSortDirection.Ascending));        }        #endregion        #region Update UI Status        /// <summary>        /// Initialise spectrum acquisition settings        /// </summary>        private void InitialSpectrumSettingsContent()        {            if (OIHelper.EdSpectrumSettings.EdCapabilities.HasHardwareConnection)            {                this.tbSpectrumLabel.Text = @"Spectrum 1";                this.cbAcquisitionMode.Items.Add(EdAcquireMode.LiveTime);                this.cbAcquisitionMode.Items.Add(EdAcquireMode.RealTime);                this.cbAcquisitionMode.SelectedIndex = 0;                this.tbAcquisitionTime.Text = @"10000";                this.cbProcessTime.ItemsSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedProcessTimes;                this.cbProcessTime.SelectedIndex = 3;                this.cbEnergyRange.ItemsSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedEnergyRanges;                this.cbEnergyRange.SelectedIndex = 1;                this.cbNumOfChannels.ItemsSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedNumberOfChannels;                this.cbNumOfChannels.SelectedIndex = 1;                this.enabledDetectors = new List<DetectorEnableModel>();                foreach (var kvp in OIHelper.EdSpectrumSettings.EdSettings.IsHardwareEnabled)                {                    this.enabledDetectors.Add(new DetectorEnableModel(kvp.Key, kvp.Value));                }                this.enabledDetectors = this.enabledDetectors.OrderBy(x => x.DisplayName).ToList();                this.DetectorListBox.DataContext = this;                this.cbPrimaryDetector.ItemsSource = this.enabledDetectors.Select(x => x.DisplayName).ToList();                this.cbPrimaryDetector.SelectedIndex = 0;            }        }        public IReadOnlyList<DetectorEnableModel> EnabledDetectors        {            get            {                return this.enabledDetectors;            }        }        /// <summary>        /// Initialise SpectrumViewer        /// </summary>        private void InitialSpectrumViewer()        {            this.spectrumViewer.IsXAxisAutoScaleEnabled = false;            this.spectrumViewer.IsYAxisAutoScaleEnabled = true;        }        /// <summary>        /// InitialImageSettingsContent        /// </summary>        private void InitialImageSettingsContent()        {            var settings = OIHelper.ImageSettings;            if (settings.ImageCapabilities.HasHardwareConnection)            {                this.cbImageScanSize.ItemsSource = OIHelper.GetScanSizes(                    settings.ScanCapabilities.MinimumPixelSize,                    settings.ScanCapabilities.MaximumPixelSize);                this.cbImageScanSize.SelectedIndex = this.cbImageScanSize.Items.Count > 4 ? 4 : this.cbImageScanSize.Items.Count - 1;                this.cbInputSignal.ItemsSource = settings.ImageCapabilities.AllowedInputSources;                this.cbInputSignal.SelectedIndex = 0;                this.tbNumberOfFrames.Text = @"1";                this.tbDwellTime.Text = @"40";            }        }        /// <summary>        /// Initialize Processing settings        /// </summary>        private void InitialProcessingSettingsContent()        {            this.ExcludedListBox.DataContext = this;            ((INotifyCollectionChanged)this.PeriodicTable.IncludedElements).CollectionChanged += this.IncludedElements_CollectionChanged;            this.AllElementsRadioButton.IsChecked = true;            this.CombinedElementTextBox.IsEnabled = false;            this.CombinedElementTextBox.Text = @"8";            this.NumberOfIonsTextBox.IsEnabled = false;            this.NumberOfIonsTextBox.Text = @"3.0";            this.IsCoatedCheckBox.IsChecked = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;            this.CoatingElementTextBox.IsEnabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;            this.CoatingElementTextBox.Text = OIHelper.SEMQuantSettings.SampleCoating.CoatingElement.ToString(CultureInfo.InvariantCulture);            this.DensityTextBox.IsEnabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;            this.DensityTextBox.Text = OIHelper.SEMQuantSettings.SampleCoating.Density.ToString(CultureInfo.InvariantCulture);            this.ThicknessTextBox.IsEnabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;            this.ThicknessTextBox.Text = OIHelper.SEMQuantSettings.SampleCoating.Thickness.ToString(CultureInfo.InvariantCulture);            foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)            {                this.DeconvolutionElementListTextBox.Text += i.ToString(CultureInfo.InvariantCulture) + System.Environment.NewLine;            }            this.ThresholdingCheckBox.IsChecked = false;            this.SigmaLevelTextBox.IsEnabled = false;            this.SigmaLevelTextBox.Text = @"3.0";        }        /// <summary>        /// Initialises the element dictionary.        /// </summary>        private void InitialiseElementDictionary()        {            this.elementDictionary = new Dictionary<int, string>();            for (int elementNo = 1; elementNo <= 103; elementNo++)            {                this.elementDictionary.Add(elementNo, this.PeriodicTable.LookupElementInfo(elementNo).ElementName);            }            this.EnableElementsList.DataContext = this;        }        /// <summary>        /// Initialise acquisition timer        /// </summary>        private void InitialTimers()        {            this.acquisitionStatusTimer = new DispatcherTimer();            this.acquisitionStatusTimer.Tick += new EventHandler(this.OnAcquisitionStatusTimer_Tick);            this.acquisitionStatusTimer.Interval = TimeSpan.FromMilliseconds(100);            this.acquisitionStatusTimer.Start();            this.identifyElementsTimer = new DispatcherTimer();            this.identifyElementsTimer.Interval = TimeSpan.FromMilliseconds(1000);        }        /// <summary>        /// Update the Periodic Table content after acquisition finished        /// </summary>        private void UpdatePeriodicTable()        {            this.PeriodicTable.IncludeElements(this.spectrumViewer.Spectrum.IdentifiedElements.ToArray());        }        #endregion        #region Menu items actions        /// <summary>        /// Exit application        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void AppExit(object sender, RoutedEventArgs e)        {            Application.Current.Shutdown();        }        /// <summary>        /// Show detector control panel        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void DetectorControl_Click(object sender, RoutedEventArgs e)        {            new DetectorControlPanel(OIHelper.EDDetectorControl) { Owner = this }.Show();        }        /// <summary>        /// Show microscope control panel        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void MicroscopeControl_Click(object sender, RoutedEventArgs e)        {            new MicroscopeControlPanel { Owner = this }.Show();        }        /// <summary>        /// Calibrate Click event handler.        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void Calibrate_Click(object sender, RoutedEventArgs e)        {            new Calibrate { Owner = this }.Show();        }        /// <summary>        /// Auto ID settings Window        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void AutoIdSetting_Click(object sender, RoutedEventArgs e)        {            new AutoIdSettings { Owner = this }.Show();        }        /// <summary>        /// Handles the Click event of the PeriodicTableSettings menu item.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void PeriodicTableSettings_Click(object sender, RoutedEventArgs e)        {            new PeriodicTableVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the SpectrumViewerSettings menu item.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void SpectrumViewerSettings_Click(object sender, RoutedEventArgs e)        {            new SpectrumViewerVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the ImageViewerSettings menu item.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void ImageViewerSettings_Click(object sender, RoutedEventArgs e)        {            new ImageViewerVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the ElementComboSettings menu item.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void ElementComboSettings_Click(object sender, RoutedEventArgs e)        {            new ElementComboVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the BC Dialog settings menu item.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void BCDialogSettings_Click(object sender, RoutedEventArgs e)        {            new BCDialogVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the DetailDialogsVisualStyles control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void DetailsDialogVisualStyles_Click(object sender, RoutedEventArgs e)        {            new DetailDialogVisualSettings { Owner = this, ShowInTaskbar = false }.Show();        }        /// <summary>        /// Handles the Click event of the elementComboVisualSettingsToolStripMenuItem control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">instance containing the event data.</param>        private void ExportSpectrumAs_Click(object sender, RoutedEventArgs e)        {            try            {                short width = (short)this.spectrumViewer.ActualWidth;                short height = (short)this.spectrumViewer.ActualHeight;                Bitmap bitmap = OIHelper.EdSpectrumProcessing.CreateBitmap(this.spectrumViewer.Spectrum, width, height);                SaveFileDialog exportSpectrumDialog = new SaveFileDialog();                exportSpectrumDialog.Filter = @"Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|JPEG (*.jpg)|*.jpg";                exportSpectrumDialog.Title = @"Export Spectrum As...";                if (exportSpectrumDialog.ShowDialog() == true)                {                    switch (exportSpectrumDialog.FilterIndex)                    {                        case 1:                            bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Bmp);                            break;                        case 2:                            bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Png);                            break;                        case 3:                            bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Tiff);                            break;                        case 4:                            bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Jpeg);                            break;                        default:                            bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Bmp);                            break;                    }                }            }            catch (ArgumentException)            {            }        }        /// <summary>        /// LoadSpectrumData menuitem click event        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void LoadSpectrumData_Click(object sender, RoutedEventArgs e)        {            var of_dlg = new OpenFileDialog();            of_dlg.Title = @"Select a file";            of_dlg.Filter = @"Esd(*.esd)|*.esd|Iex(*.iex)|*.iex";            of_dlg.FilterIndex = 1;            if (of_dlg.ShowDialog() == true)            {                if (!File.Exists(of_dlg.FileName))                {                    throw new InvalidOperationException("File does not exists!");                }                this.spectrumViewer.ClearComparisonSpectra();                switch (of_dlg.FilterIndex)                {                    case 1:                        this.spectrumViewer.Spectrum = DataFactory.LoadEdSpectrum(OIHelper.FileToByteArray(of_dlg.FileName));                        break;                    case 2:                        this.spectrumViewer.Spectrum = DataFactory.LoadEdSpectrumFromIex(of_dlg.FileName);                        break;                }                // Initial Periodic table to show the element state                this.PeriodicTable.ClearAll();                this.PeriodicTable.IncludeElements(this.spectrumViewer.Spectrum.IdentifiedElements.ToArray());            }        }        /// <summary>        /// SaveSpectrumData menuitem click event        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void SaveSpectrumData_Click(object sender, RoutedEventArgs e)        {            var spectrum = this.spectrumViewer.Spectrum;            if (spectrum != null)            {                try                {                    var sf_dlg = new SaveFileDialog();                    sf_dlg.Title = @"Save spectrum as";                    sf_dlg.Filter = @"Esd(*.esd)|*.esd|Iex(*.iex)|*.iex";                    sf_dlg.FilterIndex = 1;                    if (sf_dlg.ShowDialog() == true)                    {                        switch (sf_dlg.FilterIndex)                        {                            case 1:                                byte[] spectrumData = spectrum.Save();                                OIHelper.ByteArrayToFile(sf_dlg.FileName, spectrumData);                                break;                            case 2:                                OIHelper.EdSpectrumProcessing.CreateIexSpectrum(sf_dlg.FileName, spectrum);                                break;                        }                    }                }                catch (ArgumentException)                {                }            }        }        /// <summary>        /// LoadImageData_Click        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void LoadImageData_Click(object sender, RoutedEventArgs e)        {            try            {                OpenFileDialog of_dlg = new OpenFileDialog();                of_dlg.Title = @"Select a file";                of_dlg.Filter = @"Img(*.img)|*.img";                of_dlg.FilterIndex = 1;                if (of_dlg.ShowDialog() == true)                {                    if (!File.Exists(of_dlg.FileName))                    {                        throw new System.InvalidOperationException("File does not exists!");                    }                    this.imageViewer.DisplayImage = DataFactory.LoadElectronImage(OIHelper.FileToByteArray(of_dlg.FileName));                }            }            catch (SerializationException)            {            }        }        /// <summary>        /// SaveImageData_Click        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void SaveImageData_Click(object sender, RoutedEventArgs e)        {            string filter = @"Img(*.img)|*.img";            string title = @"Save image as";            if (this.imageViewer.DisplayImage != null)            {                try                {                    var electronImage = this.imageViewer.DisplayImage;                    SaveFileDialog sf_dlg = new SaveFileDialog();                    sf_dlg.Filter = filter;                    sf_dlg.Title = title;                    sf_dlg.FilterIndex = 1;                    if (sf_dlg.ShowDialog() == true)                    {                        byte[] imageData = electronImage.Save();                        OIHelper.ByteArrayToFile(sf_dlg.FileName, imageData);                    }                }                catch (ArgumentException)                {                }            }        }        /// <summary>        /// ChordListAcquire_Click        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void ChordListAcquire_Click(object sender, RoutedEventArgs e)        {            try            {                Window edChordListAcquireWindow = new EdChordListAcquisition()                {                    Owner = this,                    WindowStartupLocation = WindowStartupLocation.CenterOwner,                };                edChordListAcquireWindow.ShowDialog();            }            catch (LicenseCheckException)            {                MessageBox.Show(@"Not supported by the license");            }        }        #endregion        #region Control from UI        /// <summary>        /// Start spectrum acquisition        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void StartAcquisition(object sender, RoutedEventArgs e)        {            this.spectrumViewer.ClearComparisonSpectra();            this.AcquireSpectrum(false);        }        /// <summary>        /// Resume spectrum acquisition        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void ResumeAcquisitionButton_Click(object sender, RoutedEventArgs e)        {            if (this.spectrumViewer.Spectrum != null && !this.IsAcquiringSpectrum)            {                this.AcquireSpectrum(true);            }        }        /// <summary>        /// Adds a spectrum for comparison        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void OnAddCompareSpectraButtonClick(object sender, RoutedEventArgs e)        {            var spectrum = this.cbCompareSpectra.SelectedItem as IEdSpectrum;            if (spectrum != null)            {                ////add current spectrum to compare spectrum                this.spectrumViewer.AddComparisonSpectrum(spectrum, this.compareSpectrumColor.SelectedColor);                this.compareSpectra.Add(spectrum);                this.compareSpectrumColor.SelectedColor = GetRandomColor();            }        }        /// <summary>        /// Acquires the spectrum.        /// </summary>        /// <param name="isResume">if set to <c>true</c> [is resume].</param>        private void AcquireSpectrum(bool isResume)        {            IEdSpectrum edSpectrum = null;            // Spectrum acquisition settings confirm            var edSpectrumSettings = OIHelper.EdSpectrumSettings;            var edSpectrumAcquisitionController = OIHelper.EdSpectrumAcquisitionController;            edSpectrumSettings.EdSettings.AcquisitionMode = (EdAcquireMode)this.cbAcquisitionMode.SelectedItem;            edSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(int.Parse(this.tbAcquisitionTime.Text, CultureInfo.CurrentCulture));            if (edSpectrumSettings.EdSettings.AcquisitionTime > edSpectrumSettings.EdCapabilities.MaximumAcquisitionTime)            {                edSpectrumSettings.EdSettings.AcquisitionTime = edSpectrumSettings.EdCapabilities.MaximumAcquisitionTime;                this.tbAcquisitionTime.Text = edSpectrumSettings.EdSettings.AcquisitionTime.TotalMilliseconds.ToString(CultureInfo.CurrentCulture);            }            if (edSpectrumSettings.EdSettings.AcquisitionTime < edSpectrumSettings.EdCapabilities.MinimumAcquisitionTime)            {                edSpectrumSettings.EdSettings.AcquisitionTime = edSpectrumSettings.EdCapabilities.MinimumAcquisitionTime;                this.tbAcquisitionTime.Text = edSpectrumSettings.EdSettings.AcquisitionTime.TotalSeconds.ToString(CultureInfo.CurrentCulture);            }            edSpectrumSettings.EdSettings.ProcessTime = (int)this.cbProcessTime.SelectedValue;            edSpectrumSettings.EdSettings.EnergyRange = (int)this.cbEnergyRange.SelectedValue;            edSpectrumSettings.EdSettings.NumberOfChannels = (int)this.cbNumOfChannels.SelectedValue;            // Enable the appropriate detectors            foreach (var item in this.EnabledDetectors)            {                edSpectrumSettings.EdSettings.EnableDevice(item.EdHardwareId, item.IsDetectorEnabled);            }            int selectedIndex = this.cbPrimaryDetector.SelectedIndex;            if (selectedIndex >= 0 && selectedIndex < this.EnabledDetectors.Count)            {                DetectorEnableModel primaryDetectorModel = this.EnabledDetectors[selectedIndex];                edSpectrumSettings.EdSettings.PrimaryHardwareId = primaryDetectorModel.EdHardwareId;                edSpectrumSettings.EdSettings.EnableDevice(primaryDetectorModel.EdHardwareId, true);                primaryDetectorModel.IsDetectorEnabled = true; // ensure the ui reflects settings            }            edSpectrumSettings.ScanSettings.AcquisitionRegion.CreateMicroscopeRegion();            this.spectrumViewer.MaximumEnergy = edSpectrumSettings.EdSettings.EnergyRange;            // If Ed hardware is not ready, the software will crash.            if (edSpectrumAcquisitionController.IsEdHardwareReady(OIHelper.EdSpectrumSettings))            {                // *** If this is the first acquisition, call BeginMultipleAcquisition                // *** on the AcquisitionController to suppress external scan switching                if (!this.currentSpectrumAcquisitions.Any())                {                    edSpectrumAcquisitionController.BeginMultipleAcquisition();                }                if (!isResume)                {                    try                    {                        edSpectrum = edSpectrumAcquisitionController.StartAcquisition(edSpectrumSettings);                        edSpectrum.Label = string.Format(CultureInfo.CurrentCulture, @"{0} ({1:HH:mm:ss})", this.tbSpectrumLabel.Text, DateTime.Now);                    }                    catch (InvalidSettingsException invalidSettingsException)                    {                        MessageBox.Show(invalidSettingsException.Message);                    }                    catch (AcquisitionStartException acquisitionStartException)                    {                        MessageBox.Show(acquisitionStartException.Message);                    }                }                else                {                    edSpectrumAcquisitionController.ResumeAcquisition(edSpectrumSettings, this.spectrumViewer.Spectrum);                    edSpectrum = this.spectrumViewer.Spectrum;                }                if (edSpectrum != null)                {                    this.currentSpectrumAcquisitions.Add(edSpectrum, new AcquisitionItem(edSpectrum, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionMode, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionTime.TotalMilliseconds));                    this.AcquisitionQueueListBox.Items.Add(this.currentSpectrumAcquisitions[edSpectrum]);                }            }        }        private void OnEdSpectrumDataChanged(object sender, EventArgs e)        {            // execute on ui thread            this.Dispatcher.BeginInvoke(() =>            {                this.dataChangedCount++;                this.lbDataChanged.Content = string.Format(CultureInfo.InvariantCulture, @"Data Changed: ({0})", this.dataChangedCount);            });        }        /// <summary>        /// StopAcquisition        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void StopAcquisition(object sender, RoutedEventArgs e)        {            if (OIHelper.EdSpectrumAcquisitionController.IsAcquiring == true)            {                OIHelper.EdSpectrumAcquisitionController.StopAcquisition();            }        }        /// <summary>        /// Handles the Click event of btStartImageScan button.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>        private void StartImageScan_Click(object sender, EventArgs e)        {            // Image acquisition settings comfirm            var imageSettings = OIHelper.ImageSettings.ImageSettings;            var imageCapabilities = OIHelper.ImageSettings.ImageCapabilities;            var scanSettings = OIHelper.ImageSettings.ScanSettings;            var imageAcquisitionController = OIHelper.ImageAcquisitionController;            this.tbImageData.Text = string.Empty;            int value = int.Parse(this.tbDwellTime.Text, CultureInfo.InvariantCulture);            if (value > imageCapabilities.MaximumImageDwellMicroseconds)            {                imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;            }            if (value < imageCapabilities.MinimumImageDwellMicroseconds)            {                imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;            }            else            {                imageSettings.DwellTimeMicroSeconds = value;            }            // *** Disable all input sources before enabling the one that the user has chosen            imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));            imageSettings.EnableInputSource((ImageInputSources)this.cbInputSignal.SelectedItem, true);            scanSettings.FrameCount = int.Parse(this.tbNumberOfFrames.Text, CultureInfo.InvariantCulture);            var pixelSize = 1d / double.Parse(this.cbImageScanSize.Text, CultureInfo.InvariantCulture);            scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);            var images = imageAcquisitionController.StartAcquisition(OIHelper.ImageSettings).ToList();            images.ForEach(i =>                {                    i.Label = string.Format(CultureInfo.CurrentCulture, @"{0} ({1:HH:mm:ss})", @"ElectronImage", DateTime.Now);                    this.electronImageList.Add(i);                    this.ImageAcquisitionQueueListBox.Items.Add(i.Label);                });        }        /// <summary>        /// Handles the Click event of btStopImageScan button.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>        private void StopImageScan_Click(object sender, EventArgs e)        {            if (OIHelper.ImageAcquisitionController.IsAcquiring)            {                OIHelper.ImageAcquisitionController.StopAcquisition();            }        }        /// <summary>        /// AutoID elements.        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">RoutedEventArgs</param>        private void AutoIdButton_Click(object sender, RoutedEventArgs e)        {            this.DoAutoID();        }        /// <summary>        /// Do AutoID        /// </summary>        private void DoAutoID()        {            var elements = new List<string>();            var spectrum = this.spectrumViewer.Spectrum;            if (spectrum != null)            {                elements = OIHelper.EdSpectrumProcessing.IdentifyElements(spectrum, OIHelper.AutoIdSettings)                    .Select(i => ElementProperties.GetElementSymbol(i))                    .ToList();                // Update spectrum peak label                this.UpdateSpectrumPeakLabels();            }            this.IdentifiedElements = elements;            this.SelectedIdentifiedElement = elements.FirstOrDefault();        }        public IReadOnlyList<string> IdentifiedElements        {            get            {                return this.identifiedElements;            }            set            {                this.identifiedElements = value.ToList();                this.RaisePropertyChanged(nameof(this.IdentifiedElements));            }        }        public string SelectedIdentifiedElement        {            get            {                return this.selectedIdentifiedElement;            }            set            {                this.selectedIdentifiedElement = value;                this.RaisePropertyChanged(nameof(this.SelectedIdentifiedElement));            }        }        /// <summary>        /// Confirm excluded element        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void AddElementButton_Click(object sender, RoutedEventArgs e)        {            try            {                this.PeriodicTable.ExcludeElements(this.CurrentExclusionElement);            }            catch (FormatException fe)            {                MessageBox.Show(@"Please enter valid element!" + fe.Message);            }        }        /// <summary>        /// Detete Excluded Elements        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void RemoveElementButton_Click(object sender, RoutedEventArgs e)        {            try            {                this.PeriodicTable.Clear(this.CurrentExclusionElement);            }            catch (FormatException fe)            {                MessageBox.Show(@"Please enter valid element!" + fe.Message);            }        }        /// <summary>        /// Clear Excluded element list        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void ClearButton_Click(object sender, RoutedEventArgs e)        {            this.PeriodicTable.ClearAll();        }        /// <summary>        /// Do quant button click handler.        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void QuantButton_Click(object sender, RoutedEventArgs e)        {            this.DoQuant();        }        /// <summary>        /// Do quantitative processing and display        /// </summary>        private void DoQuant()        {            switch (OIHelper.SEMQuantSettings.ProcessingOption)            {                case ProcessingOption.ElementByDifference:                    OIHelper.SEMQuantSettings.CombinedElement = int.Parse(this.CombinedElementTextBox.Text, CultureInfo.InvariantCulture);                    break;                case ProcessingOption.ElementByStoichiometry:                    // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.                    OIHelper.SEMQuantSettings.CombinedElement = 8;                    OIHelper.SEMQuantSettings.TypeOfIon = IonType.Anion;                    OIHelper.SEMQuantSettings.NumberOfIons = double.Parse(this.NumberOfIonsTextBox.Text, CultureInfo.InvariantCulture);                    break;                case ProcessingOption.AllElements:                default:                    break;            }            if (this.IsCoatedCheckBox.IsChecked == true)            {                OIHelper.SEMQuantSettings.SampleCoating.IsCoated = true;                OIHelper.SEMQuantSettings.SampleCoating.Thickness = double.Parse(this.ThicknessTextBox.Text, CultureInfo.InvariantCulture);                OIHelper.SEMQuantSettings.SampleCoating.Density = double.Parse(this.DensityTextBox.Text, CultureInfo.InvariantCulture);                OIHelper.SEMQuantSettings.SampleCoating.CoatingElement = int.Parse(this.CoatingElementTextBox.Text, CultureInfo.InvariantCulture);            }            else            {                OIHelper.SEMQuantSettings.SampleCoating.IsCoated = false;                this.ThicknessTextBox.IsEnabled = false;                this.DensityTextBox.IsEnabled = false;            }            if (this.ThresholdingCheckBox.IsChecked == true)            {                OIHelper.SEMQuantSettings.Thresholding = true;                OIHelper.SEMQuantSettings.SigmaLevel = double.Parse(this.SigmaLevelTextBox.Text, CultureInfo.InvariantCulture);            }            else            {                OIHelper.SEMQuantSettings.Thresholding = false;                this.SigmaLevelTextBox.IsEnabled = false;            }            OIHelper.SEMQuantSettings.Normalised = this.NormalisedCheckBox.IsChecked.Value;            this.QuantResultDataGrid.Items.Clear();            try            {                ISEMQuantStatus status = OIHelper.EdSpectrumProcessing.SEMQuantifySpectrum(this.spectrumViewer.Spectrum, OIHelper.SEMQuantSettings);                this.Status = status.Status;                this.StatusMessage = status.StatusMessage;                double totalWeightPercent = 0;                double totalAtomicPercent = 0;                foreach (ISEMQuantResult result in status.Results)                {                    if (result.IsQuantLine)                    {                        var elementRow = new DataGridRow();                        elementRow.Item = new                        {                            Element = ElementProperties.GetElementSymbol(result.AtomicNumber),                            Line = result.LineType,                            WtPcent = Math.Round(result.WeightPercent, 2),                            WtSigma = Math.Round(result.WeightPercentSigma, 6),                            AtPcent = Math.Round(result.AtomicPercent, 2),                        };                        this.QuantResultDataGrid.Items.Add(elementRow);                    }                    totalWeightPercent += result.WeightPercent;                    totalAtomicPercent += result.AtomicPercent;                }                var totalRow = new DataGridRow();                totalRow.Item = new                {                    Element = @"Total",                    WtPcent = Math.Round(totalWeightPercent, 2),                    AtPcent = Math.Round(totalAtomicPercent, 2),                };                this.QuantResultDataGrid.Items.Add(totalRow);                this.spectrumViewer.SEMQuantify(OIHelper.SEMQuantSettings);            }            catch (QuantificationException ex)            {                MessageBox.Show(ex.Message, @"Quantification Error", MessageBoxButton.OK, MessageBoxImage.Warning);            }            catch (ArgumentNullException)            {            }        }        public QuantAnalysisStatus Status        {            get            {                return this.status;            }            set            {                this.status = value;                this.RaisePropertyChanged(nameof(this.Status));            }        }        public string StatusMessage        {            get            {                return this.statusMessage;            }            set            {                this.statusMessage = value;                this.RaisePropertyChanged(nameof(this.StatusMessage));            }        }        private void CurrentSpectrumRadioButton_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.TypeOfElementList = ElementListType.CurrentSpectrum;            this.FixedListRadioButton.IsChecked = false;            this.CurrentAndFixed.IsChecked = false;        }        private void FixedList_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.TypeOfElementList = ElementListType.Fixed;            // Set the element as an example of fixed list            OIHelper.SEMQuantSettings.SetElement(72, true);            this.CurrentAndFixed.IsChecked = false;            this.CurrentSpectrumRadioButton.IsChecked = false;        }        private void CurrentAndFixed_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.TypeOfElementList = ElementListType.FixedAndCurrentSpectrum;            this.FixedListRadioButton.IsChecked = false;            this.CurrentSpectrumRadioButton.IsChecked = false;        }        /// <summary>        /// Used when processing spectra from specimens in which all elements yield X-rays which can be readily detected.        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void AllElementsRadioButton_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.AllElements;            this.NormalisedCheckBox.IsEnabled = true;            this.CombinedElementTextBox.IsEnabled = false;            this.NumberOfIonsTextBox.IsEnabled = false;        }        /// <summary>        /// Calculated assuming that the difference between the analyzed total and 100%        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void ElementByDifferenceRadioButton_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.ElementByDifference;            this.NormalisedCheckBox.IsChecked = false;            this.NormalisedCheckBox.IsEnabled = false;            this.CombinedElementTextBox.IsEnabled = true;            this.NumberOfIonsTextBox.IsEnabled = false;        }        /// <summary>        /// Concentration of oxygen to be calculated.        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void ElementByStoichiometryRadioButton_Click(object sender, RoutedEventArgs e)        {            OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.ElementByStoichiometry;            this.NormalisedCheckBox.IsEnabled = true;            this.CombinedElementTextBox.IsEnabled = false;            this.NumberOfIonsTextBox.IsEnabled = true;        }        /// <summary>        /// Add deconvolution element to list        /// </summary>        /// <param name="sender">sender object</param>        /// <param name="e">EventArgs</param>        private void AddDeconvolutionButton_Click(object sender, RoutedEventArgs e)        {            this.DeconvolutionElementListTextBox.Clear();            try            {                OIHelper.SEMQuantSettings.SetDeconvolutionElement(this.CurrentDeconvolutionElement, true);                foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)                {                    this.DeconvolutionElementListTextBox.Text += i.ToString(CultureInfo.InvariantCulture) + Environment.NewLine;                }            }            catch (FormatException fe)            {                this.DeconvolutionElementListTextBox.Text = fe.Message;            }            catch (ArgumentOutOfRangeException ae)            {                this.DeconvolutionElementListTextBox.Text = ae.Message;            }        }        /// <summary>        /// Remove 1 deconvolution element from elment list.        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void DeconvolutionRemoveButton_Click(object sender, RoutedEventArgs e)        {            this.DeconvolutionElementListTextBox.Clear();            try            {                OIHelper.SEMQuantSettings.SetDeconvolutionElement(this.CurrentDeconvolutionElement, false);                foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)                {                    this.DeconvolutionElementListTextBox.Text += i.ToString(CultureInfo.InvariantCulture) + Environment.NewLine;                }            }            catch (FormatException fe)            {                this.DeconvolutionElementListTextBox.Text = fe.Message;            }        }        /// <summary>        /// Clear DeconvolutionElementList        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void DeconvolutionClearButton_Click(object sender, RoutedEventArgs e)        {            this.DeconvolutionElementListTextBox.Clear();            OIHelper.SEMQuantSettings.ClearDeconvolutionElements();            foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)            {                this.DeconvolutionElementListTextBox.Text += i.ToString(CultureInfo.InvariantCulture) + Environment.NewLine;            }        }        /// <summary>        /// ThicknessTextBox and DensityTextBox will be enabled when IsCoatedCheckBox_Checked        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void IsCoatedCheckBox_Click(object sender, RoutedEventArgs e)        {            this.CoatingElementTextBox.IsEnabled = (bool)this.IsCoatedCheckBox.IsChecked;            this.DensityTextBox.IsEnabled = (bool)this.IsCoatedCheckBox.IsChecked;            this.ThicknessTextBox.IsEnabled = (bool)this.IsCoatedCheckBox.IsChecked;        }        /// <summary>        /// SigmaLevelTextBox will be enabled when ThresholdingCheckBox_Checked        /// </summary>        /// <param name="sender">sender</param>        /// <param name="e">RoutedEventArgs</param>        private void ThresholdingCheckBox_Click(object sender, RoutedEventArgs e)        {            this.SigmaLevelTextBox.IsEnabled = (bool)this.ThresholdingCheckBox.IsChecked;        }        /// <summary>        /// Handles the CheckedChanged event of the MultiselectCheckBox control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>        private void MultiselectCheckBox_CheckedChanged(object sender, RoutedEventArgs e)        {            this.PeriodicTable.SelectionMode = this.MultiselectCheckBox.IsChecked.Value ? SelectionMode.Multiple : SelectionMode.Single;        }        /// <summary>        /// Handles the Checked event of the ElementEnable control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void ElementEnable_Checked(object sender, RoutedEventArgs e)        {            CheckBox check = sender as CheckBox;            this.PeriodicTable.EnableElements(true, (int)check.Tag);        }        /// <summary>        /// Handles the Unchecked event of the ElementEnable control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void ElementEnable_Unchecked(object sender, RoutedEventArgs e)        {            CheckBox check = sender as CheckBox;            this.PeriodicTable.EnableElements(false, (int)check.Tag);        }        /// <summary>        /// Handles the click Calculate button event.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void CalculatePeakParameterButton_Click(object sender, RoutedEventArgs e)        {            try            {                int atomicNum = ElementProperties.GetAtomicNumberByElementSymbol(this.SelectedIdentifiedElement);                var peakParameters = OIHelper.EdSpectrumProcessing.CalculatePeakParameters(this.spectrumViewer.Spectrum, atomicNum);                this.tbEnergy.Text = string.Format(CultureInfo.InvariantCulture, @"{0:F4} keV", peakParameters.Centre);                this.tbFWHM.Text = string.Format(CultureInfo.InvariantCulture, @"{0:F2} eV", peakParameters.FWHM);            }            catch (NullReferenceException)            {            }            catch (FormatException)            {            }            catch (InvalidOperationException)            {            }        }        /// <summary>        /// Handles the Click event of the GetPixelData button.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">instance containing the event data.</param>        private void GetPixelData_Click(object sender, RoutedEventArgs e)        {            if (OIHelper.ImageAcquisitionController.IsAcquiring != true && this.imageViewer.DisplayImage != null)            {                try                {                    var electronImage = this.imageViewer.DisplayImage as IElectronImage;                    int x = int.Parse(this.tbPointX.Text, CultureInfo.InvariantCulture);                    int y = int.Parse(this.tbPointY.Text, CultureInfo.InvariantCulture);                    this.sb.Append(string.Format(CultureInfo.CurrentCulture, @" Point({0},{1}), Value: {2}", x, y, electronImage.GetData(new System.Drawing.Point(x, y))));                    this.sb.Append(System.Environment.NewLine);                    this.tbImageData.Text = this.sb.ToString();                }                catch (ArgumentOutOfRangeException)                {                    //// stringBuilder - The length of the expanded string would exceed MaxCapacity.                }                catch (FormatException)                {                    //// int.Parse - The x or y coordinates are not in the correct format.                }            }        }        /// <summary>        /// The Window is closing. Cancel if acquiring a spectrum.        /// </summary>        /// <param name="sender">The sender.</param>        /// <param name="e">The <see cref="CancelEventArgs"/> instance containing the event data.</param>        private void WindowClosing(object sender, CancelEventArgs e)        {            e.Cancel = this.IsAcquiringSpectrum;        }        #endregion        /// <summary>        /// Loads the comparison spectra.        /// </summary>        private void LoadComparisonSpectra()        {            byte[] data;            Uri uri;            StreamResourceInfo info;            this.comparisonSpectra = new List<IEdSpectrum>();            uri = new Uri(@"/ComparisonSpectra/Nickel.spec", UriKind.Relative);            info = Application.GetResourceStream(uri);            data = new byte[info.Stream.Length];            info.Stream.Read(data, 0, (int)info.Stream.Length);            this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));            uri = new Uri(@"/ComparisonSpectra/Preview spectrum.spec", UriKind.Relative);            info = Application.GetResourceStream(uri);            data = new byte[info.Stream.Length];            info.Stream.Read(data, 0, (int)info.Stream.Length);            this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));            uri = new Uri(@"/ComparisonSpectra/Tungsten particle.spec", UriKind.Relative);            info = Application.GetResourceStream(uri);            data = new byte[info.Stream.Length];            info.Stream.Read(data, 0, (int)info.Stream.Length);            this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));            this.cbCompareSpectra.DataContext = this;            this.cbCompareSpectra.SelectedIndex = 0;        }        #region INotifyPropertyChanged Members        /// <summary>        /// PropertyChanged event        /// </summary>        public event PropertyChangedEventHandler PropertyChanged;        private void RaisePropertyChanged(string propertyName)        {            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));        }        #endregion        /// <summary>        /// The last element set        /// </summary>        private int lastElementSet = -1;        /// <summary>        /// Handles the PropertyChanged event of the ExcludeElementCombo control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>        private void ExcludeElementCombo_PropertyChanged(object sender, PropertyChangedEventArgs e)        {            if (e.PropertyName == nameof(this.ExcludeElementCombo.CurrentElement))            {                this.UpdateHighlightedElementInTable();            }        }        /// <summary>        /// Updates the highlighted element in table.        /// </summary>        private void UpdateHighlightedElementInTable()        {            if (this.lastElementSet != -1)            {                this.PeriodicTable.SetElementColor(this.lastElementSet, System.Windows.Media.Colors.Transparent);            }            System.Windows.Media.Color currentElementColor = System.Windows.Media.Color.FromArgb(255, 200, 255, 0);            this.PeriodicTable.SetElementColor(this.ExcludeElementCombo.CurrentElement, currentElementColor);            this.PeriodicTable.ShowElementColors = true;            this.lastElementSet = this.ExcludeElementCombo.CurrentElement;            this.PeriodicTable.SelectedElement = this.lastElementSet;        }        /// <summary>        /// Handles the Click event of the exportTiffButton control.        /// </summary>        /// <param name="sender">The source of the event.</param>        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>        private void ExportTiffButton_Click(object sender, RoutedEventArgs e)        {            // Export a tiff synchronously on the calling thread.            var syncTiff = this.spectrumViewer.CreateImage(ExportImageType.Tiff);            File.WriteAllBytes(Path.Combine(Path.GetTempPath(), @"zSpectrumSync.tiff"), syncTiff);            // Export a tiff asynchronously on a background thread.            // The thread must be created with an apartment state set to STA.            // Calls to CreateImage() must be made on the same thread that created the viewer.            var spectrum = this.spectrumViewer.Spectrum;            var thread = new System.Threading.Thread(() =>            {                // For optimum performance, create a single spectrum viewer and change the spectrum                // and viewer settings where required for all spectra that you need images for.                var backgroundViewer = new SpectrumViewer();                // A height and width must be set to provide the image dimensions in pixels.                backgroundViewer.Width = 500;                backgroundViewer.Height = 500;                backgroundViewer.IsYAxisAutoScaleEnabled = true;                backgroundViewer.YAxisType = ScaleType.Logarithmic;                backgroundViewer.MaximumEnergy = 8;                backgroundViewer.Spectrum = spectrum;                var asyncTiff = backgroundViewer.CreateImage(ExportImageType.Tiff);                File.WriteAllBytes(Path.Combine(Path.GetTempPath(), @"zSpectrumAsync.tiff"), asyncTiff);                backgroundViewer.Dispatcher.InvokeShutdown();            });            thread.IsBackground = true;            thread.SetApartmentState(ApartmentState.STA);            thread.Start();        }    }}
 |