123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- 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<ZAxisScanModel.Item> m_ZScanImages = new List<ZAxisScanModel.Item>();
- 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<string> 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);
- }
- }
- /// <summary>
- /// 开始/停止,拍摄
- /// </summary>
- 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<string>();
- // 禁用控件
- 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<ZAxisScanModel>(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<ZipXmlModel>(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 控件
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- 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");
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- 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;
- }
- }
- }
- }
|