using System.Windows.Controls;
using System.Windows.Input;
using TeamAAS_VP.Models;
using System.Text;
using System.Windows;
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Collections.Generic;
namespace TeamAAS_VP.Views.Product
{
///
/// Interaction logic for RobotPointParams
///
public partial class RobotPointParams : UserControl
{
public RobotPointParams()
{
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
}
private void dgPoints_PreviewKeyDown(object sender, KeyEventArgs e)
{
var dg = sender as DataGrid;
if (dg == null) return;
// handle Ctrl+C
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
var item = dg.SelectedItem as RPoint;
if (item != null)
{
var sb = new StringBuilder();
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);
Clipboard.SetText(sb.ToString());
e.Handled = true;
}
}
// handle Ctrl+V - paste as new row
if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
var text = Clipboard.ContainsText() ? Clipboard.GetText() : string.Empty;
if (!string.IsNullOrWhiteSpace(text))
{
// split by tabs or newlines
var tokens = text.Split(new[] { '\t', '\r', '\n' });
if (tokens.Length >= 3)
{
// attempt to parse fields in expected order: Number, Label, X, Y, Z, U, V, W, Hand, Tool
int number = 0;
int.TryParse(tokens[0], out number);
string label = tokens.Length > 1 ? tokens[1] : string.Empty;
float ParseFloat(string s)
{
if (float.TryParse(s, NumberStyles.Float, CultureInfo.CurrentCulture, out float v)) return v;
if (float.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out v)) return v;
return 0f;
}
float x = tokens.Length > 2 ? ParseFloat(tokens[2]) : 0f;
float y = tokens.Length > 3 ? ParseFloat(tokens[3]) : 0f;
float z = tokens.Length > 4 ? ParseFloat(tokens[4]) : 0f;
float u = tokens.Length > 5 ? ParseFloat(tokens[5]) : 0f;
float v2 = tokens.Length > 6 ? ParseFloat(tokens[6]) : 0f;
float w = tokens.Length > 7 ? ParseFloat(tokens[7]) : 0f;
// Hand may be enum or number
TeamAAS_VP.Enums.RobotHand hand = TeamAAS_VP.Enums.RobotHand.Left;
if (tokens.Length > 8)
{
var hstr = tokens[8];
if (!string.IsNullOrWhiteSpace(hstr))
{
if (int.TryParse(hstr, out int ih))
{
if (System.Enum.IsDefined(typeof(TeamAAS_VP.Enums.RobotHand), ih)) hand = (TeamAAS_VP.Enums.RobotHand)ih;
}
else
{
TeamAAS_VP.Enums.RobotHand eh;
if (System.Enum.TryParse(hstr, true, out eh)) hand = eh;
}
}
}
int tool = 0;
if (tokens.Length > 9) int.TryParse(tokens[9], out tool);
// create new RPoint and insert into underlying collection
var newPoint = new RPoint()
{
Number = number,
Label = label,
X = x,
Y = y,
Z = z,
U = u,
V = v2,
W = w,
Hand = hand,
Tool = tool
};
// try to insert into ItemsSource if it is IList
var items = dg.ItemsSource as IList;
if (items != null)
{
// if there's a selected item, overwrite it
var selectedExisting = dg.SelectedItem as RPoint;
if (selectedExisting != null)
{
//selectedExisting.Number = newPoint.Number;
selectedExisting.Label = newPoint.Label;
selectedExisting.X = newPoint.X;
selectedExisting.Y = newPoint.Y;
selectedExisting.Z = newPoint.Z;
selectedExisting.U = newPoint.U;
selectedExisting.V = newPoint.V;
selectedExisting.W = newPoint.W;
selectedExisting.Hand = newPoint.Hand;
selectedExisting.Tool = newPoint.Tool;
// keep selection
dg.SelectedItem = selectedExisting;
}
else
{
int insertIndex = dg.SelectedIndex >= 0 ? dg.SelectedIndex + 1 : items.Count;
// append/insert when no selection
if (items is IList typed)
{
// append to end for paste
typed.Add(newPoint);
dg.SelectedIndex = typed.Count - 1;
}
else if (items is IList genericList)
{
genericList.Add(newPoint);
dg.SelectedIndex = genericList.Count - 1;
}
else
{
// fallback: try to add via DataContext.SelectedRobot.Points
var vm = this.DataContext as TeamAAS_VP.ViewModels.Product.RobotPointParamsViewModel;
if (vm != null && vm.SelectedRobot != null && vm.SelectedRobot.Points != null)
{
vm.SelectedRobot.Points.Add(newPoint);
dg.SelectedIndex = vm.SelectedRobot.Points.Count - 1;
}
}
}
e.Handled = true;
}
}
}
}
// handle Delete - clear coordinates to default (0)
if (e.Key == Key.Delete)
{
var item = dg.SelectedItem as RPoint;
if (item != null)
{
item.X = 0;
item.Y = 0;
item.Z = 0;
item.U = 0;
item.V = 0;
item.W = 0;
// optional: clear other fields
item.Description = string.Empty;
MessageBox.Show("已清除所选点的坐标数据。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
e.Handled = true;
}
}
}
}
}