using CanFly.Canvas.Shape; using CanFly.Helper; using CanFly.UI; using CanFly.UI.GuidePanel; using CanFly.Util; using HalconDotNet; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Windows.Forms; namespace XKRS.CanFly { public partial class FrmMain3 : Form { private string _currentImageFile = ""; private System.Windows.Forms.Timer _statusTimer = new System.Windows.Forms.Timer(); private BaseGuideControl? _currentGuideCtrl; private GuideCircleCtrl guideCircleCtrl = new GuideCircleCtrl(); private GuideLineCircleCtrl guideLineCircleCtrl = new GuideLineCircleCtrl(); private GuideLineLineCtrl guideLineLineCtrl = new GuideLineLineCtrl(); private GuideLineCtrl guideLineCtrl = new GuideLineCtrl(); private GuideHeightCtrl guideHeightCtrl = new GuideHeightCtrl(); string Type=string.Empty; public string inputtext=string.Empty; public string outtext = string.Empty; public FrmMain3(string type) { InitializeComponent(); Type=type; guideCircleCtrl.Dock = DockStyle.Fill; guideCircleCtrl.OnControlCloseEvent -= () => panelContent.Controls.Clear(); guideCircleCtrl.OnControlCloseEvent += () => panelContent.Controls.Clear(); guideLineCircleCtrl.Dock = DockStyle.Fill; guideLineCircleCtrl.OnControlCloseEvent -= () => panelContent.Controls.Clear(); guideLineCircleCtrl.OnControlCloseEvent += () => panelContent.Controls.Clear(); guideLineLineCtrl.Dock = DockStyle.Fill; guideLineLineCtrl.OnControlCloseEvent -= () => panelContent.Controls.Clear(); guideLineLineCtrl.OnControlCloseEvent += () => panelContent.Controls.Clear(); guideLineCtrl.Dock = DockStyle.Fill; guideLineCtrl.OnControlCloseEvent -= () => panelContent.Controls.Clear(); guideLineCtrl.OnControlCloseEvent += () => panelContent.Controls.Clear(); guideHeightCtrl.Dock = DockStyle.Fill; guideHeightCtrl.OnControlCloseEvent -= () => panelContent.Controls.Clear(); guideHeightCtrl.OnControlCloseEvent += () => panelContent.Controls.Clear(); } private void FrmMain_Load(object sender, EventArgs e) { switch (Type) { case "1": SwitchMeasureMode(guideCircleCtrl); break; case "2": SwitchMeasureMode(guideLineCtrl); break; case "3": SwitchMeasureMode(guideLineLineCtrl); break; case "4": SwitchMeasureMode(guideLineCircleCtrl); break; case "5": SwitchMeasureMode(guideHeightCtrl); break; default: break; } } private void btnLoadImage_Click(object sender, EventArgs e) { //OpenFileDialog ofd = new OpenFileDialog(); //ofd.Filter = "图像文件|*.jpg;*.png"; //ofd.Multiselect = false; //if (ofd.ShowDialog() == DialogResult.OK) //{ // _currentImageFile = ofd.FileName; // Bitmap bitmap = (Bitmap)Image.FromFile(_currentImageFile); // this.canvas.LoadPixmap(bitmap); // this.btnCreateCircle.Enabled = true; //} } private void btnMeasureCircle_Click(object sender, EventArgs e) { //var contentCtrls = panelContent.Controls; //if (contentCtrls.Count > 0) //{ // if (contentCtrls[0] == guideCircleCtrl) // { // return; // } //} //panelContent.Controls.Clear(); //panelContent.Controls.Add(guideCircleCtrl); SwitchMeasureMode(guideCircleCtrl); } private void btnMeasureLineCircle_Click(object sender, EventArgs e) { SwitchMeasureMode(guideLineCircleCtrl); } private void SwitchMeasureMode(BaseGuideControl control) { var contentCtrls = panelContent.Controls; if (contentCtrls.Count > 0) { if (contentCtrls[0] == control) { return; } } panelContent.Controls.Clear(); control.OnDataPassed -= Control_OnDataPassed; control.OnDataPassed += Control_OnDataPassed; //control.Dock = DockStyle.Fill; //control.OnControlCloseEvent -= () => panelContent.Controls.Clear(); //control.OnControlCloseEvent += () => panelContent.Controls.Clear(); panelContent.Controls.Add(control); } private void Control_OnDataPassed(string obj,string obj1) { inputtext = obj; outtext = obj1; this.Close(); } private void btnCreateRect_Click(object sender, EventArgs e) { //this.canvas.StartDraw(ShapeTypeEnum.Rectangle); //this.btnCreateCircle.Enabled = false; //this.btnStopDraw.Enabled = true; //this.canvas.Enabled = true; } private void btnStopDraw_Click(object sender, EventArgs e) { //panelGuide.Controls.Clear(); StopDrawMode(); } private void StartDrawMode() { } private void StopDrawMode() { //this.canvas.StopDraw(); //this.btnStopDraw.Enabled = false; //this.btnCreateCircle.Enabled = true; } private void Status(string message, int delay = 5000) { //_statusTimer.Stop(); //// 显示消息 //lblStatus.Text = message; //// 创建一个计时器 //_statusTimer.Interval = delay; // 设置延迟时间 //_statusTimer.Tick += (sender, e) => //{ // _statusTimer.Stop(); // 停止计时器 // lblStatus.Text = string.Empty; // 清空状态消息 //}; //_statusTimer.Start(); // 启动计时器 } private void Canvas_mouseMoved(PointF pos) { //if (InvokeRequired) //{ // Invoke(Canvas_mouseMoved, pos); // return; //} //lblStatus.Text = $"X:{pos.X}, Y:{pos.Y}"; } private void Canvas_selectionChanged(List shapes) { if (shapes.Count != 1) { // panelGuide.Controls.Clear(); return; } //SwitchGuideForm(shapes[0].ShapeType); Canvas_OnShapeUpdateEvent(shapes[0]); } private void SwitchGuideForm(ShapeTypeEnum shapeType) { //if (_currentGuideCtrl == null) //{ // switch (shapeType) // { // case ShapeTypeEnum.Point: // break; // case ShapeTypeEnum.Line: // break; // case ShapeTypeEnum.Rectangle: // break; // case ShapeTypeEnum.Circle: // _currentGuideCtrl = new GuideCircleCtrl(); // _currentGuideCtrl.ImageFile = _currentImageFile; // _currentGuideCtrl.OnDrawCircle += this.canvas.DrawCircle; // _currentGuideCtrl.OnClose += () => // { // panelGuide.Controls.Clear(); // StopDrawMode(); // }; // break; // case ShapeTypeEnum.Polygon: // break; // case ShapeTypeEnum.LineStrip: // break; // default: // break; // } //} //_currentGuideCtrl?.AddToPanel(panelGuide); } private void Canvas_OnShapeMoving(List shapes) { //if (shapes.Count != 1) //{ // panelGuide.Controls.Clear(); // return; //} //_currentGuideCtrl?.UpdateShape(shapes[0]); } private void Canvas_OnShapeUpdateEvent(FlyShape shape) { switch (shape.ShapeType) { case ShapeTypeEnum.Point: break; case ShapeTypeEnum.Line: break; case ShapeTypeEnum.Rectangle: break; case ShapeTypeEnum.Circle: { //_currentGuideCtrl?.UpdateShape(shape); } break; case ShapeTypeEnum.Polygon: break; case ShapeTypeEnum.LineStrip: break; default: break; } } private void btnTestOutsideDraw_Click(object sender, EventArgs e) { //Random random = new Random((int)DateTime.Now.Ticks); //for (int i = 0; i < 10; i++) //{ // // this.canvas.DrawCircle(new PointF(500, 500), 100); // int x = random.Next() % 500; // int y = random.Next() % 500; // int r = random.Next() % 200; // Debug.WriteLine($"X:{x}\tY:{y}\tR:{r}"); // this.canvas.DrawCircle(new PointF(x, y), r); //} } private void btnTestClearDraw_Click(object sender, EventArgs e) { //this.canvas.ClearDraw(); } private async void btnTestCircleMeasure_Click(object sender, EventArgs e) { //string dir = Path.Combine(Environment.CurrentDirectory, "hscripts"); //string file = "CircleMeasure.hdvp"; //string filePath = Path.Combine(dir, file); //if (!File.Exists(filePath)) //{ // MessageBox.Show($"文件 {filePath} 不存在"); // return; //} //HObject? hImage = null; //try //{ // HDevEngineTool tool = new HDevEngineTool(dir); // tool.LoadProcedure(Path.GetFileNameWithoutExtension(file)); // // string imageFile = Path.Combine(Environment.CurrentDirectory, "hscripts", "image.png"); // HOperatorSet.ReadImage(out hImage, _currentImageFile); // tool.InputImageDic["INPUT_Image"] = hImage; // tool.InputTupleDic["XCenter"] = 981.625; // tool.InputTupleDic["YCenter"] = 931.823; // tool.InputTupleDic["Radius"] = 900.141; // Stopwatch sw = new Stopwatch(); // sw.Start(); // if (!tool.RunProcedure(out string error, out _)) // { // throw new Exception(); // } // sw.Stop(); // var flag = tool.GetResultTuple("OUTPUT_Flag").HTupleToDouble(); // List x = tool.GetResultTuple("RXCenter").HTupleToDouble(); // var y = tool.GetResultTuple("RYCenter").HTupleToDouble(); // var r = tool.GetResultTuple("RRadius").HTupleToDouble(); // if (flag.Count > 0 && x.Count > 0 && y.Count > 0 && r.Count > 0) // { // this.canvas.DrawCircle(new PointF((float)x[0], (float)y[0]), (float)r[0]); // } // // // Debug.WriteLine(""); //} //catch (Exception) //{ // throw; //} //finally //{ // hImage?.Dispose(); //} } private void btnTest_Click(object sender, EventArgs e) { //this.canvas.DrawRectangle(new PointF(300, 300), // new PointF(800, 500), 33f); } private void btnRotateTest_Click(object sender, EventArgs e) { //if (this.canvas.Shapes.Count == 0) //{ // return; //} //this.canvas.Shapes[0]._currentRotateAngle += 10; //this.canvas.Invalidate(); } private void btnMeasureLineline_Click(object sender, EventArgs e) { SwitchMeasureMode(guideLineLineCtrl); } private void btnMeasureLine_Click(object sender, EventArgs e) { SwitchMeasureMode(guideLineCtrl); } } }