using System; using System.Collections.Generic; using System.IO; using System.IO.Ports; using System.Threading; using System.Windows.Forms; using PaintDotNet.Base.CommTool; using PaintDotNet.Base.Functionodel; using PaintDotNet.Base.SettingModel; using PaintDotNet.Camera; using PaintDotNet.Camera; using PaintDotNet.ImageCollect.CameraEDOF; using StageController; using StageController.M3H; namespace PaintDotNet.ImageCollect { internal partial class CameraZaxisScan : FloatingToolForm, IStageEvent { private AppWorkspace m_appWorkspace; TransferProgressDialog _ZscanProgressDialog; List m_ZScanImages = new List(); private double m_currentHeight => m_Stage.Z; // 操作台参数配置 private LoadingStageModel m_loadingStageModel = Startup.instance.loadingStageModel; // 已移动次数 private int m_movedTimes; private ICamera m_camera; private string m_picturePath = Application.StartupPath + "\\ZAxisScan\\"; private AxisController m_Stage; private ParameterOneControl m_parameterOne; private bool m_isWorking; private Button btnZClear; private System.Windows.Forms.Timer timer1; private List m_imageList; public CameraZaxisScan(AppWorkspace appWorkspace) { m_appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); } private void CameraZaxisScan_Load(object sender, EventArgs e) { try { m_camera = CameraManager.CurrentCamera; m_Stage = AxisController.GetInstance(); m_parameterOne = new ParameterOneControl(m_Stage); m_parameterOne.Dock = DockStyle.Fill; controlPanel.Controls.Add(m_parameterOne); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// /// 开始/停止,拍摄 /// private void btnWorking_Click(object sender, EventArgs e) { try { if (Directory.Exists(m_picturePath)) { DelectDir(m_picturePath); } else { Directory.CreateDirectory(m_picturePath); } if (!m_camera.IsOpen()) { MessageBox.Show(PdnResources.GetString("Menu.merafoundpleaseconfirthatthecameraisconne.Text")); return; } if (m_isWorking) { StopWorking(); // 更改控件状态 m_parameterOne.Enabled = true; return; } if (m_parameterOne.StartPos == 0 && m_parameterOne.StopPos == 0) { MessageBox.Show(PdnResources.GetString("Menu.Pleasesetthestartandstoppositions2.Text")); return; } // 重新自动计算,防止没有手动点击计算 m_parameterOne.Calculate(); var zscanparm = m_parameterOne.ZScanParameter; _ZscanProgressDialog = TransferProgressDialog.CreatDialog("Z轴扫描", "扫描中......", (s1, e1) => StopWorking(), "Stop"); m_Stage.ZScan(zscanparm.Start, zscanparm.Track, zscanparm.Times, Shooting, Package, StopWorking); m_isWorking = true; btnWorking.Text = PdnResources.GetString("Menu.stop.text"); m_movedTimes = 0; m_imageList = new List(); // 禁用控件 m_parameterOne.Enabled = false; _ZscanProgressDialog.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Package() { this.Invoke(new Action(() => _ZscanProgressDialog.ProcessMsg = "打包中......" )); // 生成XML ZAxisScanModel scanModel = new ZAxisScanModel(); scanModel.items = m_ZScanImages; var rule = new PicConfigModel.Rule(); var ruleDB = Startup.instance.ruleDB; if (ruleDB != null) { var zxm = new ZipXmlModel(); zxm.rule = rule; ruleDB.initZipXmlModel(zxm); } else { rule.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text"); rule.gain_multiple = 1; rule.pixel_length = 100; rule.physical_length = 100; rule.ruler_units = (int)Startup.instance.measurementUnit; } scanModel.rule = rule; string stageModelXml = XmlSerializeHelper.XmlSerialize(scanModel); string filePath = m_picturePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml"; FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create); // 打包图片在主界面打开 string zipPath = ZipHandleHelper.CompressedZipPackage(m_picturePath); if (!string.IsNullOrEmpty(zipPath)) { this.Invoke(new Action(() => m_appWorkspace.OpenFileInNewWorkspace(zipPath))); } new Thread(() => { ZipXmlModel zipXmlModel = new ZipXmlModel(); //ZipXmlModel zipXmlModel = new ZipXmlModel(); if (System.IO.File.Exists(Application.StartupPath + "\\BackupImages\\backupImages.xml")) zipXmlModel = XmlSerializeHelper.DESerializer(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Open)); if (Startup.instance.ruleDB != null) Startup.instance.ruleDB.initZipXmlModel(zipXmlModel); else { zipXmlModel.rule = new PicConfigModel.Rule(); zipXmlModel.rule.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text"); zipXmlModel.rule.gain_multiple = 1; zipXmlModel.rule.pixel_length = 100; zipXmlModel.rule.physical_length = 100; zipXmlModel.rule.ruler_units = (int)Startup.instance.measurementUnit; } //备份拍摄的图片以便恢复 FileOperationHelper.BackupImages(m_picturePath, zipXmlModel); }).Start(); } private void StopWorking() { if (!m_isWorking) return; m_Stage.FreeStage(); m_movedTimes = 0; m_isWorking = false; } private void Shooting() { Thread.Sleep(100); if (!m_isWorking) { return; } // 修改拍摄高度 this.BeginInvoke(new Action(() => { m_parameterOne.updatelblHeight(m_currentHeight); })); //拍照 if (!Directory.Exists(m_picturePath)) { Directory.CreateDirectory(m_picturePath);//不存在就创建目录 } var img = m_camera.OneShoot(); string file = m_picturePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".bmp"; img?.Save(file); ZAxisScanModel.Item item = new ZAxisScanModel.Item(); item.fileName = file; item.height = (decimal)m_currentHeight; m_ZScanImages.Add(item); m_imageList.Add(file); } private void CameraZaxisScan_FormClosing(object sender, FormClosingEventArgs e) { if (Directory.Exists(m_picturePath)) { DelectDir(m_picturePath); } } public static void DelectDir(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { System.IO.File.Delete(i.FullName); //删除指定文件 } } } catch (Exception) { } } #region 控件 /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } // Windows Form Designer generated code private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text"); this.groupBox2.Text = PdnResources.GetString("Menu.paramssettings.text"); this.Text = PdnResources.GetString("Menu.ImageCollection.ZaxisScan.Text"); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnZClear = new System.Windows.Forms.Button(); this.btnWorking = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.controlPanel = new System.Windows.Forms.Panel(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.btnZClear); this.groupBox1.Controls.Add(this.btnWorking); this.groupBox1.Location = new System.Drawing.Point(9, 10); this.groupBox1.Margin = new System.Windows.Forms.Padding(2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Padding = new System.Windows.Forms.Padding(2); this.groupBox1.Size = new System.Drawing.Size(292, 64); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // btnZClear // this.btnZClear.Location = new System.Drawing.Point(131, 24); this.btnZClear.Name = "btnZClear"; this.btnZClear.Size = new System.Drawing.Size(75, 23); this.btnZClear.TabIndex = 1; this.btnZClear.Text = "位置重置"; this.btnZClear.UseVisualStyleBackColor = true; this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click); // // btnWorking // this.btnWorking.Location = new System.Drawing.Point(211, 23); this.btnWorking.Margin = new System.Windows.Forms.Padding(2); this.btnWorking.Name = "btnWorking"; this.btnWorking.Size = new System.Drawing.Size(61, 24); this.btnWorking.TabIndex = 0; this.btnWorking.Text = "拍摄"; this.btnWorking.UseVisualStyleBackColor = true; this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click); // // groupBox2 // this.groupBox2.Controls.Add(this.controlPanel); this.groupBox2.Location = new System.Drawing.Point(9, 86); this.groupBox2.Margin = new System.Windows.Forms.Padding(2); this.groupBox2.Name = "groupBox2"; this.groupBox2.Padding = new System.Windows.Forms.Padding(2); this.groupBox2.Size = new System.Drawing.Size(292, 312); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "参数设置"; // // controlPanel // this.controlPanel.Location = new System.Drawing.Point(4, 19); this.controlPanel.Margin = new System.Windows.Forms.Padding(2); this.controlPanel.Name = "controlPanel"; this.controlPanel.Size = new System.Drawing.Size(284, 288); this.controlPanel.TabIndex = 0; // // timer1 // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // CameraZaxisScan // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(314, 421); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Margin = new System.Windows.Forms.Padding(2); this.Name = "CameraZaxisScan"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Z轴扫描"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraZaxisScan_FormClosing); this.Load += new System.EventHandler(this.CameraZaxisScan_Load); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.ResumeLayout(false); } private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnWorking; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.Panel controlPanel; #endregion #region StageEvents public void OnUpdatePosition() { try //可能出现窗口销毁后回调 { if (!this.IsDisposed) this.Invoke(new Action(() => { m_parameterOne.PositionZ = m_Stage.Z; })); } catch { } } public void OnTimeoutConnect() { StopWorking(); MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text")); } public void OnErrorSend() { StopWorking(); MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text")); } #endregion private void btnZClear_Click(object sender, EventArgs e) { m_Stage.ClearPosZ(); } private void timer1_Tick(object sender, EventArgs e) { btnWorking.Enabled = !m_isWorking; m_parameterOne.Enabled = !m_isWorking; // 更改控件状态 btnWorking.Text = PdnResources.GetString("Menu.ImageCollection.ShootAction.Text"); if (_ZscanProgressDialog != null && !m_isWorking) { _ZscanProgressDialog.Close(); _ZscanProgressDialog = null; } } } }