Measure.cs 79 KB

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