HardwareBaseDialog.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using PaintDotNet.Adjust.BaseImage;
  4. using PaintDotNet.Annotation;
  5. using PaintDotNet.Annotation.Enum;
  6. using PaintDotNet.Annotation.ImageCollect;
  7. using PaintDotNet.Annotation.Measure;
  8. using PaintDotNet.Base.SettingModel;
  9. using PaintDotNet.CustomControl;
  10. using PaintDotNet.ImageCollect;
  11. using StageController;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Data;
  16. using System.Drawing;
  17. using System.Drawing.Drawing2D;
  18. using System.Drawing.Imaging;
  19. using System.IO;
  20. using System.IO.Ports;
  21. using System.Linq;
  22. using System.Management;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using System.Windows.Forms;
  28. using System.Windows.Interop;
  29. using TUCAMAPI;
  30. using TUCamera;
  31. namespace PaintDotNet.Hardware
  32. {
  33. internal class HardwareBaseDialog : FloatingToolForm, IStageEvent
  34. {
  35. protected AppWorkspace m_appWorkspace;
  36. #region 声明组件
  37. protected GroupBox gboxXYspeed;
  38. protected GroupBox gboxZspeed;
  39. protected DocumentPreview m_documentWorkspace;
  40. protected Label lblXPosition;
  41. protected Label lblYPosition;
  42. protected Label lblZPosition;
  43. protected Label lblXDistance;
  44. protected Label lblYDistance;
  45. protected Label lblLDistance;
  46. protected Button btnFreeloadstage;
  47. protected Button btnStoploadstage;
  48. protected Button btnResetloadstage;
  49. protected Button btnCenterloadstage;
  50. protected Button btnInloadstage;
  51. protected Button btnOutloadstage;
  52. protected Button btnZStop;
  53. protected Button btnZFree;
  54. protected TextBox txtXStep;
  55. protected TextBox txtYStep;
  56. protected TextBox txtZStep;
  57. protected ComboBox cmbXYStepUnit;
  58. protected ComboBox cmbZStepUnit;
  59. protected Button btnContinuityLower;
  60. protected Button btnContinuityUp;
  61. protected Button btnFixedLower;
  62. protected Button btnFixedUp;
  63. protected Button btnCenterDisplay;
  64. protected Button btnStopall;
  65. protected Button btnDsRocker;
  66. protected Button btnSetting;
  67. #endregion
  68. #region Axis Fields
  69. protected AxisController m_Stage;
  70. protected LoadingStageModel m_loadingStageModel;
  71. protected List<LoadingStageModel.Item> m_speedLists = new List<LoadingStageModel.Item>();
  72. /// <summary>
  73. /// X轴單次长度
  74. /// </summary>
  75. protected virtual double StepLenthX
  76. {
  77. get
  78. {
  79. string text = txtXStep.Text.Trim();
  80. if (string.IsNullOrEmpty(text))
  81. {
  82. throw new Exception("X" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text"));
  83. }
  84. var value = double.Parse(text);
  85. if (cmbXYStepUnit.SelectedItem.ToString().Equals("mm"))
  86. {
  87. value *= 1000;
  88. }
  89. return value;
  90. }
  91. }
  92. /// <summary>
  93. /// Y轴單次进长度
  94. /// </summary>
  95. protected virtual double StepLenthY
  96. {
  97. get
  98. {
  99. string text = txtYStep.Text.Trim();
  100. if (string.IsNullOrEmpty(text))
  101. {
  102. throw new Exception("Y" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text"));
  103. }
  104. var value = double.Parse(text);
  105. if (cmbXYStepUnit.SelectedItem.ToString().Equals("mm"))
  106. {
  107. value *= 1000;
  108. }
  109. return value;
  110. }
  111. }
  112. /// <summary>
  113. /// Z轴步进长度
  114. /// </summary>
  115. protected virtual double StepLenthZ
  116. {
  117. get
  118. {
  119. string text = txtZStep.Text.Trim();
  120. if (string.IsNullOrEmpty(text))
  121. {
  122. throw new Exception("Z" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text"));
  123. }
  124. var value = double.Parse(text);
  125. if (cmbZStepUnit.SelectedItem.ToString().Equals("mm"))
  126. {
  127. value *= 1000;
  128. }
  129. return value;
  130. }
  131. }
  132. protected virtual bool IsSpeedHarmonize
  133. {
  134. get;
  135. }
  136. public static int m_xyspeed = 0;//xy轴运行速度
  137. public static int m_zspeed = 0; //z轴运行速度
  138. protected double m_initStepX = 0; //清零时X轴所在位置
  139. protected double m_initStepY = 0; //清零时Y轴所在位置
  140. protected double m_stageWidth; // 行程X(操作台高度)
  141. protected double m_stageHeight; // 行程Y(操作台宽度)
  142. #endregion Axis Fields
  143. #region Camera Fields
  144. /// <summary>
  145. /// 视场宽度
  146. /// </summary>
  147. protected virtual double VisionWidth => m_viewWidth * m_PxLength;
  148. /// <summary>
  149. /// 视场高度
  150. /// </summary>
  151. protected virtual double VisionHeight => m_viewHeight * m_PxLength;
  152. protected int m_viewWidth; // 视场宽(像素)
  153. protected int m_viewHeight; // 视场高(像素)
  154. protected int VisionWidthPixel
  155. {
  156. get => m_viewWidth;
  157. set
  158. {
  159. if (value == m_viewWidth)
  160. return;
  161. m_documentWorkspace.ViewWidth = value;
  162. m_viewWidth = value;
  163. }
  164. }
  165. protected int VisionHeightPixel
  166. {
  167. get => m_viewHeight;
  168. set
  169. {
  170. if (value == m_viewHeight)
  171. return;
  172. m_documentWorkspace.ViewHeigth = value;
  173. m_viewHeight = value;
  174. }
  175. }
  176. /// <summary>
  177. /// 平台是否复位
  178. /// </summary>
  179. protected bool m_isReset;
  180. //标尺比例
  181. protected double m_PxLength;
  182. #endregion
  183. protected string[] m_unit = new string[] { "mm", "um" };
  184. protected bool m_isShowCenter = false;
  185. private System.Windows.Forms.Timer timerStageUpdate;
  186. private IContainer components;
  187. // 窗口缩小比例
  188. protected int m_docspaceScale = 1;
  189. public HardwareBaseDialog()
  190. {
  191. InitializeComponent();
  192. }
  193. public HardwareBaseDialog(AppWorkspace appWorkspace)
  194. {
  195. m_appWorkspace = appWorkspace;
  196. Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out m_PxLength);
  197. InitializeComponent();
  198. InitializeButtonBackgroundImage();
  199. m_documentWorkspace = new DocumentPreview();
  200. m_documentWorkspace.AppWorkspaceTop = appWorkspace;
  201. m_documentWorkspace.Visible = true;
  202. m_documentWorkspace.RulersEnabled = true;
  203. m_documentWorkspace.Units = MeasurementUnit.Micron;
  204. m_docspaceScale = 10;
  205. VisionWidthPixel = 2448;
  206. VisionHeightPixel = 2048;
  207. Bitmap bitmap = new Bitmap(m_viewWidth, m_viewHeight);
  208. m_documentWorkspace.Document = Document.FromImage(bitmap);
  209. m_documentWorkspace.Dock = DockStyle.Fill;
  210. m_documentWorkspace.ScaleFactor = new ScaleFactor(1, m_docspaceScale);
  211. m_documentWorkspace.SetViewPoint(0, 0);
  212. ToolPointer.GetMouseLeftClickPoint += GetMouseLeftClickPoint;
  213. Load += OnLoad;
  214. FormClosing += OnFormClosing;
  215. }
  216. private void OnFormClosing(object sender, FormClosingEventArgs e)
  217. {
  218. UninitCamera();
  219. UninitStage();
  220. UninitDocumentWorkspace();
  221. ToolPointer.GetMouseLeftClickPoint -= GetMouseLeftClickPoint;
  222. GC.Collect();
  223. base.Dispose();
  224. }
  225. private void OnLoad(object sender, EventArgs e)
  226. {
  227. m_loadingStageModel = Startup.instance.loadingStageModel;
  228. InitializeCamera();
  229. InitStage();
  230. ReloadingStageParam(m_loadingStageModel);
  231. timerStageUpdate.Start();
  232. }
  233. private void InitializeComponent()
  234. {
  235. this.components = new System.ComponentModel.Container();
  236. this.gboxXYspeed = new System.Windows.Forms.GroupBox();
  237. this.gboxZspeed = new System.Windows.Forms.GroupBox();
  238. this.lblYPosition = new System.Windows.Forms.Label();
  239. this.lblXPosition = new System.Windows.Forms.Label();
  240. this.lblZPosition = new System.Windows.Forms.Label();
  241. this.lblLDistance = new System.Windows.Forms.Label();
  242. this.lblYDistance = new System.Windows.Forms.Label();
  243. this.lblXDistance = new System.Windows.Forms.Label();
  244. this.btnFreeloadstage = new System.Windows.Forms.Button();
  245. this.btnStoploadstage = new System.Windows.Forms.Button();
  246. this.btnResetloadstage = new System.Windows.Forms.Button();
  247. this.btnCenterloadstage = new System.Windows.Forms.Button();
  248. this.btnInloadstage = new System.Windows.Forms.Button();
  249. this.btnOutloadstage = new System.Windows.Forms.Button();
  250. this.btnZFree = new System.Windows.Forms.Button();
  251. this.btnZStop = new System.Windows.Forms.Button();
  252. this.btnFixedLower = new System.Windows.Forms.Button();
  253. this.btnFixedUp = new System.Windows.Forms.Button();
  254. this.btnContinuityLower = new System.Windows.Forms.Button();
  255. this.btnContinuityUp = new System.Windows.Forms.Button();
  256. this.btnCenterDisplay = new System.Windows.Forms.Button();
  257. this.btnStopall = new System.Windows.Forms.Button();
  258. this.btnDsRocker = new System.Windows.Forms.Button();
  259. this.btnSetting = new System.Windows.Forms.Button();
  260. this.txtXStep = new System.Windows.Forms.TextBox();
  261. this.txtYStep = new System.Windows.Forms.TextBox();
  262. this.txtZStep = new System.Windows.Forms.TextBox();
  263. this.cmbZStepUnit = new System.Windows.Forms.ComboBox();
  264. this.cmbXYStepUnit = new System.Windows.Forms.ComboBox();
  265. this.timerStageUpdate = new System.Windows.Forms.Timer(this.components);
  266. this.SuspendLayout();
  267. //
  268. // gboxXYspeed
  269. //
  270. this.gboxXYspeed.Location = new System.Drawing.Point(0, 0);
  271. this.gboxXYspeed.Name = "gboxXYspeed";
  272. this.gboxXYspeed.Size = new System.Drawing.Size(200, 100);
  273. this.gboxXYspeed.TabIndex = 0;
  274. this.gboxXYspeed.TabStop = false;
  275. //
  276. // gboxZspeed
  277. //
  278. this.gboxZspeed.Location = new System.Drawing.Point(0, 0);
  279. this.gboxZspeed.Name = "gboxZspeed";
  280. this.gboxZspeed.Size = new System.Drawing.Size(200, 100);
  281. this.gboxZspeed.TabIndex = 0;
  282. this.gboxZspeed.TabStop = false;
  283. //
  284. // lblYPosition
  285. //
  286. this.lblYPosition.Location = new System.Drawing.Point(0, 0);
  287. this.lblYPosition.Name = "lblYPosition";
  288. this.lblYPosition.Size = new System.Drawing.Size(100, 23);
  289. this.lblYPosition.TabIndex = 0;
  290. //
  291. // lblXPosition
  292. //
  293. this.lblXPosition.Location = new System.Drawing.Point(0, 0);
  294. this.lblXPosition.Name = "lblXPosition";
  295. this.lblXPosition.Size = new System.Drawing.Size(100, 23);
  296. this.lblXPosition.TabIndex = 0;
  297. //
  298. // lblZPosition
  299. //
  300. this.lblZPosition.Location = new System.Drawing.Point(0, 0);
  301. this.lblZPosition.Name = "lblZPosition";
  302. this.lblZPosition.Size = new System.Drawing.Size(100, 23);
  303. this.lblZPosition.TabIndex = 0;
  304. //
  305. // lblLDistance
  306. //
  307. this.lblLDistance.Location = new System.Drawing.Point(0, 0);
  308. this.lblLDistance.Name = "lblLDistance";
  309. this.lblLDistance.Size = new System.Drawing.Size(100, 23);
  310. this.lblLDistance.TabIndex = 0;
  311. //
  312. // lblYDistance
  313. //
  314. this.lblYDistance.Location = new System.Drawing.Point(0, 0);
  315. this.lblYDistance.Name = "lblYDistance";
  316. this.lblYDistance.Size = new System.Drawing.Size(100, 23);
  317. this.lblYDistance.TabIndex = 0;
  318. //
  319. // lblXDistance
  320. //
  321. this.lblXDistance.Location = new System.Drawing.Point(0, 0);
  322. this.lblXDistance.Name = "lblXDistance";
  323. this.lblXDistance.Size = new System.Drawing.Size(100, 23);
  324. this.lblXDistance.TabIndex = 0;
  325. //
  326. // btnFreeloadstage
  327. //
  328. this.btnFreeloadstage.Location = new System.Drawing.Point(0, 0);
  329. this.btnFreeloadstage.Name = "btnFreeloadstage";
  330. this.btnFreeloadstage.Size = new System.Drawing.Size(75, 23);
  331. this.btnFreeloadstage.TabIndex = 0;
  332. this.btnFreeloadstage.Click += new System.EventHandler(this.btnLockXY_Click);
  333. //
  334. // btnStoploadstage
  335. //
  336. this.btnStoploadstage.Location = new System.Drawing.Point(0, 0);
  337. this.btnStoploadstage.Name = "btnStoploadstage";
  338. this.btnStoploadstage.Size = new System.Drawing.Size(75, 23);
  339. this.btnStoploadstage.TabIndex = 0;
  340. this.btnStoploadstage.Click += new System.EventHandler(this.btnStopXY_Click);
  341. //
  342. // btnResetloadstage
  343. //
  344. this.btnResetloadstage.Location = new System.Drawing.Point(0, 0);
  345. this.btnResetloadstage.Name = "btnResetloadstage";
  346. this.btnResetloadstage.Size = new System.Drawing.Size(75, 23);
  347. this.btnResetloadstage.TabIndex = 0;
  348. this.btnResetloadstage.Click += new System.EventHandler(this.btnResetloadstage_Click);
  349. //
  350. // btnCenterloadstage
  351. //
  352. this.btnCenterloadstage.Location = new System.Drawing.Point(0, 0);
  353. this.btnCenterloadstage.Name = "btnCenterloadstage";
  354. this.btnCenterloadstage.Size = new System.Drawing.Size(75, 23);
  355. this.btnCenterloadstage.TabIndex = 0;
  356. this.btnCenterloadstage.Click += new System.EventHandler(this.btnCenterloadstage_Click);
  357. //
  358. // btnInloadstage
  359. //
  360. this.btnInloadstage.Location = new System.Drawing.Point(0, 0);
  361. this.btnInloadstage.Name = "btnInloadstage";
  362. this.btnInloadstage.Size = new System.Drawing.Size(75, 23);
  363. this.btnInloadstage.TabIndex = 0;
  364. this.btnInloadstage.Click += new System.EventHandler(this.btnInloadstage_Click);
  365. //
  366. // btnOutloadstage
  367. //
  368. this.btnOutloadstage.Location = new System.Drawing.Point(0, 0);
  369. this.btnOutloadstage.Name = "btnOutloadstage";
  370. this.btnOutloadstage.Size = new System.Drawing.Size(75, 23);
  371. this.btnOutloadstage.TabIndex = 0;
  372. this.btnOutloadstage.Click += new System.EventHandler(this.btnOutloadstage_Click);
  373. //
  374. // btnZFree
  375. //
  376. this.btnZFree.Location = new System.Drawing.Point(0, 0);
  377. this.btnZFree.Name = "btnZFree";
  378. this.btnZFree.Size = new System.Drawing.Size(75, 23);
  379. this.btnZFree.TabIndex = 0;
  380. this.btnZFree.Click += new System.EventHandler(this.btnZLock_Click);
  381. //
  382. // btnZStop
  383. //
  384. this.btnZStop.Location = new System.Drawing.Point(0, 0);
  385. this.btnZStop.Name = "btnZStop";
  386. this.btnZStop.Size = new System.Drawing.Size(75, 23);
  387. this.btnZStop.TabIndex = 0;
  388. this.btnZStop.Click += new System.EventHandler(this.btnZStop_Click);
  389. //
  390. // btnFixedLower
  391. //
  392. this.btnFixedLower.Location = new System.Drawing.Point(0, 0);
  393. this.btnFixedLower.Name = "btnFixedLower";
  394. this.btnFixedLower.Size = new System.Drawing.Size(75, 23);
  395. this.btnFixedLower.TabIndex = 0;
  396. btnFixedLower.Click += btnFixedLower_Click;
  397. //
  398. // btnFixedUp
  399. //
  400. this.btnFixedUp.Location = new System.Drawing.Point(0, 0);
  401. this.btnFixedUp.Name = "btnFixedUp";
  402. this.btnFixedUp.Size = new System.Drawing.Size(75, 23);
  403. this.btnFixedUp.TabIndex = 0;
  404. btnFixedUp.Click += btnFixedUp_Click;
  405. //
  406. // btnContinuityLower
  407. //
  408. this.btnContinuityLower.Location = new System.Drawing.Point(0, 0);
  409. this.btnContinuityLower.Name = "btnContinuityLower";
  410. this.btnContinuityLower.Size = new System.Drawing.Size(75, 23);
  411. this.btnContinuityLower.TabIndex = 0;
  412. btnContinuityLower.MouseDown += btnContinuityLower_Start;
  413. btnContinuityLower.MouseUp += btnZStop_Click;
  414. //
  415. // btnContinuityUp
  416. //
  417. this.btnContinuityUp.Location = new System.Drawing.Point(0, 0);
  418. this.btnContinuityUp.Name = "btnContinuityUp";
  419. this.btnContinuityUp.Size = new System.Drawing.Size(75, 23);
  420. this.btnContinuityUp.TabIndex = 0;
  421. btnContinuityUp.MouseDown += btnContinuityUp_Start;
  422. btnContinuityUp.MouseUp += btnZStop_Click;
  423. //
  424. // btnCenterDisplay
  425. //
  426. this.btnCenterDisplay.Location = new System.Drawing.Point(0, 0);
  427. this.btnCenterDisplay.Name = "btnCenterDisplay";
  428. this.btnCenterDisplay.Size = new System.Drawing.Size(75, 23);
  429. this.btnCenterDisplay.TabIndex = 0;
  430. this.btnCenterDisplay.Click += new System.EventHandler(this.btnCenteDisplay_Click);
  431. //
  432. // btnStopall
  433. //
  434. this.btnStopall.Location = new System.Drawing.Point(0, 0);
  435. this.btnStopall.Name = "btnStopall";
  436. this.btnStopall.Size = new System.Drawing.Size(75, 23);
  437. this.btnStopall.TabIndex = 0;
  438. //
  439. // btnDsRocker
  440. //
  441. this.btnDsRocker.Location = new System.Drawing.Point(0, 0);
  442. this.btnDsRocker.Name = "btnDsRocker";
  443. this.btnDsRocker.Size = new System.Drawing.Size(75, 23);
  444. this.btnDsRocker.TabIndex = 0;
  445. //
  446. // btnSetting
  447. //
  448. this.btnSetting.Location = new System.Drawing.Point(0, 0);
  449. this.btnSetting.Name = "btnSetting";
  450. this.btnSetting.Size = new System.Drawing.Size(75, 23);
  451. this.btnSetting.TabIndex = 0;
  452. this.btnSetting.Click += btnSetting_Click;
  453. //
  454. // txtXStep
  455. //
  456. this.txtXStep.Location = new System.Drawing.Point(0, 0);
  457. this.txtXStep.Name = "txtXStep";
  458. this.txtXStep.Size = new System.Drawing.Size(100, 21);
  459. this.txtXStep.TabIndex = 0;
  460. this.txtXStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric);
  461. this.txtXStep.Leave += new System.EventHandler(this.txtStep_Leave);
  462. //
  463. // txtYStep
  464. //
  465. this.txtYStep.Location = new System.Drawing.Point(0, 0);
  466. this.txtYStep.Name = "txtYStep";
  467. this.txtYStep.Size = new System.Drawing.Size(100, 21);
  468. this.txtYStep.TabIndex = 0;
  469. this.txtYStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric);
  470. this.txtYStep.Leave += new System.EventHandler(this.txtStep_Leave);
  471. //
  472. // txtZStep
  473. //
  474. this.txtZStep.Location = new System.Drawing.Point(0, 0);
  475. this.txtZStep.Name = "txtZStep";
  476. this.txtZStep.Size = new System.Drawing.Size(100, 21);
  477. this.txtZStep.TabIndex = 0;
  478. this.txtZStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric);
  479. this.txtZStep.Leave += new System.EventHandler(this.txtStep_Leave);
  480. //
  481. // cmbZStepUnit
  482. //
  483. this.cmbZStepUnit.Location = new System.Drawing.Point(0, 0);
  484. this.cmbZStepUnit.Name = "cmbZStepUnit";
  485. this.cmbZStepUnit.Size = new System.Drawing.Size(121, 20);
  486. this.cmbZStepUnit.TabIndex = 0;
  487. //
  488. // cmbXYStepUnit
  489. //
  490. this.cmbXYStepUnit.Location = new System.Drawing.Point(0, 0);
  491. this.cmbXYStepUnit.Name = "cmbXYStepUnit";
  492. this.cmbXYStepUnit.Size = new System.Drawing.Size(121, 20);
  493. this.cmbXYStepUnit.TabIndex = 0;
  494. //
  495. // timerStageUpdate
  496. //
  497. this.timerStageUpdate.Interval = 15;
  498. this.timerStageUpdate.Tick += new System.EventHandler(this.timerStageUpdate_Tick);
  499. //
  500. // HardwareBaseDialog
  501. //
  502. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  503. this.ClientSize = new System.Drawing.Size(292, 271);
  504. this.Location = new System.Drawing.Point(0, 0);
  505. this.Name = "HardwareBaseDialog";
  506. this.ResumeLayout(false);
  507. }
  508. protected void InitializeLanguageText()
  509. {
  510. btnFreeloadstage.Text = "自由";// PdnResources.GetString("Menu.freeing.text");
  511. btnStoploadstage.Text = PdnResources.GetString("Menu.stop.text");
  512. btnResetloadstage.Text = PdnResources.GetString("Menu.reset.Text");
  513. btnCenterloadstage.Text = PdnResources.GetString("Menu.center.text");
  514. btnInloadstage.Text = PdnResources.GetString("Menu.enterfilm.text");
  515. btnOutloadstage.Text = PdnResources.GetString("Menu.outslice.text");
  516. btnZFree.Text = "自由";// PdnResources.GetString("Menu.locking.text");
  517. btnZStop.Text = PdnResources.GetString("Menu.stop.text");
  518. btnCenterDisplay.Text = PdnResources.GetString("Menu.Centerdisplay.text");
  519. btnStopall.Text = PdnResources.GetString("Menu.Stopall.text");
  520. btnDsRocker.Text = PdnResources.GetString("Menu.Disabletherocker.text");
  521. btnSetting.Text = PdnResources.GetString("Menu.SerialportSettings.Text");
  522. }
  523. private void InitializeButtonBackgroundImage()
  524. {
  525. btnContinuityUp.FlatStyle = FlatStyle.Flat;
  526. btnContinuityUp.FlatAppearance.BorderSize = 0;
  527. btnContinuityUp.BackgroundImageLayout = ImageLayout.Zoom;
  528. btnContinuityUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference;
  529. btnContinuityLower.FlatStyle = FlatStyle.Flat;
  530. btnContinuityLower.FlatAppearance.BorderSize = 0;
  531. btnContinuityLower.BackgroundImageLayout = ImageLayout.Zoom;
  532. btnContinuityLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference;
  533. btnFixedUp.FlatStyle = FlatStyle.Flat;
  534. btnFixedUp.FlatAppearance.BorderSize = 0;
  535. btnFixedUp.BackgroundImageLayout = ImageLayout.Zoom;
  536. btnFixedUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference;
  537. btnFixedLower.FlatStyle = FlatStyle.Flat;
  538. btnFixedLower.FlatAppearance.BorderSize = 0;
  539. btnFixedLower.BackgroundImageLayout = ImageLayout.Zoom;
  540. btnFixedLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference;
  541. }
  542. public void InitStage()
  543. {
  544. m_Stage = AxisController.GetInstance();
  545. if (!m_Stage.IsOpen)
  546. {
  547. MessageBox.Show(PdnResources.GetString("Message.AxisController.NotConnected"));
  548. }
  549. m_Stage.AddApp(this);
  550. }
  551. #region Dispose
  552. public void UninitStage()
  553. {
  554. m_Stage.RemoveApp(this);
  555. }
  556. public void UninitDocumentWorkspace()
  557. {
  558. if (m_documentWorkspace != null && !m_documentWorkspace.IsDisposed)
  559. {
  560. m_documentWorkspace.Dispose();
  561. }
  562. }
  563. #endregion
  564. /// <summary>
  565. /// 视场模式操作台
  566. /// </summary>
  567. public CircleControl initViewCircal(int size)
  568. {
  569. var circleControl = new CircleControl(size);
  570. circleControl.mouseDown += new EventHandler(visionClick);
  571. return circleControl;
  572. }
  573. /// <summary>
  574. /// 运动模式操作台
  575. /// </summary>
  576. public CircleControl initSportCircal(int size)
  577. {
  578. var circleSportControl = new CircleControl(size);
  579. circleSportControl.mouseUp += new EventHandler(sportMouseUp);
  580. circleSportControl.mouseDown += new EventHandler(sportMouseDown);
  581. return circleSportControl;
  582. }
  583. /// <summary>
  584. /// 初始化设置信息
  585. /// </summary>
  586. /// <param name="loadingStageModel"></param>
  587. public void ReloadingStageParam(LoadingStageModel loadingStageModel)
  588. {
  589. m_loadingStageModel = loadingStageModel;
  590. m_stageWidth = double.Parse(m_loadingStageModel.TripX);
  591. m_stageHeight = double.Parse(m_loadingStageModel.TripY);
  592. ReinitSpeedUI();
  593. }
  594. #region Speed
  595. /// <summary>
  596. /// gboxzspeed gboxxyspeed初始化
  597. /// </summary>
  598. protected virtual void ReinitSpeedUI()
  599. {
  600. InitGroupSpeed(gboxXYspeed, XSpeed_CheckedChanged);
  601. InitGroupSpeed(gboxZspeed, ZSpeed_CheckedChanged);
  602. if (gboxXYspeed.Controls.Count == 0 || gboxZspeed.Controls.Count == 0) return;
  603. m_Stage.SetSpeedXY(m_speedLists[0].LSpeed);
  604. m_Stage.SetSpeedZ(m_speedLists[0].ZSpeed);
  605. }
  606. protected virtual void InitGroupSpeed(Control group, Action<object, EventArgs> action)
  607. {
  608. m_speedLists = m_loadingStageModel.items;
  609. int pw = (group.Width - 20) / 3;
  610. group.Controls.Clear();
  611. for (int i = 0; i < m_speedLists.Count; ++i)
  612. {
  613. if (m_speedLists[i].SChecked == 1)
  614. {
  615. var radioButton = new RadioButton();
  616. radioButton.Text = (i + 1).ToString();
  617. radioButton.AutoSize = true;
  618. radioButton.Checked = i == 0;
  619. radioButton.CheckedChanged += new System.EventHandler(action);
  620. radioButton.Tag = i;
  621. radioButton.Location = new System.Drawing.Point(18 + (i % 3) * pw, 21 + (i / 3) * 25);
  622. group.Controls.Add(radioButton);
  623. }
  624. }
  625. }
  626. // 速度选择按钮
  627. protected virtual void XSpeed_CheckedChanged(object sender, EventArgs e)
  628. {
  629. RadioButton radio = (RadioButton)sender;
  630. if (radio.Checked)
  631. {
  632. int index = (int)radio.Tag;
  633. m_Stage.SetSpeedXY(m_speedLists[index].LSpeed);
  634. // 联动调速
  635. if (IsSpeedHarmonize)
  636. {
  637. int count = gboxZspeed.Controls.Count;
  638. for (int i = 0; i < count; ++i)
  639. {
  640. ((RadioButton)gboxZspeed.Controls[i]).Checked = i == index;
  641. }
  642. }
  643. }
  644. }
  645. protected virtual void ZSpeed_CheckedChanged(object sender, EventArgs e)
  646. {
  647. RadioButton radio = (RadioButton)sender;
  648. if (!radio.Checked) return;
  649. int index = (int)radio.Tag;
  650. m_Stage.SetSpeedZ(m_speedLists[index].ZSpeed);
  651. // 联动调速
  652. if (IsSpeedHarmonize)
  653. {
  654. int count = gboxXYspeed.Controls.Count;
  655. for (int i = 0; i < count; ++i)
  656. {
  657. ((RadioButton)gboxXYspeed.Controls[i]).Checked = i == index;
  658. }
  659. }
  660. }
  661. #endregion
  662. protected int ConvertUMToPX(double length)
  663. {
  664. // if (length < 0) length = -length;
  665. return (int)(length / m_PxLength);
  666. }
  667. protected double ConvertPXToUm(double length)
  668. {
  669. return length * m_PxLength;
  670. }
  671. #region Position Update
  672. public void OnUpdatePosition()
  673. {
  674. try
  675. {
  676. SetViewLocation(m_Stage.X, m_Stage.Y, m_Stage.Z); // 移动视图
  677. ChangeDistanceAndCheckStop(m_Stage.X, m_Stage.Y);
  678. //if (m_Stage.StateX == 0 || m_Stage.StateY == 0) ; // 平台电机自由后需要重新复位
  679. // m_isReset = false;
  680. }
  681. catch
  682. { }
  683. }
  684. /// <summary>
  685. /// 设置视场的位置
  686. /// </summary>
  687. /// <param name="position"></param>
  688. protected virtual void SetViewLocation(double x, double y, double z)
  689. {
  690. var b = this.IsDisposed;
  691. Invoke(new Action(() => UpdatePositionDisplay(x, y, z)));
  692. x = x < 0 ? 0 : x;
  693. y = y < 0 ? 0 : y;
  694. this.BeginInvoke(new Action(() =>
  695. {
  696. m_documentWorkspace.SetViewPoint(ConvertUMToPX(x), ConvertUMToPX(y));
  697. }));
  698. }
  699. protected virtual void UpdatePositionDisplay(double x, double y, double z)
  700. {
  701. lblXPosition.Text = x.ToString();
  702. lblYPosition.Text = y.ToString();
  703. lblZPosition.Text = z.ToString();
  704. }
  705. protected virtual void ChangeDistanceAndCheckStop(double x, double y)
  706. {
  707. var distanceX = x - m_initStepX;
  708. var distanceY = y - m_initStepY;
  709. var distanceL = Math.Sqrt(distanceX * distanceX + distanceY * distanceY);
  710. Invoke(new Action(() =>
  711. {
  712. lblXDistance.Text = "X:" + distanceX.ToString("0");
  713. lblYDistance.Text = "Y:" + distanceY.ToString("0");
  714. lblLDistance.Text = "L:" + distanceL.ToString("0");
  715. }));
  716. }
  717. #endregion
  718. #region 视场圆环
  719. // 视场圆环按钮被点击
  720. protected virtual void visionClick(object sender, EventArgs e)
  721. {
  722. var circleSportControl = (CircleControl)sender;
  723. var p = circleSportControl._position;
  724. if (p < 0 || p > 15) return;
  725. var map = circleSportControl.positionMap[p % 8];
  726. if (p < 8)
  727. m_Stage.Move(VisionWidth * map[0] / 2, VisionHeight * map[1] / 2);
  728. else
  729. m_Stage.Move(VisionWidth * map[0], VisionHeight * map[1]);
  730. }
  731. #endregion
  732. #region 运动圆环
  733. protected virtual void sportMouseDown(object sender, EventArgs e)
  734. {
  735. var circleSportControl = (CircleControl)sender;
  736. if (circleSportControl._position < 8)
  737. {
  738. var p = circleSportControl._position;
  739. if (p < 0 || p > 7)
  740. return;
  741. var map = circleSportControl.positionMap[p];
  742. m_Stage.Move(StepLenthX * map[0], StepLenthY * map[1]);
  743. }
  744. else
  745. {
  746. var direction = circleSportControl._position - 8;
  747. m_Stage.Split(direction);
  748. }
  749. }
  750. protected virtual void sportMouseUp(object sender, EventArgs e)
  751. {
  752. var circleSportControl = (CircleControl)sender;
  753. if (circleSportControl._position > 7)
  754. btnStopXY_Click(null, null);
  755. }
  756. #endregion
  757. //参数设置按钮点击
  758. protected virtual void btnSetting_Click(object sender, EventArgs e)
  759. {
  760. //参数设置按钮点击弹窗
  761. using (ParamSetingDialog af = new ParamSetingDialog(this))
  762. {
  763. af.Setted += ReloadingStageParam;
  764. af.ShowDialog();
  765. }
  766. }
  767. #region Z 运动
  768. /// <summary>
  769. /// Z轴连续向上移动
  770. /// </summary>
  771. /// <param name="sender"></param>
  772. /// <param name="e"></param>
  773. protected virtual void btnContinuityUp_Start(object sender, EventArgs e)
  774. {
  775. m_Stage.GoTop(true);
  776. }
  777. /// <summary>
  778. /// Z轴连续向下移动
  779. /// </summary>
  780. /// <param name="sender"></param>
  781. /// <param name="e"></param>
  782. protected virtual void btnContinuityLower_Start(object sender, EventArgs e)
  783. {
  784. m_Stage.GoTop(false);
  785. }
  786. /// <summary>
  787. /// Z轴定长向上移动
  788. /// </summary>
  789. /// <param name="sender"></param>
  790. /// <param name="e"></param>
  791. protected virtual void btnFixedUp_Click(object sender, EventArgs e)
  792. {
  793. m_Stage.Up(StepLenthZ);
  794. }
  795. /// <summary>
  796. /// Z轴定长向下移动
  797. /// </summary>
  798. /// <param name="sender"></param>
  799. /// <param name="e"></param>
  800. protected virtual void btnFixedLower_Click(object sender, EventArgs e)
  801. {
  802. m_Stage.Up(-StepLenthZ);
  803. }
  804. #endregion
  805. protected virtual void CheckInputIsNumeric(object sender, KeyPressEventArgs e)
  806. {
  807. var text = ((TextBox)sender).Text;
  808. var key = (int)e.KeyChar;
  809. if ((key < 48 || key > 57) && key != 8 && key != 46)
  810. e.Handled = true;
  811. if (key == 46) //小数点
  812. {
  813. if (text.Length <= 0)
  814. e.Handled = true; //小数点不能在第一位
  815. else
  816. {
  817. float f, oldf;
  818. var b1 = float.TryParse(text, out oldf);
  819. var b2 = float.TryParse(text + key.ToString(), out f);
  820. if (!b2 && b1)
  821. e.Handled = true;
  822. }
  823. }
  824. }
  825. #region Axis Stop
  826. /// <summary>
  827. /// Z轴自由
  828. /// </summary>
  829. /// <param name="sender"></param>
  830. /// <param name="e"></param>
  831. protected virtual void btnZLock_Click(object sender, EventArgs e)
  832. {
  833. m_Stage.FreeZ();
  834. }
  835. /// <summary>
  836. /// Z轴停止
  837. /// </summary>
  838. /// <param name="sender"></param>
  839. /// <param name="e"></param>
  840. protected virtual void btnZStop_Click(object sender, EventArgs e)
  841. {
  842. m_Stage.FreeZ();
  843. }
  844. /// <summary>
  845. /// XY轴自由
  846. /// </summary>
  847. /// <param name="sender"></param>
  848. /// <param name="e"></param>
  849. protected virtual void btnLockXY_Click(object sender, EventArgs e)
  850. {
  851. m_Stage.FreeStage();
  852. }
  853. /// <summary>
  854. /// XY轴停止
  855. /// </summary>
  856. /// <param name="sender"></param>
  857. /// <param name="e"></param>
  858. protected virtual void btnStopXY_Click(object sender, EventArgs e)
  859. {
  860. m_Stage.LockStage();
  861. }
  862. /// <summary>
  863. /// 全部停止
  864. /// </summary>
  865. /// <param name="sender"></param>
  866. /// <param name="e"></param>
  867. protected virtual void btnStopall_Click(object sender, EventArgs e)
  868. {
  869. btnZStop_Click(null, null);
  870. btnStopXY_Click(null, null);
  871. }
  872. #endregion Axis Stop
  873. /// <summary>
  874. /// 禁用摇杆
  875. /// </summary>
  876. /// <param name="sender"></param>
  877. /// <param name="e"></param>
  878. protected virtual void btnDsRocker_Click(object sender, EventArgs e)
  879. {
  880. m_Stage.SetRockerEnabel(false);
  881. }
  882. protected virtual void GetMouseLeftClickPoint(object sender, EventArgs e)
  883. {
  884. if (!m_isReset)
  885. {
  886. MessageBox.Show(PdnResources.GetString("Menu.Pleaseresetfirst.Text"));
  887. return;
  888. }
  889. PointF pointF = (PointF)sender;
  890. List<DrawObject> graphicsList = m_documentWorkspace.GraphicsList.GetDrawClassList(DrawClass.Stitch);
  891. int count = graphicsList.Count;
  892. var x = ConvertPXToUm((int)(pointF.X));
  893. var y = ConvertPXToUm((int)(pointF.Y));
  894. for (int i = 0; i < count; i++)
  895. {
  896. List<Dictionary<int, object>> points = ((DrawStithchingBase)graphicsList[i]).GetViewPoints();
  897. for (int j = 0; j < points.Count; j++)
  898. {
  899. Dictionary<int, object> point = points[j];
  900. RectangleF rectangleF = new RectangleF((PointF)point[0], new SizeF(m_viewWidth, m_viewHeight));
  901. if (rectangleF.Contains(pointF))
  902. {
  903. pointF = (PointF)point[0];
  904. x = ConvertPXToUm((int)(pointF.X));
  905. y = ConvertPXToUm((int)(pointF.Y));
  906. m_Stage.To(x, y); return;
  907. }
  908. }
  909. }
  910. m_Stage.To(x, y);
  911. }
  912. #region Moveflow
  913. /// <summary>
  914. /// 复位
  915. /// </summary>
  916. /// <param name="sender"></param>
  917. /// <param name="e"></param>
  918. protected virtual void btnResetloadstage_Click(object sender, EventArgs e)
  919. {
  920. var dialog = TransferProgressDialog.CreatDialog("平台复位", "复位中...", OnProgressCancel, "Stop");
  921. m_Stage.ResetStage(
  922. () =>
  923. {
  924. m_isReset = true;
  925. this.Invoke(new Action(dialog.Close));
  926. });
  927. if (!m_isReset)
  928. dialog.ShowDialog();
  929. }
  930. protected virtual void OnProgressCancel(object sender, EventArgs e)
  931. {
  932. btnStopall_Click(null, null);
  933. }
  934. /// <summary>
  935. /// 出片
  936. /// </summary>
  937. /// <param name="sender"></param>
  938. /// <param name="e"></param>
  939. protected virtual void btnOutloadstage_Click(object sender, EventArgs e)
  940. {
  941. m_Stage.Split(1);
  942. }
  943. /// <summary>
  944. /// 进片
  945. /// </summary>
  946. /// <param name="sender"></param>
  947. /// <param name="e"></param>
  948. protected virtual void btnInloadstage_Click(object sender, EventArgs e)
  949. {
  950. m_Stage.Split(3);
  951. }
  952. /// <summary>
  953. /// 中心
  954. /// </summary>
  955. /// <param name="sender"></param>
  956. /// <param name="e"></param>
  957. protected virtual void btnCenterloadstage_Click(object sender, EventArgs e)
  958. {
  959. m_Stage.ToCenter();
  960. }
  961. /// <summary>
  962. /// 中心显示
  963. /// </summary>
  964. protected virtual void btnCenteDisplay_Click(object sender, EventArgs e)
  965. {
  966. m_isShowCenter = !m_isShowCenter;
  967. m_documentWorkspace.SetCenter(m_isShowCenter);
  968. if (m_isShowCenter)
  969. {
  970. (sender as Control).Text = PdnResources.GetString("Menu.Cancelcenter.text");
  971. }
  972. else
  973. {
  974. (sender as Control).Text = PdnResources.GetString("Menu.Centerdisplay.text");
  975. }
  976. }
  977. protected virtual void DepthMerge() { }
  978. #endregion
  979. protected virtual void txtStep_Leave(object sender, EventArgs e)
  980. {
  981. var tbx = sender as TextBox;
  982. if (string.IsNullOrEmpty(tbx.Text))
  983. {
  984. MessageBox.Show(PdnResources.GetString("Menu.Step-sizbeempty.text"));
  985. tbx.Text = "0";
  986. }
  987. }
  988. public void OnTimeoutConnect()
  989. {
  990. MessageBox.Show("平台连接超时");
  991. }
  992. public void OnErrorSend()
  993. {
  994. MessageBox.Show("平台连接异常");
  995. }
  996. #region Camera
  997. protected TUCamera.TUCamera m_camera;
  998. protected CameraParamModel m_cameraParamModel;
  999. protected CameraConfigs m_cameraConfig;
  1000. /// <summary>
  1001. /// 当前拍摄图片
  1002. /// </summary>
  1003. protected Bitmap m_BitmapCurrent;
  1004. public void UninitCamera()
  1005. {
  1006. if (m_camera != null)
  1007. {
  1008. m_camera.m_bufHandler -= CallbackDraw;
  1009. m_camera.StopWaitForFrame();
  1010. }
  1011. if (m_BitmapCurrent != null)
  1012. {
  1013. m_BitmapCurrent.Dispose();
  1014. }
  1015. }
  1016. /// <summary>
  1017. /// 窗体打开后初始化相机
  1018. /// </summary>
  1019. protected void InitializeCamera()
  1020. {
  1021. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  1022. if (m_camera != null)
  1023. {
  1024. m_cameraConfig = CameraConfigs.GetInstance();
  1025. m_cameraParamModel = m_cameraConfig.GetCurrentCameraParamModel();
  1026. //m_cameraConfig.CameraParamInit();
  1027. // 开启预览
  1028. m_camera.m_bufHandler += CallbackDraw;
  1029. m_camera.StartWaitForFrame();
  1030. }
  1031. Thread.Sleep(100);//相机开启缓冲时间
  1032. initCameraSetting();
  1033. }
  1034. protected virtual void initCameraSetting()
  1035. {
  1036. }
  1037. object _lockobj = new object();
  1038. public void CallbackDraw(Bitmap bitmap)
  1039. {
  1040. lock (_lockobj)
  1041. {
  1042. try
  1043. {
  1044. VisionWidthPixel = bitmap.Width;
  1045. VisionHeightPixel = bitmap.Height;
  1046. m_documentWorkspace.CompositionSurface = null;
  1047. if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
  1048. {
  1049. var mat = BitmapConverter.ToMat(bitmap);
  1050. m_documentWorkspace.Document = Document.FromByteArr(mat);
  1051. }
  1052. else
  1053. {
  1054. m_documentWorkspace.Document = Document.FromByteArr(bitmap.ToByteArray(), bitmap.Width, bitmap.Height);
  1055. }
  1056. m_BitmapCurrent = bitmap;
  1057. }
  1058. catch (Exception)
  1059. {
  1060. }
  1061. }
  1062. }
  1063. #endregion
  1064. private void timerStageUpdate_Tick(object sender, EventArgs e)
  1065. {
  1066. var x = Math.Round(m_Stage.X, 2);
  1067. var y = Math.Round(m_Stage.Y, 2);
  1068. var z = Math.Round(m_Stage.Z, 2);
  1069. lblXPosition.Text = x.ToString();
  1070. lblYPosition.Text = y.ToString();
  1071. lblZPosition.Text = z.ToString();
  1072. m_documentWorkspace.SetViewPoint(ConvertUMToPX(x), ConvertUMToPX(y));
  1073. var distanceX = x - m_initStepX;
  1074. var distanceY = y - m_initStepY;
  1075. var distanceL = Math.Sqrt(distanceX * distanceX + distanceY * distanceY);
  1076. lblXDistance.Text = "X:" + distanceX.ToString("0");
  1077. lblYDistance.Text = "Y:" + distanceY.ToString("0");
  1078. lblLDistance.Text = "L:" + distanceL.ToString("0");
  1079. }
  1080. }
  1081. }