PlcPointParams.xaml.cs 6.2 KB

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