Measure.cs 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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. //12拍照
  1469. String fileName6 = data_path + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1470. arg.Picture_Information.Picture_FullPath = fileName6;
  1471. arg.Picture_Information.Work_Status = "SEM";
  1472. arg.Picture_Information.Work_Voltage = iSEM.GetSEMVoltage();
  1473. Thread.Sleep(1000);
  1474. arg.Picture_Information.Magnification = iSEM.GetMagnification();
  1475. Thread.Sleep(1000);
  1476. arg.Picture_Information.Work_Distance = iSEM.GetWorkingDistance();
  1477. Thread.Sleep(1000);
  1478. if (!GetImage(ImageMode.SEM, fileName6))
  1479. {
  1480. arg.Message = "SEM拍照失败";
  1481. arg.State = false;
  1482. SendMsg("1-29");
  1483. return false;
  1484. }
  1485. Thread.Sleep(3000);
  1486. arg.State = true;
  1487. image18 = fileName6;
  1488. arg.Message = "SEM拍照成功";
  1489. SendMsg("1-29");
  1490. //判断是否停止进程
  1491. if (key_stop)
  1492. {
  1493. return false;
  1494. }
  1495. //14光束复位
  1496. iSEM.SetBeamShiftX(0);
  1497. iSEM.SetBeamShiftY(0);
  1498. Thread.Sleep(3000);
  1499. arg.Message = "光束复位成功";
  1500. arg.State = true;
  1501. SendMsg("1-30");
  1502. //判断是否停止进程
  1503. if (key_stop)
  1504. {
  1505. return false;
  1506. }
  1507. return true;
  1508. }
  1509. //第一个孔的测试过程
  1510. public bool FirstHole()
  1511. {
  1512. MeasureData.CutHole firstHole = m_cutHoles[0];
  1513. //首先默认为SEM模式
  1514. arg.Picture_Information.Work_Status = "SEM";
  1515. //12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  1516. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1517. {
  1518. if (!InsertPT())
  1519. {
  1520. arg.Message = "插入PT针失败";
  1521. arg.State = false;
  1522. SendMsg("1-0");
  1523. return false;
  1524. }
  1525. arg.State = true;
  1526. arg.Message = "插入PT针成功";
  1527. SendMsg("1-0");
  1528. }
  1529. //判断是否停止进程
  1530. if (key_stop)
  1531. {
  1532. return false;
  1533. }
  1534. //13. 自动 定位功能
  1535. if (!GetPoistion(true))
  1536. {
  1537. return false;
  1538. }
  1539. //判断是否停止进程
  1540. if (key_stop)
  1541. {
  1542. return false;
  1543. }
  1544. //14.自动控制FIB切割
  1545. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1546. {
  1547. if (!FIBCross())
  1548. {
  1549. return false;
  1550. }
  1551. }
  1552. //判断是否停止进程
  1553. if (key_stop)
  1554. {
  1555. return false;
  1556. }
  1557. //15.根据样品类型决定是否撤出PT针
  1558. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1559. {
  1560. if (!RetractPT())
  1561. {
  1562. arg.Message = "撤出PT针失败";
  1563. arg.State = false;
  1564. SendMsg("1-15");
  1565. return false;
  1566. }
  1567. arg.State = true;
  1568. arg.Message = "撤出PT针成功";
  1569. SendMsg("1-15");
  1570. }
  1571. //判断是否停止进程
  1572. if (key_stop)
  1573. {
  1574. return false;
  1575. }
  1576. // 16.找到切割位置
  1577. if (m_MsParam.Is_Photograph == false)
  1578. {
  1579. if (!FindCross())
  1580. {
  1581. return false;
  1582. }
  1583. //判断是否停止进程
  1584. if (key_stop)
  1585. {
  1586. return false;
  1587. }
  1588. }
  1589. //将过程17最后的拍照图片提出给18步进行调用
  1590. String img18 = "";
  1591. //17.自动控制SEM拍截面照
  1592. if (!ShotSection(ref img18))
  1593. {
  1594. return false;
  1595. }
  1596. //判断是否停止进程
  1597. if (key_stop)
  1598. {
  1599. return false;
  1600. }
  1601. //18.自动层高分析
  1602. if (m_MsParam.Is_Photograph == false)
  1603. {
  1604. float size = iSEM.GetPixelSize();
  1605. float mag = iSEM.GetMagnification();
  1606. int state = 0;
  1607. wr.Img_Measure_Size(img18, mag, size, out state);
  1608. if (state == 0)
  1609. {
  1610. arg.Message = "测量尺寸失败";
  1611. arg.State = false;
  1612. SendMsg("1-31");
  1613. return false;
  1614. }
  1615. arg.State = true;
  1616. arg.Message = "测量尺寸成功";
  1617. SendMsg("1-31");
  1618. }
  1619. #region 测试程序
  1620. //最后保存测量数据-测量文件、测量状态,m_cutHoles[i].STATE!=MeasureData.CutHole.State.Success.
  1621. //中间停止更新状态为Unmeasured,过程中return false时,更新状态为failed,保存测量文件
  1622. //if (DialogResult.Yes == MessageBox.Show("图像拍摄已完成?", "确认消息", MessageBoxButtons.YesNo))
  1623. //{
  1624. // SendMsg("拍摄照片完成");
  1625. // //return true;
  1626. //}
  1627. //else
  1628. //{
  1629. // SendMsg("拍摄照片失败");
  1630. // return false;
  1631. //}
  1632. //SendMsg("图像校正36度");
  1633. ////3.设置SEM角度补偿cos36度
  1634. //if (!TiltCorrection(36))
  1635. //{
  1636. // return false;
  1637. //}
  1638. //Thread.Sleep(3000);
  1639. //string fn;
  1640. //用宏抓一般图
  1641. //Thread.Sleep(5000);
  1642. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1643. //SendMsg("用宏抓一般图" + fn);
  1644. //iSEM.CMDMCFFilename(fn);
  1645. ////延时1s??
  1646. //Thread.Sleep(5000);
  1647. ////用宏抓好图
  1648. //fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  1649. //SendMsg("用宏抓好图" + fn);
  1650. //iSEM.CMDMCFFilename(fn);
  1651. ////延时1s??
  1652. //Thread.Sleep(30000);
  1653. //Thread.Sleep(10000);
  1654. //WorkingFolder = m_measureFile.FilePath;
  1655. //String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1656. //SendMsg("SEM拍照存储到" + fileName6);
  1657. //if (!GetImage(ImageMode.SEM, fileName6))
  1658. //{
  1659. // SendMsg("SEM拍照失败");
  1660. // return false;
  1661. //}
  1662. ////用宏抓标志图
  1663. //fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  1664. //SendMsg("用宏抓标志图" + fn);
  1665. //iSEM.CMDMCFFilename(fn);
  1666. ////延时1s??
  1667. //Thread.Sleep(30000);
  1668. //Thread.Sleep(5000);
  1669. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1670. //SendMsg("用宏抓一般图" + fn);
  1671. //iSEM.CMDMCFFilename(fn);
  1672. ////延时1s??
  1673. //Thread.Sleep(5000);
  1674. //SendMsg("所有图结束");
  1675. ////17.自动控制SEM拍截面照
  1676. //{
  1677. // //1.控制SEM放大到指定参数大小范围,6000x
  1678. // if (!iSEM.SetMagnification(6000))
  1679. // {
  1680. // return false;
  1681. // }
  1682. // //2.控制SEM自动对焦、消像散、亮度、对比度
  1683. // if (!MeasParam.FocusMode)
  1684. // {
  1685. // //弹出手动对焦的窗口
  1686. // }
  1687. // else
  1688. // {
  1689. // //调用自动对焦模块
  1690. // }
  1691. // //3.设置SEM角度补偿cos36度
  1692. // if (!TiltCorrection(36))
  1693. // {
  1694. // return false;
  1695. // }
  1696. // //4.控制SEM拍照
  1697. // //5.保存照片4
  1698. // String fileName5 = WorkingFolder + ImageName5;
  1699. // if (!GetImage(ImageMode.SEM, fileName5))
  1700. // {
  1701. // return false;
  1702. // }
  1703. // //6.将照片传给客户,获取偏移坐标
  1704. // float x6 = 0, y6 = 0;
  1705. // float angle1 = 0;
  1706. // float mage = 10000;
  1707. // //7.根据坐标控制SEM移动到分析位置
  1708. // if (!iSEM.MoveStageXY(x6, y6))
  1709. // {
  1710. // return false;
  1711. // }
  1712. // //8.验证移动准确性:获取当前SEM中心位置坐标,与客户返回坐标对比,验证是否移动正确
  1713. // float x7 = iSEM.GetStageAtX();
  1714. // float y7 = iSEM.GetStageAtY();
  1715. // if (Math.Abs(x6 - x7) > fMin && Math.Abs(y6 - y7) > fMin)
  1716. // {
  1717. // return false;
  1718. // }
  1719. // //9.控制SEM平行校正,并记录校正前初始值
  1720. // float foldAnagle = iSEM.GetScanRotation();
  1721. // if (foldAnagle == float.NaN)
  1722. // {
  1723. // return false;
  1724. // }
  1725. // if (!iSEM.SetScanRotation(angle1))
  1726. // {
  1727. // return false;
  1728. // }
  1729. // //10.控制SEM放大到指定参数大小范围
  1730. // if (!iSEM.SetMagnification(mage))
  1731. // {
  1732. // return false;
  1733. // }
  1734. // //11.控制SEM自动对焦、消像散、亮度、对比度
  1735. // if (!MeasParam.FocusMode)
  1736. // {
  1737. // //弹出手动对焦的窗口
  1738. // }
  1739. // else
  1740. // {
  1741. // //调用自动对焦模块
  1742. // }
  1743. // //12.控制SEM对分析位置拍照
  1744. // String fileName6 = WorkingFolder + ImageName6;
  1745. // if (!GetImage(ImageMode.SEM, fileName6))
  1746. // {
  1747. // return false;
  1748. // }
  1749. // //13.保存照片
  1750. // //14.控制SEM取消电子束校正,回到初始值
  1751. // if (!iSEM.SetScanRotation(foldAnagle))
  1752. // {
  1753. // return false;
  1754. // }
  1755. //}
  1756. ////18.自动层高分析
  1757. //{
  1758. // //1.获取SEM Pixel Size给客户传入参数
  1759. // if (iSEM.GetPixelSize() == float.NaN)
  1760. // {
  1761. // return false;
  1762. // }
  1763. // //2.将照片传给客户,客户进行层高分析(返回分析后的图像、相对坐标、分辨率、各层编号以及各层对应的层高数据),如果客户自行出分析报告则无需返回数据
  1764. //}
  1765. ////19.自动能谱分析
  1766. //{
  1767. // //1. 确定能谱位置
  1768. // //2. 控制牛津打能谱
  1769. // //3. 能谱分析——面扫+线扫描
  1770. //}
  1771. #endregion
  1772. return true;
  1773. }
  1774. //非第一个孔的测试过程
  1775. public bool CommonWork()
  1776. {
  1777. //首先默认为SEM模式
  1778. arg.Picture_Information.Work_Status = "SEM";
  1779. //12.根据样品类型参数确定是否需要PT沉积,控制PT针插入
  1780. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1781. {
  1782. if (!InsertPT())
  1783. {
  1784. arg.Message = "插入PT针失败";
  1785. SendMsg("1-0");
  1786. return false;
  1787. }
  1788. arg.Message = "插入PT针成功";
  1789. SendMsg("1-0");
  1790. }
  1791. //判断是否停止进程
  1792. if (key_stop)
  1793. {
  1794. return false;
  1795. }
  1796. //13. 自动 定位功能
  1797. if (!GetPoistion(false))
  1798. {
  1799. return false;
  1800. }
  1801. //判断是否停止进程
  1802. if (key_stop)
  1803. {
  1804. return false;
  1805. }
  1806. //14.自动控制FIB切割
  1807. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1808. {
  1809. if (!FIBCross())
  1810. {
  1811. return false;
  1812. }
  1813. }
  1814. //判断是否停止进程
  1815. if (key_stop)
  1816. {
  1817. return false;
  1818. }
  1819. //15.根据样品类型决定是否撤出PT针
  1820. if (m_MsParam.Is_Photograph == false && m_MsParam.PT == true)
  1821. {
  1822. if (!RetractPT())
  1823. {
  1824. arg.Message = "撤出PT针失败";
  1825. SendMsg("1-15");
  1826. return false;
  1827. }
  1828. arg.Message = "撤出PT针成功";
  1829. SendMsg("1-15");
  1830. }
  1831. //判断是否停止进程
  1832. if (key_stop)
  1833. {
  1834. return false;
  1835. }
  1836. // 16.找到切割位置
  1837. if (m_MsParam.Is_Photograph == false)
  1838. {
  1839. if (!FindCross())
  1840. {
  1841. return false;
  1842. }
  1843. //判断是否停止进程
  1844. if (key_stop)
  1845. {
  1846. return false;
  1847. }
  1848. }
  1849. //将过程17最后的拍照图片提出给18步进行调用
  1850. String img18 = "";
  1851. //17.自动控制SEM拍截面照
  1852. if (!ShotSection(ref img18))
  1853. {
  1854. return false;
  1855. }
  1856. //判断是否停止进程
  1857. if (key_stop)
  1858. {
  1859. return false;
  1860. }
  1861. //18.自动层高分析
  1862. if (m_MsParam.Is_Photograph == false)
  1863. {
  1864. float size = iSEM.GetPixelSize();
  1865. float mag = iSEM.GetMagnification();
  1866. int state = 0;
  1867. wr.Img_Measure_Size(img18, mag, size, out state);
  1868. if (state == 0)
  1869. {
  1870. arg.Message = "测量尺寸失败";
  1871. SendMsg("1-31");
  1872. return false;
  1873. }
  1874. arg.Message = "测量尺寸成功";
  1875. SendMsg("1-31");
  1876. }
  1877. #region 测试程序
  1878. //string fn;
  1879. //用宏抓一般图
  1880. //Thread.Sleep(5000);
  1881. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1882. //SendMsg("用宏抓一般图" + fn);
  1883. //iSEM.CMDMCFFilename(fn);
  1884. ////延时1s??
  1885. //Thread.Sleep(5000);
  1886. ////用宏抓好图
  1887. //fn = m_ProgramFolder + "\\Macro\\" + MacoGoodPic;
  1888. //SendMsg("用宏抓好图" + fn);
  1889. //iSEM.CMDMCFFilename(fn);
  1890. ////延时1s??
  1891. //Thread.Sleep(30000);
  1892. //Thread.Sleep(10000);
  1893. //WorkingFolder = m_measureFile.FilePath;
  1894. //String fileName6 = WorkingFolder + "\\" + m_nWorkHoleNo.ToString() + "Hole" + ImageName6;
  1895. //SendMsg("SEM拍照存储到" + fileName6);
  1896. //if (!GetImage(ImageMode.SEM, fileName6))
  1897. //{
  1898. // SendMsg("SEM拍照失败");
  1899. // return false;
  1900. //}
  1901. ////用宏抓标志图
  1902. //fn = m_ProgramFolder + "\\Macro\\" + MacoExportTif;
  1903. //SendMsg("用宏抓标志图" + fn);
  1904. //iSEM.CMDMCFFilename(fn);
  1905. ////延时1s??
  1906. //Thread.Sleep(30000);
  1907. ////恢复简单抓图
  1908. //Thread.Sleep(5000);
  1909. //fn = m_ProgramFolder + "\\Macro\\" + MacoScanPic;
  1910. //SendMsg("用宏抓一般图" + fn);
  1911. //iSEM.CMDMCFFilename(fn);
  1912. //SendMsg("所有图结束");
  1913. #endregion
  1914. return true;
  1915. }
  1916. //测试完第一个孔后的样品台初始化操作。
  1917. //再调用CommonWork执行非第一个孔的测试过程
  1918. public bool OtherHole()
  1919. {
  1920. //SendMsg("当前切孔号为"+ m_nWorkHoleNo.ToString());
  1921. CutHole currentHole = m_cutHoles[m_nWorkHoleNo];
  1922. #region 1. 初始化
  1923. //1. 设置FIB解冻:先读取状态,如果冻结则进行解冻
  1924. //读取图像冻结状态
  1925. float ffrozen = iSEM.GetImageFrozen();
  1926. if (ffrozen == float.NaN)
  1927. {
  1928. return false;
  1929. }
  1930. if (DialogResult.Yes == MessageBox.Show("样品台可以旋转恢复到水平吗?", "确认消息", MessageBoxButtons.YesNo))
  1931. {
  1932. //SendMsg("样品台恢复到水平");
  1933. //return true;
  1934. }
  1935. else
  1936. {
  1937. //SendMsg("禁止样品台恢复到水平");
  1938. return false;
  1939. }
  1940. //2. 调节样品1号孔的样品台6轴坐标
  1941. //3. 将样品1号孔坐标2存入数据库
  1942. //4. 控制样品台T轴归0,R轴变为坐标1、Z/M轴作为坐标2保存不变
  1943. if (!iSEM.SetStageGotoT(0))
  1944. {
  1945. return false;
  1946. }
  1947. while (true)
  1948. {
  1949. Thread.Sleep(10000);
  1950. if (iSEM.GetStageIs() == 0)
  1951. {
  1952. break;
  1953. }
  1954. }
  1955. //5. 获取光镜2号孔XY坐标
  1956. //SendMsg("移动到"+m_nWorkHoleNo+"号孔位置:(" + currentHole.Position.X.ToString()+","+currentHole.Position.Y.ToString()+")");
  1957. //6. 根据光镜坐标控制样品台移动
  1958. if (!iSEM.MoveStageXY(currentHole.Position.X, currentHole.Position.Y))
  1959. {
  1960. //SendMsg("移动失败");
  1961. return false;
  1962. }
  1963. while (true)
  1964. {
  1965. Thread.Sleep(10000);
  1966. if (iSEM.GetStageIs() == 0)
  1967. {
  1968. break;
  1969. }
  1970. }
  1971. //7. 控制样品台,调整T轴54度、M/Z/R轴不变
  1972. if (DialogResult.Yes == MessageBox.Show("样品台可以旋转吗?", "确认消息", MessageBoxButtons.YesNo))
  1973. {
  1974. //SendMsg("样品台旋转");
  1975. //return true;
  1976. }
  1977. else
  1978. {
  1979. //SendMsg("禁止样品台旋转");
  1980. return false;
  1981. }
  1982. if (!iSEM.SetStageGotoT(54))
  1983. {
  1984. return false;
  1985. }
  1986. while (true)
  1987. {
  1988. Thread.Sleep(10000);
  1989. if (iSEM.GetStageIs() == 0)
  1990. {
  1991. break;
  1992. }
  1993. }
  1994. //SendMsg("拉直操作");
  1995. #endregion
  1996. //其他操作与第一个孔相同
  1997. if (!CommonWork())
  1998. {
  1999. return false;
  2000. }
  2001. //2. 定位切割
  2002. {
  2003. //1. 拉直操作
  2004. {
  2005. //1.1 控制SEM进行拍照
  2006. //1.2 图片传给客户,返回偏移角度
  2007. //1.3 根据返回角度,控制样品台转动
  2008. }
  2009. //2. 与1号孔13步相同
  2010. }
  2011. return true;
  2012. }
  2013. //角度补偿
  2014. public bool TiltCorrection(float a_fAngle)
  2015. {
  2016. //记录原来电镜的状态
  2017. // bool bTilt = false;
  2018. //float fOldTilt = iSEM.GetTiltCorrection();
  2019. //if (fOldTilt == float.NaN)
  2020. //{
  2021. // return false;
  2022. //}
  2023. //else if (fOldTilt == 1)
  2024. //{
  2025. // bTilt = true;
  2026. //}
  2027. //else if (fOldTilt == 0)
  2028. //{
  2029. // bTilt = false;
  2030. //}
  2031. //float fOldAngle = iSEM.GetTiltAngle();
  2032. //if (fOldAngle == float.NaN)
  2033. //{
  2034. // return false;
  2035. //}
  2036. //开启校正
  2037. //SendMsg("开启校正");
  2038. if (!iSEM.SetTiltAngleOn())
  2039. {
  2040. return false;
  2041. }
  2042. //设置校正角度
  2043. //if (!iSEM.SetTiltAngle(fOldAngle))
  2044. //{
  2045. // return false;
  2046. //}
  2047. //恢复原始状态
  2048. //SendMsg("校正角度"+ a_fAngle.ToString());
  2049. if (!iSEM.SetTiltAngle(a_fAngle))
  2050. {
  2051. return false;
  2052. }
  2053. //if (bTilt)
  2054. //{
  2055. // if (!iSEM.SetTiltCorrectionOn())
  2056. // {
  2057. // return false;
  2058. // }
  2059. //}
  2060. //else
  2061. //{
  2062. // if (!iSEM.SetTiltCorrectionOff())
  2063. // {
  2064. // return false;
  2065. // }
  2066. //}
  2067. return true;
  2068. }
  2069. //拍图
  2070. public bool GetImage(ImageMode a_mode, String a_fileName)
  2071. {
  2072. //1. 图像解冻
  2073. float foldFrozen = iSEM.GetImageFrozen();
  2074. if (foldFrozen == float.NaN)
  2075. {
  2076. return false;
  2077. }
  2078. if (!iSEM.ImageLive())
  2079. {
  2080. return false;
  2081. }
  2082. //2. 确认图像模式
  2083. if (ImageMode.SEM == a_mode)
  2084. {
  2085. if (!iSEM.CmdFIBModeSEM())
  2086. {
  2087. return false;
  2088. }
  2089. }
  2090. else if (ImageMode.FIB == a_mode)
  2091. {
  2092. if (!iSEM.CmdFIBModeFIB())
  2093. {
  2094. return false;
  2095. }
  2096. }
  2097. else
  2098. {
  2099. return false;
  2100. }
  2101. //3. 获取分辨率
  2102. int[] ImageSize = iSEM.GetImageStore();
  2103. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  2104. {
  2105. return false;
  2106. }
  2107. short width = (short)ImageSize[0];
  2108. short height = (short)ImageSize[1];
  2109. //4. 抓图
  2110. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  2111. {
  2112. return false;
  2113. }
  2114. //5. 恢复初始状态
  2115. if (foldFrozen == 0)
  2116. {
  2117. if (!iSEM.ImageLive())
  2118. {
  2119. return false;
  2120. }
  2121. }
  2122. else if (foldFrozen == 1)
  2123. {
  2124. if (!iSEM.ImageFrozen())
  2125. {
  2126. return false;
  2127. }
  2128. }
  2129. return true;
  2130. }
  2131. //自动聚焦
  2132. public bool AutoFocus(FocusParam a_param, out int a_nWd, String step_code)
  2133. {
  2134. a_nWd = 0;
  2135. float wd = iSEM.GetWorkingDistance();
  2136. a_nWd = (int)Math.Ceiling(wd * 100000000 + 0.5);//获取当前的工作距离
  2137. if (a_param == null)
  2138. {
  2139. return false;
  2140. }
  2141. string path = a_param.Path;
  2142. if (!Directory.Exists(path))
  2143. {
  2144. return false;
  2145. }
  2146. string path_coarse = a_param.Path + "\\Coarse";
  2147. if (!Directory.Exists(path_coarse))
  2148. {
  2149. Directory.CreateDirectory(path_coarse);
  2150. }
  2151. string path_fine = a_param.Path + "\\Fine";
  2152. if (!Directory.Exists(path_fine))
  2153. {
  2154. Directory.CreateDirectory(path_fine);
  2155. }
  2156. //1. 拍照
  2157. //1.1, 粗拍照
  2158. //1.1.1, 根据参数计算拍照次数
  2159. double nUp = a_param.UP * 100.0 ;
  2160. double nDown = a_param.Down * 100.0;
  2161. double nStep = a_param.Step * 100.0;
  2162. double nTimes = Math.Ceiling((nDown - nUp) / nStep + 0.5);
  2163. //SendMsg("粗对焦:最小值" + nUp.ToString() + ",最大值" + nDown.ToString() + ",步长" + nStep.ToString() + "拍照张数" + nTimes.ToString());
  2164. //粗拍照
  2165. for (int i = 0; i < nTimes; i++)
  2166. {
  2167. int cWd =(int) Math.Ceiling(nUp + i * nStep + 0.5);
  2168. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  2169. arg.Picture_Information.Work_Distance = (float)(cWd * 0.00000001);
  2170. Thread.Sleep(8000);
  2171. int swd = cWd / 100;
  2172. //SendMsg("wd = " + swd.ToString() +"um");
  2173. String fileName = path_coarse + "\\" + cWd.ToString() + ".tif";
  2174. arg.Picture_Information.Picture_FullPath = fileName;
  2175. if (!GetImage(ImageMode.SEM, fileName))
  2176. {
  2177. return false;
  2178. }
  2179. SendMsg(step_code);
  2180. }
  2181. //1.2, 选定细拍照的工作距离
  2182. int CorWd;
  2183. ChooseBest(path_coarse,out CorWd);
  2184. a_nWd = CorWd;
  2185. //SendMsg("粗选工作距离wd = " + a_nWd.ToString() );
  2186. //1.3, 细拍照
  2187. if ( (CorWd - a_param.Range * 100.0) <=0)
  2188. {
  2189. return false;
  2190. }
  2191. double nfUp = CorWd - a_param.Range * 100.0;
  2192. double nfStep = a_param.fStep * 100.0;
  2193. double nfTimes = Math.Ceiling((a_param.Range * 100.0 * 2) / nfStep + 0.5);
  2194. //SendMsg("粗对焦:最小值" + nfUp.ToString() + ",步长" + nfStep.ToString() + "拍照张数" + nfTimes.ToString());
  2195. for (int i = 0; i < nfTimes; i++)
  2196. {
  2197. int cWd = (int)Math.Ceiling(nfUp + i * nfStep + 0.5);
  2198. iSEM.SetWorkingDistance((float)(cWd * 0.00000001));
  2199. arg.Picture_Information.Work_Distance = (float)(cWd * 0.00000001);
  2200. Thread.Sleep(8000);
  2201. int swd = cWd / 100;
  2202. //SendMsg("wd = " + swd.ToString() + "um");
  2203. String fileName = path_fine + "\\" + cWd.ToString() + ".tif";
  2204. arg.Picture_Information.Picture_FullPath = fileName;
  2205. if (!GetImage(ImageMode.SEM, fileName))
  2206. {
  2207. return false;
  2208. }
  2209. SendMsg(step_code);
  2210. }
  2211. int fineWd;
  2212. ChooseBest(path_fine, out fineWd);
  2213. //SendMsg("wd fine max = " + fineWd.ToString());
  2214. a_nWd = fineWd;
  2215. //SendMsg("细选工作距离wd = " + a_nWd.ToString());
  2216. return true;
  2217. }
  2218. //LoG算子计算
  2219. private double LoGMean(string a_strImgPath)
  2220. {
  2221. //读入Img文件
  2222. if (!File.Exists(a_strImgPath))
  2223. {
  2224. return 0;
  2225. }
  2226. Mat src, gray_src;
  2227. int kernel_size = 3;
  2228. src = Cv2.ImRead(a_strImgPath);
  2229. gray_src = new Mat();
  2230. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2231. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2232. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2233. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2234. Cv2.Laplacian(src, src, MatType.CV_8UC3, kernel_size);
  2235. Cv2.ConvertScaleAbs(src, src);
  2236. //图像的平均灰度
  2237. double meanValue = 0.0;
  2238. meanValue = Cv2.Mean(src)[0];
  2239. return meanValue;
  2240. }
  2241. //梯度计算
  2242. private double Tenengrad(string a_strImgPath)
  2243. {
  2244. //读入Img文件
  2245. if (!File.Exists(a_strImgPath))
  2246. {
  2247. return 0;
  2248. }
  2249. Mat src;
  2250. src = Cv2.ImRead(a_strImgPath);
  2251. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2252. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2253. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2254. Cv2.GaussianBlur(src, src, new OpenCvSharp.Size(3, 3), 0, 0);
  2255. Cv2.Sobel(src, src, MatType.CV_8UC1, 1, 1);
  2256. //图像的平均灰度
  2257. double meanValue = 0.0;
  2258. meanValue = Cv2.Mean(src)[0];
  2259. return meanValue;
  2260. }
  2261. //梯度计算
  2262. private double TTgrad(string a_strImgPath)
  2263. {
  2264. //读入Img文件
  2265. if (!File.Exists(a_strImgPath))
  2266. {
  2267. return 0;
  2268. }
  2269. Mat src;
  2270. src = Cv2.ImRead(a_strImgPath);
  2271. src.CvtColor(ColorConversionCodes.BGR2GRAY);
  2272. Mat kernel = Cv2.GetStructuringElement(MorphShapes.Cross, new OpenCvSharp.Size(10, 10));
  2273. Cv2.MorphologyEx(src, src, MorphTypes.Close, kernel);
  2274. Mat scalex = new Mat();
  2275. Mat scaley = new Mat();
  2276. Cv2.Scharr(src, src, src.Depth(), 1, 0);
  2277. Cv2.ConvertScaleAbs(src, scalex);
  2278. Cv2.Scharr(src, src, src.Depth(), 0, 1);
  2279. Cv2.ConvertScaleAbs(src, scaley);
  2280. Cv2.AddWeighted(scalex, 0.5, scaley, 0.5, 0, src);
  2281. //图像的平均灰度
  2282. double meanValue = 0.0;
  2283. meanValue = Cv2.Mean(src)[0];
  2284. return meanValue;
  2285. }
  2286. //三选二算法
  2287. private void ChooseBest(string a_strImgPath, out int a_nWd)
  2288. {
  2289. a_nWd = -1;
  2290. var files = Directory.GetFiles(a_strImgPath, "*.tif");
  2291. float[] values = new float[files.Length];
  2292. double smax1 = LoGMean(files[0]);
  2293. int wdmax1 = int.Parse(System.IO.Path.GetFileNameWithoutExtension(files[0]));
  2294. double smax2 = Tenengrad(files[0]);
  2295. int wdmax2 = wdmax1;
  2296. double smax3 = TTgrad(files[0]);
  2297. int wdmax3 = wdmax1;
  2298. int x = 0;
  2299. foreach (var file in files)
  2300. {
  2301. int wd = int.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
  2302. double svalue1 = LoGMean(file);
  2303. double svalue2 = Tenengrad(file);
  2304. double svalue3 = TTgrad(file);
  2305. if (smax1 < svalue1)
  2306. {
  2307. smax1 = svalue1;
  2308. wdmax1 = wd;
  2309. }
  2310. if (smax2 < svalue2)
  2311. {
  2312. smax2 = svalue2;
  2313. wdmax2 = wd;
  2314. }
  2315. if (smax3 < svalue3)
  2316. {
  2317. smax3 = svalue3;
  2318. wdmax3 = wd;
  2319. }
  2320. x++;
  2321. }
  2322. //SendMsg("LoG = " + wdmax1.ToString() + ",梯度 = " + wdmax2.ToString() + ",滤波 = " + wdmax3.ToString());
  2323. if (wdmax1 == wdmax2 || wdmax1 == wdmax3)
  2324. {
  2325. a_nWd = wdmax1;
  2326. //SendMsg("焦距 = " + wdmax1.ToString());
  2327. }
  2328. else if (wdmax1 == wdmax2 || wdmax2 == wdmax3)
  2329. {
  2330. a_nWd = wdmax2;
  2331. //SendMsg("焦距 = " + wdmax2.ToString());
  2332. }
  2333. else if (wdmax3 == wdmax2 || wdmax1 == wdmax3)
  2334. {
  2335. a_nWd = wdmax3;
  2336. //SendMsg("焦距 = " + wdmax3.ToString());
  2337. }
  2338. else
  2339. {
  2340. //SendMsg("对焦失败。");
  2341. }
  2342. }
  2343. }
  2344. }