VisionDynamicAccuracyAnalyzer.xaml.cs 3.8 KB

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