| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using Cognex.VisionPro;
- using Cognex.VisionPro.Dimensioning;
- using OxyPlot;
- using System;
- using System.Windows.Controls;
- using TeamAAS_VP.ViewModels.Product;
- namespace TeamAAS_VP.Views.Product
- {
- /// <summary>
- /// Interaction logic for PlcPointParams
- /// </summary>
- public partial class PlcPointParams : UserControl
- {
- PlcPointParamsViewModel VM;
- public PlcPointParams()
- {
- InitializeComponent();
- VM = DataContext as PlcPointParamsViewModel;
- VM.PropertyChanged += VM_PropertyChanged;
- VM.PropertyChanged += Vm_SelectedIndexChanged;
- // initialize visibility
- try
- {
- if (VM != null && colProcedure != null)
- colProcedure.Visibility = VM.SelectedIndex == 3 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
- }
- catch { }
- this.display.HorizontalScrollBar = false;
- this.display.VerticalScrollBar = false;
- this.display.AutoFit = true;
- this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
- }
- private void Vm_SelectedIndexChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if (e.PropertyName == nameof(PlcPointParamsViewModel.SelectedIndex))
- {
- try
- {
- var visible = VM.SelectedIndex == 3 || VM.SelectedIndex == 5 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
- this.Dispatcher.BeginInvoke(new Action(() =>
- {
- if (colProcedure != null)
- colProcedure.Visibility = visible;
- }));
- }
- catch { }
- }
- }
- private void ProcedureCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- try
- {
- var combo = sender as System.Windows.Controls.ComboBox;
- if (combo == null) return;
- var proc = combo.SelectedItem as TeamAAS_VP.Models.ProcedureModel;
- var point = combo.DataContext as TeamAAS_VP.Models.PLC.PlcPoint;
- if (point != null && proc != null)
- {
- // SelectedValue binding already sets VisionProcessID; sync name
- point.VisionProcessName = proc.Name;
- }
- }
- catch { }
- }
- private int _imageWidth;
- private int _imageHeight;
- private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
- {
- if (e.PropertyName == "Image")
- {
- if (this.display.Image == null)
- {
- this.display.Image = VM.Image;
- //图像中心点坐标
- _imageWidth = this.display.Image.Width;
- _imageHeight = this.display.Image.Height;
- CogGraphicCollection graphic = new CogGraphicCollection();
- CogCreateLineTool line1 = new CogCreateLineTool();
- CogCreateLineTool line2 = new CogCreateLineTool();
- line1.InputImage = this.display.Image;
- line2.InputImage = this.display.Image;
- line1.Line.X = _imageWidth / 2;
- line1.Line.Y = _imageHeight / 2;
- line1.Line.Rotation = 0;
- line2.Line.X = _imageWidth / 2;
- line2.Line.Y = _imageHeight / 2;
- line2.Line.Rotation = Math.PI / 180 * 90;
- line1.Run();
- line2.Run();
- graphic.Add(line1.GetOutputLine());
- graphic.Add(line2.GetOutputLine());
- this.display.StaticGraphics.Clear();
- this.display.StaticGraphics.AddList(graphic, "");
- }
- else
- {
- this.display.Image = VM.Image;
- //如果图像的长宽发生变化,则重新绘制十字线
- if (_imageWidth != this.display.Image.Width || _imageHeight != this.display.Image.Height)
- {
- _imageWidth = this.display.Image.Width;
- _imageHeight = this.display.Image.Height;
- CogGraphicCollection graphic = new CogGraphicCollection();
- CogCreateLineTool line1 = new CogCreateLineTool();
- CogCreateLineTool line2 = new CogCreateLineTool();
- line1.InputImage = this.display.Image;
- line2.InputImage = this.display.Image;
- line1.Line.X = _imageWidth / 2;
- line1.Line.Y = _imageHeight / 2;
- line1.Line.Rotation = 0;
- line2.Line.X = _imageWidth / 2;
- line2.Line.Y = _imageHeight / 2;
- line2.Line.Rotation = Math.PI / 180 * 90;
- line1.Run();
- line2.Run();
- graphic.Add(line1.GetOutputLine());
- graphic.Add(line2.GetOutputLine());
- this.display.StaticGraphics.Clear();
- this.display.StaticGraphics.AddList(graphic, "");
- }
- }
- }
- }
- }
- }
|