12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072 |
-
- using OINA.Extender;
- using OINA.Extender.Acquisition;
- using OINA.Extender.Acquisition.Ed;
- using OINA.Extender.Acquisition.Image;
- using OINA.Extender.Data;
- using OINA.Extender.Data.Ed;
- using OINA.Extender.Data.Image;
- using OINA.Extender.MicroscopeControl;
- using OINA.Extender.Processing;
- using OINA.Extender.Processing.Ed;
- using OINA.Extender.Processing.Quant;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows;
- using System.Windows.Forms;
- using static OxfordExtenderWrapper.ExtenderIpcUI;
- namespace OxfordExtenderWrapper
- {
- enum OxfordControllerState
- {
- READY = 0,
- WORKING = 1,
- SUCCEEDED = 2,
- FAILED = 3,
- ABORT = 4
- };
- [Serializable]
- public class Segment
- {
- private int m_nX;
- private int m_nY;
- private int m_nLength;
- public int X
- {
- get { return m_nX; }
- set
- {
- if (value > 0)
- {
- m_nX = value;
- }
- }
- }
- public int Y
- {
- get { return m_nY; }
- set
- {
- if (value > 0)
- {
- m_nY = value;
- }
- }
- }
- public int Length
- {
- get { return m_nLength; }
- set
- {
- if (value > 0)
- {
- m_nLength = value;
- }
- }
- }
- };
- /// <summary>
- /// Enum EDSConst definition.
- /// </summary>
- enum EDSConst
- {
- MAX_XRAY_BATCH = 1024,
- XANA_CHANNELS = 2000,
- XANA_CHANNELS_MAX = 4192,
- MAX_AMPTIME_CONSTANTS = 10,
- MAX_EV_PER_CHANNELS = 5,
- MAX_ANALYZERS = 5,
- MAX_LEN_ANALYZER_NAME = 16
- };
- [Serializable]
- struct OxfordChord
- {
- public long m_nX;
- public long m_nY;
- public long m_nLength;
- };
- enum OxfordCommandType
- {
- GetMagnification = 0,
- SetMagnification = 1,
- GetWorkingDistance = 2,
- SetWorkingDistance = 3,
- GetBrightness = 4,
- SetBrightness = 5,
- GetContrast = 6,
- SetContrast = 7,
- GetSEMVoltage = 8,
- SetSEMVoltage = 9,
- //样品台
- GetStagePosition = 10,
- SetStagePosition = 11,
- GetStageAtX = 12,
- GetStageAtY = 13,
- GetStageAtZ = 14,
- GetStageAtT = 15,
- GetStageAtR = 16,
- SetStageGotoX = 17,
- SetStageGotoY = 18,
- SetStageGotoZ = 19,
- SetStageGotoT = 20,
- SetStageGotoR = 21,
- MoveStageXY = 22,
- //拍图
- ImageAquisition = 23,
- //采集参数设置
- SetImageAcquistionSetting = 24,
- //获取分辨率
- GetImageResolution = 25,
- //设置分辨率
- SetImageResolution = 26,
- //获取bitmap
- GetBitmap = 27,
- //X-ray
- //点采集
- XrayPointCollection = 29,
- COLLECT_XRAYPOINTS = 30,
- //面采集
- XrayAreaCollection = 31,
- COLLECT_XRAYFEATURES = 32,
- Exit = 100,
- }
- [Serializable]
- public class ImageAquistionParam
- {
- public enum ImageRegionType
- {
- FullField = 0,
- Rectangle = 1
- }
- public ImageAquistionParam()
- {
- this.ImageData = new byte[width * height];
- regionType = ImageRegionType.FullField;
- }
- //in
- public ImageRegionType regionType;
- public int width;
- public int height;
- public Rect ImgRectRegion;
- public double DwellTime;
- public ImageInputSourceType sourceType;
- //out
- public byte[] ImageData;
- }
- [Serializable]
- public class PointXrayParam
- {
- public PointXrayParam()
- {
- this.XrayData = new uint[2000];
- this.listElement = new Dictionary<string, double>();
- }
- public double dMilliSecondsTime;
- public double x;
- public double y;
- //out
- public uint[] XrayData;
- public Dictionary<string, double> listElement;
- public bool b_quant;
- }
- [Serializable]
- public class AreaXrayParam
- {
- public AreaXrayParam()
- {
- this.XrayData = new uint[2000];
- this.listElement = new Dictionary<string, double>();
- }
- public double dMilliSecondsTime;
- public List<Segment> a_listChord = new List<Segment>();
- public uint[] XrayData;
- public Dictionary<string, double> listElement;
- public bool b_quant;
- }
- [Serializable]
- public struct MoveStageParam
- {
- public float x;
- public float y;
- }
- struct oxfordCommandData
- {
- public OxfordCommandType commandType;
- public bool returnType;
- public MoveStageParam moveStagePrm;
- public ImageAquistionParam grabImgParam;
- public PointXrayParam pointXrayPrm;
- public AreaXrayParam areaXrayPrm;
- public List<PointXrayParam> XrayPrmForPoints;
- public int PointXrayDataReceived;
- public List<AreaXrayParam> XrayPrmForFeatures;
- public int AreaXrayDataReceived;
- }
- public class ExtenderWrapper : MarshalByRefObject
- {
- NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
- private oxfordCommandData currentCommand;
- //控制电镜
- private IMicroscopeController microscopeController = null;
- /// <summary>
- /// IImageAcquisitionController object
- /// </summary>
- private IImageAcquisitionController imageAcquisitionController = null;
- /// <summary>
- /// IImageSettings object
- /// </summary>
- private IImageAcquisitionSettings imageAcquisitionSettings = null;
- //控制器
- private IEdSpectrumAcquisitionController EdSpectrumAcquisitionController = null;
- private IEdSpectrumSettings EdSpectrumSettings = null;
- private IEdSpectrumProcessing EdSpectrumProcessing = null;
- // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
- private IAutoIdSettings autoIdSettings = null;
- private ISEMQuantSettings quantSettings = null;
- //private IEdChordListAcquisitionController _edsChordListController = null;
- private IEdChordListSettings _edsChordListSetting;
- private int XRayChannelLength = 2000;
- private int EDSColletionTimeOut = 10000;
- const int g_nOxfordControllerProcessTime = 4;
- const int g_nOxfordControllerEnergyRange = 20;
- const int XrayQuantityLimitPerTime = 256;
- private bool m_bXrayDone = false;
- //电压
- private double m_dHighVoltage=0;
- //放大倍数
- private double m_dMagnification=0;
- //工作距离
- private double m_dWorkingDistance=0;
- //亮度
- private double m_dBirghtness=0;
- //对比度
- private double m_dContrast = 0;
- //BeamOn
- private bool m_bBeamOn;
- //FilamentOn
- private bool m_bFilamentOn;
- //样品台位置
- private double m_dStageX;
- private double m_dStageY;
- private double m_dStageZ;
- private double m_dStageR;
- private double m_dStageT;
- private byte[] m_ImageBit = null;
- private long m_nImageWidth = 0;
- private long m_nImageHeight = 0;
- private double m_dImagePixelsize = 0;//it will be initialized when we get an image from the EDS.
- bool m_bAcquistionDone = false;
- private bool m_StageUpdated;
- private bool m_CollumnUpdated;
- private bool m_ExternalScanUpdated;
- const String theElementNameList = ("H,He") +
- (",Li,Be,B,C,N,O,F,Ne") +
- (",Na,Mg,Al,Si,P,S,Cl,Ar") +
- (",K,Ca,Sc,Ti,V,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr") +
- (",Rb,Sr,Y,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,I,Xe") +
- (",Cs,Ba,La") +
- (",Ce,Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu") +
- (",Hf,Ta,W,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At,Rn") +
- (",Fr,Ra,Ac") +
- (",Th,Pa,U,Np,Pu,Am,Cm,Bk,Cf,Es,Fm,Md,No,Lr");
- private bool m_ifautoid = true;
- private string m_knownelements;
- //构造函数
- public ExtenderWrapper()
- {
- //ConnectToEDSHardware();
- }
- public bool ConnectToEDSHardware()
- {
- try
- {
- InitMicroscopeController();
- InitImageAcquisition();
- InitXrayAcquistion();
- return true;
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- return false;
- }
- }
- //结束
- public void CloseExtender()
- {
- CloseMicroscopeController();
- CloseImageAcquisition();
- CloseXrayAcquistion();
- }
- public bool AquisitionImage(ref ImageAquistionParam p)
- {
- currentCommand.grabImgParam = p;
- currentCommand.commandType = OxfordCommandType.ImageAquisition;
- SetImageAcquistionSetting(p);
- try
- {
- int lastingTime = 0;
- m_bAcquistionDone = false;
- imageAcquisitionController.BeginMultipleAcquisition();
- IEnumerable<IElectronImage> images = imageAcquisitionController.StartAcquisition(imageAcquisitionSettings);
- int time1 = Environment.TickCount;
- while (true)
- {
- if (m_bAcquistionDone)
- {
- p.ImageData = m_ImageBit;
- currentCommand.returnType = true;
- break;
- }
- Application.DoEvents();
- int time2;
- time2 = Environment.TickCount;
- if (time2 - time1 > EDSColletionTimeOut * 6)
- {
- currentCommand.returnType = false;
- break;
- }
- }
- }
- catch (InvalidSettingsException settingsException)
- {
- var sb = new StringBuilder(@"Invalid settings have been supplied: ");
- sb.AppendLine();
- settingsException.ValidationResults.ValidationErrors.ToList().ForEach(ve => sb.AppendFormat("{0}{1}", Environment.NewLine, ve));
- NLog.LogManager.GetCurrentClassLogger().Error(sb.ToString());
- }
- catch (AcquisitionStartException startException)
- {
- string msg = string.Format(@"AcquisitionStartException: {0}", startException.Message);
- NLog.LogManager.GetCurrentClassLogger().Error(msg);
- }
- if (currentCommand.returnType == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool MoveStageXY(float x, float y)
- {
- currentCommand.moveStagePrm = new MoveStageParam();
- currentCommand.moveStagePrm.x = x;
- currentCommand.moveStagePrm.y = y;
- currentCommand.commandType = OxfordCommandType.MoveStageXY;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageX, (double)x/1000.0 },
- { Stage.StageY, (double)y /1000.0}
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- currentCommand.returnType = true;
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 60000)
- {
- currentCommand.returnType = false;
- break;
- }
- }
- if (currentCommand.returnType == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool XrayAreaCollecting(ref AreaXrayParam p)
- {
- currentCommand.areaXrayPrm = p;
- currentCommand.commandType = OxfordCommandType.XrayAreaCollection;
- m_bXrayDone = false;
- SetXrayAcquisitionParam(p.dMilliSecondsTime);
- List<Chord> Chords = new List<Chord>();
- foreach (Segment seg in p.a_listChord)
- {
- Chord chord = new Chord(seg.X, seg.Y, seg.Length);
- Chords.Add(chord);
- }
- var chordsList = new ChordList(Chords, m_dImagePixelsize);
- EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
- EdSpectrumAcquisitionController.BeginMultipleAcquisition();
- try
- {
- IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
- var time1 = Environment.TickCount;
- while (true)
- {
- if (m_bXrayDone)
- {
- currentCommand.returnType = true;
- break;
- }
- Application.DoEvents();
- var time2 = Environment.TickCount;
- if (time2 - time1 > EDSColletionTimeOut * 6)
- {
- currentCommand.returnType = false;
- break;
- }
- }
- }
- catch (InvalidSettingsException invalidSettingsException)
- {
- string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
- invalidSettingsException.Message,
- invalidSettingsException.ValidationResults.ValidationErrors);
- log.Error(msg);
- }
- catch (AcquisitionStartException acquisitionStartException)
- {
- string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
- log.Error(msg);
- }
- if (currentCommand.returnType == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool XrayPointCollecting(ref PointXrayParam p)
- {
- currentCommand.pointXrayPrm = p;
- currentCommand.commandType = OxfordCommandType.XrayPointCollection;
- m_bXrayDone = false;
- p.XrayData = new uint[XRayChannelLength];
- p.listElement = new Dictionary<string, double>();
- SetXrayAcquisitionParam(p.dMilliSecondsTime);
- EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(p.x * m_dImagePixelsize, p.y * m_dImagePixelsize));
- EdSpectrumAcquisitionController.BeginMultipleAcquisition();
- try
- {
- m_bXrayDone = false;
- IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
- var time1 = Environment.TickCount;
- while (true)
- {
- if (m_bXrayDone)
- {
- currentCommand.returnType = true;
- break;
- }
- Application.DoEvents();
- var time2 = Environment.TickCount;
- if (time2 - time1 > EDSColletionTimeOut * 6)
- {
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- log.Warn("XrayStartAcquisition 超时!");
- currentCommand.returnType = false;
- }
- }
- }
- catch (InvalidSettingsException invalidSettingsException)
- {
- string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
- invalidSettingsException.Message,
- invalidSettingsException.ValidationResults.ValidationErrors);
- log.Error(msg);
- }
- catch (AcquisitionStartException acquisitionStartException)
- {
- string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
- log.Error(msg);
- }
- if (currentCommand.returnType == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool CollectXrayByPoints(ref List<PointXrayParam> a_listPoints, uint a_nXRayAQTime, bool a_bElementInfo)
- {
- int nSize = a_listPoints.Count;
- List<PointXrayParam> singleGroup = new List<PointXrayParam>();
- int nTimes = nSize / XrayQuantityLimitPerTime;
- for (int i = 0; i < nTimes; i++)
- {
- singleGroup.Clear();
- for (int m = 0; m < XrayQuantityLimitPerTime; m++)
- {
- singleGroup.Add(a_listPoints[i * XrayQuantityLimitPerTime + m]);
- }
- if (!CollectXrayByPointsOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
- {
- return false;
- }
- }
- int nLast = nSize % XrayQuantityLimitPerTime;
- if (nLast != 0)
- {
- singleGroup.Clear();
- for (int m = 0; m < nLast; m++)
- {
- singleGroup.Add(a_listPoints[m]);
- }
- if (!CollectXrayByPointsOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
- {
- return false;
- }
- }
- return true;
- }
- public bool CollectXrayByPointsOnHardwareLimit(ref List<PointXrayParam> a_listPoints, uint a_nXRayAQTime, bool a_bElementInfo)
- {
- if (a_listPoints.Count > XrayQuantityLimitPerTime)
- {
- return false;
- }
- currentCommand.XrayPrmForPoints = a_listPoints;
- currentCommand.commandType = OxfordCommandType.COLLECT_XRAYPOINTS;
- //log.Info("线程:开始采集多点xray");
- var PointXrayDatas = currentCommand.XrayPrmForPoints;
- currentCommand.PointXrayDataReceived = 0;
- m_bXrayDone = false;
- var dMilliSecondsTime = PointXrayDatas[0].dMilliSecondsTime;
- SetXrayAcquisitionParam(dMilliSecondsTime);
- EdSpectrumAcquisitionController.BeginMultipleAcquisition();
- foreach (var prm in PointXrayDatas)
- {
- prm.b_quant = a_bElementInfo;
- EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreatePointRegion(new System.Windows.Point(prm.x * m_dImagePixelsize, prm.y * m_dImagePixelsize));
- try
- {
- m_bXrayDone = false;
- IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
- }
- catch (InvalidSettingsException invalidSettingsException)
- {
- string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
- invalidSettingsException.Message,
- invalidSettingsException.ValidationResults.ValidationErrors);
- log.Error(msg);
- }
- catch (AcquisitionStartException acquisitionStartException)
- {
- string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
- log.Error(msg);
- }
- }
- var time1 = Environment.TickCount;
- while (true)
- {
- if (m_bXrayDone)
- {
- currentCommand.returnType = true;
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- break;
- }
- Application.DoEvents();
- var time2 = Environment.TickCount;
- if (time2 - time1 > EDSColletionTimeOut * PointXrayDatas.Count)
- {
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- currentCommand.returnType = false;
- }
- }
- if (currentCommand.returnType == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool CollectXrayByFeatures(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
- {
- int nSize = a_listFeatures.Count;
- List<AreaXrayParam> singleGroup = new List<AreaXrayParam>();
- int nTimes = nSize / XrayQuantityLimitPerTime;
- for (int i = 0; i < nTimes; i++)
- {
- singleGroup.Clear();
- for (int m = 0; m < XrayQuantityLimitPerTime; m++)
- {
- singleGroup.Add(a_listFeatures[i * XrayQuantityLimitPerTime + m]);
- }
- if (!CollectXrayByFeaturesOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
- {
- return false;
- }
- }
- int nLast = nSize % XrayQuantityLimitPerTime;
- if (nLast != 0)
- {
- singleGroup.Clear();
- for (int m = 0; m < nLast; m++)
- {
- singleGroup.Add(a_listFeatures[m]);
- }
- if (!CollectXrayByFeaturesOnHardwareLimit(ref singleGroup, a_nXRayAQTime, a_bElementInfo))
- {
- return false;
- }
- }
- return true;
- }
- public bool CollectXrayByFeaturesOnHardwareLimit(ref List<AreaXrayParam> a_listFeatures, double a_nXRayAQTime, bool a_bElementInfo)
- {
- if (a_listFeatures.Count > XrayQuantityLimitPerTime)
- {
- return false;
- }
- currentCommand.XrayPrmForFeatures = a_listFeatures;
- currentCommand.commandType = OxfordCommandType.COLLECT_XRAYFEATURES;
- var p = currentCommand.XrayPrmForFeatures;
- currentCommand.AreaXrayDataReceived = 0;
- m_bXrayDone = false;
- var dMilliSecondsTime = p[0].dMilliSecondsTime;
- SetXrayAcquisitionParam(p[0].dMilliSecondsTime);
- EdSpectrumAcquisitionController.BeginMultipleAcquisition();
- foreach (var prm in p)
- {
- prm.b_quant = a_bElementInfo;
- List<Chord> Chords = new List<Chord>();
- Chord chord;
- foreach (Segment seg in prm.a_listChord)
- {
- if (seg.Length >= 3)
- {
- chord = new Chord(seg.X + 1, seg.Y, seg.Length - 2);//get the middle part
- }
- else
- {
- chord = new Chord(seg.X, seg.Y, seg.Length);
- }
- Chords.Add(chord);
- }
- var chordsList = new ChordList(Chords, m_dImagePixelsize);
- EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
- try
- {
- m_bXrayDone = false;
- IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
- }
- catch (InvalidSettingsException invalidSettingsException)
- {
- string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
- invalidSettingsException.Message,
- invalidSettingsException.ValidationResults.ValidationErrors);
- log.Error(msg);
- }
- catch (AcquisitionStartException acquisitionStartException)
- {
- string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
- log.Error(msg);
- }
- }
- var time1 = Environment.TickCount;
- while (true)
- {
- if (m_bXrayDone)
- {
- currentCommand.returnType = true;
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- break;
- }
- Application.DoEvents();
- var time2 = Environment.TickCount;
- if (time2 - time1 > EDSColletionTimeOut * p.Count)
- {
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- currentCommand.returnType = false;
- break;
- }
- }
- if (currentCommand.returnType == true)
- {
- a_listFeatures = currentCommand.XrayPrmForFeatures;
- return true;
- }
- else
- {
- return false;
- }
- }
- //控制电镜初始化
- void InitMicroscopeController()
- {
- this.microscopeController = AcquireFactory.CreateMicroscopeControl();
- this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
- this.microscopeController.StageChange += this.OnMicroscopeStageChange;
- this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
- this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
- this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
- //ReadMicroscopeColumn();
- //ReadStage();
- }
- //控制电镜释放
- void CloseMicroscopeController()
- {
- if (microscopeController != null)
- {
- this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
- this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
- this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
- this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
- this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
- microscopeController = null;
- }
- }
- //读取当前的电镜控制值
- private void ReadMicroscopeColumn()
- {
- var columnCapabilities = this.microscopeController.ColumnCapabilities;
- var columnConditions = this.microscopeController.ColumnConditions;
- if (columnCapabilities.Magnification.CanRead)
- {
- m_dMagnification = columnConditions.Magnification;
- }
- if (columnCapabilities.WorkingDistance.CanRead)
- {
- m_dWorkingDistance = columnConditions.WorkingDistance;
- }
- if (columnCapabilities.HighVoltage.CanRead)
- {
- m_dHighVoltage = columnConditions.HighVoltage;
- }
- if (columnCapabilities.Brightness.CanRead)
- {
- m_dBirghtness = columnConditions.Brightness;
- }
- if (columnCapabilities.Contrast.CanRead)
- {
- m_dContrast = columnConditions.Contrast;
- }
- if (columnCapabilities.BeamOn.CanRead)
- {
- m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
- }
- if (columnCapabilities.FilamentOn.CanRead)
- {
- m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
- }
- }
- //读取样品台位置
- private void ReadStage()
- {
- var stageCapabilities = this.microscopeController.StageCapabilities;
- var stageConditions = this.microscopeController.StageConditions;
- while (!stageCapabilities.StageX.CanRead)
- {
- Thread.Sleep(100);
- }
- if (stageCapabilities.StageX.CanRead)
- {
- this.m_dStageX = stageConditions.StageX * 1000.0;
- }
- if (stageCapabilities.StageY.CanRead)
- {
- this.m_dStageY = stageConditions.StageY * 1000.0;
- }
- if (stageCapabilities.StageZ.CanRead)
- {
- this.m_dStageZ = stageConditions.StageZ;
- }
- if (stageCapabilities.StageT.CanRead)
- {
- this.m_dStageT = stageConditions.StageT;
- }
- if (stageCapabilities.StageR.CanRead)
- {
- this.m_dStageR = stageConditions.StageR;
- }
- }
- //电镜控制改变事件
- private void OnMicroscopeColumnChange(object sender, EventArgs e)
- {
- ReadMicroscopeColumn();
- }
- //样品台控制改变事件
- private void OnMicroscopeStageChange(object sender, EventArgs e)
- {
- ReadStage();
- }
- //镜筒控制连接或断开时的事件
- private void OnMicroscopeColumnConnected(object sender, EventArgs e)
- {
- ReadMicroscopeColumn();
- }
- //样品台控制连接或断开时的事件
- private void OnMicroscopeStageConnected(object sender, EventArgs e)
- {
- ReadStage();
- }
- //样品台控制、电镜控制、外围控制的事件改变完成
- private void OnMicroscopeChangeCompleted(object sender, CompletedEventArgs e)
- {
- if (e.Control == MicroscopeControlType.Stage)
- {
- if (e.Success)
- {
- m_StageUpdated = true;
- }
- }
- else if (e.Control == MicroscopeControlType.Column)
- {
- if (e.Success)
- {
- m_CollumnUpdated = true;
- }
- }
- else if (e.Control == MicroscopeControlType.ExternalScan)
- {
- if (e.Success)
- {
- m_ExternalScanUpdated = true;
- }
- }
- ReadMicroscopeColumn();
- ReadStage();
- }
- bool ifMagRead=false;
- public float GetMagnification()
- {
- ReadMicroscopeColumn();
- if (m_dMagnification != 0)
- {
- ifMagRead=true;
- }
- return (float)m_dMagnification;
- }
- public Boolean SetMagnification(float set)
- {
- if (!ifMagRead)
- {
- return false;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.Magnification, (double)set }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 10000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- bool ifwdread = false;
- //焦距
- public float GetWorkingDistance()
- {
- ReadMicroscopeColumn();
- if (m_dWorkingDistance != 0)
- {
- bool ifwdread = true;
- }
- return (float)m_dWorkingDistance;
- }
- public Boolean SetWorkingDistance(float set)
- {
- if (!ifwdread)
- {
- return false;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.WorkingDistance, (double)set }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 10000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- bool ifBrightnessRead = false;
- //亮度
- public float GetBrightness()
- {
- ReadMicroscopeColumn();
- if (m_dBirghtness != 0)
- {
- ifBrightnessRead = true;
- }
- return (float)m_dBirghtness;
- }
- public Boolean SetBrightness(float set)
- {
- if (!ifBrightnessRead)
- {
- return false;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.Brightness, (double)set }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 2000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- bool ifContrastRead = false;
- //对比度
- public float GetContrast()
- {
- ReadMicroscopeColumn();
- if (m_dContrast != 0)
- {
- ifContrastRead = true;
- }
- return (float)m_dContrast;
- }
- public Boolean SetContrast(float set)
- {
- if (!ifContrastRead)
- {
- return false;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.Contrast, (double)set }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 2000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- //SEM电压
- public float GetSEMVoltage()
- {
- ReadMicroscopeColumn();
- log.Warn(m_dHighVoltage);
- return (float)m_dHighVoltage;
- }
- public Boolean SetSEMVoltage(float set)
- {
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.HighVoltage, (double)set }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 10000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- public bool SetSemScanExternal(bool b)
- {
- m_ExternalScanUpdated = false;
- if (!b)
- {
- EndMultipleAquisition();
- }
- this.microscopeController.SetExternalScan(b);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_ExternalScanUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 2000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- //样品台
- public float[] GetStagePosition()
- {
- ReadStage();
- float[] ret = new float[5];
- ret[0] = (float)m_dStageX;
- ret[1] = (float)m_dStageY;
- ret[2] = (float)m_dStageZ;
- ret[3] = (float)m_dStageT;
- ret[4] = (float)m_dStageR;
- return ret;
- }
- public Boolean SetStagePosition(float[] set)
- {
- double stageX = (double)set[0];
- double stageY = (double)set[1];
- double stageZ = (double)set[2];
- double stageT = (double)set[3];
- double stageR = (double)set[4];
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageX, (double)stageX/1000.0 },
- { Stage.StageY, (double)stageY/1000.0 },
- { Stage.StageZ, (double)stageZ },
- { Stage.StageT, (double)stageT },
- { Stage.StageR, (double)stageR }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- public float GetStageAtX()
- {
- ReadStage();
- return (float)m_dStageX;
- }
- public float GetStageAtY()
- {
- ReadStage();
- return (float)m_dStageY;
- }
- public float GetStageAtZ()
- {
- ReadStage();
- return (float)m_dStageZ;
- }
- public float GetStageAtT()
- {
- ReadStage();
- return (float)m_dStageT;
- }
- public float GetStageAtR()
- {
- ReadStage();
- return (float)m_dStageR;
- }
- public Boolean SetStageGotoX(float set)
- {
- double stageX = (double)set;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageX, (double)stageX/1000.0 }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- public Boolean SetStageGotoY(float set)
- {
- double stageY = (double)set;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageY, (double)stageY/1000.0 }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- public Boolean SetStageGotoZ(float set)
- {
- double stageZ = (double)set;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageZ, (double)stageZ }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- public Boolean SetStageGotoT(float set)
- {
- double stageT = (double)set;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageT, (double)stageT }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- public Boolean SetStageGotoR(float set)
- {
- double stageR = (double)set;
- var stageDictionary = new Dictionary<Stage, double>
- {
- { Stage.StageR, (double)stageR }
- };
- m_StageUpdated = false;
- this.microscopeController.SetStageConditions(stageDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_StageUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 20000)
- {
- currentCommand.returnType = false;
- return false;
- //break;
- }
- }
- return true;
- }
- #region 拍图
- //图像扫描尺寸
- public double[] ImageScanSize =
- {
- 32,
- 64,
- 128,
- 256,
- 512,
- 704,
- 768,
- 1408,
- 1024,
- 1536,
- 2048,
- 3072,
- 4096,
- 8192
- };
- public double GetDImagePixelsize()
- {
- return m_dImagePixelsize;
- }
- public void SetDImagePixelsize(double value)
- {
- m_dImagePixelsize = value;
- }
- void InitImageAcquisition()
- {
- imageAcquisitionController = AcquireFactory.CreateImageServer();
- imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
- imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
- imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
- }
- void InitXrayAcquistion()
- {
- EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
- EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
- EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
- // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
- autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
- quantSettings = ProcessingFactory.CreateSEMQuantSettings();
- EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
- //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
- }
- //控制电镜释放
- void CloseImageAcquisition()
- {
- if (imageAcquisitionController != null)
- {
- imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
- imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
- imageAcquisitionController = null;
- }
- }
- void CloseXrayAcquistion()
- {
- if (EdSpectrumAcquisitionController != null)
- {
- EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
- //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
- EdSpectrumAcquisitionController = null;
- }
- }
- /// <summary>
- /// OnImageExperimentStarted
- /// </summary>
- private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
- {
- NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
- log.Info("拍图开始事件!");
- }
- //int m_nState;
- /// <summary>
- /// OnImageExperimentFinished
- /// </summary>
- private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
- {
- IElectronImage electronImage = e.Value[0];
- if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight, out m_dImagePixelsize))
- {
- NLog.LogManager.GetCurrentClassLogger().Error("图像采集完成,获取图像像素失败!");
- }
- if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
- {
- m_bAcquistionDone = true;
- }
- }
- bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth, out double a_nPixelSize)
- {
- a_nImageHeight = 0;
- a_nImageWidth = 0;
- a_nPixelSize = 0;
- a_pImageBits = null;
- if (a_electronImage == null)
- {
- return false;
- }
- a_nImageHeight = a_electronImage.Height;
- a_nImageWidth = a_electronImage.Width;
- a_nPixelSize = a_electronImage.PixelSize;
- int nBytesPerPixel = a_electronImage.BytesPerPixel;
- long nImageSize = a_nImageHeight * a_nImageWidth;
- long nBufferSize = nImageSize * nBytesPerPixel;
- Byte[] imageData = new Byte[nBufferSize];
- a_electronImage.GetData(imageData);
- a_pImageBits = new Byte[nImageSize];
- // default, oxford will return short image, we need to convert to byte
- if (nBytesPerPixel == 2)
- {
- int nBSEValue = 0;
- for (int i = 0; i < nImageSize; ++i)
- {
- nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
- a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
- }
- }
- else
- {
- string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
- NLog.LogManager.GetCurrentClassLogger().Error(msg);
- int nOffset = nBytesPerPixel - 1;
- for (int i = 0; i < nImageSize; ++i)
- {
- a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
- }
- }
- return true;
- }
- //a_dDwellTime : 1~100000之间的数
- //a_sImageType : 1: SE, 2: Bse
- public bool SetImageAcquistionSetting(ImageAquistionParam p)
- {
- IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
- IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
- IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
- if (p.DwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
- {
- imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
- }
- if (p.DwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
- {
- imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
- }
- else
- {
- imageSettings.DwellTimeMicroSeconds = p.DwellTime;
- }
- imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
- imageSettings.EnableInputSource((ImageInputSources)p.sourceType, true);
- if (!ImageScanSize.Contains(p.width))
- {
- NLog.LogManager.GetCurrentClassLogger().Error("图像尺寸输入无效");
- return false;
- }
- var pixelSize = 1d / p.width;
- if (p.regionType == ImageAquistionParam.ImageRegionType.FullField)
- {
- scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
- }
- else
- {
- scanSettings.AcquisitionRegion.CreateRectangleRegion(p.ImgRectRegion, pixelSize);
- }
- return true;
- }
- #endregion
- #region X-ray
- void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
- {
- EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
- EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
- EdSpectrumSettings.EdSettings.ProcessTime = 4;
- EdSpectrumSettings.EdSettings.EnergyRange = 20;
- EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
- }
- private bool GetAtomicNumberByEleName(string name, out int atomicNum)
- {
- var allelenames = theElementNameList.Split(',');
- for (int i = 0; i < allelenames.Length; i++)
- {
- if (allelenames[i] == name)
- {
- atomicNum = i + 1;
- return true;
- }
- }
- atomicNum = 0;
- return false;
- }
- /// <summary>
- /// Called when IEdSpectrumAcquisitionController Experiment Finished
- /// </summary>
- /// <param name="sender">sender object</param>
- /// <param name="e">The instance containing the event data.</param>
- private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
- {
- IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
- uint[] m_XrayData;
- NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
- IEdSpectrum edSpectrum = e.Value;
- if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
- {
- NLog.LogManager.GetCurrentClassLogger().Error("Xray采集完成,获取xray失败!");
- }
- long nXraycount = 0;
- for (int i = 0; i < 2000; i++)
- {
- nXraycount += m_XrayData[i];
- }
- //Quantify processing
- bool bquant = false;
- if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
- {
- bquant = currentCommand.pointXrayPrm.b_quant;
- }
- else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
- {
- bquant = currentCommand.areaXrayPrm.b_quant;
- }
- else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
- {
- var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
- bquant = curXrayData.b_quant;
- }
- else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
- {
- var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
- bquant = curXrayData.b_quant;
- }
- var m_listElement = new Dictionary<string, double>();
- if (bquant)
- {
- if (m_ifautoid == false)
- {
- log.Warn(autoIdSettings.KnownElements);
- autoIdSettings.ClearKnownElements();
- //m_knownelements = "K,Ca,Sc,Ti,V,Mn,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr";
- var knownelenames = m_knownelements.Split(',');
- foreach (var ele in knownelenames)
- {
- int atomicnum;
- if (GetAtomicNumberByEleName(ele, out atomicnum))
- {
- autoIdSettings.SetKnownElement(atomicnum, true);
- }
- }
- log.Warn(autoIdSettings.KnownElements);
- }
- else
- {
- }
- EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
- // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
- quantSettings.CombinedElement = 8;
- quantSettings.Normalised = true;
- ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, quantSettings);//(a_nChannelData, OIHelper::SEMQuantSettings);
- IEnumerable<ISEMQuantResult> Results = quantStatus.Results;
- var ie = Results.GetEnumerator();
- while (ie.MoveNext())
- {
- ISEMQuantResult result = ie.Current;
- if (result.WeightPercent != 0)
- {
- m_listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent);
- }
- }
- }
- //------------------------
- if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
- {
- if (currentCommand.commandType == OxfordCommandType.XrayPointCollection)
- {
- currentCommand.pointXrayPrm.XrayData = m_XrayData;
- currentCommand.pointXrayPrm.listElement = m_listElement;
- m_bXrayDone = true;
- }
- else if (currentCommand.commandType == OxfordCommandType.XrayAreaCollection)
- {
- currentCommand.areaXrayPrm.XrayData = m_XrayData;
- currentCommand.areaXrayPrm.listElement = m_listElement;
- m_bXrayDone = true;
- }
- else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYPOINTS)
- {
- var curXrayData = currentCommand.XrayPrmForPoints[currentCommand.PointXrayDataReceived];
- curXrayData.XrayData = m_XrayData;
- curXrayData.listElement = m_listElement;
- currentCommand.PointXrayDataReceived += 1;
- if (currentCommand.PointXrayDataReceived == currentCommand.XrayPrmForPoints.Count)
- {
- m_bXrayDone = true;
- }
- }
- else if (currentCommand.commandType == OxfordCommandType.COLLECT_XRAYFEATURES)
- {
- var curXrayData = currentCommand.XrayPrmForFeatures[currentCommand.AreaXrayDataReceived];
- curXrayData.XrayData = m_XrayData;
- curXrayData.listElement = m_listElement;
- currentCommand.AreaXrayDataReceived += 1;
- if (currentCommand.AreaXrayDataReceived == currentCommand.XrayPrmForFeatures.Count)
- {
- m_bXrayDone = true;
- }
- }
- }
- }
- bool ReadXrayData(IEdSpectrum a_spectrum, out uint[] a_pSpectrumData, int a_nBufferSize)
- {
- a_pSpectrumData = new uint[a_nBufferSize];
- int[] xrayData = new int[a_spectrum.NumberOfChannels];
- a_spectrum.GetChannelData(xrayData);
- double dZeroChannelValue = a_spectrum.ZeroChannelValue;
- int nChannelStart = 0;
- if (dZeroChannelValue < 0) // zero channel value should less than zero
- {
- nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
- }
- int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
- double dStep1 = 1.0 / nDataLength;
- double dStep2 = 1.0 / a_nBufferSize;
- for (int i = 0; i < nDataLength; ++i)
- {
- uint nValue = (uint)(xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0);
- double dBinPos = i * dStep1;
- long nLeftBin = (long)(dBinPos / dStep2);
- // calculate % into left bin
- double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
- // calculate data into the left bin
- uint nValueToLeftBin = (uint)((double)nValue * dLeft_Percent + 0.5);
- // put data into bins
- a_pSpectrumData[nLeftBin] += nValueToLeftBin;
- if ((nLeftBin + 1) < a_nBufferSize)
- {
- a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
- }
- }
- return true;
- }
- public bool IsAcquiringSpectrum()
- {
- return EdSpectrumAcquisitionController.IsAcquiring;
- }
- public void BeginMultipleAquisition()
- {
- EdSpectrumAcquisitionController.BeginMultipleAcquisition();
- }
- public void EndMultipleAquisition()
- {
- EdSpectrumAcquisitionController.EndMultipleAcquisition();
- }
- public bool GetSemBeamOn()
- {
- var beamon = microscopeController.ColumnConditions.BeamOn;
- if (beamon == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool SetSemBeamOnOrOff(bool val)
- {
- double beamon;
- if (val)
- {
- beamon = 1;
- }
- else
- {
- beamon = 0;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.BeamOn, (double)beamon }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 10000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- public bool SetSemFilamentOnOrOff(bool val)
- {
- double beamon;
- if (val)
- {
- beamon = 1;
- }
- else
- {
- beamon = 0;
- }
- Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
- {
- { Column.FilamentOn, (double)beamon }
- };
- m_CollumnUpdated = false;
- this.microscopeController.SetColumnConditions(columnDictionary);
- int time1 = Environment.TickCount;
- int time2;
- while (!m_CollumnUpdated)
- {
- Application.DoEvents();
- time2 = Environment.TickCount;
- if (time2 - time1 > 10000)
- {
- currentCommand.returnType = false;
- return false;
- }
- }
- return true;
- }
- public void StopXrayAquisition()
- {
- EdSpectrumAcquisitionController.StopAcquisition();
- }
- public void SetQuantifiCationParam(bool IfAutoId, string knownElements)
- {
- m_ifautoid = IfAutoId;
- m_knownelements = knownElements;
- }
- #endregion
- }
- }
|