Measure.cs 100 KB

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