CameraZaxisScan.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.Camera;
  11. using PaintDotNet.Camera;
  12. using PaintDotNet.ImageCollect.CameraEDOF;
  13. using StageController;
  14. using StageController.M3H;
  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 ICamera 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 = CameraManager.CurrentCamera;
  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. if (!Directory.Exists(m_picturePath))
  182. {
  183. Directory.CreateDirectory(m_picturePath);//不存在就创建目录
  184. }
  185. var img = m_camera.OneShoot();
  186. string file = m_picturePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".bmp";
  187. img?.Save(file);
  188. ZAxisScanModel.Item item = new ZAxisScanModel.Item();
  189. item.fileName = file;
  190. item.height = (decimal)m_currentHeight;
  191. m_ZScanImages.Add(item);
  192. m_imageList.Add(file);
  193. }
  194. private void CameraZaxisScan_FormClosing(object sender, FormClosingEventArgs e)
  195. {
  196. if (Directory.Exists(m_picturePath))
  197. {
  198. DelectDir(m_picturePath);
  199. }
  200. }
  201. public static void DelectDir(string srcPath)
  202. {
  203. try
  204. {
  205. DirectoryInfo dir = new DirectoryInfo(srcPath);
  206. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  207. foreach (FileSystemInfo i in fileinfo)
  208. {
  209. if (i is DirectoryInfo) //判断是否文件夹
  210. {
  211. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  212. subdir.Delete(true); //删除子目录和文件
  213. }
  214. else
  215. {
  216. System.IO.File.Delete(i.FullName); //删除指定文件
  217. }
  218. }
  219. }
  220. catch (Exception) { }
  221. }
  222. #region 控件
  223. /// <summary>
  224. /// Required designer variable.
  225. /// </summary>
  226. private System.ComponentModel.IContainer components = null;
  227. /// <summary>
  228. /// Clean up any resources being used.
  229. /// </summary>
  230. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  231. protected override void Dispose(bool disposing)
  232. {
  233. if (disposing && (components != null))
  234. {
  235. components.Dispose();
  236. }
  237. base.Dispose(disposing);
  238. }
  239. // Windows Form Designer generated code
  240. private void InitializeLanguageText()
  241. {
  242. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  243. this.btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text");
  244. this.groupBox2.Text = PdnResources.GetString("Menu.paramssettings.text");
  245. this.Text = PdnResources.GetString("Menu.ImageCollection.ZaxisScan.Text");
  246. }
  247. /// <summary>
  248. /// Required method for Designer support - do not modify
  249. /// the contents of this method with the code editor.
  250. /// </summary>
  251. private void InitializeComponent()
  252. {
  253. this.components = new System.ComponentModel.Container();
  254. this.groupBox1 = new System.Windows.Forms.GroupBox();
  255. this.btnZClear = new System.Windows.Forms.Button();
  256. this.btnWorking = new System.Windows.Forms.Button();
  257. this.groupBox2 = new System.Windows.Forms.GroupBox();
  258. this.controlPanel = new System.Windows.Forms.Panel();
  259. this.timer1 = new System.Windows.Forms.Timer(this.components);
  260. this.groupBox1.SuspendLayout();
  261. this.groupBox2.SuspendLayout();
  262. this.SuspendLayout();
  263. //
  264. // groupBox1
  265. //
  266. this.groupBox1.Controls.Add(this.btnZClear);
  267. this.groupBox1.Controls.Add(this.btnWorking);
  268. this.groupBox1.Location = new System.Drawing.Point(9, 10);
  269. this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
  270. this.groupBox1.Name = "groupBox1";
  271. this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
  272. this.groupBox1.Size = new System.Drawing.Size(292, 64);
  273. this.groupBox1.TabIndex = 0;
  274. this.groupBox1.TabStop = false;
  275. this.groupBox1.Text = "操作";
  276. //
  277. // btnZClear
  278. //
  279. this.btnZClear.Location = new System.Drawing.Point(131, 24);
  280. this.btnZClear.Name = "btnZClear";
  281. this.btnZClear.Size = new System.Drawing.Size(75, 23);
  282. this.btnZClear.TabIndex = 1;
  283. this.btnZClear.Text = "位置重置";
  284. this.btnZClear.UseVisualStyleBackColor = true;
  285. this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click);
  286. //
  287. // btnWorking
  288. //
  289. this.btnWorking.Location = new System.Drawing.Point(211, 23);
  290. this.btnWorking.Margin = new System.Windows.Forms.Padding(2);
  291. this.btnWorking.Name = "btnWorking";
  292. this.btnWorking.Size = new System.Drawing.Size(61, 24);
  293. this.btnWorking.TabIndex = 0;
  294. this.btnWorking.Text = "拍摄";
  295. this.btnWorking.UseVisualStyleBackColor = true;
  296. this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
  297. //
  298. // groupBox2
  299. //
  300. this.groupBox2.Controls.Add(this.controlPanel);
  301. this.groupBox2.Location = new System.Drawing.Point(9, 86);
  302. this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
  303. this.groupBox2.Name = "groupBox2";
  304. this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
  305. this.groupBox2.Size = new System.Drawing.Size(292, 312);
  306. this.groupBox2.TabIndex = 1;
  307. this.groupBox2.TabStop = false;
  308. this.groupBox2.Text = "参数设置";
  309. //
  310. // controlPanel
  311. //
  312. this.controlPanel.Location = new System.Drawing.Point(4, 19);
  313. this.controlPanel.Margin = new System.Windows.Forms.Padding(2);
  314. this.controlPanel.Name = "controlPanel";
  315. this.controlPanel.Size = new System.Drawing.Size(284, 288);
  316. this.controlPanel.TabIndex = 0;
  317. //
  318. // timer1
  319. //
  320. this.timer1.Enabled = true;
  321. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  322. //
  323. // CameraZaxisScan
  324. //
  325. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  326. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  327. this.ClientSize = new System.Drawing.Size(314, 421);
  328. this.Controls.Add(this.groupBox2);
  329. this.Controls.Add(this.groupBox1);
  330. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  331. this.Margin = new System.Windows.Forms.Padding(2);
  332. this.Name = "CameraZaxisScan";
  333. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  334. this.Text = "Z轴扫描";
  335. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraZaxisScan_FormClosing);
  336. this.Load += new System.EventHandler(this.CameraZaxisScan_Load);
  337. this.Controls.SetChildIndex(this.groupBox1, 0);
  338. this.Controls.SetChildIndex(this.groupBox2, 0);
  339. this.groupBox1.ResumeLayout(false);
  340. this.groupBox2.ResumeLayout(false);
  341. this.ResumeLayout(false);
  342. }
  343. private System.Windows.Forms.GroupBox groupBox1;
  344. private System.Windows.Forms.Button btnWorking;
  345. private System.Windows.Forms.GroupBox groupBox2;
  346. private System.Windows.Forms.Panel controlPanel;
  347. #endregion
  348. #region StageEvents
  349. public void OnUpdatePosition()
  350. {
  351. try //可能出现窗口销毁后回调
  352. {
  353. if (!this.IsDisposed)
  354. this.Invoke(new Action(() =>
  355. {
  356. m_parameterOne.PositionZ = m_Stage.Z;
  357. }));
  358. }
  359. catch { }
  360. }
  361. public void OnTimeoutConnect()
  362. {
  363. StopWorking();
  364. MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
  365. }
  366. public void OnErrorSend()
  367. {
  368. StopWorking();
  369. MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
  370. }
  371. #endregion
  372. private void btnZClear_Click(object sender, EventArgs e)
  373. {
  374. m_Stage.ClearPosZ();
  375. }
  376. private void timer1_Tick(object sender, EventArgs e)
  377. {
  378. btnWorking.Enabled = !m_isWorking;
  379. m_parameterOne.Enabled = !m_isWorking;
  380. // 更改控件状态
  381. btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text");
  382. if (_ZscanProgressDialog != null && !m_isWorking)
  383. {
  384. _ZscanProgressDialog.Close();
  385. _ZscanProgressDialog = null;
  386. }
  387. }
  388. }
  389. }