Measure.cs 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. //时间:20200610
  2. //作者:郝爽
  3. //功能:测量线程
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.IO;
  12. using SmartSEMControl;
  13. using MeasureData;
  14. using FileManager;
  15. using WebManager;
  16. using OpenCvSharp;
  17. namespace MeasureThread
  18. {
  19. //图片模式
  20. public enum ImageMode
  21. {
  22. FIB,
  23. SEM
  24. }
  25. public delegate void ThreadStatusHandler(object sender, ThreadStatusEventArgs e); //声明委托
  26. public delegate void CutHolesStatusHandler(object sender, CutHolesStatusEventArgs e);
  27. public class ThreadStatusEventArgs
  28. {
  29. //状态
  30. public Boolean State
  31. {
  32. get { return m_state; }
  33. set { m_state = value; }
  34. }
  35. private Boolean m_state;
  36. //时间
  37. public DateTime Time
  38. {
  39. get { return m_time; }
  40. set { m_time = value; }
  41. }
  42. private DateTime m_time;
  43. //步骤代码
  44. public String Step_Code
  45. {
  46. get { return step_Code; }
  47. set { step_Code = value; }
  48. }
  49. private String step_Code;
  50. //其他消息
  51. public String Message
  52. {
  53. get { return this.message; }
  54. set { this.message = value; }
  55. }
  56. private String message;
  57. //孔名
  58. public String HoleName
  59. {
  60. get { return this.holeName; }
  61. set { this.holeName = value; }
  62. }
  63. private String holeName;
  64. //图片信息
  65. public class PictureInformation
  66. {
  67. public String Picture_FullPath
  68. {
  69. get { return picture_FullPath; }
  70. set { picture_FullPath = value; }
  71. }
  72. private String picture_FullPath;
  73. public double Work_Voltage
  74. {
  75. get { return work_Voltage; }
  76. set { work_Voltage = value; }
  77. }
  78. private double work_Voltage;
  79. public double Magnification
  80. {
  81. get { return magnification; }
  82. set { magnification = value; }
  83. }
  84. private double magnification;
  85. public double Work_Distance
  86. {
  87. get { return work_Distance; }
  88. set { work_Distance = value; }
  89. }
  90. private double work_Distance;
  91. public String Work_Status
  92. {
  93. get { return work_Status; }
  94. set { work_Status = value; }
  95. }
  96. private String work_Status;
  97. }
  98. //图片信息实例类
  99. public PictureInformation Picture_Information
  100. {
  101. get { return picture_Information; }
  102. set { picture_Information = value; }
  103. }
  104. private PictureInformation picture_Information;
  105. //图像信息
  106. public class ImageInformation
  107. {
  108. public int Method_Name
  109. {
  110. get { return method_Name; }
  111. set { method_Name = value; }
  112. }
  113. private int method_Name;
  114. public Source_Img_Degree_Direction SIDD;
  115. public Cut_Position CP;
  116. public Cut_Success CS;
  117. public Trapezoid_Top_Center_Position TTCP;
  118. public Auto_Foucs AF;
  119. public Auto_Stigmatic AS;
  120. public Center_Position_OffsetAngle_Direction CPOD;
  121. public Measure_Size MS;
  122. public ImageInformation()
  123. {
  124. SIDD = new Source_Img_Degree_Direction();
  125. CP = new Cut_Position();
  126. CS = new Cut_Success();
  127. TTCP = new Trapezoid_Top_Center_Position();
  128. AF = new Auto_Foucs();
  129. AS = new Auto_Stigmatic();
  130. CPOD = new Center_Position_OffsetAngle_Direction();
  131. MS = new Measure_Size();
  132. }
  133. //1-计算原始图像偏移角度及方向
  134. public class Source_Img_Degree_Direction
  135. {
  136. public Boolean Is_Image = false;
  137. public double Degree
  138. {
  139. get { return degree; }
  140. set { degree = value; }
  141. }
  142. private double degree;
  143. public int Direction
  144. {
  145. get { return direction; }
  146. set { direction = value; }
  147. }
  148. private int direction;
  149. public int State
  150. {
  151. get { return state; }
  152. set { state = value; }
  153. }
  154. private int state;
  155. }
  156. //2-计算切割点位置
  157. public class Cut_Position
  158. {
  159. public Boolean Is_Image = false;
  160. public double Offsetx
  161. {
  162. get { return offsetx; }
  163. set { offsetx = value; }
  164. }
  165. private double offsetx;
  166. public double Offsety
  167. {
  168. get { return offsety; }
  169. set { offsety = value; }
  170. }
  171. private double offsety;
  172. public int State
  173. {
  174. get { return state; }
  175. set { state = value; }
  176. }
  177. private int state;
  178. }
  179. //3-是否切割成功
  180. public class Cut_Success
  181. {
  182. public Boolean Is_Image = false;
  183. public int State
  184. {
  185. get { return state; }
  186. set { state = value; }
  187. }
  188. private int state;
  189. }
  190. //4-计算切割后图像梯形区域上边中心点坐标
  191. public class Trapezoid_Top_Center_Position
  192. {
  193. public Boolean Is_Image = false;
  194. public double TopCenterX
  195. {
  196. get { return topCenterX; }
  197. set { topCenterX = value; }
  198. }
  199. private double topCenterX;
  200. public double TopCenterY
  201. {
  202. get { return topCenterY; }
  203. set { topCenterY = value; }
  204. }
  205. private double topCenterY;
  206. public int State
  207. {
  208. get { return state; }
  209. set { state = value; }
  210. }
  211. private int state;
  212. }
  213. //5-自动对焦
  214. public class Auto_Foucs
  215. {
  216. public Boolean Is_Image = true;
  217. public String Img_Path
  218. {
  219. get { return img_Path; }
  220. set { img_Path = value; }
  221. }
  222. private String img_Path;
  223. }
  224. //6-自动像闪
  225. public class Auto_Stigmatic
  226. {
  227. public Boolean Is_Image = true;
  228. public String Img_Path
  229. {
  230. get { return img_Path; }
  231. set { img_Path = value; }
  232. }
  233. private String img_Path;
  234. }
  235. //7-计算切割面区域中心坐标,以及偏移角度及方向
  236. public class Center_Position_OffsetAngle_Direction
  237. {
  238. public Boolean Is_Image = false;
  239. public double CenterX
  240. {
  241. get { return centerX; }
  242. set { centerX = value; }
  243. }
  244. private double centerX;
  245. public double CenterY
  246. {
  247. get { return centerY; }
  248. set { centerY = value; }
  249. }
  250. private double centerY;
  251. public double Degree
  252. {
  253. get { return degree; }
  254. set { degree = value; }
  255. }
  256. private double degree;
  257. public int Direction
  258. {
  259. get { return direction; }
  260. set { direction = value; }
  261. }
  262. private int direction;
  263. public int State
  264. {
  265. get { return state; }
  266. set { state = value; }
  267. }
  268. private int state;
  269. }
  270. //8-测量尺寸
  271. public class Measure_Size
  272. {
  273. public Boolean Is_Image = false;
  274. public int State
  275. {
  276. get { return state; }
  277. set { state = value; }
  278. }
  279. private int state;
  280. }
  281. }
  282. //图像信息实例类
  283. public ImageInformation Image_Information
  284. {
  285. get { return image_Information; }
  286. set { image_Information = value; }
  287. }
  288. private ImageInformation image_Information;
  289. public ThreadStatusEventArgs(string a_State)
  290. {
  291. picture_Information = new PictureInformation();
  292. image_Information = new ImageInformation();
  293. }
  294. }
  295. public class CutHolesStatusEventArgs
  296. {
  297. public string State
  298. {
  299. get { return m_state; }
  300. set { m_state = value; }
  301. }
  302. private string m_state;
  303. public string HoleName
  304. {
  305. get { return m_holeName; }
  306. set { m_holeName = value; }
  307. }
  308. private string m_holeName;
  309. public CutHolesStatusEventArgs(string a_state, string a_holeName)
  310. {
  311. this.m_state = a_state;
  312. this.m_holeName = a_holeName;
  313. }
  314. }
  315. public class Measure
  316. {
  317. /// 图像拉直算法
  318. /// </summary>
  319. /// <param name="Slope">倾斜角度</param>
  320. /// <param name="x0">圆心坐标x</param>
  321. /// <param name="y0">圆心坐标y</param>
  322. /// <param name="x1">倾斜情况下帧图的坐标x</param>
  323. /// <param name="y1">倾斜情况下帧图的坐标y</param>
  324. /// <param name="Hx">复位到原位置需要移动的x值(返回值)</param>
  325. /// <param name="Hy">复位到原位置需要移动的y值(返回值)</param>
  326. /// <returns></returns>
  327. public bool Straightening(float Slope, float x0, float y0, float x1, float y1, ref float Hx, ref float Hy)
  328. {
  329. try
  330. {
  331. float xr = x1 - x0;//倾斜图到中心的距离差x
  332. float yr = y1 - y0;//倾斜图到中心的距离差y
  333. float R = (float)Math.Sqrt(xr * xr + yr * yr);//圆心到倾斜图的长度
  334. //double D = 2 * R * Math.Sin(ScanRotation / 2);//倾斜图与矫正图底边的长度
  335. float k = (float)(Math.Atan(yr / xr) / Math.PI * 180);//x1,y1的直角三角形圆心角度
  336. float k_S = k - Slope;//通过夹角差求x2,y2
  337. //int y2 = (int)Math.Round(Math.Sin(Math.PI / (180 / k_S)) * R);
  338. //int x2 = (int)Math.Round(Math.Cos(Math.PI / (180 / k_S)) * R);
  339. float y2 = (float)(Math.Sin(Math.PI / (180 / k_S)) * R);
  340. float x2 = (float)(Math.Cos(Math.PI / (180 / k_S)) * R);
  341. if (Slope > 0 || Slope < 0)
  342. {
  343. Hx = x0 + x2;
  344. Hy = y0 + y2;
  345. }
  346. else
  347. {
  348. Hx = 0;
  349. Hy = 0;
  350. }
  351. return true;
  352. }
  353. catch (Exception)
  354. {
  355. return false;
  356. }
  357. }
  358. //Web接口类
  359. public WebResult wr = null;
  360. //扫描周期
  361. private float cycle_time = 0;
  362. //样品台保护值
  363. public float X_Min = 0;
  364. public float X_Max = 0.13f;
  365. public float Y_Min = 0;
  366. public float Y_Max = 0.13f;
  367. public float Z_Min = 0;
  368. public float Z_Max = 0.05f;
  369. public float T_Min = -4;
  370. public float T_Max = 70;
  371. public float R_Min = -380;
  372. public float R_Max = 380;
  373. public float M_Min = 0;
  374. public float M_Max = 0.012f;
  375. public float x_center_point = 65;
  376. public float y_center_point = 65;
  377. public event ThreadStatusHandler SendThreadStatus; // 声明事件
  378. public event CutHolesStatusHandler SendCutHolesStatus; // 声明事件
  379. //定义一个全局的消息类
  380. ThreadStatusEventArgs arg = new ThreadStatusEventArgs("0-0");
  381. //全局只有一个fatorySEM
  382. static FactoryHardware factorySEM = FactoryHardware.Instance;
  383. ISEMControl iSEM = factorySEM.ISEM;
  384. //@的作用是不用转义字符\\只打一个就行
  385. const String ImageName0 = @"Straighten.tif"; //传给客户,原始图像,为拉直操作
  386. const String ImageName1 = @"FindCutPostion.tif"; //传给客户,作水平校正
  387. const String ImageName2 = @"FIBCutPostion.tif";//传给客户,找到切割点
  388. const String ImageName31 = @"FIBBefore.tif";
  389. const String ImageName32 = @"FIBAfter.tif";
  390. const String ImageName4 = @"ImageSEMTrapCP.tif";//传给客户,找到已经切割点
  391. const String ImageName5 = @"ImageSEMDegreeTrap.tif";//传给客户,水平校正
  392. const String ImageName6 = @"ImageSEMMagEnd.tif";//传给客户,测量层高
  393. const String MacoInsertPt = "GIS Insert.MLF"; //传入PT针
  394. const String MacoRetractPt = "GIS Retract.MLF"; //退出PT针
  395. const String MacoScanPic = "Scan picture.MLF";//一般质量抓图的宏
  396. const String MacoGoodPic = "Good picture.MLF";//高质量抓图的宏
  397. const String MacoExportTif = "Export TIFF.MLF";// 导出有标尺的图像,这个图像为使用工具SmartSEM工具的图像
  398. const String ElyDeposition = @"Deposition.ely"; //沉积
  399. const String ElyCrossSection = @"CrossSection.ely"; //切割
  400. const float fMin = (float)0.0000002; //单位是米
  401. public Boolean key_stop = false;
  402. String focus_path = "";
  403. String data_path = "";
  404. int m_nWorkHoleNo = -1;
  405. //测量文件
  406. private MeasureFile m_measureFile;
  407. public MeasureFile MeasureFile
  408. {
  409. get { return this.m_measureFile; }
  410. set { this.m_measureFile = value; }
  411. }
  412. //测量的切割孔
  413. private List<MeasureData.CutHole> m_cutHoles;
  414. public List<MeasureData.CutHole> cutHoles
  415. {
  416. get { return this.m_cutHoles; }
  417. set { this.m_cutHoles = value; }
  418. }
  419. //工作文件夹
  420. private string m_WorkingFolder;
  421. public string WorkingFolder
  422. {
  423. get { return this.m_WorkingFolder; }
  424. set { this.m_WorkingFolder = value; }
  425. }
  426. //程序当前路径
  427. private string m_ProgramFolder = Directory.GetCurrentDirectory();
  428. //线程状态
  429. private ThreadStatus m_ThreadStatus;
  430. public ThreadStatus TStatus
  431. {
  432. get { return this.m_ThreadStatus; }
  433. set { this.m_ThreadStatus = value; }
  434. }
  435. //测量参数
  436. private MeasureParam m_MsParam;
  437. public MeasureParam MeasParam
  438. {
  439. get { return this.m_MsParam; }
  440. set { this.m_MsParam = value; }
  441. }
  442. //构造函数
  443. public Measure(String webServerIP, String webServerPort, String webServerUrl)
  444. {
  445. MeasParam = new MeasureParam();
  446. TStatus = new ThreadStatus();
  447. cutHoles = new List<MeasureData.CutHole>();
  448. //配置远程连接
  449. wr = new WebResult(webServerIP,webServerPort,webServerUrl);
  450. }
  451. //初始化测量业务, 读测量文件,判断是否有可测试的切孔
  452. public bool InitMeas(MeasureFile a_measureFile)
  453. {
  454. m_measureFile = a_measureFile;
  455. List<CutHole> listHoles = m_measureFile.ListCutHole;
  456. foreach (CutHole h in listHoles)
  457. {
  458. if (h.SWITCH == true)
  459. {
  460. m_cutHoles.Add(h);
  461. }
  462. }
  463. if (m_cutHoles.Count == 0)
  464. return false;
  465. this.m_MsParam = m_measureFile.MParam;
  466. return true;
  467. }
  468. public void SendMsg(string step_code)
  469. {
  470. arg.Step_Code = step_code;
  471. arg.Time = DateTime.Now;
  472. SendThreadStatus(this, arg);
  473. }
  474. public void SendCutHoleMsg(string a_state, string a_holeName)
  475. {
  476. CutHolesStatusEventArgs arg = new CutHolesStatusEventArgs(a_state, a_holeName);
  477. arg.HoleName = a_holeName;
  478. arg.State = a_state;
  479. SendCutHolesStatus(this, arg);
  480. }
  481. //测量流程
  482. public void DoMeasure()
  483. {
  484. //创建线程的测量状态的更新
  485. this.TStatus.ComputeTime(THREAD_TIME_TYPE.START);
  486. if (SendThreadStatus != null)
  487. {
  488. //SendMsg("开始测量。");
  489. }
  490. LogManager.AddHardwareLog("开始测量",true);
  491. //检查硬件连接是否正常
  492. if (!ConnectHardware())
  493. {
  494. //SendMsg("连接硬件失败。");
  495. this.TStatus.ComputeTime(THREAD_TIME_TYPE.STOPPED);
  496. return;
  497. }
  498. //设置工作文件夹
  499. if (!SetWorkingFolderStr())
  500. {
  501. //SendMsg("设置工作文件夹失败");
  502. this.TStatus.ComputeTime(THREAD_TIME_TYPE.STOPPED);
  503. return;
  504. }
  505. //将停止键复位
  506. key_stop = false;
  507. //开始测量就将模式设置为SEM
  508. if (!iSEM.CmdFIBModeSEM())
  509. {
  510. LogManager.AddHardwareLog("开始切换到SEM模式失败,程序退出。", true);
  511. return;
  512. }
  513. arg.Picture_Information.Work_Status = "SEM";
  514. //自动亮度对比度
  515. if (!iSEM.SetAutoVideoBrightnessAndContrast())
  516. {
  517. LogManager.AddHardwareLog("开始设置自动亮度、对比度失败,程序退出。", true);
  518. return;
  519. }
  520. Thread.Sleep(5000);
  521. //设置扫描周期
  522. iSEM.CmdFocusRate();
  523. cycle_time = iSEM.GetCycleTime();
  524. if (cycle_time == 0)
  525. {
  526. LogManager.AddHardwareLog("获取扫描周期时间失败", true);
  527. return;
  528. }
  529. for (int i = 0; i < m_cutHoles.Count; i++)
  530. {
  531. m_nWorkHoleNo = i;
  532. arg.HoleName = m_cutHoles[i].HoleName;
  533. //创建文件夹,孔名文件夹结构
  534. //最终数据存放目录
  535. data_path = WorkingFolder + "\\" + m_cutHoles[i].HoleName;
  536. //对焦数据存放目录
  537. focus_path = WorkingFolder + "\\" + m_cutHoles[i].HoleName + "\\Focus";
  538. m_MsParam.AutoFocus.Path = focus_path;
  539. if (!Directory.Exists(focus_path))
  540. {
  541. Directory.CreateDirectory(focus_path);
  542. }
  543. //判断孔状态
  544. if(m_cutHoles[i].STATE != State.Ready)
  545. {
  546. continue;
  547. }
  548. //设置拉直的放大倍数
  549. if (!iSEM.SetMagnification(m_measureFile.MParam.Stretch_Magnification))
  550. {
  551. LogManager.AddHardwareLog("拉直放大倍数设置失败,程序退出。", true);
  552. return;
  553. }
  554. //开始循环工作
  555. if (i == 0)
  556. {
  557. m_cutHoles[i].START = DateTime.Now;
  558. //切孔操作-开始
  559. m_cutHoles[i].STATE = State.InProcess;
  560. //SendCutHoleMsg(((int)ThreadState.InProcess).ToString(), m_cutHoles[i].HoleName);
  561. SendCutHoleMsg(((int)m_cutHoles[i].STATE).ToString(), m_cutHoles[i].HoleName);
  562. //成功
  563. if(FirstHole())
  564. {
  565. //保存文件,将测量状态更改
  566. m_cutHoles[i].STATE = State.Success;
  567. m_cutHoles[i].END = DateTime.Now;
  568. m_measureFile.Save();
  569. }
  570. //失败
  571. else
  572. {
  573. m_cutHoles[i].STATE = State.Failed;
  574. m_cutHoles[i].END = DateTime.Now;
  575. m_measureFile.Save();
  576. }
  577. //切孔操作-完成
  578. //SendCutHoleMsg(((int)ThreadState.Success).ToString(), m_cutHoles[i].HoleName);
  579. SendCutHoleMsg(((int)m_cutHoles[i].STATE).ToString(), m_cutHoles[i].HoleName);
  580. }
  581. else
  582. {
  583. //其他孔测量之前的初始化
  584. if (!OtherHole_Init())
  585. {
  586. arg.Message = "当前测量点位置初始失败!";
  587. arg.State = false;
  588. SendMsg("0-1");
  589. Thread.Sleep(5000);
  590. continue;
  591. }
  592. //其他孔的测量
  593. m_cutHoles[i].START = DateTime.Now;
  594. //非第一个孔的测试
  595. //SendMsg("第" + i.ToString() + "个切孔开始测量");
  596. //切孔操作-开始
  597. //SendCutHoleMsg(((int)ThreadState.InProcess).ToString(), m_cutHoles[i].HoleName);
  598. m_cutHoles[i].STATE = State.InProcess;
  599. SendCutHoleMsg(((int)m_cutHoles[i].STATE).ToString(), m_cutHoles[i].HoleName);
  600. //其他孔的测试
  601. //成功
  602. if (OtherHole())
  603. {
  604. //保存文件,将测量状态更改
  605. m_cutHoles[i].STATE = State.Success;
  606. m_cutHoles[i].END = DateTime.Now;
  607. m_measureFile.Save();
  608. }
  609. //失败
  610. else
  611. {
  612. m_cutHoles[i].STATE = State.Failed;
  613. m_cutHoles[i].END = DateTime.Now;
  614. m_measureFile.Save();
  615. }
  616. //切孔操作-完成
  617. //SendCutHoleMsg(((int)ThreadState.Success).ToString(), m_cutHoles[i].HoleName);
  618. SendCutHoleMsg(((int)m_cutHoles[i].STATE).ToString(), m_cutHoles[i].HoleName);
  619. }
  620. if(key_stop)
  621. {
  622. m_cutHoles[i].END = DateTime.Now;
  623. m_cutHoles[i].STATE = State.Ready;
  624. m_measureFile.Save();
  625. arg.Message = "用户停止测量";
  626. SendMsg("0-0");
  627. break;
  628. }
  629. }
  630. }
  631. //检查硬件连接是否正常
  632. public bool ConnectHardware()
  633. {
  634. //返回硬件的连接状态
  635. return iSEM.ConnectStatus();
  636. }
  637. //设置工作文件夹
  638. public bool SetWorkingFolderStr()
  639. {
  640. WorkingFolder = m_measureFile.FilePath;
  641. return true;
  642. }
  643. //插入PT针
  644. public bool InsertPT()
  645. {
  646. string fn = m_ProgramFolder + "\\Macro\\" + MacoInsertPt;
  647. //SendMsg("调用宏插入PT针宏文件" + fn);
  648. iSEM.CMDMCFFilename(fn);
  649. //延时1s??
  650. Thread.Sleep(1000);
  651. return true;
  652. }
  653. //撤出PT针
  654. public bool RetractPT()
  655. {
  656. string fn = m_ProgramFolder + "\\Macro\\" + MacoRetractPt;
  657. //SendMsg("调用宏撤出PT针宏文件" + fn);
  658. iSEM.CMDMCFFilename(fn);
  659. //延时1s??
  660. Thread.Sleep(1000);
  661. return true;
  662. }
  663. //PT沉积
  664. public bool PTWork()
  665. {
  666. //执行PT沉积的ELY文件
  667. //string fn = m_ProgramFolder + "\\Macro\\" + ElyDeposition;
  668. //SendMsg("执行PT沉积Ely文件:" + fn);
  669. if (!ExcuteEly(m_MsParam.PTTemp))
  670. {
  671. return false;
  672. }
  673. //等待沉积完成
  674. while (true)
  675. {
  676. Thread.Sleep(10000);
  677. if (iSEM.GetFIBMode() == 0)
  678. {
  679. break;
  680. }
  681. }
  682. return true;
  683. }
  684. //FIB切割
  685. public bool FIBWork()
  686. {
  687. //执行PT沉积的ELY文件
  688. //string fn = m_ProgramFolder + "\\Macro\\" + ElyCrossSection;
  689. //SendMsg("执行FIB切割Ely文件:" + fn);
  690. if (!ExcuteEly(m_MsParam.FIBTemp))
  691. {
  692. return false;
  693. }
  694. //等待切割完成
  695. while (true)
  696. {
  697. Thread.Sleep(10000);
  698. if (iSEM.GetFIBMode() == 0)
  699. {
  700. break;
  701. }
  702. }
  703. return true;
  704. }
  705. //执行ELY文件的步骤
  706. public bool ExcuteEly(string a_filename)
  707. {
  708. //执行ELy文件有三个动作
  709. //1. 选择ELY文件
  710. //SendMsg("选择ELY文件");
  711. if (!iSEM.CmdFIBLoadELY(a_filename))
  712. {
  713. //SendMsg("选择ELY文件失败");
  714. return false;
  715. }
  716. Thread.Sleep(1000);
  717. //2. 确认ELY文件
  718. //SendMsg("确认ELY文件");
  719. if (!iSEM.CmdFIBEXPOSUREELY())
  720. {
  721. //SendMsg("确认ELY文件失败");
  722. return false;
  723. }
  724. Thread.Sleep(1000);
  725. //3. 执行ELY文件
  726. //SendMsg("执行ELY文件");
  727. if (!iSEM.CmdFIBSTARTELY())
  728. {
  729. //SendMsg("执行ELY文件失败");
  730. return false;
  731. }
  732. Thread.Sleep(1000);
  733. return true;
  734. }
  735. //执行自动对焦
  736. public bool ImageFocus(String step_code)
  737. {
  738. if (m_measureFile.MParam.FocusMode == 1)//手动
  739. {
  740. if (DialogResult.Yes == MessageBox.Show("图像自动对焦已完成?", "确认消息", MessageBoxButtons.YesNo))
  741. {
  742. }
  743. else
  744. {
  745. return false;
  746. }
  747. }
  748. else if(m_measureFile.MParam.FocusMode == 2)//自有自动
  749. {
  750. //郝工增加自动对焦算法
  751. //处理文件夹
  752. if (Directory.Exists(focus_path))
  753. {
  754. Directory.Delete(focus_path, true);
  755. }
  756. Thread.Sleep(3000);
  757. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  758. Thread.Sleep(1000);
  759. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  760. Thread.Sleep(1000);
  761. //再创建文件夹
  762. Directory.CreateDirectory(focus_path);
  763. //1、对焦参数类,2、输出工作距离
  764. int wd = 0;
  765. //20201015 根据当前的工作距离重新计算对焦位置
  766. float currentWd = iSEM.GetWorkingDistance();
  767. m_MsParam.AutoFocus.UP = currentWd * 1000000 - m_MsParam.AutoFocus.Step;
  768. m_MsParam.AutoFocus.Down = currentWd * 1000000 + m_MsParam.AutoFocus.Step;
  769. LogManager.AddHardwareLog("修改对焦参数", true);
  770. AutoFocus(m_MsParam.AutoFocus, out wd, step_code);
  771. //LogManager.AddHardwareLog("算法输出"+ wd.ToString(), true);
  772. //设置工作距离
  773. iSEM.SetWorkingDistance((float)(wd * 0.00000001));
  774. //设置完工作距离后必须延迟5秒
  775. Thread.Sleep(5000);
  776. //处理图片位置
  777. //File.Copy(focus_path + "\\fine\\" + wd.ToString() + ".tif", data_path + "\\" + ImageName);
  778. }
  779. else if(m_MsParam.FocusMode == 3) //客户自动
  780. {
  781. //后期和客户对接接口
  782. List<string> filenames = new List<string>();
  783. String retfilename = wr.Img_Auto_Focus(filenames);
  784. }
  785. else //蔡司自动对焦
  786. {
  787. iSEM.CmdAutoFocusCoarse();
  788. while(true)
  789. {
  790. Thread.Sleep(10000);
  791. if(0 == iSEM.GetAutoFunction())
  792. {
  793. break;
  794. }
  795. }
  796. iSEM.CmdAutoFocusFine();
  797. while (true)
  798. {
  799. Thread.Sleep(10000);
  800. if (0 == iSEM.GetAutoFunction())
  801. {
  802. break;
  803. }
  804. }
  805. }
  806. return true;
  807. }
  808. /// <summary>
  809. ///13. 自动 定位功能,沉积
  810. /// </summary>
  811. /// <returns></returns>
  812. public bool GetPoistion()
  813. {
  814. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  815. int state = 0;
  816. //1.控制SEM放大600X
  817. if (!iSEM.SetSEMVoltage(m_MsParam.Location_Voltage))
  818. {
  819. arg.Message = "电压设置" + m_MsParam.Location_Voltage.ToString("0.0") + "失败";
  820. arg.State = false;
  821. SendMsg("1-1");
  822. return false;
  823. }
  824. if (!iSEM.SetMagnification(m_measureFile.MParam.Location_Magnification))
  825. {
  826. arg.Message = "放大" + m_measureFile.MParam.Location_Magnification.ToString("0.0") + "倍失败";
  827. arg.State = false;
  828. SendMsg("1-1");
  829. return false;
  830. }
  831. arg.State = true;
  832. arg.Message = "放大" + m_measureFile.MParam.Location_Magnification.ToString("0.0") + "倍成功";
  833. SendMsg("1-1");
  834. //判断是否停止进程
  835. if (key_stop)
  836. {
  837. return false;
  838. }
  839. //2.控制SEM自动对焦、亮度、对比度
  840. if(ImageFocus("1-2"))
  841. {
  842. arg.Message = "自动对焦完成";
  843. arg.State = true;
  844. SendMsg("1-2");
  845. }
  846. else
  847. {
  848. LogManager.AddHardwareLog("设置观测点后,自动对焦失败。", true);
  849. arg.Message = "自动对焦失败";
  850. arg.State = false;
  851. SendMsg("1-2");
  852. }
  853. //判断是否停止进程
  854. if (key_stop)
  855. {
  856. return false;
  857. }
  858. //3.控制SEM拍照,找到切割位置
  859. String fileName1 = data_path + "\\" + m_cutHoles[m_nWorkHoleNo].HoleName + "_" + m_MsParam.Location_Magnification.ToString("0") + ImageName1;
  860. arg.Picture_Information.Picture_FullPath = fileName1;
  861. arg.Picture_Information.Work_Voltage = m_MsParam.Location_Voltage;
  862. arg.Picture_Information.Magnification = m_MsParam.Location_Magnification;
  863. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  864. Thread.Sleep(1000);
  865. //拍照前改变速度,延时
  866. iSEM.CmdSaveRate();
  867. cycle_time = iSEM.GetCycleTime();
  868. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  869. if (!GetImage(ImageMode.SEM, fileName1))
  870. {
  871. arg.Message = "SEM拍照失败";
  872. arg.State = false;
  873. SendMsg("1-4");
  874. return false;
  875. }
  876. arg.State = true;
  877. arg.Message = "SEM拍照成功";
  878. SendMsg("1-4");
  879. iSEM.CmdFocusRate();
  880. cycle_time = iSEM.GetCycleTime();
  881. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  882. //判断是否停止进程
  883. if (key_stop)
  884. {
  885. return false;
  886. }
  887. //判断是否为仅拍照,不是则执行FIB操作
  888. if (!m_MsParam.Is_Photograph)
  889. {
  890. //6.设置FIB拍照参数——扫描时间、束流等
  891. //7.控制FIB自动亮度、对比度
  892. if (!iSEM.CmdFIBModeFIB())
  893. {
  894. arg.Message = "FIB模式切换失败";
  895. arg.State = false;
  896. SendMsg("1-5");
  897. return false;
  898. }
  899. arg.State = true;
  900. arg.Picture_Information.Work_Status = "FIB";
  901. arg.Message = "FIB模式切换成功";
  902. SendMsg("1-5");
  903. Thread.Sleep(10000);
  904. //判断是否停止进程
  905. if (key_stop)
  906. {
  907. return false;
  908. }
  909. //8.控制FIB拍照
  910. //9.保存照片======更改路径
  911. String fileName2 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName2;
  912. arg.Picture_Information.Picture_FullPath = fileName2;
  913. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  914. Thread.Sleep(1000);
  915. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  916. Thread.Sleep(1000);
  917. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  918. Thread.Sleep(1000);
  919. //FIB的放大位数,将来会改
  920. iSEM.SetFIBMagnification(m_MsParam.FIB_Magnification);
  921. Thread.Sleep(1000);
  922. //拍照前改变速度,延时
  923. iSEM.CmdSaveRate();
  924. cycle_time = iSEM.GetCycleTime();
  925. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  926. if (!GetImage(ImageMode.FIB, fileName2))
  927. {
  928. arg.Message = "FIB拍照失败";
  929. arg.State = false;
  930. SendMsg("1-6");
  931. return false;
  932. }
  933. arg.State = true;
  934. arg.Message = "FIB拍照成功";
  935. SendMsg("1-6");
  936. iSEM.CmdFocusRate();
  937. cycle_time = iSEM.GetCycleTime();
  938. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  939. //判断是否停止进程
  940. if (key_stop)
  941. {
  942. return false;
  943. }
  944. //10.将照片传给客户,返回梯形位置坐标,及样品类型参数(是否需要PT沉积,PT坐标位置,PT宽度、PT高度、梯形上、下边及深度、扫描时间、束流、样品放大倍数1、样品放大倍数2等切割参数)
  945. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  946. {
  947. arg.Message = "样品台移动到指定位置";
  948. arg.State = true;
  949. SendMsg("1-7");
  950. x1 = iSEM.GetStageAtX();
  951. y1 = iSEM.GetStageAtY();
  952. }
  953. else
  954. {
  955. LogManager.AddHardwareLog("准备移动样品台", true);
  956. LogManager.AddHardwareLog("文件名="+fileName2, true);
  957. LogManager.AddHardwareLog("样品类型="+m_MsParam.SampleName, true);
  958. LogManager.AddHardwareLog("供应商名称="+m_MsParam.Firm, true);
  959. wr.Img_Cut_Position(fileName2, Convert.ToInt32(m_MsParam.SampleName), m_MsParam.Firm, out x1, out y1, out x2, out y2, out state);
  960. LogManager.AddHardwareLog("FIB梯形左上角和右上角位置信息= " + x1.ToString() + ", " + y1.ToString() + ", " + x2.ToString() + ", " + y2.ToString(), true);
  961. LogManager.AddHardwareLog("准备移动样品台返回状态=" + state.ToString(), true);
  962. if (state == 1)
  963. {
  964. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  965. float xc = (x1 + x2) / 2;
  966. float yc = (y1 + y2) / 2;
  967. if (!MoveToPix(xc,yc))
  968. {
  969. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  970. arg.State = false;
  971. SendMsg("1-7");
  972. return false;
  973. }
  974. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置成功";
  975. arg.State = true;
  976. SendMsg("1-7");
  977. MessageBox.Show("Web返回数据移动是否正确?\n如果错误请手动移动到指定位置后按确认", "确认消息", MessageBoxButtons.OK);
  978. }
  979. else
  980. {
  981. if (DialogResult.Yes != MessageBox.Show("网络参数State返回为0。\n是否继续操作?。", "确认消息", MessageBoxButtons.YesNo))
  982. {
  983. arg.Message = "样品台移动失败";
  984. arg.State = false;
  985. SendMsg("1-7");
  986. return false;
  987. }
  988. }
  989. x1 = iSEM.GetStageAtX();
  990. y1 = iSEM.GetStageAtY();
  991. }
  992. //判断是否停止进程
  993. if (key_stop)
  994. {
  995. return false;
  996. }
  997. //11.自动工具样品类型参数确定是否需要PT沉积
  998. if (m_MsParam.PT)
  999. {
  1000. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  1001. //第10步已经移动好位置
  1002. //2. 验证移动准确性:获取当前FIB中心位置坐标,与客户返回坐标对比,验证是否一定正确
  1003. x0 = iSEM.GetStageAtX();
  1004. y0 = iSEM.GetStageAtY();
  1005. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  1006. {
  1007. arg.Message = "目标位置(" + x0.ToString() + "," + y0.ToString() + ")移动失败";
  1008. arg.State = false;
  1009. SendMsg("1-8");
  1010. return false;
  1011. }
  1012. arg.State = true;
  1013. arg.Message = "目标位置(" + x0.ToString() + "," + y0.ToString() + ")移动成功";
  1014. SendMsg("1-8");
  1015. //判断是否停止进程
  1016. if (key_stop)
  1017. {
  1018. return false;
  1019. }
  1020. //12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  1021. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1022. {
  1023. if (!InsertPT())
  1024. {
  1025. arg.Message = "插入PT针失败";
  1026. arg.State = false;
  1027. SendMsg("1-0");
  1028. return false;
  1029. }
  1030. arg.State = true;
  1031. arg.Message = "插入PT针成功";
  1032. SendMsg("1-0");
  1033. }
  1034. //判断是否停止进程
  1035. if (key_stop)
  1036. {
  1037. return false;
  1038. }
  1039. //3. 根据坐标进行PT沉积
  1040. if (!PTWork())
  1041. {
  1042. arg.Message = "PT沉积失败";
  1043. arg.State = false;
  1044. SendMsg("1-9");
  1045. return false;
  1046. }
  1047. arg.State = true;
  1048. arg.Message = "PT沉积成功";
  1049. SendMsg("1-9");
  1050. //判断是否停止进程
  1051. if (key_stop)
  1052. {
  1053. return false;
  1054. }
  1055. //15.根据样品类型决定是否撤出PT针
  1056. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1057. {
  1058. if (!RetractPT())
  1059. {
  1060. arg.Message = "撤出PT针失败";
  1061. arg.State = false;
  1062. SendMsg("1-15");
  1063. return false;
  1064. }
  1065. arg.State = true;
  1066. arg.Message = "撤出PT针成功";
  1067. SendMsg("1-15");
  1068. }
  1069. //判断是否停止进程
  1070. if (key_stop)
  1071. {
  1072. return false;
  1073. }
  1074. }
  1075. }
  1076. //12.根据梯形坐标控制FIB调整到中心位置
  1077. //13.验证移动准确性:获取当前FIB中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1078. //以上两步已经将程序移动到11内部去做
  1079. //14.保存样品1第1号孔中心位置6轴坐标1 XYZMRT到数据库,保存客户返回值信息到数据库
  1080. float[] firstPosition = iSEM.GetStagePosition();
  1081. m_cutHoles[0].Position.X = firstPosition[0];
  1082. m_cutHoles[0].Position.Y = firstPosition[1];
  1083. m_cutHoles[0].Position.Z = firstPosition[2];
  1084. m_cutHoles[0].Position.T = firstPosition[3];
  1085. m_cutHoles[0].Position.R = firstPosition[4];
  1086. m_cutHoles[0].Position.M = firstPosition[5];
  1087. //这里要调用文件保存功能
  1088. arg.Message = "坐标1(" + firstPosition[0].ToString() + ","
  1089. + firstPosition[1].ToString() + ","
  1090. + firstPosition[2].ToString() + ","
  1091. + firstPosition[3].ToString() + ","
  1092. + firstPosition[4].ToString() + ","
  1093. + firstPosition[5].ToString() + ")";
  1094. arg.State = true;
  1095. SendMsg("1-10");
  1096. //判断是否停止进程
  1097. if (key_stop)
  1098. {
  1099. return false;
  1100. }
  1101. return true;
  1102. }
  1103. /// <summary>
  1104. /// 14. FIB切割
  1105. /// </summary>
  1106. /// <returns></returns>
  1107. public bool FIBCross()
  1108. {
  1109. int state = 0;
  1110. //切之前拍照保存,,将模式改为SEM拍照,然后再改回FIB
  1111. //切换到SEM模式
  1112. if (!iSEM.CmdFIBModeFIB())
  1113. {
  1114. return false;
  1115. }
  1116. Thread.Sleep(Convert.ToInt32(cycle_time)+1000);
  1117. String fileName31 = data_path + "\\" + m_cutHoles[m_nWorkHoleNo].HoleName + "_" + m_MsParam.FIB_Magnification.ToString() + "_" + ImageName31;
  1118. arg.Picture_Information.Picture_FullPath = fileName31;
  1119. arg.Picture_Information.Work_Status = "FIB";
  1120. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1121. Thread.Sleep(1000);
  1122. arg.Picture_Information.Magnification = m_MsParam.FIB_Magnification;
  1123. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1124. Thread.Sleep(1000);
  1125. //拍照前改变速度,延时
  1126. iSEM.CmdSaveRate();
  1127. cycle_time = iSEM.GetCycleTime();
  1128. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1129. if (!GetImage(ImageMode.SEM, fileName31))
  1130. {
  1131. arg.Message = "FIB切割前拍照SEM模式照失败";
  1132. arg.State = false;
  1133. SendMsg("1-11");
  1134. return false;
  1135. }
  1136. arg.State = true;
  1137. arg.Message = "FIB切割前拍照SEM模式照成功";
  1138. SendMsg("1-11");
  1139. iSEM.CmdFocusRate();
  1140. cycle_time = iSEM.GetCycleTime();
  1141. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1142. //判断是否停止进程
  1143. if (key_stop)
  1144. {
  1145. return false;
  1146. }
  1147. //14.自动控制FIB切割
  1148. //1.根据参数设置FIB草率时间(使图清晰),设置梯形上下边及深度、设置束流
  1149. //2.控制FIB进行切割
  1150. //以上1、2步全部用ELY文件代替
  1151. if (!FIBWork())
  1152. {
  1153. arg.Message = "FIB切割失败";
  1154. arg.State = false;
  1155. SendMsg("1-12");
  1156. return false;
  1157. }
  1158. arg.State = true;
  1159. arg.Message = "FIB切割成功";
  1160. SendMsg("1-12");
  1161. Thread.Sleep(5000);
  1162. //判断是否停止进程
  1163. if (key_stop)
  1164. {
  1165. return false;
  1166. }
  1167. //切割后会切换到SEM,所以需要再切换回FIB模式
  1168. if (!iSEM.CmdFIBModeFIB())
  1169. {
  1170. return false;
  1171. }
  1172. //3.控制FIB拍照600X
  1173. //底层没有控制方法。
  1174. //4.保存图片
  1175. String fileName32 = data_path + "\\" + m_cutHoles[m_nWorkHoleNo].HoleName + "_" + m_MsParam.FIB_Magnification.ToString() + "_" + ImageName32;
  1176. arg.Picture_Information.Picture_FullPath = fileName32;
  1177. arg.Picture_Information.Work_Status = "FIB";
  1178. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1179. Thread.Sleep(1000);
  1180. arg.Picture_Information.Magnification = m_MsParam.FIB_Magnification;
  1181. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1182. Thread.Sleep(1000);
  1183. //拍照前改变速度,延时
  1184. iSEM.CmdSaveRate();
  1185. cycle_time = iSEM.GetCycleTime();
  1186. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1187. if (!GetImage(ImageMode.FIB, fileName32))
  1188. {
  1189. arg.Message = "FIB切割后拍照失败";
  1190. arg.State = false;
  1191. SendMsg("1-13");
  1192. return false;
  1193. }
  1194. arg.State = true;
  1195. arg.Message = "FIB切割后拍照成功";
  1196. SendMsg("1-13");
  1197. iSEM.CmdFocusRate();
  1198. cycle_time = iSEM.GetCycleTime();
  1199. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1200. //判断是否停止进程
  1201. if (key_stop)
  1202. {
  1203. return false;
  1204. }
  1205. //5.验证切割准确性:与切割前对比,如果对比误差大,则停止自动执行,进行报警
  1206. wr.Img_Cut_Success(fileName31, fileName32, out state);
  1207. if(state == 0)
  1208. {
  1209. if (DialogResult.Yes != MessageBox.Show("网络参数State返回为0。\n是否继续操作?。", "确认消息", MessageBoxButtons.YesNo))
  1210. {
  1211. arg.Message = "FIB切割校验失败";
  1212. arg.State = false;
  1213. SendMsg("1-14");
  1214. return false;
  1215. }
  1216. }
  1217. arg.State = true;
  1218. arg.Message = "FIB切割校验成功";
  1219. SendMsg("1-14");
  1220. //判断是否停止进程
  1221. if (key_stop)
  1222. {
  1223. return false;
  1224. }
  1225. //6.设置FIB解冻:先读取状态,如果冻结状态则进行解冻
  1226. //拍完照自动解冻,这里不做处理。
  1227. return true;
  1228. }
  1229. /// <summary>
  1230. /// 16.自动调整SEM找到切割位置,这里不是简单的拉直旋转
  1231. /// </summary>
  1232. /// <returns></returns>
  1233. public bool FindCross()
  1234. {
  1235. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  1236. int state = 0;
  1237. //切换到SEM模式
  1238. if (!iSEM.CmdFIBModeSEM())
  1239. {
  1240. arg.Message = "SEM模式切换失败";
  1241. arg.State = false;
  1242. SendMsg("1-16");
  1243. return false;
  1244. }
  1245. arg.State = true;
  1246. arg.Message = "SEM模式切换成功";
  1247. SendMsg("1-16");
  1248. arg.Picture_Information.Work_Status = "SEM";
  1249. Thread.Sleep(10000);
  1250. //判断是否停止进程
  1251. if (key_stop)
  1252. {
  1253. return false;
  1254. }
  1255. //1.控制SEM放大到300倍
  1256. if (!iSEM.SetSEMVoltage(m_MsParam.Location_Voltage))
  1257. {
  1258. arg.Message = "电压设置" + m_MsParam.Location_Voltage.ToString("0.0") + "失败";
  1259. arg.State = false;
  1260. SendMsg("1-17");
  1261. return false;
  1262. }
  1263. if (!iSEM.SetMagnification(m_measureFile.MParam.Location_Magnification/2))
  1264. {
  1265. arg.Message = "放大" + (m_measureFile.MParam.Location_Magnification / 2).ToString("0.0") + "倍失败";
  1266. arg.State = false;
  1267. SendMsg("1-17");
  1268. return false;
  1269. }
  1270. arg.State = true;
  1271. arg.Message = "放大" + (m_measureFile.MParam.Location_Magnification/2).ToString("0.0") + "倍成功";
  1272. SendMsg("1-17");
  1273. //判断是否停止进程
  1274. if (key_stop)
  1275. {
  1276. return false;
  1277. }
  1278. //2.控制SEM自动对焦、亮度、对比度-接口,,将自动对焦提出单独函数调用
  1279. if (ImageFocus("1-18"))
  1280. {
  1281. arg.Message = "自动对焦完成";
  1282. arg.State = true;
  1283. SendMsg("1-18");
  1284. }
  1285. else
  1286. {
  1287. arg.Message = "自动对焦失败";
  1288. arg.State = false;
  1289. SendMsg("1-18");
  1290. }
  1291. Thread.Sleep(5000);
  1292. //判断是否停止进程
  1293. if (key_stop)
  1294. {
  1295. return false;
  1296. }
  1297. //3.控制SEM拍照
  1298. String fileName4 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName4;
  1299. arg.Picture_Information.Picture_FullPath = fileName4;
  1300. arg.Picture_Information.Work_Status = "SEM";
  1301. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1302. Thread.Sleep(1000);
  1303. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  1304. Thread.Sleep(1000);
  1305. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1306. Thread.Sleep(1000);
  1307. //拍照前改变速度,延时
  1308. iSEM.CmdSaveRate();
  1309. cycle_time = iSEM.GetCycleTime();
  1310. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1311. if (!GetImage(ImageMode.SEM, fileName4))
  1312. {
  1313. arg.State = false;
  1314. arg.Message = "SEM拍照失败";
  1315. SendMsg("1-19");
  1316. return false;
  1317. }
  1318. arg.State = true;
  1319. arg.Message = "SEM拍照成功";
  1320. SendMsg("1-19");
  1321. iSEM.CmdFocusRate();
  1322. cycle_time = iSEM.GetCycleTime();
  1323. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1324. //判断是否停止进程
  1325. if (key_stop)
  1326. {
  1327. return false;
  1328. }
  1329. //4.将照片传给客户,获取偏移坐标,以及偏移角度
  1330. //5.根据坐标控制SEM移动到切孔位置,居中
  1331. //这里是否用到拉直操作,我没有加
  1332. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  1333. {
  1334. arg.State = true;
  1335. arg.Message = "样品台移动到指定位置";
  1336. SendMsg("1-20");
  1337. x1 = iSEM.GetStageAtX();
  1338. y1 = iSEM.GetStageAtY();
  1339. }
  1340. else
  1341. {
  1342. LogManager.AddHardwareLog("准备移动样品台", true);
  1343. LogManager.AddHardwareLog("文件名=" + fileName4, true);
  1344. wr.Img_Trapezoid_Top_Center_Position(fileName4,out x2, out y2, out state);
  1345. LogManager.AddHardwareLog("梯形中心点返回数据=" + x2.ToString() + ", " + y2.ToString(), true);
  1346. LogManager.AddHardwareLog("准备移动样品台返回状态=" + state.ToString(), true);
  1347. if (state == 1)
  1348. {
  1349. //Straightening(angle, 65, 65, x1, y1, ref x2, ref y2);
  1350. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  1351. if (!MoveToPix(x2, y2))
  1352. {
  1353. arg.State = false;
  1354. arg.Message = "移动到新(" + x2.ToString() + "," + y2.ToString() + ")位置失败";
  1355. SendMsg("1-20");
  1356. return false;
  1357. }
  1358. //判断是否移动完成
  1359. while (true)
  1360. {
  1361. Thread.Sleep(5000);
  1362. if (iSEM.GetStageIs() == 0)
  1363. {
  1364. break;
  1365. }
  1366. }
  1367. arg.State = true;
  1368. arg.Message = "移动到新(" + x2.ToString() + "," + y2.ToString() + ")位置失败";
  1369. SendMsg("1-20");
  1370. MessageBox.Show("Web返回数据移动是否正确?\n如果错误请手动移动到指定位置后按确认", "确认消息", MessageBoxButtons.OK);
  1371. }
  1372. else
  1373. {
  1374. if (DialogResult.Yes != MessageBox.Show("网络参数State返回为0。\n是否继续操作?。", "确认消息", MessageBoxButtons.YesNo))
  1375. {
  1376. arg.Message = "样品台移动失败";
  1377. arg.State = false;
  1378. SendMsg("1-20");
  1379. return false;
  1380. }
  1381. }
  1382. x1 = iSEM.GetStageAtX();
  1383. y1 = iSEM.GetStageAtY();
  1384. }
  1385. //判断是否停止进程
  1386. if (key_stop)
  1387. {
  1388. return false;
  1389. }
  1390. //6.验证移动准确性:获取当前SEM中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1391. x0 = iSEM.GetStageAtX();
  1392. y0 = iSEM.GetStageAtY();
  1393. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  1394. {
  1395. arg.Message = "位置校验失败";
  1396. arg.State = false;
  1397. SendMsg("1-21");
  1398. return false;
  1399. }
  1400. arg.State = true;
  1401. arg.Message = "位置校验成功";
  1402. SendMsg("1-21");
  1403. //判断是否停止进程
  1404. if (key_stop)
  1405. {
  1406. return false;
  1407. }
  1408. return true;
  1409. }
  1410. /// <summary>
  1411. /// 17.自动控制SEM拍截面照
  1412. /// </summary>
  1413. /// <returns></returns>
  1414. public bool ShotSection(ref String image18)
  1415. {
  1416. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  1417. int state = 0;
  1418. //1、放大6000倍,放大倍数参数photograph_
  1419. if (!iSEM.SetSEMVoltage(m_MsParam.Photograph_Voltage))
  1420. {
  1421. arg.Message = "电压设置" + m_MsParam.Photograph_Voltage.ToString("0.0") + "失败";
  1422. arg.State = false;
  1423. SendMsg("1-22");
  1424. return false;
  1425. }
  1426. if (!iSEM.SetMagnification(m_MsParam.Photograph_Magnification))
  1427. {
  1428. arg.Message = "放大倍数调整失败";
  1429. arg.State = false;
  1430. SendMsg("1-22");
  1431. return false;
  1432. }
  1433. arg.Message = "放大倍数调整成功";
  1434. arg.State = true;
  1435. SendMsg("1-22");
  1436. //判断是否停止进程
  1437. if (key_stop)
  1438. {
  1439. return false;
  1440. }
  1441. //2.控制SEM自动对焦、亮度、对比度-接口
  1442. if (ImageFocus("1-23"))
  1443. {
  1444. arg.Message = "自动对焦完成";
  1445. arg.State = true;
  1446. SendMsg("1-23");
  1447. }
  1448. else
  1449. {
  1450. arg.Message = "自动对焦失败";
  1451. arg.State = false;
  1452. SendMsg("1-23");
  1453. }
  1454. //判断是否停止进程
  1455. if (key_stop)
  1456. {
  1457. return false;
  1458. }
  1459. //3、设置SEM补偿角度
  1460. if (m_MsParam.Is_Photograph == false)
  1461. {
  1462. if (!TiltCorrection(m_measureFile.MParam.Correction_Angle))
  1463. {
  1464. arg.Message = "设置SEM进行角度补偿" + m_measureFile.MParam.Correction_Angle.ToString("0") + "度失败";
  1465. arg.State = false;
  1466. SendMsg("1-24");
  1467. return false;
  1468. }
  1469. arg.State = true;
  1470. arg.Message = "设置SEM进行角度补偿" + m_measureFile.MParam.Correction_Angle.ToString("0") + "度成功";
  1471. SendMsg("1-24");
  1472. Thread.Sleep(5000);
  1473. //判断是否停止进程
  1474. if (key_stop)
  1475. {
  1476. return false;
  1477. }
  1478. }
  1479. //4、拍照,5、保存照片
  1480. String fileName5 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName5;
  1481. arg.Picture_Information.Picture_FullPath = fileName5;
  1482. arg.Picture_Information.Work_Status = "SEM";
  1483. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1484. Thread.Sleep(1000);
  1485. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  1486. Thread.Sleep(1000);
  1487. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1488. Thread.Sleep(1000);
  1489. //拍照前改变速度,延时
  1490. iSEM.CmdSaveRate();
  1491. cycle_time = iSEM.GetCycleTime();
  1492. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1493. if (!GetImage(ImageMode.SEM, fileName5))
  1494. {
  1495. arg.Message = "SEM拍照失败";
  1496. arg.State = false;
  1497. SendMsg("1-25");
  1498. return false;
  1499. }
  1500. arg.State = true;
  1501. arg.Message = "SEM拍照成功";
  1502. SendMsg("1-25");
  1503. iSEM.CmdFocusRate();
  1504. cycle_time = iSEM.GetCycleTime();
  1505. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1506. //判断是否停止进程
  1507. if (key_stop)
  1508. {
  1509. return false;
  1510. }
  1511. //6、获取偏移坐标,7、移动到位
  1512. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  1513. {
  1514. arg.Message = "样品台移动到指定位置";
  1515. arg.State = true;
  1516. SendMsg("1-26");
  1517. x1 = iSEM.GetStageAtX();
  1518. y1 = iSEM.GetStageAtY();
  1519. }
  1520. else
  1521. {
  1522. //8,计算切割面区域偏移角度及方向
  1523. float degree = 0;
  1524. int direction = 0;
  1525. wr.Img_Center_Position_OffsetAngle_Direction(fileName5, out degree, out direction, out state);
  1526. LogManager.AddHardwareLog("样品" + m_nWorkHoleNo.ToString() + "计算切割面区域的角度=" + degree.ToString(), true);
  1527. //接口返回像素,*pixelsize,得到坐标点。判断移动方式
  1528. if (state == 1)
  1529. {
  1530. //梯形角度
  1531. iSEM.SetScanRotationOn();
  1532. Thread.Sleep(1000);
  1533. iSEM.SetScanRotation(degree);
  1534. Thread.Sleep(1000);
  1535. //if (direction == 2)
  1536. //{
  1537. // degree = -degree;
  1538. //}
  1539. ////旋转R轴
  1540. //iSEM.SetStageGotoR(degree);
  1541. //while (true)
  1542. //{
  1543. // Thread.Sleep(5000);
  1544. // if (iSEM.GetStageIs() == 0)
  1545. // {
  1546. // break;
  1547. // }
  1548. //}
  1549. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  1550. //if (!MoveToPix(x1, y1))
  1551. //{
  1552. // arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  1553. // arg.State = false;
  1554. // SendMsg("1-26");
  1555. // return false;
  1556. //}
  1557. ////判断是否移动完成
  1558. //while (true)
  1559. //{
  1560. // Thread.Sleep(5000);
  1561. // if (iSEM.GetStageIs() == 0)
  1562. // {
  1563. // break;
  1564. // }
  1565. //}
  1566. arg.State = true;
  1567. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  1568. SendMsg("1-26");
  1569. MessageBox.Show("Web返回数据移动是否正确?\n如果错误请手动移动到指定位置后按确认", "确认消息", MessageBoxButtons.OK);
  1570. }
  1571. else
  1572. {
  1573. if (DialogResult.Yes != MessageBox.Show("网络参数State返回为0。\n是否继续操作?。", "确认消息", MessageBoxButtons.YesNo))
  1574. {
  1575. arg.Message = "样品台移动失败";
  1576. arg.State = false;
  1577. SendMsg("1-26");
  1578. return false;
  1579. }
  1580. }
  1581. x1 = iSEM.GetStageAtX();
  1582. y1 = iSEM.GetStageAtY();
  1583. }
  1584. //判断是否停止进程
  1585. if (key_stop)
  1586. {
  1587. return false;
  1588. }
  1589. //8、验证移动准确性
  1590. x0 = iSEM.GetStageAtX();
  1591. y0 = iSEM.GetStageAtY();
  1592. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  1593. {
  1594. arg.Message = "位置校验失败";
  1595. arg.State = false;
  1596. SendMsg("1-27");
  1597. return false;
  1598. }
  1599. arg.State = true;
  1600. arg.Message = "位置校验成功";
  1601. SendMsg("1-27");
  1602. //判断是否停止进程
  1603. if (key_stop)
  1604. {
  1605. return false;
  1606. }
  1607. //9、-10不操作
  1608. //11、自动对焦。与之前操作相同
  1609. if (ImageFocus("1-28"))
  1610. {
  1611. arg.Message = "自动对焦完成";
  1612. arg.State = true;
  1613. SendMsg("1-28");
  1614. }
  1615. else
  1616. {
  1617. arg.Message = "自动对焦失败";
  1618. arg.State = false;
  1619. SendMsg("1-28");
  1620. }
  1621. //判断是否停止进程
  1622. if (key_stop)
  1623. {
  1624. return false;
  1625. }
  1626. Thread.Sleep(2000);
  1627. //12拍照
  1628. String fileName6 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1629. arg.Picture_Information.Picture_FullPath = fileName6;
  1630. arg.Picture_Information.Work_Status = "SEM";
  1631. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1632. Thread.Sleep(1000);
  1633. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  1634. Thread.Sleep(1000);
  1635. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1636. Thread.Sleep(1000);
  1637. //拍照前改变速度,延时
  1638. iSEM.CmdSaveRate();
  1639. cycle_time = iSEM.GetCycleTime();
  1640. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1641. if (!GetImage(ImageMode.SEM, fileName6))
  1642. {
  1643. arg.Message = "SEM拍照失败";
  1644. arg.State = false;
  1645. SendMsg("1-29");
  1646. return false;
  1647. }
  1648. arg.State = true;
  1649. image18 = fileName6;
  1650. arg.Message = "SEM拍照成功";
  1651. SendMsg("1-29");
  1652. iSEM.CmdFocusRate();
  1653. cycle_time = iSEM.GetCycleTime();
  1654. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  1655. //判断是否停止进程
  1656. if (key_stop)
  1657. {
  1658. return false;
  1659. }
  1660. //华为程序还没有做出来
  1661. //14光束复位和Rotation关闭开关
  1662. iSEM.SetScanRotationOff();
  1663. Thread.Sleep(1000);
  1664. iSEM.SetBeamShiftX(0);
  1665. Thread.Sleep(1000);
  1666. iSEM.SetBeamShiftY(0);
  1667. Thread.Sleep(3000);
  1668. arg.Message = "光束复位成功";
  1669. arg.State = true;
  1670. SendMsg("1-30");
  1671. //判断是否停止进程
  1672. if (key_stop)
  1673. {
  1674. return false;
  1675. }
  1676. return true;
  1677. }
  1678. //第一个孔的测试过程
  1679. public bool FirstHole()
  1680. {
  1681. MeasureData.CutHole firstHole = m_cutHoles[0];
  1682. //1.自动化流程-每个点都需要补偿54度
  1683. if (m_MsParam.Is_Photograph == false)
  1684. {
  1685. if (!TiltCorrection(54.0f))
  1686. {
  1687. arg.Message = "设置SEM进行角度补偿54度失败";
  1688. arg.State = false;
  1689. SendMsg("1-3");
  1690. return false;
  1691. }
  1692. Thread.Sleep(2000);
  1693. arg.State = true;
  1694. arg.Message = "设置SEM进行角度补偿54度成功";
  1695. SendMsg("1-3");
  1696. //判断是否停止进程
  1697. if (key_stop)
  1698. {
  1699. return false;
  1700. }
  1701. }
  1702. //2.移动样品台到第一个观测点位置,先移动R轴,再移动XY轴
  1703. if (!iSEM.SetStageGotoR(firstHole.Position.R))
  1704. {
  1705. LogManager.AddHardwareLog("样品台R轴移动失败。", true);
  1706. return false;
  1707. }
  1708. while (true)
  1709. {
  1710. Thread.Sleep(5000);
  1711. if (iSEM.GetStageIs() == 0)
  1712. {
  1713. break;
  1714. }
  1715. }
  1716. //判断是否停止进程
  1717. if (key_stop)
  1718. {
  1719. return false;
  1720. }
  1721. //移动XY轴
  1722. if(!iSEM.MoveStageXY(firstHole.Position.X, firstHole.Position.Y))
  1723. {
  1724. LogManager.AddHardwareLog("样品台XY轴移动失败。", true);
  1725. return false;
  1726. }
  1727. while (true)
  1728. {
  1729. Thread.Sleep(5000);
  1730. if (iSEM.GetStageIs() == 0)
  1731. {
  1732. break;
  1733. }
  1734. }
  1735. //判断是否停止进程
  1736. if (key_stop)
  1737. {
  1738. return false;
  1739. }
  1740. //3.拉直操作
  1741. if (!Straighten_Handle())
  1742. {
  1743. LogManager.AddHardwareLog("拉直操作失败。", true);
  1744. return false;
  1745. }
  1746. //判断是否停止进程
  1747. if (key_stop)
  1748. {
  1749. return false;
  1750. }
  1751. //13. 自动 定位功能
  1752. if (!GetPoistion())
  1753. {
  1754. LogManager.AddHardwareLog("自动定位失败,程序退出。", true);
  1755. return false;
  1756. }
  1757. //判断是否停止进程
  1758. if (key_stop)
  1759. {
  1760. return false;
  1761. }
  1762. //14.自动控制FIB切割
  1763. if (m_MsParam.Is_Photograph == false)
  1764. {
  1765. if (!FIBCross())
  1766. {
  1767. return false;
  1768. }
  1769. }
  1770. //判断是否停止进程
  1771. if (key_stop)
  1772. {
  1773. return false;
  1774. }
  1775. // 16.找到切割位置
  1776. if (m_MsParam.Is_Photograph == false)
  1777. {
  1778. if (!FindCross())
  1779. {
  1780. return false;
  1781. }
  1782. //判断是否停止进程
  1783. if (key_stop)
  1784. {
  1785. return false;
  1786. }
  1787. }
  1788. //将过程17最后的拍照图片提出给18步进行调用
  1789. String img18 = "";
  1790. //17.自动控制SEM拍截面照
  1791. if (!ShotSection(ref img18))
  1792. {
  1793. return false;
  1794. }
  1795. //判断是否停止进程
  1796. if (key_stop)
  1797. {
  1798. return false;
  1799. }
  1800. //18.自动层高分析
  1801. if (m_MsParam.Is_Photograph == false)
  1802. {
  1803. float size = iSEM.GetPixelSize();
  1804. float mag = iSEM.GetMagnification();
  1805. int state = 1;
  1806. //wr.Img_Measure_Size(img18, mag, size, out state);
  1807. if (state == 0)
  1808. {
  1809. arg.Message = "测量尺寸失败";
  1810. arg.State = false;
  1811. SendMsg("1-31");
  1812. return false;
  1813. }
  1814. arg.State = true;
  1815. arg.Message = "测量尺寸成功";
  1816. SendMsg("1-31");
  1817. }
  1818. #region 测试程序
  1819. //最后保存测量数据-测量文件、测量状态,m_cutHoles[i].STATE!=MeasureData.CutHole.State.Success.
  1820. //中间停止更新状态为Unmeasured,过程中return false时,更新状态为failed,保存测量文件
  1821. //if (DialogResult.Yes == MessageBox.Show("图像拍摄已完成?", "确认消息", MessageBoxButtons.YesNo))
  1822. //{
  1823. // SendMsg("拍摄照片完成");
  1824. // //return true;
  1825. //}
  1826. //else
  1827. //{
  1828. // SendMsg("拍摄照片失败");
  1829. // return false;
  1830. //}
  1831. //SendMsg("图像校正36度");
  1832. ////3.设置SEM角度补偿cos36度
  1833. //if (!TiltCorrection(36))
  1834. //{
  1835. // return false;
  1836. //}
  1837. //Thread.Sleep(3000);
  1838. //string fn;
  1839. //用宏抓一般图
  1840. //Thread.Sleep(5000);
  1841. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1842. //SendMsg("用宏抓一般图" + fn);
  1843. //iSEM.CMDMCFFilename(fn);
  1844. ////延时1s??
  1845. //Thread.Sleep(5000);
  1846. ////用宏抓好图
  1847. //fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  1848. //SendMsg("用宏抓好图" + fn);
  1849. //iSEM.CMDMCFFilename(fn);
  1850. ////延时1s??
  1851. //Thread.Sleep(30000);
  1852. //Thread.Sleep(10000);
  1853. //WorkingFolder = m_measureFile.FilePath;
  1854. //String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1855. //SendMsg("SEM拍照存储到" + fileName6);
  1856. //if (!GetImage(ImageMode.SEM, fileName6))
  1857. //{
  1858. // SendMsg("SEM拍照失败");
  1859. // return false;
  1860. //}
  1861. ////用宏抓标志图
  1862. //fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  1863. //SendMsg("用宏抓标志图" + fn);
  1864. //iSEM.CMDMCFFilename(fn);
  1865. ////延时1s??
  1866. //Thread.Sleep(30000);
  1867. //Thread.Sleep(5000);
  1868. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1869. //SendMsg("用宏抓一般图" + fn);
  1870. //iSEM.CMDMCFFilename(fn);
  1871. ////延时1s??
  1872. //Thread.Sleep(5000);
  1873. //SendMsg("所有图结束");
  1874. ////17.自动控制SEM拍截面照
  1875. //{
  1876. // //1.控制SEM放大到指定参数大小范围,6000x
  1877. // if (!iSEM.SetMagnification(6000))
  1878. // {
  1879. // return false;
  1880. // }
  1881. // //2.控制SEM自动对焦、消像散、亮度、对比度
  1882. // if (!MeasParam.FocusMode)
  1883. // {
  1884. // //弹出手动对焦的窗口
  1885. // }
  1886. // else
  1887. // {
  1888. // //调用自动对焦模块
  1889. // }
  1890. // //3.设置SEM角度补偿cos36度
  1891. // if (!TiltCorrection(36))
  1892. // {
  1893. // return false;
  1894. // }
  1895. // //4.控制SEM拍照
  1896. // //5.保存照片4
  1897. // String fileName5 = WorkingFolder + ImageName5;
  1898. // if (!GetImage(ImageMode.SEM, fileName5))
  1899. // {
  1900. // return false;
  1901. // }
  1902. // //6.将照片传给客户,获取偏移坐标
  1903. // float x6 = 0, y6 = 0;
  1904. // float angle1 = 0;
  1905. // float mage = 10000;
  1906. // //7.根据坐标控制SEM移动到分析位置
  1907. // if (!iSEM.MoveStageXY(x6, y6))
  1908. // {
  1909. // return false;
  1910. // }
  1911. // //8.验证移动准确性:获取当前SEM中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1912. // float x7 = iSEM.GetStageAtX();
  1913. // float y7 = iSEM.GetStageAtY();
  1914. // if (Math.Abs(x6 - x7) > fMin && Math.Abs(y6 - y7) > fMin)
  1915. // {
  1916. // return false;
  1917. // }
  1918. // //9.控制SEM平行校正,并记录校正前初始值
  1919. // float foldAnagle = iSEM.GetScanRotation();
  1920. // if (foldAnagle == float.NaN)
  1921. // {
  1922. // return false;
  1923. // }
  1924. // if (!iSEM.SetScanRotation(angle1))
  1925. // {
  1926. // return false;
  1927. // }
  1928. // //10.控制SEM放大到指定参数大小范围
  1929. // if (!iSEM.SetMagnification(mage))
  1930. // {
  1931. // return false;
  1932. // }
  1933. // //11.控制SEM自动对焦、消像散、亮度、对比度
  1934. // if (!MeasParam.FocusMode)
  1935. // {
  1936. // //弹出手动对焦的窗口
  1937. // }
  1938. // else
  1939. // {
  1940. // //调用自动对焦模块
  1941. // }
  1942. // //12.控制SEM对分析位置拍照
  1943. // String fileName6 = WorkingFolder + ImageName6;
  1944. // if (!GetImage(ImageMode.SEM, fileName6))
  1945. // {
  1946. // return false;
  1947. // }
  1948. // //13.保存照片
  1949. // //14.控制SEM取消电子束校正,回到初始值
  1950. // if (!iSEM.SetScanRotation(foldAnagle))
  1951. // {
  1952. // return false;
  1953. // }
  1954. //}
  1955. ////18.自动层高分析
  1956. //{
  1957. // //1.获取SEM Pixel Size给客户传入参数
  1958. // if (iSEM.GetPixelSize() == float.NaN)
  1959. // {
  1960. // return false;
  1961. // }
  1962. // //2.将照片传给客户,客户进行层高分析(返回分析后的图像、相对坐标、分辨率、各层编号以及各层对应的层高数据),如果客户自行出分析报告则无需返回数据
  1963. //}
  1964. ////19.自动能谱分析
  1965. //{
  1966. // //1. 确定能谱位置
  1967. // //2. 控制牛津打能谱
  1968. // //3. 能谱分析——面扫+线扫描
  1969. //}
  1970. #endregion
  1971. return true;
  1972. }
  1973. //非第一个孔的测试过程
  1974. public bool OtherHole()
  1975. {
  1976. //首先默认为SEM模式
  1977. arg.Picture_Information.Work_Status = "SEM";
  1978. if (m_MsParam.Is_Photograph == false)
  1979. {
  1980. if (!TiltCorrection(54.0f))
  1981. {
  1982. arg.Message = "设置SEM进行角度补偿54度失败";
  1983. arg.State = false;
  1984. SendMsg("1-3");
  1985. return false;
  1986. }
  1987. Thread.Sleep(5000);
  1988. arg.State = true;
  1989. arg.Message = "设置SEM进行角度补偿54度成功";
  1990. SendMsg("1-3");
  1991. //判断是否停止进程
  1992. if (key_stop)
  1993. {
  1994. return false;
  1995. }
  1996. }
  1997. else
  1998. {
  1999. Thread.Sleep(2000);
  2000. arg.State = true;
  2001. arg.Message = "设置SEM进行角度补偿54度成功";
  2002. SendMsg("1-3");
  2003. }
  2004. ////12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  2005. //if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  2006. //{
  2007. // if (!InsertPT())
  2008. // {
  2009. // arg.Message = "插入PT针失败";
  2010. // SendMsg("1-0");
  2011. // return false;
  2012. // }
  2013. // arg.Message = "插入PT针成功";
  2014. // SendMsg("1-0");
  2015. //}
  2016. ////判断是否停止进程
  2017. //if (key_stop)
  2018. //{
  2019. // return false;
  2020. //}
  2021. //13. 自动 定位功能
  2022. if (!GetPoistion())
  2023. {
  2024. return false;
  2025. }
  2026. //判断是否停止进程
  2027. if (key_stop)
  2028. {
  2029. return false;
  2030. }
  2031. //14.自动控制FIB切割
  2032. if (m_MsParam.Is_Photograph == false)
  2033. {
  2034. if (!FIBCross())
  2035. {
  2036. return false;
  2037. }
  2038. }
  2039. //判断是否停止进程
  2040. if (key_stop)
  2041. {
  2042. return false;
  2043. }
  2044. // 16.找到切割位置
  2045. if (m_MsParam.Is_Photograph == false)
  2046. {
  2047. if (!FindCross())
  2048. {
  2049. return false;
  2050. }
  2051. //判断是否停止进程
  2052. if (key_stop)
  2053. {
  2054. return false;
  2055. }
  2056. }
  2057. //将过程17最后的拍照图片提出给18步进行调用
  2058. String img18 = "";
  2059. //17.自动控制SEM拍截面照
  2060. if (!ShotSection(ref img18))
  2061. {
  2062. return false;
  2063. }
  2064. //判断是否停止进程
  2065. if (key_stop)
  2066. {
  2067. return false;
  2068. }
  2069. //18.自动层高分析
  2070. if (m_MsParam.Is_Photograph == false)
  2071. {
  2072. float size = iSEM.GetPixelSize();
  2073. float mag = iSEM.GetMagnification();
  2074. int state = 1;
  2075. //wr.Img_Measure_Size(img18, mag, size, out state);
  2076. if (state == 0)
  2077. {
  2078. arg.Message = "测量尺寸失败";
  2079. SendMsg("1-31");
  2080. return false;
  2081. }
  2082. arg.Message = "测量尺寸成功";
  2083. SendMsg("1-31");
  2084. }
  2085. #region 测试程序
  2086. //string fn;
  2087. //用宏抓一般图
  2088. //Thread.Sleep(5000);
  2089. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  2090. //SendMsg("用宏抓一般图" + fn);
  2091. //iSEM.CMDMCFFilename(fn);
  2092. ////延时1s??
  2093. //Thread.Sleep(5000);
  2094. ////用宏抓好图
  2095. //fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  2096. //SendMsg("用宏抓好图" + fn);
  2097. //iSEM.CMDMCFFilename(fn);
  2098. ////延时1s??
  2099. //Thread.Sleep(30000);
  2100. //Thread.Sleep(10000);
  2101. //WorkingFolder = m_measureFile.FilePath;
  2102. //String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  2103. //SendMsg("SEM拍照存储到" + fileName6);
  2104. //if (!GetImage(ImageMode.SEM, fileName6))
  2105. //{
  2106. // SendMsg("SEM拍照失败");
  2107. // return false;
  2108. //}
  2109. ////用宏抓标志图
  2110. //fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  2111. //SendMsg("用宏抓标志图" + fn);
  2112. //iSEM.CMDMCFFilename(fn);
  2113. ////延时1s??
  2114. //Thread.Sleep(30000);
  2115. ////恢复简单抓图
  2116. //Thread.Sleep(5000);
  2117. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  2118. //SendMsg("用宏抓一般图" + fn);
  2119. //iSEM.CMDMCFFilename(fn);
  2120. //SendMsg("所有图结束");
  2121. #endregion
  2122. return true;
  2123. }
  2124. //测试完第一个孔后的样品台初始化操作。
  2125. //再调用CommonWork执行非第一个孔的测试过程
  2126. public bool OtherHole_Init()
  2127. {
  2128. CutHole currentHole = m_cutHoles[m_nWorkHoleNo];
  2129. #region 1. 初始化
  2130. //1. 设置FIB解冻:先读取状态,如果冻结则进行解冻
  2131. //读取图像冻结状态
  2132. float ffrozen = iSEM.GetImageFrozen();
  2133. if (ffrozen == float.NaN)
  2134. {
  2135. return false;
  2136. }
  2137. if (m_MsParam.Is_Photograph == false)
  2138. {
  2139. if (DialogResult.Yes == MessageBox.Show("样品台T轴可以旋转恢复到水平吗?", "确认消息", MessageBoxButtons.YesNo))
  2140. {
  2141. //SendMsg("样品台恢复到水平");
  2142. //return true;
  2143. LogManager.AddHardwareLog("样品台T轴旋转恢复到水平位置", true);
  2144. }
  2145. else
  2146. {
  2147. //SendMsg("禁止样品台恢复到水平");
  2148. return false;
  2149. }
  2150. //判断是否停止进程
  2151. if (key_stop)
  2152. {
  2153. return false;
  2154. }
  2155. //2. 调节样品1号孔的样品台6轴坐标
  2156. //3. 将样品1号孔坐标2存入数据库
  2157. //4. 控制样品台T轴归0,R轴变为坐标1、Z/M轴作为坐标2保存不变
  2158. if (!iSEM.SetStageGotoT(0))
  2159. {
  2160. return false;
  2161. }
  2162. while (true)
  2163. {
  2164. Thread.Sleep(10000);
  2165. if (iSEM.GetStageIs() == 0)
  2166. {
  2167. break;
  2168. }
  2169. }
  2170. //判断是否停止进程
  2171. if (key_stop)
  2172. {
  2173. return false;
  2174. }
  2175. //54度
  2176. iSEM.SetTiltAngleOff();
  2177. }
  2178. //5. 获取光镜2号孔XY坐标
  2179. //SendMsg("移动到"+m_nWorkHoleNo+"号孔位置:(" + currentHole.Position.X.ToString()+","+currentHole.Position.Y.ToString()+")");
  2180. //6. 根据光镜坐标控制样品台移动
  2181. if (!iSEM.SetStageGotoR(currentHole.Position.R))
  2182. {
  2183. //SendMsg("移动失败");
  2184. return false;
  2185. }
  2186. LogManager.AddHardwareLog("移动R轴到="+ currentHole.Position.R.ToString(), true);
  2187. while (true)
  2188. {
  2189. Thread.Sleep(5000);
  2190. if (iSEM.GetStageIs() == 0)
  2191. {
  2192. break;
  2193. }
  2194. }
  2195. //判断是否停止进程
  2196. if (key_stop)
  2197. {
  2198. return false;
  2199. }
  2200. if(!iSEM.MoveStageXY(currentHole.Position.X, currentHole.Position.Y))
  2201. {
  2202. //SendMsg("移动失败");
  2203. return false;
  2204. }
  2205. LogManager.AddHardwareLog("移动X轴Y轴=" + currentHole.Position.X.ToString() + " " + currentHole.Position.Y.ToString(), true);
  2206. while (true)
  2207. {
  2208. Thread.Sleep(10000);
  2209. if (iSEM.GetStageIs() == 0)
  2210. {
  2211. break;
  2212. }
  2213. }
  2214. //判断是否停止进程
  2215. if (key_stop)
  2216. {
  2217. return false;
  2218. }
  2219. //拉直操作在T轴水平状态下进行
  2220. //7. 控制样品台,调整T轴54度、M/Z/R轴不变
  2221. if (m_MsParam.Is_Photograph == false)
  2222. {
  2223. if (DialogResult.Yes == MessageBox.Show("样品台T轴可以倾斜吗?", "确认消息", MessageBoxButtons.YesNo))
  2224. {
  2225. //SendMsg("样品台旋转");
  2226. //return true;
  2227. }
  2228. else
  2229. {
  2230. //SendMsg("禁止样品台旋转");
  2231. return false;
  2232. }
  2233. if (!iSEM.SetStageGotoT(54))
  2234. {
  2235. return false;
  2236. }
  2237. while (true)
  2238. {
  2239. Thread.Sleep(5000);
  2240. if (iSEM.GetStageIs() == 0)
  2241. {
  2242. break;
  2243. }
  2244. }
  2245. }
  2246. //判断是否停止进程
  2247. if (key_stop)
  2248. {
  2249. return false;
  2250. }
  2251. //拉直操作
  2252. if(!Straighten_Handle())
  2253. {
  2254. return false;
  2255. }
  2256. //判断是否停止进程
  2257. if (key_stop)
  2258. {
  2259. return false;
  2260. }
  2261. #endregion
  2262. return true;
  2263. }
  2264. //初始化拉直操作
  2265. public bool Straighten_Handle()
  2266. {
  2267. //1、自动对焦
  2268. if (!ImageFocus("0-9"))
  2269. {
  2270. LogManager.AddHardwareLog("拉直操作自动对焦失败,程序退出。", true);
  2271. return false;
  2272. }
  2273. //2、拍张照片
  2274. String fileName0 = data_path + "\\" + m_cutHoles[m_nWorkHoleNo].HoleName + "_" + m_MsParam.Stretch_Magnification.ToString("0") + "_" + ImageName0;
  2275. //拍照前改变速度,延时
  2276. iSEM.CmdSaveRate();
  2277. cycle_time = iSEM.GetCycleTime();
  2278. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  2279. if (!GetImage(ImageMode.SEM, fileName0))
  2280. {
  2281. arg.Message = "SEM拍照失败";
  2282. arg.State = false;
  2283. SendMsg("0-0");
  2284. return false;
  2285. }
  2286. arg.Message = "SEM拍照成功";
  2287. arg.State = true;
  2288. SendMsg("0-0");
  2289. iSEM.CmdFocusRate();
  2290. cycle_time = iSEM.GetCycleTime();
  2291. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  2292. //3、拉直操作
  2293. float degree = 0;
  2294. int direction = 0;
  2295. int state = 1;
  2296. wr.Img_OffsetAngle_Direction(fileName0, Convert.ToInt32(m_MsParam.SampleName), m_MsParam.Firm, out degree, out direction, out state);
  2297. LogManager.AddHardwareLog("样品" + m_nWorkHoleNo.ToString() + "初始化拉直角度=" + degree.ToString(), true);
  2298. if (state == 0)
  2299. {
  2300. if (DialogResult.Yes != MessageBox.Show("网络参数State返回为0。\n是否继续操作?。", "确认消息", MessageBoxButtons.YesNo))
  2301. {
  2302. arg.Message = "华为接口返回数据失败";
  2303. arg.State = false;
  2304. SendMsg("0-0");
  2305. return false;
  2306. }
  2307. }
  2308. if (direction == 2)
  2309. {
  2310. degree = -degree;
  2311. }
  2312. #region//暂时不用这个拉直
  2313. //旋转R轴
  2314. //float getr = iSEM.GetStageAtR();
  2315. //getr = getr + degree;
  2316. //if(getr>R_Max)
  2317. //{
  2318. // getr = getr - 360;
  2319. //}
  2320. //if(getr<R_Min)
  2321. //{
  2322. // getr = getr + 360;
  2323. //}
  2324. //iSEM.SetStageGotoR(getr);
  2325. //while (true)
  2326. //{
  2327. // Thread.Sleep(5000);
  2328. // if (iSEM.GetStageIs() == 0)
  2329. // {
  2330. // break;
  2331. // }
  2332. //}
  2333. //MessageBox.Show("Web返回数据移动是否正确?\n如果错误请手动移动到指定位置后按确认", "确认消息", MessageBoxButtons.OK);
  2334. //////3、拉直
  2335. //float x0 = iSEM.GetStageAtX();
  2336. //float y0 = iSEM.GetStageAtY();
  2337. //float x1 = 0, y1 = 0;
  2338. //Straightening(degree, x_center_point, y_center_point, x0, y0, ref x1, ref y1);
  2339. //if (x1 < X_Min || x1 > X_Max || y1 < Y_Min || y1 > Y_Max)
  2340. //{
  2341. // return false;
  2342. //}
  2343. //iSEM.MoveStageXY(x1, y1);
  2344. #endregion
  2345. //拉直
  2346. iSEM.SetStageDeltaR(degree);
  2347. while (true)
  2348. {
  2349. Thread.Sleep(5000);
  2350. if (iSEM.GetStageIs() == 0)
  2351. {
  2352. break;
  2353. }
  2354. }
  2355. MessageBox.Show("拉直操作完成,如果不正确请手动进行拉直然后点击OK按钮。", "确认消息", MessageBoxButtons.OK);
  2356. return true;
  2357. }
  2358. //角度补偿
  2359. public bool TiltCorrection(float a_fAngle)
  2360. {
  2361. if (!iSEM.SetTiltAngleOn())
  2362. {
  2363. return false;
  2364. }
  2365. //恢复原始状态
  2366. if (!iSEM.SetTiltAngle(a_fAngle))
  2367. {
  2368. return false;
  2369. }
  2370. return true;
  2371. }
  2372. //拍图
  2373. public bool GetImage(ImageMode a_mode, String a_fileName)
  2374. {
  2375. //3. 获取分辨率
  2376. int[] ImageSize = iSEM.GetImageStore();
  2377. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  2378. {
  2379. return false;
  2380. }
  2381. short width = (short)ImageSize[0];
  2382. short height = (short)ImageSize[1];
  2383. //4. 抓图
  2384. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  2385. {
  2386. return false;
  2387. }
  2388. return true;
  2389. }
  2390. //自动聚焦
  2391. public bool AutoFocus(FocusParam a_param, out int a_nWd, String step_code)
  2392. {
  2393. a_nWd = 0;
  2394. float wd = iSEM.GetWorkingDistance();
  2395. a_nWd = (int)Math.Ceiling(wd * 100000000 + 0.5);//获取当前的工作距离
  2396. if (a_param == null)
  2397. {
  2398. LogManager.AddHardwareLog("空参数", true);
  2399. return false;
  2400. }
  2401. string path = a_param.Path;
  2402. if (!Directory.Exists(path))
  2403. {
  2404. LogManager.AddHardwareLog("文件夹丢失", true);
  2405. return false;
  2406. }
  2407. string path_coarse = a_param.Path + "\\Coarse";
  2408. if (!Directory.Exists(path_coarse))
  2409. {
  2410. Directory.CreateDirectory(path_coarse);
  2411. }
  2412. string path_fine = a_param.Path + "\\Fine";
  2413. if (!Directory.Exists(path_fine))
  2414. {
  2415. Directory.CreateDirectory(path_fine);
  2416. }
  2417. //1. 拍照
  2418. //1.1, 粗拍照
  2419. //1.1.1, 根据参数计算拍照次数
  2420. double nUp = a_param.UP * 100.0 ;
  2421. double nDown = a_param.Down * 100.0;
  2422. double nStep = a_param.Step * 100.0;
  2423. double nTimes = Math.Ceiling((nDown - nUp) / nStep + 0.5);
  2424. LogManager.AddHardwareLog("初调拍照次数=" + nTimes.ToString(), true);
  2425. //SendMsg("粗对焦:最小值" + nUp.ToString() + ",最大值" + nDown.ToString() + ",步长" + nStep.ToString() + "拍照张数" + nTimes.ToString());
  2426. //粗拍照
  2427. for (int i = 0; i < nTimes; i++)
  2428. {
  2429. int cWd =(int) Math.Ceiling(nUp + i * nStep + 0.5);
  2430. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  2431. arg.Picture_Information.Work_Distance = (float)(cWd * 0.00000001);
  2432. Thread.Sleep(Convert.ToInt32(cycle_time) + 1000);
  2433. int swd = cWd / 100;
  2434. //SendMsg("wd = " + swd.ToString() +"um");
  2435. String fileName = path_coarse + "\\" + cWd.ToString() + ".tif";
  2436. arg.Picture_Information.Picture_FullPath = fileName;
  2437. if (!GetImage(ImageMode.SEM, fileName))
  2438. {
  2439. return false;
  2440. }
  2441. if (step_code != "0-9")
  2442. {
  2443. SendMsg(step_code);
  2444. }
  2445. //判断是否停止进程
  2446. if (key_stop)
  2447. {
  2448. return false;
  2449. }
  2450. //Thread.Sleep(3000);
  2451. }
  2452. //1.2, 选定细拍照的工作距离
  2453. int CorWd;
  2454. ChooseBest(path_coarse,out CorWd);
  2455. a_nWd = CorWd;
  2456. //SendMsg("粗选工作距离wd = " + a_nWd.ToString() );
  2457. //1.3, 细拍照
  2458. if ( (CorWd - a_param.Range * 100.0) <=0)
  2459. {
  2460. return false;
  2461. }
  2462. double nfUp = CorWd - a_param.Range * 100.0;
  2463. double nfStep = a_param.fStep * 100.0;
  2464. double nfTimes = Math.Ceiling((a_param.Range * 100.0 * 2) / nfStep + 0.5);
  2465. //SendMsg("粗对焦:最小值" + nfUp.ToString() + ",步长" + nfStep.ToString() + "拍照张数" + nfTimes.ToString());
  2466. for (int i = 0; i < nfTimes; i++)
  2467. {
  2468. int cWd = (int)Math.Ceiling(nfUp + i * nfStep + 0.5);
  2469. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  2470. arg.Picture_Information.Work_Distance = (float)(cWd * 0.00000001);
  2471. Thread.Sleep(Convert.ToInt32(cycle_time)+1000);
  2472. int swd = cWd / 100;
  2473. //SendMsg("wd = " + swd.ToString() + "um");
  2474. String fileName = path_fine + "\\" + cWd.ToString() + ".tif";
  2475. arg.Picture_Information.Picture_FullPath = fileName;
  2476. if (!GetImage(ImageMode.SEM, fileName))
  2477. {
  2478. return false;
  2479. }
  2480. if (step_code != "0-9")
  2481. {
  2482. SendMsg(step_code);
  2483. }
  2484. //判断是否停止进程
  2485. if (key_stop)
  2486. {
  2487. return false;
  2488. }
  2489. //Thread.Sleep(2000);
  2490. }
  2491. int fineWd;
  2492. ChooseBest(path_fine, out fineWd);
  2493. //SendMsg("wd fine max = " + fineWd.ToString());
  2494. a_nWd = fineWd;
  2495. //SendMsg("细选工作距离wd = " + a_nWd.ToString());
  2496. return true;
  2497. }
  2498. //LoG算子计算
  2499. private double LoGMean(string a_strImgPath)
  2500. {
  2501. //读入Img文件
  2502. if (!File.Exists(a_strImgPath))
  2503. {
  2504. return 0;
  2505. }
  2506. Mat src, gray_src;
  2507. int kernel_size = 3;
  2508. src = Cv2.ImRead(a_strImgPath);
  2509. gray_src = new Mat();
  2510. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2511. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2512. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2513. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2514. Cv2.Laplacian(src, src, MatType.CV_8UC3, kernel_size);
  2515. Cv2.ConvertScaleAbs(src, src);
  2516. //图像的平均灰度
  2517. double meanValue = 0.0;
  2518. meanValue = Cv2.Mean(src)[0];
  2519. return meanValue;
  2520. }
  2521. //梯度计算
  2522. private double Tenengrad(string a_strImgPath)
  2523. {
  2524. //读入Img文件
  2525. if (!File.Exists(a_strImgPath))
  2526. {
  2527. return 0;
  2528. }
  2529. Mat src;
  2530. src = Cv2.ImRead(a_strImgPath);
  2531. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2532. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2533. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2534. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2535. Cv2.Sobel(src, src, MatType.CV_8UC1, 1, 1);
  2536. //图像的平均灰度
  2537. double meanValue = 0.0;
  2538. meanValue = Cv2.Mean(src)[0];
  2539. return meanValue;
  2540. }
  2541. //梯度计算
  2542. private double TTgrad(string a_strImgPath)
  2543. {
  2544. //读入Img文件
  2545. if (!File.Exists(a_strImgPath))
  2546. {
  2547. return 0;
  2548. }
  2549. Mat src;
  2550. src = Cv2.ImRead(a_strImgPath);
  2551. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2552. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2553. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2554. Mat scalex = new Mat();
  2555. Mat scaley = new Mat();
  2556. Cv2.Scharr(src, src, src.Depth(), 1, 0);
  2557. Cv2.ConvertScaleAbs(src, scalex);
  2558. Cv2.Scharr(src, src, src.Depth(), 0, 1);
  2559. Cv2.ConvertScaleAbs(src, scaley);
  2560. Cv2.AddWeighted(scalex, 0.5, scaley, 0.5, 0, src);
  2561. //图像的平均灰度
  2562. double meanValue = 0.0;
  2563. meanValue = Cv2.Mean(src)[0];
  2564. return meanValue;
  2565. }
  2566. //三选二算法
  2567. private void ChooseBest(string a_strImgPath, out int a_nWd)
  2568. {
  2569. a_nWd = -1;
  2570. var files = Directory.GetFiles(a_strImgPath, "*.tif");
  2571. float[] values = new float[files.Length];
  2572. double smax1 = LoGMean(files[0]);
  2573. int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  2574. double smax2 = Tenengrad(files[0]);
  2575. int wdmax2 = wdmax1;
  2576. double smax3 = TTgrad(files[0]);
  2577. int wdmax3 = wdmax1;
  2578. int x = 0;
  2579. foreach (var file in files)
  2580. {
  2581. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  2582. double svalue1 = LoGMean(file);
  2583. double svalue2 = Tenengrad(file);
  2584. double svalue3 = TTgrad(file);
  2585. if (smax1 < svalue1)
  2586. {
  2587. smax1 = svalue1;
  2588. wdmax1 = wd;
  2589. }
  2590. if (smax2 < svalue2)
  2591. {
  2592. smax2 = svalue2;
  2593. wdmax2 = wd;
  2594. }
  2595. if (smax3 < svalue3)
  2596. {
  2597. smax3 = svalue3;
  2598. wdmax3 = wd;
  2599. }
  2600. x++;
  2601. }
  2602. //SendMsg("LoG = " + wdmax1.ToString() + ",梯度 = " + wdmax2.ToString() + ",滤波 = " + wdmax3.ToString());
  2603. if (wdmax1 == wdmax2 || wdmax1 == wdmax3)
  2604. {
  2605. a_nWd = wdmax1;
  2606. //SendMsg("焦距 = " + wdmax1.ToString());
  2607. }
  2608. else if (wdmax1 == wdmax2 || wdmax2 == wdmax3)
  2609. {
  2610. a_nWd = wdmax2;
  2611. //SendMsg("焦距 = " + wdmax2.ToString());
  2612. }
  2613. else if (wdmax3 == wdmax2 || wdmax1 == wdmax3)
  2614. {
  2615. a_nWd = wdmax3;
  2616. //SendMsg("焦距 = " + wdmax3.ToString());
  2617. }
  2618. else
  2619. {
  2620. //SendMsg("对焦失败。");
  2621. }
  2622. }
  2623. //移动到像素位置
  2624. bool MoveToPix(float xc, float yc)
  2625. {
  2626. float pixSize = iSEM.GetPixelSize();
  2627. Thread.Sleep(500);
  2628. //0:width, 1:height
  2629. int[] imageSize = iSEM.GetImageStore();
  2630. int width = imageSize[0]/2;
  2631. int height = imageSize[1]/2;
  2632. float deltX = (xc - (float)width) * pixSize;
  2633. float deltY = (yc - (float)height) * pixSize;
  2634. float xpCur = iSEM.GetStageAtX();
  2635. float ypCur = iSEM.GetStageAtY();
  2636. float xpNew = xpCur - deltX;
  2637. float ypNew = ypCur - deltY;
  2638. LogManager.AddHardwareLog("样品台移动信息:deltx=" + deltX.ToString() +
  2639. ",delty=" + deltY.ToString() + ",xpNew=" + xpNew.ToString() + ",ypNew=" + ypNew.ToString(), true);
  2640. if (deltX > 10)//使用移动样品台实现
  2641. {
  2642. if (!iSEM.SetStageGotoX(xpNew))
  2643. {
  2644. return false;
  2645. }
  2646. //判断是否移动完成
  2647. while (true)
  2648. {
  2649. Thread.Sleep(5000);
  2650. if (iSEM.GetStageIs() == 0)
  2651. {
  2652. break;
  2653. }
  2654. }
  2655. //arg.Message = "移动到新x" + xpNew.ToString() + "位置失败";
  2656. //arg.State = true;
  2657. //SendMsg("1-7");
  2658. }
  2659. else//使用移动光束实现
  2660. {
  2661. float beamX = deltX * (float)0.66;
  2662. if (!iSEM.SetBeamShiftX(beamX))
  2663. {
  2664. return false;
  2665. }
  2666. }
  2667. if (deltY > 10)//使用移动样品台实现
  2668. {
  2669. if (!iSEM.SetStageGotoY(ypNew))
  2670. {
  2671. return false;
  2672. }
  2673. //判断是否移动完成
  2674. while (true)
  2675. {
  2676. Thread.Sleep(5000);
  2677. if (iSEM.GetStageIs() == 0)
  2678. {
  2679. break;
  2680. }
  2681. }
  2682. }
  2683. else//使用移动光束实现
  2684. {
  2685. float beamY = deltY * (float)0.671;
  2686. if (!iSEM.SetBeamShiftY(beamY))
  2687. {
  2688. return false;
  2689. }
  2690. }
  2691. return true;
  2692. }
  2693. }
  2694. }