PlcPointParams.xaml.cs 5.9 KB

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