PlcPointParams.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.Dimensioning;
  3. using OxyPlot;
  4. using System;
  5. using System.Windows.Controls;
  6. using TeamAAS_VP.ViewModels.Product;
  7. namespace TeamAAS_VP.Views.Product
  8. {
  9. /// <summary>
  10. /// Interaction logic for PlcPointParams
  11. /// </summary>
  12. public partial class PlcPointParams : UserControl
  13. {
  14. PlcPointParamsViewModel VM;
  15. public PlcPointParams()
  16. {
  17. InitializeComponent();
  18. VM = DataContext as PlcPointParamsViewModel;
  19. VM.PropertyChanged += VM_PropertyChanged;
  20. VM.PropertyChanged += Vm_SelectedIndexChanged;
  21. // initialize visibility
  22. try
  23. {
  24. if (VM != null && colProcedure != null)
  25. colProcedure.Visibility = VM.SelectedIndex == 3 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
  26. }
  27. catch { }
  28. this.display.HorizontalScrollBar = false;
  29. this.display.VerticalScrollBar = false;
  30. this.display.AutoFit = true;
  31. this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
  32. }
  33. private void Vm_SelectedIndexChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  34. {
  35. if (e.PropertyName == nameof(PlcPointParamsViewModel.SelectedIndex))
  36. {
  37. try
  38. {
  39. var visible = VM.SelectedIndex == 3 || VM.SelectedIndex == 5 ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
  40. this.Dispatcher.BeginInvoke(new Action(() =>
  41. {
  42. if (colProcedure != null)
  43. colProcedure.Visibility = visible;
  44. }));
  45. }
  46. catch { }
  47. }
  48. }
  49. private void ProcedureCombo_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  50. {
  51. try
  52. {
  53. var combo = sender as System.Windows.Controls.ComboBox;
  54. if (combo == null) return;
  55. var proc = combo.SelectedItem as TeamAAS_VP.Models.ProcedureModel;
  56. var point = combo.DataContext as TeamAAS_VP.Models.PLC.PlcPoint;
  57. if (point != null && proc != null)
  58. {
  59. // SelectedValue binding already sets VisionProcessID; sync name
  60. point.VisionProcessName = proc.Name;
  61. }
  62. }
  63. catch { }
  64. }
  65. private int _imageWidth;
  66. private int _imageHeight;
  67. private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  68. {
  69. if (e.PropertyName == "Image")
  70. {
  71. if (this.display.Image==null)
  72. {
  73. this.display.Image = VM.Image;
  74. //图像中心点坐标
  75. _imageWidth= this.display.Image.Width;
  76. _imageHeight= this.display.Image.Height;
  77. CogGraphicCollection graphic = new CogGraphicCollection();
  78. CogCreateLineTool line1 = new CogCreateLineTool();
  79. CogCreateLineTool line2 = new CogCreateLineTool();
  80. line1.InputImage = this.display.Image;
  81. line2.InputImage = this.display.Image;
  82. line1.Line.X = _imageWidth / 2;
  83. line1.Line.Y = _imageHeight / 2;
  84. line1.Line.Rotation = 0;
  85. line2.Line.X = _imageWidth / 2;
  86. line2.Line.Y = _imageHeight / 2;
  87. line2.Line.Rotation = Math.PI / 180 * 90;
  88. line1.Run();
  89. line2.Run();
  90. graphic.Add(line1.GetOutputLine());
  91. graphic.Add(line2.GetOutputLine());
  92. this.display.StaticGraphics.Clear();
  93. this.display.StaticGraphics.AddList(graphic, "");
  94. }
  95. else
  96. {
  97. this.display.Image = VM.Image;
  98. //如果图像的长宽发生变化,则重新绘制十字线
  99. if (_imageWidth!= this.display.Image.Width|| _imageHeight!= this.display.Image.Height)
  100. {
  101. _imageWidth = this.display.Image.Width;
  102. _imageHeight = this.display.Image.Height;
  103. CogGraphicCollection graphic = new CogGraphicCollection();
  104. CogCreateLineTool line1 = new CogCreateLineTool();
  105. CogCreateLineTool line2 = new CogCreateLineTool();
  106. line1.InputImage = this.display.Image;
  107. line2.InputImage = this.display.Image;
  108. line1.Line.X = _imageWidth / 2;
  109. line1.Line.Y = _imageHeight / 2;
  110. line1.Line.Rotation = 0;
  111. line2.Line.X = _imageWidth / 2;
  112. line2.Line.Y = _imageHeight / 2;
  113. line2.Line.Rotation = Math.PI / 180 * 90;
  114. line1.Run();
  115. line2.Run();
  116. graphic.Add(line1.GetOutputLine());
  117. graphic.Add(line2.GetOutputLine());
  118. this.display.StaticGraphics.Clear();
  119. this.display.StaticGraphics.AddList(graphic, "");
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }