CameraZaxisScan.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Ports;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using PaintDotNet.Base.CommTool;
  8. using PaintDotNet.Base.Functionodel;
  9. using PaintDotNet.Base.SettingModel;
  10. using PaintDotNet.ImageCollect.CameraEDOF;
  11. using StageController;
  12. using StageController.M3H;
  13. using TUCAMAPI;
  14. using TUCamera;
  15. namespace PaintDotNet.ImageCollect
  16. {
  17. internal partial class CameraZaxisScan : FloatingToolForm, IStageEvent
  18. {
  19. private AppWorkspace m_appWorkspace;
  20. TransferProgressDialog _ZscanProgressDialog;
  21. List<ZAxisScanModel.Item> m_ZScanImages = new List<ZAxisScanModel.Item>();
  22. private double m_currentHeight => m_Stage.Z;
  23. // 操作台参数配置
  24. private LoadingStageModel m_loadingStageModel = Startup.instance.loadingStageModel;
  25. // 已移动次数
  26. private int m_movedTimes;
  27. private TUCamera.TUCamera m_camera;
  28. private string m_picturePath = Application.StartupPath + "\\ZAxisScan\\";
  29. private AxisController m_Stage;
  30. private ParameterOneControl m_parameterOne;
  31. private bool m_isWorking;
  32. private Button btnZClear;
  33. private System.Windows.Forms.Timer timer1;
  34. private List<string> m_imageList;
  35. public CameraZaxisScan(AppWorkspace appWorkspace)
  36. {
  37. m_appWorkspace = appWorkspace;
  38. InitializeComponent();
  39. InitializeLanguageText();
  40. }
  41. private void CameraZaxisScan_Load(object sender, EventArgs e)
  42. {
  43. try
  44. {
  45. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  46. m_Stage = AxisController.GetInstance();
  47. m_parameterOne = new ParameterOneControl(m_Stage);
  48. m_parameterOne.Dock = DockStyle.Fill;
  49. controlPanel.Controls.Add(m_parameterOne);
  50. }
  51. catch (Exception ex)
  52. {
  53. MessageBox.Show(ex.Message);
  54. }
  55. }
  56. /// <summary>
  57. /// 开始/停止,拍摄
  58. /// </summary>
  59. private void btnWorking_Click(object sender, EventArgs e)
  60. {
  61. try
  62. {
  63. if (Directory.Exists(m_picturePath))
  64. {
  65. DelectDir(m_picturePath);
  66. }
  67. else
  68. {
  69. Directory.CreateDirectory(m_picturePath);
  70. }
  71. if (!m_camera.IsOpen())
  72. {
  73. MessageBox.Show(PdnResources.GetString("Menu.merafoundpleaseconfirthatthecameraisconne.Text"));
  74. return;
  75. }
  76. if (m_isWorking)
  77. {
  78. StopWorking();
  79. // 更改控件状态
  80. m_parameterOne.Enabled = true;
  81. return;
  82. }
  83. if (m_parameterOne.StartPos == 0 && m_parameterOne.StopPos == 0)
  84. {
  85. MessageBox.Show(PdnResources.GetString("Menu.Pleasesetthestartandstoppositions2.Text"));
  86. return;
  87. }
  88. // 重新自动计算,防止没有手动点击计算
  89. m_parameterOne.Calculate();
  90. var zscanparm = m_parameterOne.ZScanParameter;
  91. _ZscanProgressDialog = TransferProgressDialog.CreatDialog("Z轴扫描", "扫描中......", (s1, e1) => StopWorking(), "Stop");
  92. m_Stage.ZScan(zscanparm.Start, zscanparm.Track, zscanparm.Times, Shooting, Package, StopWorking);
  93. m_isWorking = true;
  94. btnWorking.Text = PdnResources.GetString("Menu.stop.text");
  95. m_movedTimes = 0;
  96. m_imageList = new List<string>();
  97. // 禁用控件
  98. m_parameterOne.Enabled = false;
  99. _ZscanProgressDialog.Show();
  100. }
  101. catch (Exception ex)
  102. {
  103. MessageBox.Show(ex.Message);
  104. }
  105. }
  106. private void Package()
  107. {
  108. this.Invoke(new Action(() =>
  109. _ZscanProgressDialog.ProcessMsg = "打包中......"
  110. ));
  111. // 生成XML
  112. ZAxisScanModel scanModel = new ZAxisScanModel();
  113. scanModel.items = m_ZScanImages;
  114. var rule = new PicConfigModel.Rule();
  115. var ruleDB = Startup.instance.ruleDB;
  116. if (ruleDB != null)
  117. {
  118. var zxm = new ZipXmlModel();
  119. zxm.rule = rule;
  120. ruleDB.initZipXmlModel(zxm);
  121. }
  122. else
  123. {
  124. rule.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text");
  125. rule.gain_multiple = 1;
  126. rule.pixel_length = 100;
  127. rule.physical_length = 100;
  128. rule.ruler_units = (int)Startup.instance.measurementUnit;
  129. }
  130. scanModel.rule = rule;
  131. string stageModelXml = XmlSerializeHelper.XmlSerialize<ZAxisScanModel>(scanModel);
  132. string filePath = m_picturePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
  133. FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create);
  134. // 打包图片在主界面打开
  135. string zipPath = ZipHandleHelper.CompressedZipPackage(m_picturePath);
  136. if (!string.IsNullOrEmpty(zipPath))
  137. {
  138. this.Invoke(new Action(() => m_appWorkspace.OpenFileInNewWorkspace(zipPath)));
  139. }
  140. new Thread(() =>
  141. {
  142. ZipXmlModel zipXmlModel = new ZipXmlModel();
  143. //ZipXmlModel zipXmlModel = new ZipXmlModel();
  144. if (System.IO.File.Exists(Application.StartupPath + "\\BackupImages\\backupImages.xml"))
  145. zipXmlModel = XmlSerializeHelper.DESerializer<ZipXmlModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Open));
  146. if (Startup.instance.ruleDB != null)
  147. Startup.instance.ruleDB.initZipXmlModel(zipXmlModel);
  148. else
  149. {
  150. zipXmlModel.rule = new PicConfigModel.Rule();
  151. zipXmlModel.rule.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text");
  152. zipXmlModel.rule.gain_multiple = 1;
  153. zipXmlModel.rule.pixel_length = 100;
  154. zipXmlModel.rule.physical_length = 100;
  155. zipXmlModel.rule.ruler_units = (int)Startup.instance.measurementUnit;
  156. }
  157. //备份拍摄的图片以便恢复
  158. FileOperationHelper.BackupImages(m_picturePath, zipXmlModel);
  159. }).Start();
  160. }
  161. private void StopWorking()
  162. {
  163. if (!m_isWorking) return;
  164. m_Stage.FreeStage();
  165. m_movedTimes = 0;
  166. m_isWorking = false;
  167. }
  168. private void Shooting()
  169. {
  170. Thread.Sleep(100);
  171. if (!m_isWorking)
  172. {
  173. return;
  174. }
  175. // 修改拍摄高度
  176. this.BeginInvoke(new Action(() =>
  177. {
  178. m_parameterOne.updatelblHeight(m_currentHeight);
  179. }));
  180. //拍照
  181. var file = m_camera.OneShoot(m_picturePath);
  182. ZAxisScanModel.Item item = new ZAxisScanModel.Item();
  183. item.fileName = file;
  184. item.height = (decimal)m_currentHeight;
  185. m_ZScanImages.Add(item);
  186. m_imageList.Add(file);
  187. }
  188. private void CameraZaxisScan_FormClosing(object sender, FormClosingEventArgs e)
  189. {
  190. if (Directory.Exists(m_picturePath))
  191. {
  192. DelectDir(m_picturePath);
  193. }
  194. }
  195. public static void DelectDir(string srcPath)
  196. {
  197. try
  198. {
  199. DirectoryInfo dir = new DirectoryInfo(srcPath);
  200. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  201. foreach (FileSystemInfo i in fileinfo)
  202. {
  203. if (i is DirectoryInfo) //判断是否文件夹
  204. {
  205. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  206. subdir.Delete(true); //删除子目录和文件
  207. }
  208. else
  209. {
  210. System.IO.File.Delete(i.FullName); //删除指定文件
  211. }
  212. }
  213. }
  214. catch (Exception) { }
  215. }
  216. #region 控件
  217. /// <summary>
  218. /// Required designer variable.
  219. /// </summary>
  220. private System.ComponentModel.IContainer components = null;
  221. /// <summary>
  222. /// Clean up any resources being used.
  223. /// </summary>
  224. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  225. protected override void Dispose(bool disposing)
  226. {
  227. if (disposing && (components != null))
  228. {
  229. components.Dispose();
  230. }
  231. base.Dispose(disposing);
  232. }
  233. // Windows Form Designer generated code
  234. private void InitializeLanguageText()
  235. {
  236. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  237. this.btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text");
  238. this.groupBox2.Text = PdnResources.GetString("Menu.paramssettings.text");
  239. this.Text = PdnResources.GetString("Menu.ImageCollection.ZaxisScan.Text");
  240. }
  241. /// <summary>
  242. /// Required method for Designer support - do not modify
  243. /// the contents of this method with the code editor.
  244. /// </summary>
  245. private void InitializeComponent()
  246. {
  247. this.components = new System.ComponentModel.Container();
  248. this.groupBox1 = new System.Windows.Forms.GroupBox();
  249. this.btnZClear = new System.Windows.Forms.Button();
  250. this.btnWorking = new System.Windows.Forms.Button();
  251. this.groupBox2 = new System.Windows.Forms.GroupBox();
  252. this.controlPanel = new System.Windows.Forms.Panel();
  253. this.timer1 = new System.Windows.Forms.Timer(this.components);
  254. this.groupBox1.SuspendLayout();
  255. this.groupBox2.SuspendLayout();
  256. this.SuspendLayout();
  257. //
  258. // groupBox1
  259. //
  260. this.groupBox1.Controls.Add(this.btnZClear);
  261. this.groupBox1.Controls.Add(this.btnWorking);
  262. this.groupBox1.Location = new System.Drawing.Point(9, 10);
  263. this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
  264. this.groupBox1.Name = "groupBox1";
  265. this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
  266. this.groupBox1.Size = new System.Drawing.Size(292, 64);
  267. this.groupBox1.TabIndex = 0;
  268. this.groupBox1.TabStop = false;
  269. this.groupBox1.Text = "操作";
  270. //
  271. // btnZClear
  272. //
  273. this.btnZClear.Location = new System.Drawing.Point(131, 24);
  274. this.btnZClear.Name = "btnZClear";
  275. this.btnZClear.Size = new System.Drawing.Size(75, 23);
  276. this.btnZClear.TabIndex = 1;
  277. this.btnZClear.Text = "位置重置";
  278. this.btnZClear.UseVisualStyleBackColor = true;
  279. this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click);
  280. //
  281. // btnWorking
  282. //
  283. this.btnWorking.Location = new System.Drawing.Point(211, 23);
  284. this.btnWorking.Margin = new System.Windows.Forms.Padding(2);
  285. this.btnWorking.Name = "btnWorking";
  286. this.btnWorking.Size = new System.Drawing.Size(61, 24);
  287. this.btnWorking.TabIndex = 0;
  288. this.btnWorking.Text = "拍摄";
  289. this.btnWorking.UseVisualStyleBackColor = true;
  290. this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
  291. //
  292. // groupBox2
  293. //
  294. this.groupBox2.Controls.Add(this.controlPanel);
  295. this.groupBox2.Location = new System.Drawing.Point(9, 86);
  296. this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
  297. this.groupBox2.Name = "groupBox2";
  298. this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
  299. this.groupBox2.Size = new System.Drawing.Size(292, 312);
  300. this.groupBox2.TabIndex = 1;
  301. this.groupBox2.TabStop = false;
  302. this.groupBox2.Text = "参数设置";
  303. //
  304. // controlPanel
  305. //
  306. this.controlPanel.Location = new System.Drawing.Point(4, 19);
  307. this.controlPanel.Margin = new System.Windows.Forms.Padding(2);
  308. this.controlPanel.Name = "controlPanel";
  309. this.controlPanel.Size = new System.Drawing.Size(284, 288);
  310. this.controlPanel.TabIndex = 0;
  311. //
  312. // timer1
  313. //
  314. this.timer1.Enabled = true;
  315. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  316. //
  317. // CameraZaxisScan
  318. //
  319. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  320. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  321. this.ClientSize = new System.Drawing.Size(314, 421);
  322. this.Controls.Add(this.groupBox2);
  323. this.Controls.Add(this.groupBox1);
  324. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  325. this.Margin = new System.Windows.Forms.Padding(2);
  326. this.Name = "CameraZaxisScan";
  327. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  328. this.Text = "Z轴扫描";
  329. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraZaxisScan_FormClosing);
  330. this.Load += new System.EventHandler(this.CameraZaxisScan_Load);
  331. this.Controls.SetChildIndex(this.groupBox1, 0);
  332. this.Controls.SetChildIndex(this.groupBox2, 0);
  333. this.groupBox1.ResumeLayout(false);
  334. this.groupBox2.ResumeLayout(false);
  335. this.ResumeLayout(false);
  336. }
  337. private System.Windows.Forms.GroupBox groupBox1;
  338. private System.Windows.Forms.Button btnWorking;
  339. private System.Windows.Forms.GroupBox groupBox2;
  340. private System.Windows.Forms.Panel controlPanel;
  341. #endregion
  342. #region StageEvents
  343. public void OnUpdatePosition()
  344. {
  345. try //可能出现窗口销毁后回调
  346. {
  347. if (!this.IsDisposed)
  348. this.Invoke(new Action(() =>
  349. {
  350. m_parameterOne.PositionZ = m_Stage.Z;
  351. }));
  352. }
  353. catch { }
  354. }
  355. public void OnTimeoutConnect()
  356. {
  357. StopWorking();
  358. MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
  359. }
  360. public void OnErrorSend()
  361. {
  362. StopWorking();
  363. MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
  364. }
  365. #endregion
  366. private void btnZClear_Click(object sender, EventArgs e)
  367. {
  368. m_Stage.ClearPosZ();
  369. }
  370. private void timer1_Tick(object sender, EventArgs e)
  371. {
  372. btnWorking.Enabled = !m_isWorking;
  373. m_parameterOne.Enabled = !m_isWorking;
  374. // 更改控件状态
  375. btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text");
  376. if (_ZscanProgressDialog != null && !m_isWorking)
  377. {
  378. _ZscanProgressDialog.Close();
  379. _ZscanProgressDialog = null;
  380. }
  381. }
  382. }
  383. }