using PaintDotNet.Annotation.ImageCollect; using PaintDotNet.Base.SettingModel; using StageController; using System; using System.Collections.Generic; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.Adjust; using PaintDotNet.Camera; namespace PaintDotNet { public partial class ImageStitchingFocusDialog : PdnBaseForm { AxisController m_stage; private LoadingStageModel m_loadingStageModel; private int _lowSpeed; private int _highSpeed; protected double m_PxLength; private CurvedSurface[] ResultList; public Action CallbackResult; public ImageStitchingFocusDialog(AxisController stage, List graphicsList) { InitializeComponent(); InitializeButtonBackgroundImage(); InitLanguage(); m_stage = stage; m_loadingStageModel = Startup.instance.loadingStageModel; m_graphicsList = graphicsList; UpdateGrid(); _lowSpeed = m_loadingStageModel.items[0].ZSpeed; _highSpeed = m_loadingStageModel.items[5].ZSpeed; Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out m_PxLength); ResultList = new CurvedSurface[graphicsList.Count]; _fittingMap = new Dictionary>(); } private void InitializeButtonBackgroundImage() { btnContinuityUp.FlatStyle = FlatStyle.Flat; btnContinuityUp.FlatAppearance.BorderSize = 0; btnContinuityUp.BackgroundImageLayout = ImageLayout.Zoom; btnContinuityUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference; btnContinuityLower.FlatStyle = FlatStyle.Flat; btnContinuityLower.FlatAppearance.BorderSize = 0; btnContinuityLower.BackgroundImageLayout = ImageLayout.Zoom; btnContinuityLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference; btnFixedUp.FlatStyle = FlatStyle.Flat; btnFixedUp.FlatAppearance.BorderSize = 0; btnFixedUp.BackgroundImageLayout = ImageLayout.Zoom; btnFixedUp.BackgroundImage = PdnResources.GetImageResource("Images.ButtonUp.png").Reference; btnFixedLower.FlatStyle = FlatStyle.Flat; btnFixedLower.FlatAppearance.BorderSize = 0; btnFixedLower.BackgroundImageLayout = ImageLayout.Zoom; btnFixedLower.BackgroundImage = PdnResources.GetImageResource("Images.ButtonDown.png").Reference; } private void InitLanguage() { this.groupBox2.Text = PdnResources.GetString("Menu.Preview.text"); this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.groupBox6.Text = PdnResources.GetString("Menu.operation.text"); this.groupBox4.Text = PdnResources.GetString("Menu.continuous.text"); this.groupBox7.Text = PdnResources.GetString("Menu.Fixedlength.text"); this.groupBox3.Text = PdnResources.GetString("Menu.imagecapture.Imagestitching.Listofpuzzleareas.text"); groupBox5.Text = PdnResources.GetString("LocationList"); groupBox8.Text = PdnResources.GetString("Menu.Runningspeed.text"); btnOk.Text = PdnResources.GetString("Form.OkButton.Text"); BtnCancel.Text = PdnResources.GetString("Form.CancelButton.Text"); btnSaveOne.Text = PdnResources.GetString("Form.SaveButton.Text"); btnStartFocus.Text = PdnResources.GetString("Menu.ImageCollection.Autofocus.Text"); rbtHighS.Text = PdnResources.GetString("Button.Text.HighSpeed"); rbtLowS.Text = PdnResources.GetString("Button.Text.LowSpeed"); } private void CallbackDraw(Bitmap obj) { try { this.Invoke(new Action(() => { pctPreview.Image = (Image)obj.Clone(); })); m_BitmapCurrent = (Bitmap)obj.Clone(); } catch { } } private void UpdateGrid() { for (int i = 0; i < m_graphicsList.Count; i++) { var draw = m_graphicsList[i]; int[] tiles = draw.GetTiles(); double area = draw.GetArea(); string name = ""; if (draw is DrawStitchingRectangle) { name = PdnResources.GetString("Menu.LabelAction.DrawRectangle.Text"); } else if (draw is DrawStitchingPolygon) { name = PdnResources.GetString("Menu.LabelAction.DrawPolygon.Text"); } else { name = PdnResources.GetString("Menu.LabelAction.DrawCircle.Text"); } dgvRegionTeb.Rows.Add(i, name, draw.ColumnNum + "," + draw.RowNum, 0, area); } } private void ImageStitchingFocusDialog_Load(object sender, EventArgs e) { CameraManager.FrameCallback += CallbackDraw; rbtLowS.Checked = true; SpeedChangeClick(rbtLowS, null); } private void ImageStitchingFocusDialog_FormClosing(object sender, FormClosingEventArgs e) { CameraManager.FrameCallback -= CallbackDraw; } #region Z operations private void btnContinuityUp_MouseDown(object sender, MouseEventArgs e) { m_stage.GoTop(true); } private void btnContinuityStop_MouseUp(object sender, MouseEventArgs e) { m_stage.LockZ(); } private void btnContinuityLower_MouseDown(object sender, MouseEventArgs e) { m_stage.GoTop(false); } /// /// Z 微动距离 /// double _stepping = 1.0; private void btnFixedUp_Click(object sender, EventArgs e) { m_stage.Up(_stepping); } private void btnFixedLower_Click(object sender, EventArgs e) { m_stage.Up(-_stepping); } private void SpeedChangeClick(object sender, EventArgs e) { if (sender == rbtLowS) { m_stage.SetSpeedZ(_lowSpeed); } if (sender == rbtHighS) { m_stage.SetSpeedZ(_highSpeed); } } #endregion #region Region Grid private DrawStithchingBase m_graphicsCurrent; private List m_graphicsList; private List _labelList; private Label _selectLabel; private Dictionary> _fittingMap; private Dictionary _fittingList; private DataGridViewRow _selectRow; private Bitmap m_BitmapCurrent; private void dgvRegionTeb_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; _selectRow = dgvRegionTeb.Rows[e.RowIndex]; var select = m_graphicsList[e.RowIndex]; _fittingMap.TryGetValue(e.RowIndex, out _fittingList); if (m_graphicsCurrent != select) { _labelList = new List(); if (_fittingList == null) _fittingList = new Dictionary(); BuildGrid(select); m_graphicsCurrent = select; } } private void BuildGrid(DrawStithchingBase drawobj) { var _rowN = drawobj.RowNum; var _columnN = drawobj.ColumnNum; int width = pnlGridRegion.Width / _columnN; int height = pnlGridRegion.Height / _rowN; int i = 0; pnlGridRegion.Controls.Clear(); _labelList.Clear(); for (int r = 0; r < _rowN; r++) for (int c = 0; c < _columnN; c++) { Label label = new Label(); label.Width = width; label.Height = height; label.Location = new Point(width * c, r * height); label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; label.BackColor = System.Drawing.Color.White; label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; if (!(r % 2 == 0)) i = r * _columnN + _columnN - c - 1; else i = r * _columnN + c; label.Tag = drawobj.GetViewPoints()[i]; label.MouseClick += Label_Click; //label.DoubleClick += Label_DoubleClick; pnlGridRegion.Controls.Add(label); _labelList.Add(label); if (_fittingList.ContainsKey(i)) { label.BackColor = Color.Gold; label.Text = _fittingList[i].ToString("f1"); } } } protected double ConvertPXToUm(double length) { return length * m_PxLength; } private void Label_Click(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (m_stage.IsMoving) return; //if (_selectLabel != null) // _selectLabel.BorderStyle = BorderStyle.FixedSingle; _selectLabel = sender as Label; //_selectLabel.BorderStyle = BorderStyle.Fixed3D; PointF nextP = (PointF)(_selectLabel.Tag as Dictionary)[0]; var x = ConvertPXToUm(nextP.X); var y = ConvertPXToUm(nextP.Y); m_stage.To(x, y); } else if (e.Button == MouseButtons.Right) { var label = sender as Label; int i = _labelList.IndexOf(label); if (label.BackColor == Color.Gold) { _fittingList.Remove(i); label.BackColor = Color.White; label.Text = ""; } else { _fittingList.Add(i, m_stage.Z); label.BackColor = Color.Gold; label.Text = m_stage.Z.ToString("f1"); } } } #endregion #region Operate private void btnStartFocus_Click(object sender, EventArgs e) { new Task(new Action(() => { try { AutoFocusWorkflow.AutoFocus(m_stage, () => { return m_BitmapCurrent; }); } catch (Exception ex) { MessageBox.Show(ex.Message); } })).Start(); } private void btnSaveOne_Click(object sender, EventArgs e) { CurvedSurface curvedSurface; bool result = Calculate(out curvedSurface); if (result) { _fittingMap.Add(_selectRow.Index, _fittingList); _selectRow.DefaultCellStyle.BackColor = Color.Lime; } else { _selectRow.DefaultCellStyle.BackColor = Color.Yellow; } ResultList[_selectRow.Index] = curvedSurface; } private bool Calculate(out CurvedSurface curvedSurface) { curvedSurface = null; if (_fittingList.Keys.Count < 3) { MessageBox.Show("Need at last 3 points."); return false; } var list = new List(); foreach (var item in _fittingList) { PointF nextP = (PointF)(_labelList[item.Key].Tag as Dictionary)[0]; var x = ConvertPXToUm(nextP.X); var y = ConvertPXToUm(nextP.Y); list.Add(new CurvedSurfacePoint(x, y, item.Value)); } curvedSurface = new CurvedSurface(); curvedSurface.Init(list); try { curvedSurface.CalPara(); } catch { return false; } return true; } private void btnSetZ_Click(object sender, EventArgs e) { int i = _labelList.IndexOf(_selectLabel); if (_selectLabel.BackColor == Color.Gold) { _fittingList.Remove(i); _selectLabel.BackColor = Color.White; _selectLabel.Text = ""; } else { _fittingList.Add(i, m_stage.Z); _selectLabel.BackColor = Color.Gold; _selectLabel.Text = m_stage.Z.ToString("f1"); } } private void btnOk_Click(object sender, EventArgs e) { CallbackResult(ResultList); Close(); } #endregion } }