using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Aitex.UI.Charting.ViewModel; using System.Xml; using Aitex.UI.Charting.Model; namespace Aitex.UI.Charting.View { /// /// Interaction logic for DataSelectFrame.xaml /// public partial class DataSelectFrame : UserControl { public DataSelectFrame() { InitializeComponent(); Loaded += new RoutedEventHandler(DataSelectFrame_Loaded); _viewModel = new DataSelectFrameViewModel(); DataContext = _viewModel; } DataSelectFrameViewModel _viewModel; bool _hideRawData = true; void DataSelectFrame_Loaded(object sender, RoutedEventArgs e) { //_viewModel = new DataSelectFrameViewModel(); //DataContext = _viewModel; } /// /// 由于Combobox控件不支持ICommand接口,因此此处仍旧采用message handling方式处理 /// /// /// private void DataSource_SelectionChanged(object sender, SelectionChangedEventArgs e) { string dataSourceName = string.Empty; if (e.AddedItems != null && e.AddedItems.Count > 0) { dataSourceName = (string)e.AddedItems[0]; _viewModel.DataSourceComboxItemSelectChangeCommand.Execute(dataSourceName); } UpdateTreeView(dataSourceName); } /// /// 更新数据源 /// /// public void UpdateTreeView(string dataSourceName) { if (!string.IsNullOrEmpty(dataSourceName) && _viewModel.CommonViewModel.DataSources.Keys.Contains(dataSourceName)) { var dataSource = _viewModel.CommonViewModel.DataSources[dataSourceName]; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); var root = doc.CreateElement("Aitex"); doc.AppendChild(root); var sortNode = doc.CreateElement("GroupData"); //root.AppendChild(rawNode); root.AppendChild(sortNode); foreach (var dataName1 in dataSource.Datas.Keys) { //添加分类的节点 string dataName = ConvertToWaferDisplayIndex(dataName1); string[] arr = dataName.Split('.'); if (_hideRawData && arr.Length > 2) { if (arr[1] == "DIs" || arr[1] == "DOs" || arr[1] == "AIs" || arr[1] == "AOs") continue; } var newSortNode = doc.SelectSingleNode(string.Format("/Aitex/GroupData/Item[@NodeName='{0}']", arr[0])) as XmlElement; if (newSortNode == null) { newSortNode = doc.CreateElement("Item"); newSortNode.SetAttribute("NodeName", arr[0]); sortNode.AppendChild(newSortNode); } GenerateXmlNode(doc, newSortNode, arr.ToList(), 1, dataSource.Title, dataName); } //create tree view treeViewDataName.Items.Clear(); var treeViewNodSort = new TreeViewItem() { Header = "GroupData" }; treeViewDataName.Items.Add(treeViewNodSort); if (doc.DocumentElement.ChildNodes[0].ChildNodes.Count > 0) GenerateTreeViewNode(doc.DocumentElement.ChildNodes[0] as XmlElement, treeViewNodSort); SortTreeView(treeViewNodSort); } else { if (combBoxDataSourceList.Items.Count > 0) { combBoxDataSourceList.SelectedIndex = 0; } else { treeViewDataName.Items.Clear(); } } //默认展开TreeView控件 if (treeViewDataName.Items.Count > 0) { var tvi = treeViewDataName.Items[0] as TreeViewItem; tvi.IsExpanded = true; if (tvi.Items.Count < 10) { foreach (TreeViewItem item in tvi.Items) { item.IsExpanded = true; } } } } XmlElement GenerateXmlNode(XmlDocument doc, XmlElement node, List varPath, int varId, string dataSourceName, string dataVarName) { if (varId >= varPath.Count) { node.SetAttribute("SourceName", dataSourceName); node.SetAttribute("VarName", dataVarName); return node; } var newNode = node.SelectSingleNode(string.Format("Item[@NodeName='{0}']", varPath[varId])) as XmlElement; if (newNode == null) { newNode = doc.CreateElement("Item"); newNode.SetAttribute("NodeName", varPath[varId]); node.AppendChild(newNode); } return GenerateXmlNode(doc, newNode, varPath, varId + 1, dataSourceName, dataVarName); } TreeViewItem GenerateTreeViewNode(XmlElement node, TreeViewItem item) { if (node.ChildNodes.Count > 0) { foreach (XmlElement childNode in node.ChildNodes) { var newNode = new TreeViewItem() { Header = childNode.Attributes["NodeName"].Value }; GenerateTreeViewNode(childNode, newNode); item.Items.Add(newNode); } var attNode = node.Attributes["NodeName"]; if (attNode != null && attNode.Value == "VirtualDevice") { item.ContextMenu = new ContextMenu(); var mi0 = new MenuItem() { Header = "PM.VirtualDevice" }; mi0.Background = Brushes.Gray; mi0.Foreground = Brushes.White; mi0.IsEnabled = false; item.ContextMenu.Items.Add(mi0); var mi = new MenuItem() { Header = "增加自定义变量" }; mi.Click += new RoutedEventHandler(add_Virtual_Device_Click); mi.Background = Brushes.White; //mi.Tag = item.Tag; item.ContextMenu.Items.Add(mi); } } else { string sourceName = node.Attributes["SourceName"].Value; string varName = node.Attributes["VarName"].Value; if (varName == "PM.VirtualDevice") { //item.Tag = new Tuple(sourceName, varName, "function(x,y,z)"); //_vm.DataSources[sourceName].Data[varName]; item.ContextMenu = new ContextMenu(); var mi0 = new MenuItem() { Header = varName }; mi0.Background = Brushes.Gray; mi0.Foreground = Brushes.White; mi0.IsEnabled = false; item.ContextMenu.Items.Add(mi0); var mi = new MenuItem() { Header = "增加自定义变量" }; mi.Click += new RoutedEventHandler(add_Virtual_Device_Click); mi.Background = Brushes.White; //mi.Tag = item.Tag; item.ContextMenu.Items.Add(mi); } else { item.Tag = new KeyValuePair(sourceName, varName); //_vm.DataSources[sourceName].Data[varName]; item.ContextMenu = new ContextMenu(); var mi0 = new MenuItem() { Header = varName }; mi0.Background = Brushes.Gray; mi0.Foreground = Brushes.White; mi0.IsEnabled = false; item.ContextMenu.Items.Add(mi0); var mi = new MenuItem() { Header = "添加到绘图区" }; mi.Click += new RoutedEventHandler(mi_Click); mi.Background = Brushes.White; mi.Tag = item.Tag; item.ContextMenu.Items.Add(mi); if (String.Compare(varName, 0, "PM.VirtualDevice", 0, "PM.VirtualDevice".Length, false) == 0) { var mi2 = new MenuItem() { Header = "编辑变量" }; mi2.Click += new RoutedEventHandler(edit_Virtual_Device_Click); mi2.Background = Brushes.White; mi2.Tag = item.Tag; item.ContextMenu.Items.Add(mi2); var mi3 = new MenuItem() { Header = "删除变量" }; mi3.Click += new RoutedEventHandler(delete_Click); mi3.Background = Brushes.White; mi3.Tag = item.Tag; item.ContextMenu.Items.Add(mi3); } } } return item; } /// /// 对TreeView进行排序操作 /// /// /// TreeViewItem SortTreeView(TreeViewItem treeViewItem) { List list1 = new List(); foreach (TreeViewItem child in treeViewItem.Items) { SortTreeView(child); list1.Add(child); } list1.Sort(TreeViewItemCompare); treeViewItem.Items.Clear(); foreach (var item in list1) { treeViewItem.Items.Add(item); } return treeViewItem; } private static int TreeViewItemCompare(TreeViewItem x, TreeViewItem y) { string x1 = x.Header as string; string x2 = y.Header as string; return string.Compare(x1, x2); } void edit_Virtual_Device_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem) { var mi = (MenuItem)sender; if (mi != null && mi.Tag != null) { var dataInfo = (KeyValuePair)mi.Tag; string varName = dataInfo.Value; varName = varName.Replace("PM.VirtualDevice.", ""); var dlg = new VirtualDeviceFunctionEditor(varName, this) { Owner = Application.Current.MainWindow }; dlg.ShowDialog(); } } } void add_Virtual_Device_Click(object sender, RoutedEventArgs e) { var dlg = new VirtualDeviceFunctionEditor(null, this) { Owner = Application.Current.MainWindow }; dlg.ShowDialog(); } void delete_Click(object sender, RoutedEventArgs e) { try { if (sender is MenuItem) { var mi = (MenuItem)sender; if (mi != null && mi.Tag != null) { var dataInfo = (KeyValuePair)mi.Tag; string varName = dataInfo.Value; varName = varName.Replace("PM.VirtualDevice.", ""); if (MessageBox.Show("确认删除变量?\n" + varName, "删除", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { string sql = string.Format("DELETE FROM \"VirtualDevice\" where \"ChamberID\" ='{0}' and \"DeviceName\" = '{1}';", CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName, varName); Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(sql); ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.Datas.Remove(dataInfo.Value); UpdateTreeView(ViewModel.CommonViewModel.Instance.CurrentSelectedDataSourceName); } } } } catch (Exception ex) { MessageBox.Show("删除变量操作失败!\n" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Asterisk); } } void mi_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem) { var mi = (MenuItem)sender; if (mi != null && mi.Tag != null) { var dataInfo = (KeyValuePair)mi.Tag; _viewModel.CommonViewModel.AddDataSeries(dataInfo.Key, ConvertWaferDisplayIndex(dataInfo.Value), null, Colors.White, 1, 0, 1); } } else if (sender is TreeView) { var treeNode = treeViewDataName.SelectedItem as TreeViewItem; if (treeNode != null && treeNode.Tag != null) { var dataInfo = (KeyValuePair)treeNode.Tag; _viewModel.CommonViewModel.AddDataSeries(dataInfo.Key, ConvertWaferDisplayIndex(dataInfo.Value), null, Colors.White, 1, 0, 1); } } } private string ConvertToWaferDisplayIndex(string innerIndexString) { string str = innerIndexString; //if (source.ToLower().Contains("smart") && // (source.ToLower().Contains("reflect") || source.ToLower().Contains("curvature") || source.ToLower().Contains("temp"))) if ((innerIndexString.ToLower().Contains("smart") && innerIndexString.ToLower().Contains("reflect")) || (innerIndexString.ToLower().Contains("curvaturea") || innerIndexString.ToLower().Contains("curvatureb") || innerIndexString.ToLower().Contains("curvaturec") || innerIndexString.ToLower().Contains("curvatured")) || (innerIndexString.ToLower().Contains("reflecta") || innerIndexString.ToLower().Contains("reflectb") || innerIndexString.ToLower().Contains("reflectc") || innerIndexString.ToLower().Contains("reflectd")) || (innerIndexString.ToLower().Contains("tempa") || innerIndexString.ToLower().Contains("tempb") || innerIndexString.ToLower().Contains("tempc") || innerIndexString.ToLower().Contains("tempd"))) { if (!(string.IsNullOrEmpty(CommonViewModel.Instance.WaferDisplayIndex))) { string[] mSplit = innerIndexString.Split('_'); if (mSplit.Length >= 2) { int innderIndex; if (int.TryParse(mSplit[mSplit.Length - 1], out innderIndex)) { string[] arrDisplayIndex = CommonViewModel.Instance.WaferDisplayIndex.Split(','); if (innderIndex>=1 && innderIndex<=arrDisplayIndex.Length) { int displayIndex; if (int.TryParse(arrDisplayIndex[innderIndex-1], out displayIndex)) { str = mSplit[0] + "_"; for (int ii = 1; ii < mSplit.Length - 1; ii++) { str += mSplit[ii] + "_"; } str += (displayIndex).ToString().PadLeft(2, '0'); } } } } } } return str; } private string ConvertWaferDisplayIndex(string source) { string str = source; //if (source.ToLower().Contains("smart") && // (source.ToLower().Contains("reflect") || source.ToLower().Contains("curvature") || source.ToLower().Contains("temp"))) if ((source.ToLower().Contains("smart") && source.ToLower().Contains("reflect")) || (source.ToLower().Contains("curvaturea") || source.ToLower().Contains("curvatureb") || source.ToLower().Contains("curvaturec") || source.ToLower().Contains("curvatured")) || (source.ToLower().Contains("reflecta") || source.ToLower().Contains("reflectb") || source.ToLower().Contains("reflectc") || source.ToLower().Contains("reflectd")) || (source.ToLower().Contains("tempa") || source.ToLower().Contains("tempb") || source.ToLower().Contains("tempc") || source.ToLower().Contains("tempd"))) { if (!(string.IsNullOrEmpty(CommonViewModel.Instance.WaferDisplayIndex))) { string[] mSplit = source.Split('_'); if (mSplit.Length >= 2) { int index; bool result = int.TryParse(mSplit[mSplit.Length - 1], out index); if (result) { List _mWaferDisplayIndex = CommonViewModel.Instance.WaferDisplayIndex.Split(',').ToList(); index = _mWaferDisplayIndex.FindIndex((x) => x == (index.ToString().Trim().PadLeft(2, '0'))); index = (index >= 0 ? ++index : index); if (index >= 0 && index <= _mWaferDisplayIndex.Count) { str = mSplit[0] + "_"; for (int ii = 1; ii < mSplit.Length - 1; ii++) { str += mSplit[ii] + "_"; } str += (index).ToString().PadLeft(2, '0'); } } } } } return str; } /// /// 特殊功能键检测 /// 当用户同时按下Ctrl + Alt + T, 则Toggle原始数据的显示 /// /// /// private void treeViewVarList_KeyDown(object sender, KeyEventArgs e) { if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt) && e.Key == Key.T) { _hideRawData = !_hideRawData; UpdateTreeView(CommonViewModel.Instance.CurrentSelectedDataSource.Title); } } } }