Measure.cs 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  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 string State
  31. {
  32. get { return m_state; }
  33. set { m_state = value; }
  34. }
  35. private string 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. /// <summary>
  318. /// 图像拉直算法
  319. /// </summary>
  320. /// <param name="Slope">倾斜角度</param>
  321. /// <param name="x0">圆心坐标x</param>
  322. /// <param name="y0">圆心坐标y</param>
  323. /// <param name="x1">倾斜情况下帧图的坐标x</param>
  324. /// <param name="y1">倾斜情况下帧图的坐标y</param>
  325. /// <param name="Hx">复位到原位置需要移动的x值(返回值)</param>
  326. /// <param name="Hy">复位到原位置需要移动的y值(返回值)</param>
  327. /// <returns></returns>
  328. public bool Straightening(float Slope, float x0, float y0, float x1, float y1, ref float Hx, ref float Hy)
  329. {
  330. try
  331. {
  332. double xr = x1 - x0;//倾斜图到中心的距离差x
  333. double yr = y1 - y0;//倾斜图到中心的距离差y
  334. double R = Math.Sqrt(xr * xr + yr * yr);//圆心到倾斜图的长度
  335. //double D = 2 * R * Math.Sin(ScanRotation / 2);//倾斜图与矫正图底边的长度
  336. double k = Math.Atan(y1 / x1) / Math.PI * 180;//x1,y1的直角三角形圆心角度
  337. double k_S = k - Slope;//通过夹角差求x2,y2
  338. int y2 = (int)Math.Round(Math.Sin(Math.PI / (180 / k_S)) * R);
  339. int x2 = (int)Math.Round(Math.Cos(Math.PI / (180 / k_S)) * R);
  340. if (Slope > 0 || Slope < 0)
  341. {
  342. Hx = x1 - x2;
  343. Hy = y1 - y2;
  344. }
  345. else
  346. {
  347. Hx = 0;
  348. Hy = 0;
  349. }
  350. return true;
  351. }
  352. catch (Exception)
  353. {
  354. return false;
  355. }
  356. }
  357. public WebResult wr = null;
  358. public event ThreadStatusHandler SendThreadStatus; // 声明事件
  359. public event CutHolesStatusHandler SendCutHolesStatus; // 声明事件
  360. //定义一个全局的消息类
  361. ThreadStatusEventArgs arg = new ThreadStatusEventArgs("0-0");
  362. //全局只有一个fatorySEM
  363. static FactoryHardware factorySEM = FactoryHardware.Instance;
  364. ISEMControl iSEM = factorySEM.ISEM;
  365. //@的作用是不用转义字符\\只打一个就行
  366. const String ImageName1 = @"ImageSEMH.tif"; //传给客户,作水平校正
  367. const String ImageName2 = @"ImageFIB600X1.tif";//传给客户,找到切割点
  368. const String ImageName31 = @"ImageFIBBefore.tif";
  369. const String ImageName32 = @"ImageFIBAfter.tif";
  370. const String ImageName4 = @"ImageSEM300X.tif";//传给客户,找到已经切割点
  371. const String ImageName5 = @"ImageSEM6000X.tif";//传给客户,水平校正
  372. const String ImageName6 = @"ImageSEMMag.tif";//传给客户,测量层高
  373. const String MacoInsertPt = "GIS Insert.MLF"; //传入PT针
  374. const String MacoRetractPt = "GIS Retract.MLF"; //退出PT针
  375. const String MacoScanPic = "Scan picture.MLF";//一般质量抓图的宏
  376. const String MacoGoodPic = "Good picture.MLF";//高质量抓图的宏
  377. const String MacoExportTif = "Export TIFF.MLF";// 导出有标尺的图像,这个图像为使用工具SmartSEM工具的图像
  378. const String ElyDeposition = @"Deposition.ely"; //沉积
  379. const String ElyCrossSection = @"CrossSection.ely"; //切割
  380. const float fMin = (float)0.0000002; //单位是米
  381. public Boolean key_stop = false;
  382. String focus_path = "";
  383. String data_path = "";
  384. int m_nWorkHoleNo = -1;
  385. //测量文件
  386. private MeasureFile m_measureFile;
  387. public MeasureFile MeasureFile
  388. {
  389. get { return this.m_measureFile; }
  390. set { this.m_measureFile = value; }
  391. }
  392. //测量的切割孔
  393. private List<MeasureData.CutHole> m_cutHoles;
  394. public List<MeasureData.CutHole> cutHoles
  395. {
  396. get { return this.m_cutHoles; }
  397. set { this.m_cutHoles = value; }
  398. }
  399. //工作文件夹
  400. private string m_WorkingFolder;
  401. public string WorkingFolder
  402. {
  403. get { return this.m_WorkingFolder; }
  404. set { this.m_WorkingFolder = value; }
  405. }
  406. //程序当前路径
  407. private string m_ProgramFolder = Directory.GetCurrentDirectory();
  408. //线程状态
  409. private ThreadStatus m_ThreadStatus;
  410. public ThreadStatus TStatus
  411. {
  412. get { return this.m_ThreadStatus; }
  413. set { this.m_ThreadStatus = value; }
  414. }
  415. //测量参数
  416. private MeasureParam m_MsParam;
  417. public MeasureParam MeasParam
  418. {
  419. get { return this.m_MsParam; }
  420. set { this.m_MsParam = value; }
  421. }
  422. //
  423. //构造函数
  424. public Measure(String webServerIP, String webServerPort, String webServerUrl)
  425. {
  426. MeasParam = new MeasureParam();
  427. TStatus = new ThreadStatus();
  428. cutHoles = new List<MeasureData.CutHole>();
  429. //配置远程连接
  430. wr = new WebResult(webServerIP,webServerPort,webServerUrl);
  431. }
  432. //初始化测量业务, 读测量文件,判断是否有可测试的切孔
  433. public bool InitMeas(MeasureFile a_measureFile)
  434. {
  435. m_measureFile = a_measureFile;
  436. List<CutHole> listHoles = m_measureFile.ListCutHole;
  437. foreach (CutHole h in listHoles)
  438. {
  439. if (h.SWITCH == true)
  440. {
  441. m_cutHoles.Add(h);
  442. }
  443. }
  444. if (m_cutHoles.Count == 0)
  445. return false;
  446. this.m_MsParam = m_measureFile.MParam;
  447. return true;
  448. }
  449. public void SendMsg(string step_code)
  450. {
  451. arg.State = step_code;
  452. SendThreadStatus(this, arg);
  453. }
  454. public void SendMsg1(string step_code)
  455. {
  456. arg.Step_Code = step_code;
  457. arg.Time = DateTime.Now;
  458. switch(step_code)
  459. {
  460. //拍照
  461. case "1-5":
  462. case "1-7":
  463. case "1-16":
  464. case "1-24":
  465. //电压、放大倍数、工作距离、位置信息、图片路径
  466. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  467. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  468. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  469. break;
  470. case "1-10":
  471. case "1-18":
  472. //获取偏移信息
  473. break;
  474. case "1-12":
  475. case "1-20":
  476. //验证移动准备性
  477. break;
  478. case "1-26":
  479. //取消电子束校正并初始化
  480. break;
  481. default:
  482. arg.State = "1";
  483. break;
  484. }
  485. SendThreadStatus(this, arg);
  486. }
  487. public void SendCutHoleMsg(string a_state, string a_holeName)
  488. {
  489. CutHolesStatusEventArgs arg = new CutHolesStatusEventArgs(a_state, a_holeName);
  490. arg.HoleName = a_holeName;
  491. arg.State = a_state;
  492. SendCutHolesStatus(this, arg);
  493. }
  494. //测量流程
  495. public void DoMeasure()
  496. {
  497. //创建线程的测量状态的更新
  498. this.TStatus.ComputeTime(THREAD_TIME_TYPE.START);
  499. if (SendThreadStatus != null)
  500. {
  501. SendMsg("开始测量。");
  502. }
  503. //检查硬件连接是否正常
  504. if (!ConnectHardware())
  505. {
  506. SendMsg("连接硬件失败。");
  507. this.TStatus.ComputeTime(THREAD_TIME_TYPE.STOPPED);
  508. return;
  509. }
  510. //SendMsg("硬件连接成功。");
  511. //设置工作文件夹
  512. if (!SetWorkingFolderStr())
  513. {
  514. //SendMsg("设置工作文件夹失败");
  515. this.TStatus.ComputeTime(THREAD_TIME_TYPE.STOPPED);
  516. return;
  517. }
  518. SendMsg("创建工作文件路径为" + WorkingFolder);
  519. float[] firstPosition;
  520. //将停止键复位
  521. key_stop = false;
  522. for (int i = 0; i < m_cutHoles.Count; i++)
  523. {
  524. m_nWorkHoleNo = i;
  525. arg.HoleName = m_cutHoles[i].HoleName;
  526. //创建文件夹,孔名文件夹结构
  527. //最终数据存放目录
  528. data_path = WorkingFolder + "\\" + m_cutHoles[i].HoleName;
  529. //对焦数据存放目录
  530. focus_path = WorkingFolder + "\\" + m_cutHoles[i].HoleName + "\\Focus";
  531. m_MsParam.AutoFocus.Path = focus_path;
  532. if (!Directory.Exists(focus_path))
  533. {
  534. Directory.CreateDirectory(focus_path);
  535. }
  536. //判断孔状态
  537. if(m_cutHoles[i].STATE != State.Unmeasured)
  538. {
  539. continue;
  540. }
  541. //开始循环工作
  542. if (i == 0)
  543. {
  544. //SendMsg("第一个切孔开始测量");
  545. //切孔操作-开始
  546. SendCutHoleMsg(((int)ThreadState.InProcess).ToString(), m_cutHoles[i].HoleName);
  547. firstPosition = iSEM.GetStagePosition();
  548. //第一个孔的测试
  549. //成功
  550. if(FirstHole())
  551. {
  552. //保存文件,将测量状态更改
  553. m_cutHoles[i].STATE = State.Success;
  554. m_measureFile.Save();
  555. }
  556. //失败
  557. else
  558. {
  559. m_cutHoles[i].STATE = State.Failed;
  560. m_measureFile.Save();
  561. }
  562. //切孔操作-完成
  563. SendCutHoleMsg(((int)ThreadState.Success).ToString(), m_cutHoles[i].HoleName);
  564. }
  565. else
  566. {
  567. //非第一个孔的测试
  568. SendMsg("第" + i.ToString() + "个切孔开始测量");
  569. //切孔操作-开始
  570. SendCutHoleMsg(((int)ThreadState.InProcess).ToString(), m_cutHoles[i].HoleName);
  571. OtherHole();
  572. //切孔操作-完成
  573. SendCutHoleMsg(((int)ThreadState.Success).ToString(), m_cutHoles[i].HoleName);
  574. }
  575. if(key_stop)
  576. {
  577. m_cutHoles[i].STATE = State.Unmeasured;
  578. m_measureFile.Save();
  579. break;
  580. }
  581. }
  582. }
  583. //检查硬件连接是否正常
  584. public bool ConnectHardware()
  585. {
  586. //返回硬件的连接状态
  587. return iSEM.ConnectStatus();
  588. }
  589. //设置工作文件夹
  590. public bool SetWorkingFolderStr()
  591. {
  592. WorkingFolder = m_measureFile.FilePath;
  593. return true;
  594. }
  595. //插入PT针
  596. public bool InsertPT()
  597. {
  598. string fn = m_ProgramFolder + "\\Macro\\" + MacoInsertPt;
  599. //SendMsg("调用宏插入PT针宏文件" + fn);
  600. iSEM.CMDMCFFilename(fn);
  601. //延时1s??
  602. Thread.Sleep(1000);
  603. return true;
  604. }
  605. //撤出PT针
  606. public bool RetractPT()
  607. {
  608. string fn = m_ProgramFolder + "\\Macro\\" + MacoRetractPt;
  609. //SendMsg("调用宏撤出PT针宏文件" + fn);
  610. iSEM.CMDMCFFilename(fn);
  611. //延时1s??
  612. Thread.Sleep(1000);
  613. return true;
  614. }
  615. //PT沉积
  616. public bool PTWork()
  617. {
  618. //执行PT沉积的ELY文件
  619. string fn = m_ProgramFolder + "\\Macro\\" + ElyDeposition;
  620. //SendMsg("执行PT沉积Ely文件:" + fn);
  621. if (!ExcuteEly(fn))
  622. {
  623. return false;
  624. }
  625. //等待沉积完成
  626. while (true)
  627. {
  628. Thread.Sleep(10000);
  629. if (iSEM.GetFIBMode() == 0)
  630. {
  631. break;
  632. }
  633. }
  634. return true;
  635. }
  636. //FIB切割
  637. public bool FIBWork()
  638. {
  639. //执行PT沉积的ELY文件
  640. string fn = m_ProgramFolder + "\\Macro\\" + ElyCrossSection;
  641. //SendMsg("执行FIB切割Ely文件:" + fn);
  642. if (!ExcuteEly(fn))
  643. {
  644. return false;
  645. }
  646. //等待切割完成
  647. while (true)
  648. {
  649. Thread.Sleep(10000);
  650. if (iSEM.GetFIBMode() == 0)
  651. {
  652. break;
  653. }
  654. }
  655. return true;
  656. }
  657. //执行ELY文件的步骤
  658. public bool ExcuteEly(string a_filename)
  659. {
  660. //执行ELy文件有三个动作
  661. //1. 选择ELY文件
  662. //SendMsg("选择ELY文件");
  663. if (!iSEM.CmdFIBLoadELY(a_filename))
  664. {
  665. //SendMsg("选择ELY文件失败");
  666. return false;
  667. }
  668. Thread.Sleep(1000);
  669. //2. 确认ELY文件
  670. //SendMsg("确认ELY文件");
  671. if (!iSEM.CmdFIBEXPOSUREELY())
  672. {
  673. //SendMsg("确认ELY文件失败");
  674. return false;
  675. }
  676. Thread.Sleep(1000);
  677. //3. 执行ELY文件
  678. //SendMsg("执行ELY文件");
  679. if (!iSEM.CmdFIBSTARTELY())
  680. {
  681. //SendMsg("执行ELY文件失败");
  682. return false;
  683. }
  684. Thread.Sleep(1000);
  685. return true;
  686. }
  687. //执行自动对焦
  688. public bool ImageFocus()
  689. {
  690. if (m_measureFile.MParam.FocusMode == 1)//手动
  691. {
  692. if (DialogResult.Yes == MessageBox.Show("图像自动对焦已完成?", "确认消息", MessageBoxButtons.YesNo))
  693. {
  694. arg.Message = "自动对焦完成";
  695. SendMsg("1-3");
  696. }
  697. else
  698. {
  699. arg.Message = "自动对焦失败";
  700. SendMsg("1-3");
  701. return false;
  702. }
  703. }
  704. else if (m_measureFile.MParam.FocusMode == 2)//自有自动
  705. {
  706. //郝工增加自动对焦算法
  707. //处理文件夹
  708. if(Directory.Exists(focus_path))
  709. {
  710. Directory.Delete(focus_path,true);
  711. }
  712. Directory.CreateDirectory(focus_path);
  713. //1、对焦参数类,2、输出工作距离
  714. int wd = 0;
  715. AutoFocus(m_MsParam.AutoFocus, out wd);
  716. //设置工作距离
  717. iSEM.SetWorkingDistance((float)(wd * 0.00000001));
  718. //处理图片位置
  719. //File.Copy(focus_path + "\\fine\\" + wd.ToString() + ".tif", data_path + "\\" + ImageName);
  720. }
  721. else if (m_MsParam.FocusMode == 3) //客户自动
  722. {
  723. //后期和客户对接接口
  724. List<string> filenames = new List<string>();
  725. String retfilename = wr.Img_Auto_Focus(filenames);
  726. }
  727. else //蔡司自动对焦
  728. {
  729. }
  730. return true;
  731. }
  732. /// <summary>
  733. ///13. 自动 定位功能,沉积
  734. /// </summary>
  735. /// <returns></returns>
  736. public bool GetPoistion(Boolean isFirst)
  737. {
  738. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  739. int state = 0;
  740. //1.控制SEM放大600X
  741. arg.State = "SEM";
  742. if (!iSEM.SetMagnification(m_measureFile.MParam.Location_Magnification))
  743. {
  744. arg.Message = "放大" + m_measureFile.MParam.Location_Magnification.ToString("0.0") + "倍失败";
  745. SendMsg("1-2");
  746. return false;
  747. }
  748. arg.Message = "放大" + m_measureFile.MParam.Location_Magnification.ToString("0.0") + "倍成功";
  749. SendMsg("1-2");
  750. //判断是否停止进程
  751. if (key_stop)
  752. {
  753. return false;
  754. }
  755. //2.控制SEM自动对焦、亮度、对比度
  756. ImageFocus();
  757. //判断是否停止进程
  758. if (key_stop)
  759. {
  760. return false;
  761. }
  762. //3.第一次不需要进行角度补偿,其他需要设置SEM进行角度补偿54度
  763. if (!isFirst)
  764. {
  765. if (!TiltCorrection(54))
  766. {
  767. arg.Message = "设置SEM进行角度补偿54度失败";
  768. SendMsg("1-4");
  769. return false;
  770. }
  771. arg.Message = "设置SEM进行角度补偿54度成功";
  772. SendMsg("1-4");
  773. Thread.Sleep(5000);
  774. //判断是否停止进程
  775. if (key_stop)
  776. {
  777. return false;
  778. }
  779. }
  780. //4.控制SEM拍照,5保存照片=====需要更改路径
  781. String fileName1 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName1;
  782. arg.Picture_Information.Picture_FullPath = fileName1;
  783. arg.Picture_Information.Work_Status = "SEM";
  784. if (!GetImage(ImageMode.SEM, fileName1))
  785. {
  786. arg.Message = "SEM拍照失败";
  787. SendMsg("1-5");
  788. return false;
  789. }
  790. arg.Message = "SEM拍照成功";
  791. SendMsg("1-5");
  792. Thread.Sleep(3000);
  793. //判断是否停止进程
  794. if (key_stop)
  795. {
  796. return false;
  797. }
  798. //判断是否为仅拍照,不是则执行FIB操作
  799. if (!m_MsParam.Is_Photograph)
  800. {
  801. //6.设置FIB拍照参数——扫描时间、束流等
  802. //7.控制FIB自动亮度、对比度
  803. if (!iSEM.CmdFIBModeFIB())
  804. {
  805. arg.Message = "FIB模式切换失败";
  806. SendMsg("1-6");
  807. return false;
  808. }
  809. arg.State = "FIB";
  810. arg.Message = "FIB模式切换成功";
  811. SendMsg("1-6");
  812. Thread.Sleep(10000);
  813. //判断是否停止进程
  814. if (key_stop)
  815. {
  816. return false;
  817. }
  818. //8.控制FIB拍照
  819. //9.保存照片======更改路径
  820. String fileName2 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName2;
  821. arg.Picture_Information.Picture_FullPath = fileName1;
  822. arg.Picture_Information.Work_Status = "FIB";
  823. if (!GetImage(ImageMode.FIB, fileName2))
  824. {
  825. arg.Message = "FIB拍照失败";
  826. SendMsg("1-7");
  827. return false;
  828. }
  829. arg.Message = "FIB拍照成功";
  830. SendMsg("1-7");
  831. Thread.Sleep(3000);
  832. //判断是否停止进程
  833. if (key_stop)
  834. {
  835. return false;
  836. }
  837. //10.将照片传给客户,返回梯形位置坐标,及样品类型参数(是否需要PT沉积,PT坐标位置,PT宽度、PT高度、梯形上、下边及深度、扫描时间、束流、样品放大倍数1、样品放大倍数2等切割参数)
  838. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  839. {
  840. arg.Message = "样品台移动到指定位置";
  841. SendMsg("1-8");
  842. }
  843. else
  844. {
  845. wr.Img_Cut_Position(fileName2, m_MsParam.SampleName, m_MsParam.Firm, out x1, out y1, out x2, out y2, out state);
  846. if (state == 1)
  847. {
  848. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  849. if (!iSEM.MoveStageXY(x1, y1))
  850. {
  851. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  852. SendMsg("1-8");
  853. return false;
  854. }
  855. //判断是否移动完成
  856. while (true)
  857. {
  858. Thread.Sleep(5000);
  859. if (iSEM.GetStageIs() == 0)
  860. {
  861. break;
  862. }
  863. }
  864. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  865. SendMsg("1-8");
  866. }
  867. else
  868. {
  869. arg.Message = "样品台移动失败";
  870. SendMsg("1-8");
  871. return false;
  872. }
  873. }
  874. //判断是否停止进程
  875. if (key_stop)
  876. {
  877. return false;
  878. }
  879. //11.自动工具样品类型参数确定是否需要PT沉积
  880. if (m_MsParam.PT)
  881. {
  882. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  883. //第10步已经移动好位置
  884. //2. 验证移动准确性:获取当前FIB中心位置坐标,与客户返回坐标对比,验证是否一定正确
  885. x0 = iSEM.GetStageAtX();
  886. y0 = iSEM.GetStageAtY();
  887. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  888. {
  889. arg.Message = "目标位置(" + x0.ToString() + "," + y0.ToString() + ")移动失败";
  890. SendMsg("1-9");
  891. return false;
  892. }
  893. arg.Message = "目标位置(" + x0.ToString() + "," + y0.ToString() + ")移动成功";
  894. SendMsg("1-9");
  895. //判断是否停止进程
  896. if (key_stop)
  897. {
  898. return false;
  899. }
  900. //3. 根据坐标进行PT沉积
  901. if (!PTWork())
  902. {
  903. arg.Message = "PT沉积失败";
  904. SendMsg("1-10");
  905. return false;
  906. }
  907. arg.Message = "PT沉积成功";
  908. SendMsg("1-10");
  909. //判断是否停止进程
  910. if (key_stop)
  911. {
  912. return false;
  913. }
  914. }
  915. }
  916. //12.根据梯形坐标控制FIB调整到中心位置
  917. //13.验证移动准确性:获取当前FIB中心位置坐标,与客户返回坐标对比,验证是否移动正确
  918. //以上两步已经将程序移动到11内部去做
  919. //14.保存样品1第1号孔中心位置6轴坐标1 XYZMRT到数据库,保存客户返回值信息到数据库
  920. float[] firstPosition = iSEM.GetStagePosition();
  921. m_cutHoles[0].Position.X = firstPosition[0];
  922. m_cutHoles[0].Position.Y = firstPosition[1];
  923. m_cutHoles[0].Position.Z = firstPosition[2];
  924. m_cutHoles[0].Position.T = firstPosition[3];
  925. m_cutHoles[0].Position.R = firstPosition[4];
  926. m_cutHoles[0].Position.M = firstPosition[5];
  927. //这里要调用文件保存功能
  928. arg.Message = "坐标1(" + firstPosition[0].ToString() + ","
  929. + firstPosition[1].ToString() + ","
  930. + firstPosition[2].ToString() + ","
  931. + firstPosition[3].ToString() + ","
  932. + firstPosition[4].ToString() + ","
  933. + firstPosition[5].ToString() + ")";
  934. SendMsg("1-11");
  935. //判断是否停止进程
  936. if (key_stop)
  937. {
  938. return false;
  939. }
  940. return true;
  941. }
  942. /// <summary>
  943. /// 14. FIB切割
  944. /// </summary>
  945. /// <returns></returns>
  946. public bool FIBCross()
  947. {
  948. int state = 0;
  949. //切之前拍照保存,,
  950. String fileName31 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName31;
  951. arg.Picture_Information.Picture_FullPath = fileName31;
  952. arg.Picture_Information.Work_Status = "FIB";
  953. if (!GetImage(ImageMode.FIB, fileName31))
  954. {
  955. arg.Message = "FIB切割前拍照失败";
  956. SendMsg("1-14");
  957. return false;
  958. }
  959. arg.Message = "FIB切割前拍照成功";
  960. SendMsg("1-14");
  961. //判断是否停止进程
  962. if (key_stop)
  963. {
  964. return false;
  965. }
  966. //14.自动控制FIB切割
  967. //1.根据参数设置FIB草率时间(使图清晰),设置梯形上下边及深度、设置束流
  968. //2.控制FIB进行切割
  969. //以上1、2步全部用ELY文件代替
  970. if (!FIBWork())
  971. {
  972. arg.Message = "FIB切割失败";
  973. SendMsg("1-12");
  974. return false;
  975. }
  976. arg.Message = "FIB切割成功";
  977. SendMsg("1-12");
  978. Thread.Sleep(5000);
  979. //判断是否停止进程
  980. if (key_stop)
  981. {
  982. return false;
  983. }
  984. //3.控制FIB拍照600X
  985. //底层没有控制方法。
  986. //4.保存图片
  987. String fileName32 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName32;
  988. arg.Picture_Information.Picture_FullPath = fileName32;
  989. arg.Picture_Information.Work_Status = "FIB";
  990. if (!GetImage(ImageMode.FIB, fileName32))
  991. {
  992. arg.Message = "FIB切割后拍照失败";
  993. SendMsg("1-14");
  994. return false;
  995. }
  996. arg.Message = "FIB切割后拍照成功";
  997. SendMsg("1-14");
  998. Thread.Sleep(3000);
  999. //判断是否停止进程
  1000. if (key_stop)
  1001. {
  1002. return false;
  1003. }
  1004. //5.验证切割准确性:与切割前对比,如果对比误差大,则停止自动执行,进行报警
  1005. wr.Img_Cut_Success(fileName31, fileName32, out state);
  1006. if(state == 0)
  1007. {
  1008. arg.Message = "FIB切割校验失败";
  1009. SendMsg("1-14");
  1010. return false;
  1011. }
  1012. arg.Message = "FIB切割校验成功";
  1013. SendMsg("1-14");
  1014. //判断是否停止进程
  1015. if (key_stop)
  1016. {
  1017. return false;
  1018. }
  1019. //6.设置FIB解冻:先读取状态,如果冻结状态则进行解冻
  1020. //拍完照自动解冻,这里不做处理。
  1021. return true;
  1022. }
  1023. /// <summary>
  1024. /// 16.自动调整SEM找到切割位置,这里不是简单的拉直旋转
  1025. /// </summary>
  1026. /// <returns></returns>
  1027. public bool FindCross()
  1028. {
  1029. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  1030. float angle = 0;
  1031. int direction = 0;
  1032. int state = 0;
  1033. //切换到SEM模式
  1034. if (!iSEM.CmdFIBModeSEM())
  1035. {
  1036. //SendMsg("SEM模式切换失败");
  1037. return false;
  1038. }
  1039. arg.State = "SEM";
  1040. Thread.Sleep(10000);
  1041. //判断是否停止进程
  1042. if (key_stop)
  1043. {
  1044. return false;
  1045. }
  1046. //1.控制SEM放大到300倍
  1047. if (!iSEM.SetMagnification(m_measureFile.MParam.Location_Magnification/2))
  1048. {
  1049. arg.Message = "放大" + (m_measureFile.MParam.Location_Magnification / 2).ToString("0.0") + "倍失败";
  1050. SendMsg("1-18");
  1051. return false;
  1052. }
  1053. arg.Message = "放大" + (m_measureFile.MParam.Location_Magnification/2).ToString("0.0") + "倍成功";
  1054. SendMsg("1-18");
  1055. //判断是否停止进程
  1056. if (key_stop)
  1057. {
  1058. return false;
  1059. }
  1060. //2.控制SEM自动对焦、亮度、对比度-接口,,将自动对焦提出单独函数调用
  1061. ImageFocus();
  1062. Thread.Sleep(5000);
  1063. //判断是否停止进程
  1064. if (key_stop)
  1065. {
  1066. return false;
  1067. }
  1068. //3.控制SEM拍照
  1069. String fileName4 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName4;
  1070. arg.Picture_Information.Picture_FullPath = fileName4;
  1071. arg.Picture_Information.Work_Status = "SEM";
  1072. if (!GetImage(ImageMode.SEM, fileName4))
  1073. {
  1074. arg.Message = "SEM拍照失败";
  1075. SendMsg("1-20");
  1076. return false;
  1077. }
  1078. arg.Message = "SEM拍照成功";
  1079. SendMsg("1-20");
  1080. Thread.Sleep(3000);
  1081. //判断是否停止进程
  1082. if (key_stop)
  1083. {
  1084. return false;
  1085. }
  1086. //4.将照片传给客户,获取偏移坐标,以及偏移角度
  1087. //5.根据坐标控制SEM移动到切孔位置,居中
  1088. //这里是否用到拉直操作,我没有加
  1089. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  1090. {
  1091. arg.Message = "样品台移动到指定位置";
  1092. SendMsg("1-21");
  1093. }
  1094. else
  1095. {
  1096. wr.Img_Center_Position_OffsetAngle_Direction(fileName4, m_MsParam.SampleName,
  1097. m_MsParam.Firm, out x1, out y1, out angle, out direction, out state);
  1098. if (state == 1)
  1099. {
  1100. //拉直操作
  1101. Straightening(angle, 65, 65, x1, y1, ref x2, ref y2);
  1102. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  1103. if (!iSEM.MoveStageXY(x2, y2))
  1104. {
  1105. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  1106. SendMsg("1-21");
  1107. return false;
  1108. }
  1109. //判断是否移动完成
  1110. while (true)
  1111. {
  1112. Thread.Sleep(5000);
  1113. if (iSEM.GetStageIs() == 0)
  1114. {
  1115. break;
  1116. }
  1117. }
  1118. arg.Message = "移动到新(" + x2.ToString() + "," + y2.ToString() + ")位置失败";
  1119. SendMsg("1-21");
  1120. }
  1121. else
  1122. {
  1123. arg.Message = "样品台移动失败";
  1124. SendMsg("1-21");
  1125. return false;
  1126. }
  1127. }
  1128. //判断是否停止进程
  1129. if (key_stop)
  1130. {
  1131. return false;
  1132. }
  1133. //6.验证移动准确性:获取当前SEM中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1134. x0 = iSEM.GetStageAtX();
  1135. y0 = iSEM.GetStageAtY();
  1136. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  1137. {
  1138. return false;
  1139. }
  1140. //判断是否停止进程
  1141. if (key_stop)
  1142. {
  1143. return false;
  1144. }
  1145. return true;
  1146. }
  1147. /// <summary>
  1148. /// 17.自动控制SEM拍截面照
  1149. /// </summary>
  1150. /// <returns></returns>
  1151. public bool ShotSection(ref String image18)
  1152. {
  1153. float x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  1154. int state = 0;
  1155. //1、放大6000倍,放大倍数参数photograph_
  1156. if (!iSEM.SetMagnification(m_MsParam.Photograph_Magnification))
  1157. {
  1158. arg.Message = "放大倍数调整失败";
  1159. SendMsg("1-25");
  1160. return false;
  1161. }
  1162. arg.Message = "放大倍数调整成功";
  1163. SendMsg("1-25");
  1164. //判断是否停止进程
  1165. if (key_stop)
  1166. {
  1167. return false;
  1168. }
  1169. //2.控制SEM自动对焦、亮度、对比度-接口
  1170. ImageFocus();
  1171. //判断是否停止进程
  1172. if (key_stop)
  1173. {
  1174. return false;
  1175. }
  1176. //3、设置SEM补偿角度
  1177. if (!TiltCorrection(m_measureFile.MParam.Correction_Angle))
  1178. {
  1179. arg.Message = "设置SEM进行角度补偿" + m_measureFile.MParam.Correction_Angle.ToString("0") + "度失败";
  1180. SendMsg("1-27");
  1181. return false;
  1182. }
  1183. arg.Message = "设置SEM进行角度补偿" + m_measureFile.MParam.Correction_Angle.ToString("0") + "度成功";
  1184. SendMsg("1-27");
  1185. Thread.Sleep(5000);
  1186. //判断是否停止进程
  1187. if (key_stop)
  1188. {
  1189. return false;
  1190. }
  1191. //4、拍照,5、保存照片
  1192. String fileName5 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName5;
  1193. arg.Picture_Information.Picture_FullPath = fileName5;
  1194. arg.Picture_Information.Work_Status = "SEM";
  1195. if (!GetImage(ImageMode.SEM, fileName5))
  1196. {
  1197. arg.Message = "SEM拍照失败";
  1198. SendMsg("1-28");
  1199. return false;
  1200. }
  1201. arg.Message = "SEM拍照成功";
  1202. SendMsg("1-28");
  1203. Thread.Sleep(3000);
  1204. //判断是否停止进程
  1205. if (key_stop)
  1206. {
  1207. return false;
  1208. }
  1209. //6、获取偏移坐标,7、移动到位
  1210. if (DialogResult.Yes == MessageBox.Show("是否手动移动样品台到指定位置?\n如果点击取消则会调用Web接口。", "确认消息", MessageBoxButtons.YesNo))
  1211. {
  1212. arg.Message = "样品台移动到指定位置";
  1213. SendMsg("1-29");
  1214. }
  1215. else
  1216. {
  1217. //8,图像T形
  1218. wr.Img_Trapezoid_Top_Center_Position(fileName5, out x1, out y1, out state);
  1219. //接口返回像素,*pixelsize,得到坐标点。判断移动方式
  1220. if (state == 1)
  1221. {
  1222. //1. 根据客户PT沉积坐标控制FIB调整到中心位置???????
  1223. if (!iSEM.MoveStageXY(x1, y1))
  1224. {
  1225. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  1226. SendMsg("1-29");
  1227. return false;
  1228. }
  1229. //判断是否移动完成
  1230. while (true)
  1231. {
  1232. Thread.Sleep(5000);
  1233. if (iSEM.GetStageIs() == 0)
  1234. {
  1235. break;
  1236. }
  1237. }
  1238. arg.Message = "移动到新(" + x1.ToString() + "," + y1.ToString() + ")位置失败";
  1239. SendMsg("1-29");
  1240. }
  1241. else
  1242. {
  1243. arg.Message = "样品台移动失败";
  1244. SendMsg("1-8");
  1245. return false;
  1246. }
  1247. }
  1248. //判断是否停止进程
  1249. if (key_stop)
  1250. {
  1251. return false;
  1252. }
  1253. //8、验证移动准确性
  1254. x0 = iSEM.GetStageAtX();
  1255. y0 = iSEM.GetStageAtY();
  1256. if (Math.Abs(x0 - x1) > fMin && Math.Abs(y0 - y1) > fMin)
  1257. {
  1258. return false;
  1259. }
  1260. //判断是否停止进程
  1261. if (key_stop)
  1262. {
  1263. return false;
  1264. }
  1265. //9、-10不操作
  1266. //11、自动对焦。与之前操作相同
  1267. ImageFocus();
  1268. //判断是否停止进程
  1269. if (key_stop)
  1270. {
  1271. return false;
  1272. }
  1273. //12拍照
  1274. String fileName6 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1275. arg.Picture_Information.Picture_FullPath = fileName6;
  1276. arg.Picture_Information.Work_Status = "SEM";
  1277. if (!GetImage(ImageMode.SEM, fileName6))
  1278. {
  1279. arg.Message = "SEM拍照失败";
  1280. SendMsg("1-28");
  1281. return false;
  1282. }
  1283. image18 = fileName6;
  1284. arg.Message = "SEM拍照成功";
  1285. SendMsg("1-28");
  1286. Thread.Sleep(3000);
  1287. //判断是否停止进程
  1288. if (key_stop)
  1289. {
  1290. return false;
  1291. }
  1292. //14光束复位
  1293. iSEM.SetBeamShiftX(0);
  1294. iSEM.SetBeamShiftY(0);
  1295. //判断是否停止进程
  1296. if (key_stop)
  1297. {
  1298. return false;
  1299. }
  1300. return true;
  1301. }
  1302. public bool CommonWork()
  1303. {
  1304. SendMsg("插入PT针");
  1305. //12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  1306. //if (firstHole.PT == true)
  1307. {
  1308. if (!InsertPT())
  1309. {
  1310. SendMsg("插入PT针失败");
  1311. return false;
  1312. }
  1313. }
  1314. //13. 自动 定位功能
  1315. SendMsg("自动定位切割位置");
  1316. if (!GetPoistion(false))
  1317. {
  1318. SendMsg("自动定位切割位置失败");
  1319. return false;
  1320. }
  1321. //14.自动控制FIB切割
  1322. SendMsg("FIB切割");
  1323. if (!FIBCross())
  1324. {
  1325. SendMsg("FIB切割失败");
  1326. return false;
  1327. }
  1328. //{
  1329. // //1.根据参数设置FIB草率时间(使图清晰),设置梯形上下边及深度、设置束流
  1330. // //2.控制FIB进行切割
  1331. // //3.控制FIB拍照600X
  1332. // //4.保存图片
  1333. // {
  1334. // //1.设置图片名称
  1335. // //2.保存图片3
  1336. // String fileName3 = WorkingFolder + ImageName3;
  1337. // if (!GetImage(ImageMode.FIB, fileName3))
  1338. // {
  1339. // return false;
  1340. // }
  1341. // }
  1342. // //5.验证切割准确性:与切割前对比,如果对比误差大,则停止自动执行,进行报警
  1343. // //6.设置FIB解冻:先读取状态,如果冻结状态则进行解冻
  1344. //}
  1345. //15.根据样品类型决定是否撤出PT针
  1346. SendMsg("撤出PT针");
  1347. //if (firstHole.PT == false)
  1348. //{
  1349. if (!RetractPT())
  1350. {
  1351. SendMsg("撤出PT针失败");
  1352. return false;
  1353. }
  1354. //}
  1355. // 16.找到切割位置
  1356. SendMsg("找到切割位置");
  1357. //17.自动控制SEM拍截面照
  1358. SendMsg("观测层高");
  1359. SendMsg("切换SEM控制");
  1360. if (!iSEM.CmdFIBModeSEM())
  1361. {
  1362. SendMsg("SEM模式切换失败");
  1363. return false;
  1364. }
  1365. Thread.Sleep(10000);
  1366. SendMsg("放大6000倍");
  1367. if (!iSEM.SetMagnification(6000))
  1368. {
  1369. SendMsg("放大倍数调整失败");
  1370. return false;
  1371. }
  1372. if (DialogResult.Yes == MessageBox.Show("图像拍摄已完成?", "确认消息", MessageBoxButtons.YesNo))
  1373. {
  1374. SendMsg("拍摄照片完成");
  1375. //return true;
  1376. }
  1377. else
  1378. {
  1379. SendMsg("拍摄照片失败");
  1380. return false;
  1381. }
  1382. SendMsg("图像校正36度");
  1383. //3.设置SEM角度补偿cos36度
  1384. if (!TiltCorrection(36))
  1385. {
  1386. return false;
  1387. }
  1388. Thread.Sleep(3000);
  1389. string fn;
  1390. //用宏抓一般图
  1391. Thread.Sleep(5000);
  1392. fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1393. SendMsg("用宏抓一般图" + fn);
  1394. iSEM.CMDMCFFilename(fn);
  1395. //延时1s??
  1396. Thread.Sleep(5000);
  1397. //用宏抓好图
  1398. fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  1399. SendMsg("用宏抓好图" + fn);
  1400. iSEM.CMDMCFFilename(fn);
  1401. //延时1s??
  1402. Thread.Sleep(30000);
  1403. Thread.Sleep(10000);
  1404. WorkingFolder = m_measureFile.FilePath;
  1405. String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1406. SendMsg("SEM拍照存储到" + fileName6);
  1407. if (!GetImage(ImageMode.SEM, fileName6))
  1408. {
  1409. SendMsg("SEM拍照失败");
  1410. return false;
  1411. }
  1412. //用宏抓标志图
  1413. fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  1414. SendMsg("用宏抓标志图" + fn);
  1415. iSEM.CMDMCFFilename(fn);
  1416. //延时1s??
  1417. Thread.Sleep(30000);
  1418. //恢复简单抓图
  1419. Thread.Sleep(5000);
  1420. fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1421. SendMsg("用宏抓一般图" + fn);
  1422. iSEM.CMDMCFFilename(fn);
  1423. SendMsg("所有图结束");
  1424. return true;
  1425. }
  1426. //第一个孔的测试过程
  1427. public bool FirstHole()
  1428. {
  1429. MeasureData.CutHole firstHole = m_cutHoles[0];
  1430. //12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  1431. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1432. {
  1433. if (!InsertPT())
  1434. {
  1435. arg.Message = "插入PT针失败";
  1436. SendMsg("1-0");
  1437. return false;
  1438. }
  1439. arg.Message = "插入PT针成功";
  1440. SendMsg("1-0");
  1441. }
  1442. //判断是否停止进程
  1443. if (key_stop)
  1444. {
  1445. return false;
  1446. }
  1447. //13. 自动 定位功能
  1448. if (!GetPoistion(true))
  1449. {
  1450. arg.Message = "自动定位切割位置失败";
  1451. SendMsg("1-1");
  1452. return false;
  1453. }
  1454. arg.Message = "自动定位切割位置成功";
  1455. SendMsg("1-1");
  1456. //判断是否停止进程
  1457. if (key_stop)
  1458. {
  1459. return false;
  1460. }
  1461. //14.自动控制FIB切割
  1462. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1463. {
  1464. if (!FIBCross())
  1465. {
  1466. arg.Message = "FIB切割失败";
  1467. SendMsg("1-16");
  1468. return false;
  1469. }
  1470. arg.Message = "FIB切割成功";
  1471. SendMsg("1-16");
  1472. }
  1473. //判断是否停止进程
  1474. if (key_stop)
  1475. {
  1476. return false;
  1477. }
  1478. //15.根据样品类型决定是否撤出PT针
  1479. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1480. {
  1481. if (!RetractPT())
  1482. {
  1483. arg.Message = "撤出PT针失败";
  1484. SendMsg("1-17");
  1485. return false;
  1486. }
  1487. arg.Message = "撤出PT针成功";
  1488. SendMsg("1-17");
  1489. }
  1490. //判断是否停止进程
  1491. if (key_stop)
  1492. {
  1493. return false;
  1494. }
  1495. // 16.找到切割位置
  1496. if (m_MsParam.Is_Photograph == false)
  1497. {
  1498. if (!FindCross())
  1499. {
  1500. arg.Message = "找到切割位置失败";
  1501. SendMsg("1-24");
  1502. return false;
  1503. }
  1504. arg.Message = "找到切割位置成功";
  1505. SendMsg("1-24");
  1506. }
  1507. //判断是否停止进程
  1508. if (key_stop)
  1509. {
  1510. return false;
  1511. }
  1512. //将过程17最后的拍照图片提出给18步进行调用
  1513. String img18 = "";
  1514. //17.自动控制SEM拍截面照
  1515. if (m_MsParam.Is_Photograph == false)
  1516. {
  1517. if (!ShotSection(ref img18))
  1518. {
  1519. arg.Message = "自动控制SEM拍截面照失败";
  1520. SendMsg("1-33");
  1521. return false;
  1522. }
  1523. arg.Message = "自动控制SEM拍截面照成功";
  1524. SendMsg("1-33");
  1525. }
  1526. //判断是否停止进程
  1527. if (key_stop)
  1528. {
  1529. return false;
  1530. }
  1531. //18.自动层高分析
  1532. float size = iSEM.GetPixelSize();
  1533. float mag = iSEM.GetMagnification();
  1534. int state = 0;
  1535. wr.Img_Measure_Size(img18, mag, size, out state);
  1536. if(state==0)
  1537. {
  1538. arg.Message = "测量尺寸失败";
  1539. SendMsg("1-35");
  1540. return false;
  1541. }
  1542. arg.Message = "测量尺寸成功";
  1543. SendMsg("1-35");
  1544. #region 测试程序
  1545. //最后保存测量数据-测量文件、测量状态,m_cutHoles[i].STATE!=MeasureData.CutHole.State.Success.
  1546. //中间停止更新状态为Unmeasured,过程中return false时,更新状态为failed,保存测量文件
  1547. //if (DialogResult.Yes == MessageBox.Show("图像拍摄已完成?", "确认消息", MessageBoxButtons.YesNo))
  1548. //{
  1549. // SendMsg("拍摄照片完成");
  1550. // //return true;
  1551. //}
  1552. //else
  1553. //{
  1554. // SendMsg("拍摄照片失败");
  1555. // return false;
  1556. //}
  1557. //SendMsg("图像校正36度");
  1558. ////3.设置SEM角度补偿cos36度
  1559. //if (!TiltCorrection(36))
  1560. //{
  1561. // return false;
  1562. //}
  1563. //Thread.Sleep(3000);
  1564. //string fn;
  1565. //用宏抓一般图
  1566. //Thread.Sleep(5000);
  1567. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1568. //SendMsg("用宏抓一般图" + fn);
  1569. //iSEM.CMDMCFFilename(fn);
  1570. ////延时1s??
  1571. //Thread.Sleep(5000);
  1572. ////用宏抓好图
  1573. //fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  1574. //SendMsg("用宏抓好图" + fn);
  1575. //iSEM.CMDMCFFilename(fn);
  1576. ////延时1s??
  1577. //Thread.Sleep(30000);
  1578. //Thread.Sleep(10000);
  1579. //WorkingFolder = m_measureFile.FilePath;
  1580. //String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1581. //SendMsg("SEM拍照存储到" + fileName6);
  1582. //if (!GetImage(ImageMode.SEM, fileName6))
  1583. //{
  1584. // SendMsg("SEM拍照失败");
  1585. // return false;
  1586. //}
  1587. ////用宏抓标志图
  1588. //fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  1589. //SendMsg("用宏抓标志图" + fn);
  1590. //iSEM.CMDMCFFilename(fn);
  1591. ////延时1s??
  1592. //Thread.Sleep(30000);
  1593. //Thread.Sleep(5000);
  1594. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1595. //SendMsg("用宏抓一般图" + fn);
  1596. //iSEM.CMDMCFFilename(fn);
  1597. ////延时1s??
  1598. //Thread.Sleep(5000);
  1599. //SendMsg("所有图结束");
  1600. ////17.自动控制SEM拍截面照
  1601. //{
  1602. // //1.控制SEM放大到指定参数大小范围,6000x
  1603. // if (!iSEM.SetMagnification(6000))
  1604. // {
  1605. // return false;
  1606. // }
  1607. // //2.控制SEM自动对焦、消像散、亮度、对比度
  1608. // if (!MeasParam.FocusMode)
  1609. // {
  1610. // //弹出手动对焦的窗口
  1611. // }
  1612. // else
  1613. // {
  1614. // //调用自动对焦模块
  1615. // }
  1616. // //3.设置SEM角度补偿cos36度
  1617. // if (!TiltCorrection(36))
  1618. // {
  1619. // return false;
  1620. // }
  1621. // //4.控制SEM拍照
  1622. // //5.保存照片4
  1623. // String fileName5 = WorkingFolder + ImageName5;
  1624. // if (!GetImage(ImageMode.SEM, fileName5))
  1625. // {
  1626. // return false;
  1627. // }
  1628. // //6.将照片传给客户,获取偏移坐标
  1629. // float x6 = 0, y6 = 0;
  1630. // float angle1 = 0;
  1631. // float mage = 10000;
  1632. // //7.根据坐标控制SEM移动到分析位置
  1633. // if (!iSEM.MoveStageXY(x6, y6))
  1634. // {
  1635. // return false;
  1636. // }
  1637. // //8.验证移动准确性:获取当前SEM中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1638. // float x7 = iSEM.GetStageAtX();
  1639. // float y7 = iSEM.GetStageAtY();
  1640. // if (Math.Abs(x6 - x7) > fMin && Math.Abs(y6 - y7) > fMin)
  1641. // {
  1642. // return false;
  1643. // }
  1644. // //9.控制SEM平行校正,并记录校正前初始值
  1645. // float foldAnagle = iSEM.GetScanRotation();
  1646. // if (foldAnagle == float.NaN)
  1647. // {
  1648. // return false;
  1649. // }
  1650. // if (!iSEM.SetScanRotation(angle1))
  1651. // {
  1652. // return false;
  1653. // }
  1654. // //10.控制SEM放大到指定参数大小范围
  1655. // if (!iSEM.SetMagnification(mage))
  1656. // {
  1657. // return false;
  1658. // }
  1659. // //11.控制SEM自动对焦、消像散、亮度、对比度
  1660. // if (!MeasParam.FocusMode)
  1661. // {
  1662. // //弹出手动对焦的窗口
  1663. // }
  1664. // else
  1665. // {
  1666. // //调用自动对焦模块
  1667. // }
  1668. // //12.控制SEM对分析位置拍照
  1669. // String fileName6 = WorkingFolder + ImageName6;
  1670. // if (!GetImage(ImageMode.SEM, fileName6))
  1671. // {
  1672. // return false;
  1673. // }
  1674. // //13.保存照片
  1675. // //14.控制SEM取消电子束校正,回到初始值
  1676. // if (!iSEM.SetScanRotation(foldAnagle))
  1677. // {
  1678. // return false;
  1679. // }
  1680. //}
  1681. ////18.自动层高分析
  1682. //{
  1683. // //1.获取SEM Pixel Size给客户传入参数
  1684. // if (iSEM.GetPixelSize() == float.NaN)
  1685. // {
  1686. // return false;
  1687. // }
  1688. // //2.将照片传给客户,客户进行层高分析(返回分析后的图像、相对坐标、分辨率、各层编号以及各层对应的层高数据),如果客户自行出分析报告则无需返回数据
  1689. //}
  1690. ////19.自动能谱分析
  1691. //{
  1692. // //1. 确定能谱位置
  1693. // //2. 控制牛津打能谱
  1694. // //3. 能谱分析——面扫+线扫描
  1695. //}
  1696. #endregion
  1697. return true;
  1698. }
  1699. //非第一个孔的测试过程
  1700. public void OtherHole()
  1701. {
  1702. //SendMsg("当前切孔号为"+ m_nWorkHoleNo.ToString());
  1703. CutHole currentHole = m_cutHoles[m_nWorkHoleNo];
  1704. #region 1. 初始化
  1705. //1. 设置FIB解冻:先读取状态,如果冻结则进行解冻
  1706. //读取图像冻结状态
  1707. float ffrozen = iSEM.GetImageFrozen();
  1708. if (ffrozen == float.NaN)
  1709. {
  1710. return;
  1711. }
  1712. if (DialogResult.Yes == MessageBox.Show("样品台可以旋转恢复到水平吗?", "确认消息", MessageBoxButtons.YesNo))
  1713. {
  1714. //SendMsg("样品台恢复到水平");
  1715. //return true;
  1716. }
  1717. else
  1718. {
  1719. //SendMsg("禁止样品台恢复到水平");
  1720. return;
  1721. }
  1722. //2. 调节样品1号孔的样品台6轴坐标
  1723. //3. 将样品1号孔坐标2存入数据库
  1724. //4. 控制样品台T轴归0,R轴变为坐标1、Z/M轴作为坐标2保存不变
  1725. if (!iSEM.SetStageGotoT(0))
  1726. {
  1727. return;
  1728. }
  1729. while (true)
  1730. {
  1731. Thread.Sleep(10000);
  1732. if (iSEM.GetStageIs() == 0)
  1733. {
  1734. break;
  1735. }
  1736. }
  1737. //5. 获取光镜2号孔XY坐标
  1738. SendMsg("移动到"+m_nWorkHoleNo+"号孔位置:(" + currentHole.Position.X.ToString()+","+currentHole.Position.Y.ToString()+")");
  1739. //6. 根据光镜坐标控制样品台移动
  1740. if (!iSEM.MoveStageXY(currentHole.Position.X, currentHole.Position.Y))
  1741. {
  1742. SendMsg("移动失败");
  1743. }
  1744. while (true)
  1745. {
  1746. Thread.Sleep(10000);
  1747. if (iSEM.GetStageIs() == 0)
  1748. {
  1749. break;
  1750. }
  1751. }
  1752. //7. 控制样品台,调整T轴54度、M/Z/R轴不变
  1753. if (DialogResult.Yes == MessageBox.Show("样品台可以旋转吗?", "确认消息", MessageBoxButtons.YesNo))
  1754. {
  1755. SendMsg("样品台旋转");
  1756. //return true;
  1757. }
  1758. else
  1759. {
  1760. SendMsg("禁止样品台旋转");
  1761. return;
  1762. }
  1763. if (!iSEM.SetStageGotoT(54))
  1764. {
  1765. return;
  1766. }
  1767. while (true)
  1768. {
  1769. Thread.Sleep(10000);
  1770. if (iSEM.GetStageIs() == 0)
  1771. {
  1772. break;
  1773. }
  1774. }
  1775. #endregion
  1776. SendMsg("拉直操作");
  1777. SendMsg("其他操作与1号孔操作一致");
  1778. if (!CommonWork())
  1779. {
  1780. return;
  1781. }
  1782. //2. 定位切割
  1783. {
  1784. //1. 拉直操作
  1785. {
  1786. //1.1 控制SEM进行拍照
  1787. //1.2 图片传给客户,返回偏移角度
  1788. //1.3 根据返回角度,控制样品台转动
  1789. }
  1790. //2. 与1号孔13步相同
  1791. }
  1792. //3. 以后步骤与1号孔以后步骤一致
  1793. //}
  1794. }
  1795. //角度补偿
  1796. public bool TiltCorrection(float a_fAngle)
  1797. {
  1798. //记录原来电镜的状态
  1799. // bool bTilt = false;
  1800. //float fOldTilt = iSEM.GetTiltCorrection();
  1801. //if (fOldTilt == float.NaN)
  1802. //{
  1803. // return false;
  1804. //}
  1805. //else if (fOldTilt == 1)
  1806. //{
  1807. // bTilt = true;
  1808. //}
  1809. //else if (fOldTilt == 0)
  1810. //{
  1811. // bTilt = false;
  1812. //}
  1813. //float fOldAngle = iSEM.GetTiltAngle();
  1814. //if (fOldAngle == float.NaN)
  1815. //{
  1816. // return false;
  1817. //}
  1818. //开启校正
  1819. SendMsg("开启校正");
  1820. if (!iSEM.SetTiltAngleOn())
  1821. {
  1822. return false;
  1823. }
  1824. //设置校正角度
  1825. //if (!iSEM.SetTiltAngle(fOldAngle))
  1826. //{
  1827. // return false;
  1828. //}
  1829. //恢复原始状态
  1830. SendMsg("校正角度"+ a_fAngle.ToString());
  1831. if (!iSEM.SetTiltAngle(a_fAngle))
  1832. {
  1833. return false;
  1834. }
  1835. //if (bTilt)
  1836. //{
  1837. // if (!iSEM.SetTiltCorrectionOn())
  1838. // {
  1839. // return false;
  1840. // }
  1841. //}
  1842. //else
  1843. //{
  1844. // if (!iSEM.SetTiltCorrectionOff())
  1845. // {
  1846. // return false;
  1847. // }
  1848. //}
  1849. return true;
  1850. }
  1851. //拍图
  1852. public bool GetImage(ImageMode a_mode, String a_fileName)
  1853. {
  1854. //1. 图像解冻
  1855. float foldFrozen = iSEM.GetImageFrozen();
  1856. if (foldFrozen == float.NaN)
  1857. {
  1858. return false;
  1859. }
  1860. if (!iSEM.ImageLive())
  1861. {
  1862. return false;
  1863. }
  1864. //2. 确认图像模式
  1865. if (ImageMode.SEM == a_mode)
  1866. {
  1867. if (!iSEM.CmdFIBModeSEM())
  1868. {
  1869. return false;
  1870. }
  1871. }
  1872. else if (ImageMode.FIB == a_mode)
  1873. {
  1874. if (!iSEM.CmdFIBModeFIB())
  1875. {
  1876. return false;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. return false;
  1882. }
  1883. //3. 获取分辨率
  1884. int[] ImageSize = iSEM.GetImageStore();
  1885. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  1886. {
  1887. return false;
  1888. }
  1889. short width = (short)ImageSize[0];
  1890. short height = (short)ImageSize[1];
  1891. //4. 抓图
  1892. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  1893. {
  1894. return false;
  1895. }
  1896. //5. 恢复初始状态
  1897. if (foldFrozen == 0)
  1898. {
  1899. if (!iSEM.ImageLive())
  1900. {
  1901. return false;
  1902. }
  1903. }
  1904. else if (foldFrozen == 1)
  1905. {
  1906. if (!iSEM.ImageFrozen())
  1907. {
  1908. return false;
  1909. }
  1910. }
  1911. return true;
  1912. }
  1913. //自动聚焦
  1914. public bool AutoFocus(FocusParam a_param, out int a_nWd)
  1915. {
  1916. a_nWd = 0;
  1917. a_nWd = (int)Math.Ceiling(iSEM.GetWorkingDistance() * 100000000 + 0.5);//获取当前的工作距离
  1918. if (a_param == null)
  1919. {
  1920. return false;
  1921. }
  1922. string path = a_param.Path;
  1923. if (!Directory.Exists(path))
  1924. {
  1925. return false;
  1926. }
  1927. string path_coarse = a_param.Path + "\\Coarse";
  1928. if (!Directory.Exists(path_coarse))
  1929. {
  1930. Directory.CreateDirectory(path_coarse);
  1931. }
  1932. string path_fine = a_param.Path + "\\Fine";
  1933. if (!Directory.Exists(path_fine))
  1934. {
  1935. Directory.CreateDirectory(path_fine);
  1936. }
  1937. //1. 拍照
  1938. //1.1, 粗拍照
  1939. //1.1.1, 根据参数计算拍照次数
  1940. double nUp = a_param.UP * 100.0 ;
  1941. double nDown = a_param.Down * 100.0;
  1942. double nStep = a_param.Step * 100.0;
  1943. double nTimes = Math.Ceiling((nDown - nUp) / nStep + 0.5);
  1944. SendMsg("粗对焦:最小值" + nUp.ToString() + ",最大值" + nDown.ToString() + ",步长" + nStep.ToString() + "拍照张数" + nTimes.ToString());
  1945. //粗拍照
  1946. for (int i = 0; i < nTimes; i++)
  1947. {
  1948. int cWd =(int) Math.Ceiling(nUp + i * nStep + 0.5);
  1949. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  1950. Thread.Sleep(3000);
  1951. int swd = cWd / 100;
  1952. SendMsg("wd = " + swd.ToString() +"um");
  1953. String fileName = path_coarse + "\\" + cWd.ToString() + ".tif";
  1954. if (!GetImage(ImageMode.SEM, fileName))
  1955. {
  1956. return false;
  1957. }
  1958. Thread.Sleep(5000);
  1959. }
  1960. //1.2, 选定细拍照的工作距离
  1961. int CorWd;
  1962. ChooseBest(path_coarse,out CorWd);
  1963. a_nWd = CorWd;
  1964. SendMsg("粗选工作距离wd = " + a_nWd.ToString() );
  1965. //1.3, 细拍照
  1966. if ( (CorWd - a_param.Range * 100.0) <=0)
  1967. {
  1968. return false;
  1969. }
  1970. double nfUp = CorWd - a_param.Range * 100.0;
  1971. double nfStep = a_param.fStep * 100.0;
  1972. double nfTimes = Math.Ceiling((a_param.Range * 100.0 * 2) / nfStep + 0.5);
  1973. SendMsg("粗对焦:最小值" + nfUp.ToString() + ",步长" + nfStep.ToString() + "拍照张数" + nfTimes.ToString());
  1974. for (int i = 0; i < nfTimes; i++)
  1975. {
  1976. int cWd = (int)Math.Ceiling(nfUp + i * nfStep + 0.5);
  1977. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  1978. Thread.Sleep(3000);
  1979. int swd = cWd / 100;
  1980. SendMsg("wd = " + swd.ToString() + "um");
  1981. String fileName = path_fine + "\\" + cWd.ToString() + ".tif";
  1982. if (!GetImage(ImageMode.SEM, fileName))
  1983. {
  1984. return false;
  1985. }
  1986. Thread.Sleep(5000);
  1987. }
  1988. int fineWd;
  1989. ChooseBest(path_coarse, out fineWd);
  1990. SendMsg("wd fine max = " + fineWd.ToString());
  1991. a_nWd = fineWd;
  1992. SendMsg("细选工作距离wd = " + a_nWd.ToString());
  1993. return true;
  1994. }
  1995. //LoG算子计算
  1996. private double LoGMean(string a_strImgPath)
  1997. {
  1998. //读入Img文件
  1999. if (!File.Exists(a_strImgPath))
  2000. {
  2001. return 0;
  2002. }
  2003. Mat src, gray_src;
  2004. int kernel_size = 3;
  2005. src = Cv2.ImRead(a_strImgPath);
  2006. gray_src = new Mat();
  2007. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2008. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2009. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2010. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2011. Cv2.Laplacian(src, src, MatType.CV_8UC3, kernel_size);
  2012. Cv2.ConvertScaleAbs(src, src);
  2013. //图像的平均灰度
  2014. double meanValue = 0.0;
  2015. meanValue = Cv2.Mean(src)[0];
  2016. return meanValue;
  2017. }
  2018. //梯度计算
  2019. private double Tenengrad(string a_strImgPath)
  2020. {
  2021. //读入Img文件
  2022. if (!File.Exists(a_strImgPath))
  2023. {
  2024. return 0;
  2025. }
  2026. Mat src;
  2027. src = Cv2.ImRead(a_strImgPath);
  2028. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2029. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2030. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2031. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2032. Cv2.Sobel(src, src, MatType.CV_8UC1, 1, 1);
  2033. //图像的平均灰度
  2034. double meanValue = 0.0;
  2035. meanValue = Cv2.Mean(src)[0];
  2036. return meanValue;
  2037. }
  2038. //梯度计算
  2039. private double TTgrad(string a_strImgPath)
  2040. {
  2041. //读入Img文件
  2042. if (!File.Exists(a_strImgPath))
  2043. {
  2044. return 0;
  2045. }
  2046. Mat src;
  2047. src = Cv2.ImRead(a_strImgPath);
  2048. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2049. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2050. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2051. Mat scalex = new Mat();
  2052. Mat scaley = new Mat();
  2053. Cv2.Scharr(src, src, src.Depth(), 1, 0);
  2054. Cv2.ConvertScaleAbs(src, scalex);
  2055. Cv2.Scharr(src, src, src.Depth(), 0, 1);
  2056. Cv2.ConvertScaleAbs(src, scaley);
  2057. Cv2.AddWeighted(scalex, 0.5, scaley, 0.5, 0, src);
  2058. //图像的平均灰度
  2059. double meanValue = 0.0;
  2060. meanValue = Cv2.Mean(src)[0];
  2061. return meanValue;
  2062. }
  2063. //三选二算法
  2064. private void ChooseBest(string a_strImgPath, out int a_nWd)
  2065. {
  2066. a_nWd = -1;
  2067. var files = Directory.GetFiles(a_strImgPath, "*.tif");
  2068. float[] values = new float[files.Length];
  2069. double smax1 = LoGMean(files[0]);
  2070. int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  2071. double smax2 = Tenengrad(files[0]);
  2072. int wdmax2 = wdmax1;
  2073. double smax3 = TTgrad(files[0]);
  2074. int wdmax3 = wdmax1;
  2075. int x = 0;
  2076. foreach (var file in files)
  2077. {
  2078. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  2079. double svalue1 = LoGMean(file);
  2080. double svalue2 = Tenengrad(file);
  2081. double svalue3 = TTgrad(file);
  2082. if (smax1 < svalue1)
  2083. {
  2084. smax1 = svalue1;
  2085. wdmax1 = wd;
  2086. }
  2087. if (smax2 < svalue2)
  2088. {
  2089. smax2 = svalue2;
  2090. wdmax2 = wd;
  2091. }
  2092. if (smax3 < svalue3)
  2093. {
  2094. smax3 = svalue3;
  2095. wdmax3 = wd;
  2096. }
  2097. x++;
  2098. }
  2099. SendMsg("LoG = " + wdmax1.ToString() + ",梯度 = " + wdmax2.ToString() + ",滤波 = " + wdmax3.ToString());
  2100. if (wdmax1 == wdmax2 || wdmax1 == wdmax3)
  2101. {
  2102. a_nWd = wdmax1;
  2103. SendMsg("焦距 = " + wdmax1.ToString());
  2104. }
  2105. else if (wdmax1 == wdmax2 || wdmax2 == wdmax3)
  2106. {
  2107. a_nWd = wdmax2;
  2108. SendMsg("焦距 = " + wdmax2.ToString());
  2109. }
  2110. else if (wdmax3 == wdmax2 || wdmax1 == wdmax3)
  2111. {
  2112. a_nWd = wdmax3;
  2113. SendMsg("焦距 = " + wdmax3.ToString());
  2114. }
  2115. else
  2116. {
  2117. SendMsg("对焦失败。");
  2118. }
  2119. }
  2120. }
  2121. }