Measure.cs 86 KB

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