HardwareBaseDialog.cs 43 KB

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