RobotPointParams.xaml.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System.Windows.Controls;
  2. using System.Windows.Input;
  3. using TeamAAS_VP.Models;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Collections;
  7. using System.Collections.ObjectModel;
  8. using System.Globalization;
  9. using System.Collections.Generic;
  10. namespace TeamAAS_VP.Views.Product
  11. {
  12. /// <summary>
  13. /// Interaction logic for RobotPointParams
  14. /// </summary>
  15. public partial class RobotPointParams : UserControl
  16. {
  17. public RobotPointParams()
  18. {
  19. InitializeComponent();
  20. }
  21. private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
  22. {
  23. }
  24. private void dgPoints_PreviewKeyDown(object sender, KeyEventArgs e)
  25. {
  26. var dg = sender as DataGrid;
  27. if (dg == null) return;
  28. // handle Ctrl+C
  29. if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
  30. {
  31. var item = dg.SelectedItem as RPoint;
  32. if (item != null)
  33. {
  34. var sb = new StringBuilder();
  35. sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}", item.Number, item.Label, item.X, item.Y, item.Z, item.U, item.V, item.W, item.Hand, item.Tool);
  36. Clipboard.SetText(sb.ToString());
  37. e.Handled = true;
  38. }
  39. }
  40. // handle Ctrl+V - paste as new row
  41. if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
  42. {
  43. var text = Clipboard.ContainsText() ? Clipboard.GetText() : string.Empty;
  44. if (!string.IsNullOrWhiteSpace(text))
  45. {
  46. // split by tabs or newlines
  47. var tokens = text.Split(new[] { '\t', '\r', '\n' });
  48. if (tokens.Length >= 3)
  49. {
  50. // attempt to parse fields in expected order: Number, Label, X, Y, Z, U, V, W, Hand, Tool
  51. int number = 0;
  52. int.TryParse(tokens[0], out number);
  53. string label = tokens.Length > 1 ? tokens[1] : string.Empty;
  54. float ParseFloat(string s)
  55. {
  56. if (float.TryParse(s, NumberStyles.Float, CultureInfo.CurrentCulture, out float v)) return v;
  57. if (float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out v)) return v;
  58. return 0f;
  59. }
  60. float x = tokens.Length > 2 ? ParseFloat(tokens[2]) : 0f;
  61. float y = tokens.Length > 3 ? ParseFloat(tokens[3]) : 0f;
  62. float z = tokens.Length > 4 ? ParseFloat(tokens[4]) : 0f;
  63. float u = tokens.Length > 5 ? ParseFloat(tokens[5]) : 0f;
  64. float v2 = tokens.Length > 6 ? ParseFloat(tokens[6]) : 0f;
  65. float w = tokens.Length > 7 ? ParseFloat(tokens[7]) : 0f;
  66. // Hand may be enum or number
  67. TeamAAS_VP.Enums.RobotHand hand = TeamAAS_VP.Enums.RobotHand.Left;
  68. if (tokens.Length > 8)
  69. {
  70. var hstr = tokens[8];
  71. if (!string.IsNullOrWhiteSpace(hstr))
  72. {
  73. if (int.TryParse(hstr, out int ih))
  74. {
  75. if (System.Enum.IsDefined(typeof(TeamAAS_VP.Enums.RobotHand), ih)) hand = (TeamAAS_VP.Enums.RobotHand)ih;
  76. }
  77. else
  78. {
  79. TeamAAS_VP.Enums.RobotHand eh;
  80. if (System.Enum.TryParse<TeamAAS_VP.Enums.RobotHand>(hstr, true, out eh)) hand = eh;
  81. }
  82. }
  83. }
  84. int tool = 0;
  85. if (tokens.Length > 9) int.TryParse(tokens[9], out tool);
  86. // create new RPoint and insert into underlying collection
  87. var newPoint = new RPoint()
  88. {
  89. Number = number,
  90. Label = label,
  91. X = x,
  92. Y = y,
  93. Z = z,
  94. U = u,
  95. V = v2,
  96. W = w,
  97. Hand = hand,
  98. Tool = tool
  99. };
  100. // try to insert into ItemsSource if it is IList
  101. var items = dg.ItemsSource as IList;
  102. if (items != null)
  103. {
  104. // if there's a selected item, overwrite it
  105. var selectedExisting = dg.SelectedItem as RPoint;
  106. if (selectedExisting != null)
  107. {
  108. //selectedExisting.Number = newPoint.Number;
  109. selectedExisting.Label = newPoint.Label;
  110. selectedExisting.X = newPoint.X;
  111. selectedExisting.Y = newPoint.Y;
  112. selectedExisting.Z = newPoint.Z;
  113. selectedExisting.U = newPoint.U;
  114. selectedExisting.V = newPoint.V;
  115. selectedExisting.W = newPoint.W;
  116. selectedExisting.Hand = newPoint.Hand;
  117. selectedExisting.Tool = newPoint.Tool;
  118. // keep selection
  119. dg.SelectedItem = selectedExisting;
  120. }
  121. else
  122. {
  123. int insertIndex = dg.SelectedIndex >= 0 ? dg.SelectedIndex + 1 : items.Count;
  124. // append/insert when no selection
  125. if (items is IList<RPoint> typed)
  126. {
  127. // append to end for paste
  128. typed.Add(newPoint);
  129. dg.SelectedIndex = typed.Count - 1;
  130. }
  131. else if (items is IList genericList)
  132. {
  133. genericList.Add(newPoint);
  134. dg.SelectedIndex = genericList.Count - 1;
  135. }
  136. else
  137. {
  138. // fallback: try to add via DataContext.SelectedRobot.Points
  139. var vm = this.DataContext as TeamAAS_VP.ViewModels.Product.RobotPointParamsViewModel;
  140. if (vm != null && vm.SelectedRobot != null && vm.SelectedRobot.Points != null)
  141. {
  142. vm.SelectedRobot.Points.Add(newPoint);
  143. dg.SelectedIndex = vm.SelectedRobot.Points.Count - 1;
  144. }
  145. }
  146. }
  147. e.Handled = true;
  148. }
  149. }
  150. }
  151. }
  152. // handle Delete - clear coordinates to default (0)
  153. if (e.Key == Key.Delete)
  154. {
  155. var item = dg.SelectedItem as RPoint;
  156. if (item != null)
  157. {
  158. item.X = 0;
  159. item.Y = 0;
  160. item.Z = 0;
  161. item.U = 0;
  162. item.V = 0;
  163. item.W = 0;
  164. // optional: clear other fields
  165. item.Description = string.Empty;
  166. MessageBox.Show("已清除所选点的坐标数据。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  167. e.Handled = true;
  168. }
  169. }
  170. }
  171. }
  172. }