VisionDynamicAccuracyAnalyzer.xaml.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.Dimensioning;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Windows.Controls;
  7. using TeamAAS_VP.ViewModels.Product;
  8. namespace TeamAAS_VP.Views.Product
  9. {
  10. /// <summary>
  11. /// Interaction logic for VisionDynamicAccuracyAnalyzer
  12. /// </summary>
  13. public partial class VisionDynamicAccuracyAnalyzer : UserControl
  14. {
  15. VisionDynamicAccuracyAnalyzerViewModel VM;
  16. public VisionDynamicAccuracyAnalyzer()
  17. {
  18. InitializeComponent();
  19. VM = DataContext as VisionDynamicAccuracyAnalyzerViewModel;
  20. VM.PropertyChanged += VM_PropertyChanged;
  21. this.display.VerticalScrollBar = false;
  22. this.display.HorizontalScrollBar = false;
  23. this.display.AutoFit = true;
  24. this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
  25. }
  26. private int _imageWidth;
  27. private int _imageHeight;
  28. private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  29. {
  30. try
  31. {
  32. if (e.PropertyName == "Image")
  33. {
  34. this.display.Image = VM.Image;
  35. //图像中心点坐标
  36. _imageWidth = this.display.Image.Width;
  37. _imageHeight = this.display.Image.Height;
  38. }
  39. else if (e.PropertyName == "Graphic")
  40. {
  41. this.display.StaticGraphics.Clear();
  42. CogGraphicCollection graphic = new CogGraphicCollection();
  43. CogCreateLineTool line1 = new CogCreateLineTool();
  44. CogCreateLineTool line2 = new CogCreateLineTool();
  45. line1.InputImage = this.display.Image;
  46. line2.InputImage = this.display.Image;
  47. line1.Line.X = _imageWidth / 2;
  48. line1.Line.Y = _imageHeight / 2;
  49. line1.Line.Rotation = 0;
  50. line2.Line.X = _imageWidth / 2;
  51. line2.Line.Y = _imageHeight / 2;
  52. line2.Line.Rotation = Math.PI / 180 * 90;
  53. line1.Run();
  54. line2.Run();
  55. graphic.Add(line1.GetOutputLine());
  56. graphic.Add(line2.GetOutputLine());
  57. this.display.StaticGraphics.Clear();
  58. this.display.StaticGraphics.AddList(graphic, "");
  59. if (VM.Graphic != null)
  60. {
  61. this.display.StaticGraphics.AddList(VM.Graphic, "");
  62. }
  63. }
  64. }
  65. catch (System.Exception)
  66. {
  67. }
  68. }
  69. private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
  70. {
  71. // If the source is a DataTable, we can read the DataColumn's Caption
  72. if (e.PropertyDescriptor is PropertyDescriptor pd)
  73. {
  74. var columnName = pd.Name;
  75. var dv = dgResults.ItemsSource as DataView;
  76. if (dv != null && dv.Table != null && dv.Table.Columns.Contains(columnName))
  77. {
  78. var dataCol = dv.Table.Columns[columnName];
  79. if (!string.IsNullOrEmpty(dataCol.Caption))
  80. {
  81. e.Column.Header = dataCol.Caption;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }