PlcPointParams.xaml.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. this.display.HorizontalScrollBar = false;
  21. this.display.VerticalScrollBar = false;
  22. this.display.AutoFit = true;
  23. this.display.BackColor = System.Drawing.SystemColors.ActiveCaption;
  24. }
  25. private int _imageWidth;
  26. private int _imageHeight;
  27. private void VM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  28. {
  29. if (e.PropertyName == "Image")
  30. {
  31. if (this.display.Image==null)
  32. {
  33. this.display.Image = VM.Image;
  34. //图像中心点坐标
  35. _imageWidth= this.display.Image.Width;
  36. _imageHeight= this.display.Image.Height;
  37. CogGraphicCollection graphic = new CogGraphicCollection();
  38. CogCreateLineTool line1 = new CogCreateLineTool();
  39. CogCreateLineTool line2 = new CogCreateLineTool();
  40. line1.InputImage = this.display.Image;
  41. line2.InputImage = this.display.Image;
  42. line1.Line.X = _imageWidth / 2;
  43. line1.Line.Y = _imageHeight / 2;
  44. line1.Line.Rotation = 0;
  45. line2.Line.X = _imageWidth / 2;
  46. line2.Line.Y = _imageHeight / 2;
  47. line2.Line.Rotation = Math.PI / 180 * 90;
  48. line1.Run();
  49. line2.Run();
  50. graphic.Add(line1.GetOutputLine());
  51. graphic.Add(line2.GetOutputLine());
  52. this.display.StaticGraphics.Clear();
  53. this.display.StaticGraphics.AddList(graphic, "");
  54. }
  55. else
  56. {
  57. this.display.Image = VM.Image;
  58. //如果图像的长宽发生变化,则重新绘制十字线
  59. if (_imageWidth!= this.display.Image.Width|| _imageHeight!= this.display.Image.Height)
  60. {
  61. _imageWidth = this.display.Image.Width;
  62. _imageHeight = this.display.Image.Height;
  63. CogGraphicCollection graphic = new CogGraphicCollection();
  64. CogCreateLineTool line1 = new CogCreateLineTool();
  65. CogCreateLineTool line2 = new CogCreateLineTool();
  66. line1.InputImage = this.display.Image;
  67. line2.InputImage = this.display.Image;
  68. line1.Line.X = _imageWidth / 2;
  69. line1.Line.Y = _imageHeight / 2;
  70. line1.Line.Rotation = 0;
  71. line2.Line.X = _imageWidth / 2;
  72. line2.Line.Y = _imageHeight / 2;
  73. line2.Line.Rotation = Math.PI / 180 * 90;
  74. line1.Run();
  75. line2.Run();
  76. graphic.Add(line1.GetOutputLine());
  77. graphic.Add(line2.GetOutputLine());
  78. this.display.StaticGraphics.Clear();
  79. this.display.StaticGraphics.AddList(graphic, "");
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }