123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- 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<CurvedSurface[]> CallbackResult;
- public ImageStitchingFocusDialog(AxisController stage, List<DrawStithchingBase> 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<int, Dictionary<int, double>>();
- }
- 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);
- }
- /// <summary>
- /// Z 微动距离
- /// </summary>
- 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<DrawStithchingBase> m_graphicsList;
- private List<Control> _labelList;
- private Label _selectLabel;
- private Dictionary<int, Dictionary<int, double>> _fittingMap;
- private Dictionary<int, double> _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<Control>();
- if (_fittingList == null)
- _fittingList = new Dictionary<int, double>();
- 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<int, object>)[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<CurvedSurfacePoint>();
- foreach (var item in _fittingList)
- {
- PointF nextP = (PointF)(_labelList[item.Key].Tag as Dictionary<int, object>)[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
- }
- }
|