using LB_VisionControl;
using LB_VisionProcesses.Cameras;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using static ZXing.QrCode.Internal.Mode;
namespace LB_SmartVision.Forms.Pages.MotionControlPage
{
public partial class MotionControlForm : Form
{
BaseCamera camera { get; set; }
UserPictureBox onlinePictureBox { get; set; }
public MotionControlForm()
{
InitializeComponent();
}
public MotionControlForm(BaseCamera camera)
{
InitializeComponent();
if (camera == null)
return;
this.camera = camera;
this.Text = camera.SN;
textBox1.Text = camera.SN;
onlinePictureBox = new UserPictureBox(uiGroupBoxImage);
this.uiGroupBoxImage.Controls.Clear();
this.uiGroupBoxImage.Controls.Add(onlinePictureBox);
onlinePictureBox.Dock = DockStyle.Fill;
//加载回调函数
Subscribe();
}
private void CameraForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (camera == null)
return;
camera.StopGrabbing();
camera.StartGrabbing();
Unsubscribe();
}
public void Unsubscribe()
{
try
{
if (camera == null)
return;
//取消回调函数
camera.ImageGrabbed -= GetImageBllComplete;
}
catch (Exception ex)
{
// 记录错误信息
Debug.WriteLine($"错误: {ex.Message}");
}
}
public void Subscribe()
{
try
{
if (camera == null)
return;
//取消回调函数
camera.ImageGrabbed -= GetImageBllComplete;
//加载回调函数
camera.ImageGrabbed += GetImageBllComplete;
}
catch (Exception ex)
{
// 记录错误信息
Debug.WriteLine($"错误: {ex.Message}");
}
}
///
/// 相机回调运行
///
///
///
private void GetImageBllComplete(object sender, CameraEventArgs e)
{
if (e.Bitmap == null)
return;
lock (e.Bitmap)
{
if (this.InvokeRequired) // 检查是否需要在UI线程上调用
{
this.Invoke(new Action(() =>
{
onlinePictureBox.Image = e.Bitmap;
})); // 递归调用自身,但这次在UI线程上
}
else
{
onlinePictureBox.Image = e.Bitmap;
}
}
}
private void uiButtonGrabOnce_Click(object sender, EventArgs e)
{
if (camera == null)
return;
Task.Factory.StartNew(() =>
{
camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
DateTime startTime = DateTime.Now; // 当前时间
camera.GetImageWithSoftTrigger(out Bitmap bitmap);
});
}
private void uiButtonCloseSoftGrab_Click(object sender, EventArgs e)
{
if (camera == null)
return;
Task.Factory.StartNew(() =>
{
camera.StopGrabbing();
camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
camera.StartGrabbing();
});
}
private void uiButtonHardGrabWithPLC_Click(object sender, EventArgs e)
{
if (camera == null)
return;
Task.Factory.StartNew(() =>
{
camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
camera.StopGrabbing();
camera.StartWith_HardTriggerModel();
});
}
private void uiButtonDecreaseX_Click(object sender, EventArgs e)
{
float xPosition = 0;
xPosition = float.Parse(textBoxXPosition.Text);
MovePosition(-xPosition, 0, 0, 0);
PositionNow();
}
private void uiButtonIncreaseX_Click(object sender, EventArgs e)
{
float xPosition = 0;
xPosition = float.Parse(textBoxXPosition.Text);
MovePosition(xPosition, 0, 0, 0);
PositionNow();
}
private void uiButtonDecreaseY_Click(object sender, EventArgs e)
{
float yPosition = 0;
yPosition = float.Parse(textBoxYPosition.Text);
MovePosition(0, -yPosition, 0, 0);
PositionNow();
}
private void uiButtonIncreaseY_Click(object sender, EventArgs e)
{
float yPosition = 0;
yPosition = float.Parse(textBoxYPosition.Text);
MovePosition(0, yPosition, 0, 0);
PositionNow();
}
private void uiButtonDecreaseZ_Click(object sender, EventArgs e)
{
float zPosition = 0;
zPosition = float.Parse(textBoxZPosition.Text);
MovePosition(0, 0, -zPosition, 0);
PositionNow();
}
private void uiButtonIncreaseZ_Click(object sender, EventArgs e)
{
float zPosition = 0;
zPosition = float.Parse(textBoxZPosition.Text);
MovePosition(0, 0, zPosition, 0);
PositionNow();
}
private void uiButtonDecreasePitchAngle_Click(object sender, EventArgs e)
{
float pitchanglePosition = 0;
pitchanglePosition = float.Parse(textBoxPitchAnglePosition.Text);
MovePosition(0, 0, 0, -pitchanglePosition);
AngNow();
}
private void uiButtonIncreasePitchAngle_Click(object sender, EventArgs e)
{
float pitchanglePosition = 0;
pitchanglePosition = float.Parse(textBoxPitchAnglePosition.Text);
MovePosition(0, 0, 0, pitchanglePosition);
AngNow();
}
public float XPositionDt;
public float YPositionDt;
public float ZPositionDt;
public float AnglePositionDt;
public void MovePosition(float fDeltaX, float fDeltaY, float fDeltaZ, float fDeltAngle)
{
XPositionDt += fDeltaX;
YPositionDt += fDeltaY;
ZPositionDt += fDeltaZ;
AnglePositionDt += fDeltAngle;
}
private void textBoxPointSpeed_TextChanged(object sender, EventArgs e)
{
float pointspeed = 0;
pointspeed = float.Parse(textBoxPointSpeed.Text);
}
private void textBoxAngSpeed_TextChanged(object sender, EventArgs e)
{
float angspeed = 0;
angspeed = float.Parse(textBoxAngSpeed.Text);
}
private void textBoxPositionNow_TextChanged(object sender, EventArgs e)
{
textBoxPositionNow.Enabled = false;
string x;
string y;
string z;
x = Convert.ToString(Math.Round(XPositionDt, 3));
y = Convert.ToString(Math.Round(YPositionDt, 3));
z = Convert.ToString(Math.Round(ZPositionDt, 3));
textBoxPositionNow.Text = "(" + x + "," + y + "," + z + ")";
}
private void textBoxAngleNow_TextChanged(object sender, EventArgs e)
{
textBoxAngleNow.Enabled = false;
string ang;
ang = Convert.ToString(Math.Round(AnglePositionDt, 3));
textBoxAngleNow.Text = ang;
}
private void PositionNow()
{
string x;
string y;
string z;
x = Convert.ToString(Math.Round(XPositionDt, 3));
y = Convert.ToString(Math.Round(YPositionDt, 3));
z = Convert.ToString(Math.Round(ZPositionDt, 3));
textBoxPositionNow.Text = "(" + x + y + z + ")";
}
private void AngNow()
{
string ang;
ang = Convert.ToString(Math.Round(AnglePositionDt, 3));
textBoxAngleNow.Text = "(" + ang + ")";
}
private void uiGroupBoxMotionControl_Enter(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tableLayoutPanel3_Paint(object sender, PaintEventArgs e)
{
}
private void uiSplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
// 使用KeyDown事件更适合处理回车键
private void textBoxXPosition_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//// 验证输入
//if (ValidateXPosition())
//{
// // 执行确认操作
// ProcessXPosition();
//}
// 阻止系统声音并让文本框失去焦点
e.SuppressKeyPress = true;
e.Handled = true;
this.ActiveControl = null;
}
}
// 使用KeyPress验证实时输入
private void textBoxXPosition_KeyPress(object sender, KeyPressEventArgs e)
{
// 允许回车键(虽然KeyDown会处理它)
if (e.KeyChar == (char)Keys.Enter)
return;
// 允许退格键
if (e.KeyChar == (char)Keys.Back)
return;
// 只允许数字
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true; // 阻止输入
}
}
// 验证方法
private bool ValidateXPosition()
{
if (string.IsNullOrWhiteSpace(textBoxXPosition.Text))
return false;
return Regex.IsMatch(textBoxXPosition.Text, @"^\d+$");
}
// 处理确认的方法
private void ProcessXPosition()
{
if (int.TryParse(textBoxXPosition.Text, out int xPosition))
{
// 执行逻辑
//LogInfo?.Invoke($"X位置已更新为: {xPosition}", LogInfoType.INFO);
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
}
}