using OpenCvSharp; using OpenCvSharp.Extensions; using PaintDotNet.Adjust.BaseImage; using PaintDotNet.Annotation; using PaintDotNet.Annotation.Enum; using PaintDotNet.Annotation.ImageCollect; using PaintDotNet.Annotation.Measure; using PaintDotNet.Base.SettingModel; using PaintDotNet.Camera; using PaintDotNet.CustomControl; using PaintDotNet.ImageCollect; using StageController; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.IO.Ports; using System.Linq; using System.Management; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Interop; namespace PaintDotNet.Hardware { internal class HardwareBaseDialog : FloatingToolForm, IStageEvent { protected AppWorkspace m_appWorkspace; #region 声明组件 protected GroupBox gboxXYspeed; protected GroupBox gboxZspeed; protected DocumentPreview m_documentWorkspace; protected Label lblXPosition; protected Label lblYPosition; protected Label lblZPosition; protected Label lblXDistance; protected Label lblYDistance; protected Label lblLDistance; protected Button btnFreeloadstage; protected Button btnStoploadstage; protected Button btnResetloadstage; protected Button btnCenterloadstage; protected Button btnInloadstage; protected Button btnOutloadstage; protected Button btnZStop; protected Button btnZFree; protected TextBox txtXStep; protected TextBox txtYStep; protected TextBox txtZStep; protected ComboBox cmbXYStepUnit; protected ComboBox cmbZStepUnit; protected Button btnContinuityLower; protected Button btnContinuityUp; protected Button btnFixedLower; protected Button btnFixedUp; protected Button btnCenterDisplay; protected Button btnStopall; protected Button btnDsRocker; protected Button btnSetting; #endregion #region Axis Fields protected AxisController m_Stage; protected LoadingStageModel m_loadingStageModel; protected List m_speedLists = new List(); /// /// X轴單次长度 /// protected virtual double StepLenthX { get { string text = txtXStep.Text.Trim(); if (string.IsNullOrEmpty(text)) { throw new Exception("X" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text")); } var value = double.Parse(text); if (cmbXYStepUnit.SelectedItem.ToString().Equals("mm")) { value *= 1000; } return value; } } /// /// Y轴單次进长度 /// protected virtual double StepLenthY { get { string text = txtYStep.Text.Trim(); if (string.IsNullOrEmpty(text)) { throw new Exception("Y" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text")); } var value = double.Parse(text); if (cmbXYStepUnit.SelectedItem.ToString().Equals("mm")) { value *= 1000; } return value; } } /// /// Z轴步进长度 /// protected virtual double StepLenthZ { get { string text = txtZStep.Text.Trim(); if (string.IsNullOrEmpty(text)) { throw new Exception("Z" + PdnResources.GetString("Menu.TheXaxismovestepannotbeempty0.Text")); } var value = double.Parse(text); if (cmbZStepUnit.SelectedItem.ToString().Equals("mm")) { value *= 1000; } return value; } } protected virtual bool IsSpeedHarmonize { get; } public static int m_xyspeed = 0;//xy轴运行速度 public static int m_zspeed = 0; //z轴运行速度 protected double m_initStepX = 0; //清零时X轴所在位置 protected double m_initStepY = 0; //清零时Y轴所在位置 protected double m_stageWidth; // 行程X(操作台高度) protected double m_stageHeight; // 行程Y(操作台宽度) #endregion Axis Fields #region Camera Fields /// /// 视场宽度 /// protected virtual double VisionWidth => m_viewWidth * m_PxLength; /// /// 视场高度 /// protected virtual double VisionHeight => m_viewHeight * m_PxLength; protected int m_viewWidth; // 视场宽(像素) protected int m_viewHeight; // 视场高(像素) protected int VisionWidthPixel { get => m_viewWidth; set { if (value == m_viewWidth) return; m_documentWorkspace.ViewWidth = value; m_viewWidth = value; } } protected int VisionHeightPixel { get => m_viewHeight; set { if (value == m_viewHeight) return; m_documentWorkspace.ViewHeigth = value; m_viewHeight = value; } } /// /// 平台是否复位 /// protected bool m_isReset; //标尺比例 protected double m_PxLength; #endregion protected string[] m_unit = new string[] { "mm", "um" }; protected bool m_isShowCenter = false; private System.Windows.Forms.Timer timerStageUpdate; private IContainer components; // 窗口缩小比例 protected int m_docspaceScale = 1; public HardwareBaseDialog() { InitializeComponent(); } public HardwareBaseDialog(AppWorkspace appWorkspace) { m_appWorkspace = appWorkspace; Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out m_PxLength); InitializeComponent(); InitializeButtonBackgroundImage(); m_documentWorkspace = new DocumentPreview(); m_documentWorkspace.AppWorkspaceTop = appWorkspace; m_documentWorkspace.Visible = true; m_documentWorkspace.RulersEnabled = true; m_documentWorkspace.Units = MeasurementUnit.Micron; m_docspaceScale = 10; VisionWidthPixel = 2448; VisionHeightPixel = 2048; Bitmap bitmap = new Bitmap(m_viewWidth, m_viewHeight); m_documentWorkspace.Document = Document.FromImage(bitmap); m_documentWorkspace.Dock = DockStyle.Fill; m_documentWorkspace.ScaleFactor = new ScaleFactor(1, m_docspaceScale); m_documentWorkspace.SetViewPoint(0, 0); ToolPointer.GetMouseLeftClickPoint += GetMouseLeftClickPoint; Load += OnLoad; FormClosing += OnFormClosing; } private void OnFormClosing(object sender, FormClosingEventArgs e) { UninitCamera(); UninitStage(); UninitDocumentWorkspace(); ToolPointer.GetMouseLeftClickPoint -= GetMouseLeftClickPoint; GC.Collect(); base.Dispose(); } private void OnLoad(object sender, EventArgs e) { m_loadingStageModel = Startup.instance.loadingStageModel; InitializeCamera(); InitStage(); ReloadingStageParam(m_loadingStageModel); timerStageUpdate.Start(); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.gboxXYspeed = new System.Windows.Forms.GroupBox(); this.gboxZspeed = new System.Windows.Forms.GroupBox(); this.lblYPosition = new System.Windows.Forms.Label(); this.lblXPosition = new System.Windows.Forms.Label(); this.lblZPosition = new System.Windows.Forms.Label(); this.lblLDistance = new System.Windows.Forms.Label(); this.lblYDistance = new System.Windows.Forms.Label(); this.lblXDistance = new System.Windows.Forms.Label(); this.btnFreeloadstage = new System.Windows.Forms.Button(); this.btnStoploadstage = new System.Windows.Forms.Button(); this.btnResetloadstage = new System.Windows.Forms.Button(); this.btnCenterloadstage = new System.Windows.Forms.Button(); this.btnInloadstage = new System.Windows.Forms.Button(); this.btnOutloadstage = new System.Windows.Forms.Button(); this.btnZFree = new System.Windows.Forms.Button(); this.btnZStop = new System.Windows.Forms.Button(); this.btnFixedLower = new System.Windows.Forms.Button(); this.btnFixedUp = new System.Windows.Forms.Button(); this.btnContinuityLower = new System.Windows.Forms.Button(); this.btnContinuityUp = new System.Windows.Forms.Button(); this.btnCenterDisplay = new System.Windows.Forms.Button(); this.btnStopall = new System.Windows.Forms.Button(); this.btnDsRocker = new System.Windows.Forms.Button(); this.btnSetting = new System.Windows.Forms.Button(); this.txtXStep = new System.Windows.Forms.TextBox(); this.txtYStep = new System.Windows.Forms.TextBox(); this.txtZStep = new System.Windows.Forms.TextBox(); this.cmbZStepUnit = new System.Windows.Forms.ComboBox(); this.cmbXYStepUnit = new System.Windows.Forms.ComboBox(); this.timerStageUpdate = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // gboxXYspeed // this.gboxXYspeed.Location = new System.Drawing.Point(0, 0); this.gboxXYspeed.Name = "gboxXYspeed"; this.gboxXYspeed.Size = new System.Drawing.Size(200, 100); this.gboxXYspeed.TabIndex = 0; this.gboxXYspeed.TabStop = false; // // gboxZspeed // this.gboxZspeed.Location = new System.Drawing.Point(0, 0); this.gboxZspeed.Name = "gboxZspeed"; this.gboxZspeed.Size = new System.Drawing.Size(200, 100); this.gboxZspeed.TabIndex = 0; this.gboxZspeed.TabStop = false; // // lblYPosition // this.lblYPosition.Location = new System.Drawing.Point(0, 0); this.lblYPosition.Name = "lblYPosition"; this.lblYPosition.Size = new System.Drawing.Size(100, 23); this.lblYPosition.TabIndex = 0; // // lblXPosition // this.lblXPosition.Location = new System.Drawing.Point(0, 0); this.lblXPosition.Name = "lblXPosition"; this.lblXPosition.Size = new System.Drawing.Size(100, 23); this.lblXPosition.TabIndex = 0; // // lblZPosition // this.lblZPosition.Location = new System.Drawing.Point(0, 0); this.lblZPosition.Name = "lblZPosition"; this.lblZPosition.Size = new System.Drawing.Size(100, 23); this.lblZPosition.TabIndex = 0; // // lblLDistance // this.lblLDistance.Location = new System.Drawing.Point(0, 0); this.lblLDistance.Name = "lblLDistance"; this.lblLDistance.Size = new System.Drawing.Size(100, 23); this.lblLDistance.TabIndex = 0; // // lblYDistance // this.lblYDistance.Location = new System.Drawing.Point(0, 0); this.lblYDistance.Name = "lblYDistance"; this.lblYDistance.Size = new System.Drawing.Size(100, 23); this.lblYDistance.TabIndex = 0; // // lblXDistance // this.lblXDistance.Location = new System.Drawing.Point(0, 0); this.lblXDistance.Name = "lblXDistance"; this.lblXDistance.Size = new System.Drawing.Size(100, 23); this.lblXDistance.TabIndex = 0; // // btnFreeloadstage // this.btnFreeloadstage.Location = new System.Drawing.Point(0, 0); this.btnFreeloadstage.Name = "btnFreeloadstage"; this.btnFreeloadstage.Size = new System.Drawing.Size(75, 23); this.btnFreeloadstage.TabIndex = 0; this.btnFreeloadstage.Click += new System.EventHandler(this.btnLockXY_Click); // // btnStoploadstage // this.btnStoploadstage.Location = new System.Drawing.Point(0, 0); this.btnStoploadstage.Name = "btnStoploadstage"; this.btnStoploadstage.Size = new System.Drawing.Size(75, 23); this.btnStoploadstage.TabIndex = 0; this.btnStoploadstage.Click += new System.EventHandler(this.btnStopXY_Click); // // btnResetloadstage // this.btnResetloadstage.Location = new System.Drawing.Point(0, 0); this.btnResetloadstage.Name = "btnResetloadstage"; this.btnResetloadstage.Size = new System.Drawing.Size(75, 23); this.btnResetloadstage.TabIndex = 0; this.btnResetloadstage.Click += new System.EventHandler(this.btnResetloadstage_Click); // // btnCenterloadstage // this.btnCenterloadstage.Location = new System.Drawing.Point(0, 0); this.btnCenterloadstage.Name = "btnCenterloadstage"; this.btnCenterloadstage.Size = new System.Drawing.Size(75, 23); this.btnCenterloadstage.TabIndex = 0; this.btnCenterloadstage.Click += new System.EventHandler(this.btnCenterloadstage_Click); // // btnInloadstage // this.btnInloadstage.Location = new System.Drawing.Point(0, 0); this.btnInloadstage.Name = "btnInloadstage"; this.btnInloadstage.Size = new System.Drawing.Size(75, 23); this.btnInloadstage.TabIndex = 0; this.btnInloadstage.Click += new System.EventHandler(this.btnInloadstage_Click); // // btnOutloadstage // this.btnOutloadstage.Location = new System.Drawing.Point(0, 0); this.btnOutloadstage.Name = "btnOutloadstage"; this.btnOutloadstage.Size = new System.Drawing.Size(75, 23); this.btnOutloadstage.TabIndex = 0; this.btnOutloadstage.Click += new System.EventHandler(this.btnOutloadstage_Click); // // btnZFree // this.btnZFree.Location = new System.Drawing.Point(0, 0); this.btnZFree.Name = "btnZFree"; this.btnZFree.Size = new System.Drawing.Size(75, 23); this.btnZFree.TabIndex = 0; this.btnZFree.Click += new System.EventHandler(this.btnZLock_Click); // // btnZStop // this.btnZStop.Location = new System.Drawing.Point(0, 0); this.btnZStop.Name = "btnZStop"; this.btnZStop.Size = new System.Drawing.Size(75, 23); this.btnZStop.TabIndex = 0; this.btnZStop.Click += new System.EventHandler(this.btnZStop_Click); // // btnFixedLower // this.btnFixedLower.Location = new System.Drawing.Point(0, 0); this.btnFixedLower.Name = "btnFixedLower"; this.btnFixedLower.Size = new System.Drawing.Size(75, 23); this.btnFixedLower.TabIndex = 0; btnFixedLower.Click += btnFixedLower_Click; // // btnFixedUp // this.btnFixedUp.Location = new System.Drawing.Point(0, 0); this.btnFixedUp.Name = "btnFixedUp"; this.btnFixedUp.Size = new System.Drawing.Size(75, 23); this.btnFixedUp.TabIndex = 0; btnFixedUp.Click += btnFixedUp_Click; // // btnContinuityLower // this.btnContinuityLower.Location = new System.Drawing.Point(0, 0); this.btnContinuityLower.Name = "btnContinuityLower"; this.btnContinuityLower.Size = new System.Drawing.Size(75, 23); this.btnContinuityLower.TabIndex = 0; btnContinuityLower.MouseDown += btnContinuityLower_Start; btnContinuityLower.MouseUp += btnZStop_Click; // // btnContinuityUp // this.btnContinuityUp.Location = new System.Drawing.Point(0, 0); this.btnContinuityUp.Name = "btnContinuityUp"; this.btnContinuityUp.Size = new System.Drawing.Size(75, 23); this.btnContinuityUp.TabIndex = 0; btnContinuityUp.MouseDown += btnContinuityUp_Start; btnContinuityUp.MouseUp += btnZStop_Click; // // btnCenterDisplay // this.btnCenterDisplay.Location = new System.Drawing.Point(0, 0); this.btnCenterDisplay.Name = "btnCenterDisplay"; this.btnCenterDisplay.Size = new System.Drawing.Size(75, 23); this.btnCenterDisplay.TabIndex = 0; this.btnCenterDisplay.Click += new System.EventHandler(this.btnCenteDisplay_Click); // // btnStopall // this.btnStopall.Location = new System.Drawing.Point(0, 0); this.btnStopall.Name = "btnStopall"; this.btnStopall.Size = new System.Drawing.Size(75, 23); this.btnStopall.TabIndex = 0; // // btnDsRocker // this.btnDsRocker.Location = new System.Drawing.Point(0, 0); this.btnDsRocker.Name = "btnDsRocker"; this.btnDsRocker.Size = new System.Drawing.Size(75, 23); this.btnDsRocker.TabIndex = 0; // // btnSetting // this.btnSetting.Location = new System.Drawing.Point(0, 0); this.btnSetting.Name = "btnSetting"; this.btnSetting.Size = new System.Drawing.Size(75, 23); this.btnSetting.TabIndex = 0; this.btnSetting.Click += btnSetting_Click; // // txtXStep // this.txtXStep.Location = new System.Drawing.Point(0, 0); this.txtXStep.Name = "txtXStep"; this.txtXStep.Size = new System.Drawing.Size(100, 21); this.txtXStep.TabIndex = 0; this.txtXStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric); this.txtXStep.Leave += new System.EventHandler(this.txtStep_Leave); // // txtYStep // this.txtYStep.Location = new System.Drawing.Point(0, 0); this.txtYStep.Name = "txtYStep"; this.txtYStep.Size = new System.Drawing.Size(100, 21); this.txtYStep.TabIndex = 0; this.txtYStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric); this.txtYStep.Leave += new System.EventHandler(this.txtStep_Leave); // // txtZStep // this.txtZStep.Location = new System.Drawing.Point(0, 0); this.txtZStep.Name = "txtZStep"; this.txtZStep.Size = new System.Drawing.Size(100, 21); this.txtZStep.TabIndex = 0; this.txtZStep.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CheckInputIsNumeric); this.txtZStep.Leave += new System.EventHandler(this.txtStep_Leave); // // cmbZStepUnit // this.cmbZStepUnit.Location = new System.Drawing.Point(0, 0); this.cmbZStepUnit.Name = "cmbZStepUnit"; this.cmbZStepUnit.Size = new System.Drawing.Size(121, 20); this.cmbZStepUnit.TabIndex = 0; // // cmbXYStepUnit // this.cmbXYStepUnit.Location = new System.Drawing.Point(0, 0); this.cmbXYStepUnit.Name = "cmbXYStepUnit"; this.cmbXYStepUnit.Size = new System.Drawing.Size(121, 20); this.cmbXYStepUnit.TabIndex = 0; // // timerStageUpdate // this.timerStageUpdate.Interval = 15; this.timerStageUpdate.Tick += new System.EventHandler(this.timerStageUpdate_Tick); // // HardwareBaseDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.ClientSize = new System.Drawing.Size(292, 271); this.Location = new System.Drawing.Point(0, 0); this.Name = "HardwareBaseDialog"; this.ResumeLayout(false); } protected void InitializeLanguageText() { btnFreeloadstage.Text = "自由";// PdnResources.GetString("Menu.freeing.text"); btnFreeloadstage.Enabled = AxisController.GetInstance().FreeOrLockXYEnabled; btnStoploadstage.Text = PdnResources.GetString("Menu.stop.text"); btnResetloadstage.Text = PdnResources.GetString("Menu.reset.Text"); btnCenterloadstage.Text = PdnResources.GetString("Menu.center.text"); btnInloadstage.Text = PdnResources.GetString("Menu.enterfilm.text"); btnOutloadstage.Text = PdnResources.GetString("Menu.outslice.text"); btnZFree.Text = "自由";// PdnResources.GetString("Menu.locking.text"); btnZFree.Enabled = AxisController.GetInstance().FreeOrLockZEnabled; btnZStop.Text = PdnResources.GetString("Menu.stop.text"); btnCenterDisplay.Text = PdnResources.GetString("Menu.Centerdisplay.text"); btnStopall.Text = PdnResources.GetString("Menu.Stopall.text"); btnDsRocker.Text = PdnResources.GetString("Menu.Disabletherocker.text"); btnDsRocker.Enabled = AxisController.GetInstance().FreeOrLockZEnabled; btnSetting.Text = PdnResources.GetString("Menu.SerialportSettings.Text"); } 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; } public void InitStage() { m_Stage = AxisController.GetInstance(); if (!m_Stage.IsOpen) { MessageBox.Show(PdnResources.GetString("Message.AxisController.NotConnected")); } m_Stage.AddApp(this); } #region Dispose public void UninitStage() { m_Stage.RemoveApp(this); } public void UninitDocumentWorkspace() { if (m_documentWorkspace != null && !m_documentWorkspace.IsDisposed) { m_documentWorkspace.Dispose(); } } #endregion /// /// 视场模式操作台 /// public CircleControl initViewCircal(int size) { var circleControl = new CircleControl(size); circleControl.mouseDown += new EventHandler(visionClick); return circleControl; } /// /// 运动模式操作台 /// public CircleControl initSportCircal(int size) { var circleSportControl = new CircleControl(size); circleSportControl.mouseUp += new EventHandler(sportMouseUp); circleSportControl.mouseDown += new EventHandler(sportMouseDown); return circleSportControl; } /// /// 初始化设置信息 /// /// public void ReloadingStageParam(LoadingStageModel loadingStageModel) { m_loadingStageModel = loadingStageModel; m_stageWidth = double.Parse(m_loadingStageModel.TripX); m_stageHeight = double.Parse(m_loadingStageModel.TripY); ReinitSpeedUI(); } #region Speed /// /// gboxzspeed gboxxyspeed初始化 /// protected virtual void ReinitSpeedUI() { InitGroupSpeed(gboxXYspeed, XSpeed_CheckedChanged); InitGroupSpeed(gboxZspeed, ZSpeed_CheckedChanged); if (gboxXYspeed.Controls.Count == 0 || gboxZspeed.Controls.Count == 0) return; m_Stage.SetSpeedXY(m_speedLists[0].LSpeed); m_Stage.SetSpeedZ(m_speedLists[0].ZSpeed); } protected virtual void InitGroupSpeed(Control group, Action action) { m_speedLists = m_loadingStageModel.items; int pw = (group.Width - 20) / 3; group.Controls.Clear(); for (int i = 0; i < m_speedLists.Count; ++i) { if (m_speedLists[i].SChecked == 1) { var radioButton = new RadioButton(); radioButton.Text = (i + 1).ToString(); radioButton.AutoSize = true; radioButton.Checked = i == 0; radioButton.CheckedChanged += new System.EventHandler(action); radioButton.Tag = i; radioButton.Location = new System.Drawing.Point(18 + (i % 3) * pw, 21 + (i / 3) * 25); group.Controls.Add(radioButton); } } } // 速度选择按钮 protected virtual void XSpeed_CheckedChanged(object sender, EventArgs e) { RadioButton radio = (RadioButton)sender; if (radio.Checked) { int index = (int)radio.Tag; m_Stage.SetSpeedXY(m_speedLists[index].LSpeed); // 联动调速 if (IsSpeedHarmonize) { int count = gboxZspeed.Controls.Count; for (int i = 0; i < count; ++i) { ((RadioButton)gboxZspeed.Controls[i]).Checked = i == index; } } } } protected virtual void ZSpeed_CheckedChanged(object sender, EventArgs e) { RadioButton radio = (RadioButton)sender; if (!radio.Checked) return; int index = (int)radio.Tag; m_Stage.SetSpeedZ(m_speedLists[index].ZSpeed); // 联动调速 if (IsSpeedHarmonize) { int count = gboxXYspeed.Controls.Count; for (int i = 0; i < count; ++i) { ((RadioButton)gboxXYspeed.Controls[i]).Checked = i == index; } } } #endregion protected int ConvertUMToPX(double length) { // if (length < 0) length = -length; return (int)(length / m_PxLength); } protected double ConvertPXToUm(double length) { return length * m_PxLength; } #region Position Update public void OnUpdatePosition() { try { SetViewLocation(m_Stage.X, m_Stage.Y, m_Stage.Z); // 移动视图 ChangeDistanceAndCheckStop(m_Stage.X, m_Stage.Y); //if (m_Stage.StateX == 0 || m_Stage.StateY == 0) ; // 平台电机自由后需要重新复位 // m_isReset = false; } catch { } } /// /// 设置视场的位置 /// /// protected virtual void SetViewLocation(double x, double y, double z) { var b = this.IsDisposed; Invoke(new Action(() => UpdatePositionDisplay(x, y, z))); x = x < 0 ? 0 : x; y = y < 0 ? 0 : y; this.BeginInvoke(new Action(() => { m_documentWorkspace.SetViewPoint(ConvertUMToPX(x), ConvertUMToPX(y)); })); } protected virtual void UpdatePositionDisplay(double x, double y, double z) { lblXPosition.Text = x.ToString(); lblYPosition.Text = y.ToString(); lblZPosition.Text = z.ToString(); } protected virtual void ChangeDistanceAndCheckStop(double x, double y) { var distanceX = x - m_initStepX; var distanceY = y - m_initStepY; var distanceL = Math.Sqrt(distanceX * distanceX + distanceY * distanceY); Invoke(new Action(() => { lblXDistance.Text = "X:" + distanceX.ToString("0"); lblYDistance.Text = "Y:" + distanceY.ToString("0"); lblLDistance.Text = "L:" + distanceL.ToString("0"); })); } #endregion #region 视场圆环 // 视场圆环按钮被点击 protected virtual void visionClick(object sender, EventArgs e) { var circleSportControl = (CircleControl)sender; var p = circleSportControl._position; if (p < 0 || p > 15) return; var map = circleSportControl.positionMap[p % 8]; if (p < 8) m_Stage.Move(VisionWidth * map[0] / 2, VisionHeight * map[1] / 2); else m_Stage.Move(VisionWidth * map[0], VisionHeight * map[1]); } #endregion #region 运动圆环 protected virtual void sportMouseDown(object sender, EventArgs e) { var circleSportControl = (CircleControl)sender; if (circleSportControl._position < 8) { var p = circleSportControl._position; if (p < 0 || p > 7) return; var map = circleSportControl.positionMap[p]; m_Stage.Move(StepLenthX * map[0], StepLenthY * map[1]); } else { var direction = circleSportControl._position - 8; m_Stage.Split(direction); } } protected virtual void sportMouseUp(object sender, EventArgs e) { var circleSportControl = (CircleControl)sender; if (circleSportControl._position > 7) btnStopXY_Click(null, null); } #endregion //参数设置按钮点击 protected virtual void btnSetting_Click(object sender, EventArgs e) { //参数设置按钮点击弹窗 using (ParamSetingDialog af = new ParamSetingDialog(this)) { af.Setted += ReloadingStageParam; af.ShowDialog(); } } #region Z 运动 /// /// Z轴连续向上移动 /// /// /// protected virtual void btnContinuityUp_Start(object sender, EventArgs e) { m_Stage.GoTop(true); } /// /// Z轴连续向下移动 /// /// /// protected virtual void btnContinuityLower_Start(object sender, EventArgs e) { m_Stage.GoTop(false); } /// /// Z轴定长向上移动 /// /// /// protected virtual void btnFixedUp_Click(object sender, EventArgs e) { m_Stage.Up(StepLenthZ); } /// /// Z轴定长向下移动 /// /// /// protected virtual void btnFixedLower_Click(object sender, EventArgs e) { m_Stage.Up(-StepLenthZ); } #endregion protected virtual void CheckInputIsNumeric(object sender, KeyPressEventArgs e) { var text = ((TextBox)sender).Text; var key = (int)e.KeyChar; if ((key < 48 || key > 57) && key != 8 && key != 46) e.Handled = true; if (key == 46) //小数点 { if (text.Length <= 0) e.Handled = true; //小数点不能在第一位 else { float f, oldf; var b1 = float.TryParse(text, out oldf); var b2 = float.TryParse(text + key.ToString(), out f); if (!b2 && b1) e.Handled = true; } } } #region Axis Stop /// /// Z轴自由 /// /// /// protected virtual void btnZLock_Click(object sender, EventArgs e) { m_Stage.FreeZ(); } /// /// Z轴停止 /// /// /// protected virtual void btnZStop_Click(object sender, EventArgs e) { m_Stage.FreeZ(); } /// /// XY轴自由 /// /// /// protected virtual void btnLockXY_Click(object sender, EventArgs e) { m_Stage.FreeStage(); } /// /// XY轴停止 /// /// /// protected virtual void btnStopXY_Click(object sender, EventArgs e) { m_Stage.LockStage(); } /// /// 全部停止 /// /// /// protected virtual void btnStopall_Click(object sender, EventArgs e) { btnZStop_Click(null, null); btnStopXY_Click(null, null); } #endregion Axis Stop /// /// 禁用摇杆 /// /// /// protected virtual void btnDsRocker_Click(object sender, EventArgs e) { m_Stage.SetRockerEnabel(false); } protected virtual void GetMouseLeftClickPoint(object sender, EventArgs e) { if (!m_isReset) { MessageBox.Show(PdnResources.GetString("Menu.Pleaseresetfirst.Text")); return; } PointF pointF = (PointF)sender; List graphicsList = m_documentWorkspace.GraphicsList.GetDrawClassList(DrawClass.Stitch); int count = graphicsList.Count; var x = ConvertPXToUm((int)(pointF.X)); var y = ConvertPXToUm((int)(pointF.Y)); for (int i = 0; i < count; i++) { List> points = ((DrawStithchingBase)graphicsList[i]).GetViewPoints(); for (int j = 0; j < points.Count; j++) { Dictionary point = points[j]; RectangleF rectangleF = new RectangleF((PointF)point[0], new SizeF(m_viewWidth, m_viewHeight)); if (rectangleF.Contains(pointF)) { pointF = (PointF)point[0]; x = ConvertPXToUm((int)(pointF.X)); y = ConvertPXToUm((int)(pointF.Y)); m_Stage.To(x, y); return; } } } m_Stage.To(x, y); } #region Moveflow /// /// 复位 /// /// /// protected virtual void btnResetloadstage_Click(object sender, EventArgs e) { var dialog = TransferProgressDialog.CreatDialog("平台复位", "复位中...", OnProgressCancel, "Stop"); m_Stage.ResetStage( () => { m_isReset = true;// AxisController.GetInstance().NeedSaveResetStageStatus; this.Invoke(new Action(dialog.Close)); }); if (!m_isReset) dialog.ShowDialog(); } protected virtual void OnProgressCancel(object sender, EventArgs e) { btnStopall_Click(null, null); } /// /// 出片 /// /// /// protected virtual void btnOutloadstage_Click(object sender, EventArgs e) { m_Stage.Split(1); } /// /// 进片 /// /// /// protected virtual void btnInloadstage_Click(object sender, EventArgs e) { m_Stage.Split(3); } /// /// 中心 /// /// /// protected virtual void btnCenterloadstage_Click(object sender, EventArgs e) { m_Stage.ToCenter(); } /// /// 中心显示 /// protected virtual void btnCenteDisplay_Click(object sender, EventArgs e) { m_isShowCenter = !m_isShowCenter; m_documentWorkspace.SetCenter(m_isShowCenter); if (m_isShowCenter) { (sender as Control).Text = PdnResources.GetString("Menu.Cancelcenter.text"); } else { (sender as Control).Text = PdnResources.GetString("Menu.Centerdisplay.text"); } } protected virtual void DepthMerge() { } #endregion protected virtual void txtStep_Leave(object sender, EventArgs e) { var tbx = sender as TextBox; if (string.IsNullOrEmpty(tbx.Text)) { MessageBox.Show(PdnResources.GetString("Menu.Step-sizbeempty.text")); tbx.Text = "0"; } } public void OnTimeoutConnect() { MessageBox.Show("平台连接超时"); } public void OnErrorSend() { MessageBox.Show("平台连接异常"); } #region Camera protected ICamera m_camera => CameraManager.CurrentCamera; protected CameraParamModel m_cameraParamModel; protected CameraConfigs m_cameraConfig; /// /// 当前拍摄图片 /// protected Bitmap m_BitmapCurrent; public void UninitCamera() { CameraManager.FrameCallback += CallbackDraw; if (m_BitmapCurrent != null) { m_BitmapCurrent.Dispose(); } } /// /// 窗体打开后初始化相机 /// protected void InitializeCamera() { if (m_camera != null) { m_cameraConfig = CameraConfigs.GetInstance(); m_cameraParamModel = m_cameraConfig.GetCurrentCameraParamModel(); //m_cameraConfig.CameraParamInit(); // 开启预览 } CameraManager.FrameCallback += CallbackDraw; Thread.Sleep(100);//相机开启缓冲时间 initCameraSetting(); } protected virtual void initCameraSetting() { } object _lockobj = new object(); public void CallbackDraw(Bitmap bitmap) { lock (_lockobj) { try { if (bitmap == null) return; VisionWidthPixel = bitmap.Width; VisionHeightPixel = bitmap.Height; m_documentWorkspace.CompositionSurface = null; if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) { var mat = PaintDotNet.Camera.Tools.ToMat(bitmap); m_documentWorkspace.Document = Document.FromMat(mat); } else { m_documentWorkspace.Document = Document.FromByteArr(bitmap.ToByteArray(), bitmap.Width, bitmap.Height); } m_BitmapCurrent = bitmap; } catch (Exception) { } } } #endregion private void timerStageUpdate_Tick(object sender, EventArgs e) { var x = Math.Round(m_Stage.X, 2); var y = Math.Round(m_Stage.Y, 2); var z = Math.Round(m_Stage.Z, 2); lblXPosition.Text = x.ToString(); lblYPosition.Text = y.ToString(); lblZPosition.Text = z.ToString(); m_documentWorkspace.SetViewPoint(ConvertUMToPX(x), ConvertUMToPX(y)); var distanceX = x - m_initStepX; var distanceY = y - m_initStepY; var distanceL = Math.Sqrt(distanceX * distanceX + distanceY * distanceY); lblXDistance.Text = "X:" + distanceX.ToString("0"); lblYDistance.Text = "Y:" + distanceY.ToString("0"); lblLDistance.Text = "L:" + distanceL.ToString("0"); } } }