123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- 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
- {
- /// <summary>
- /// Interaction logic for DataSelectFrame.xaml
- /// </summary>
- 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;
- }
- /// <summary>
- /// 由于Combobox控件不支持ICommand接口,因此此处仍旧采用message handling方式处理
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- /// <summary>
- /// 更新数据源
- /// </summary>
- /// <param name="dataSourceName"></param>
- 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<string> 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<string, string, string>(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<string, string>(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;
- }
- /// <summary>
- /// 对TreeView进行排序操作
- /// </summary>
- /// <param name="treeViewItem"></param>
- /// <returns></returns>
- TreeViewItem SortTreeView(TreeViewItem treeViewItem)
- {
- List<TreeViewItem> list1 = new List<TreeViewItem>();
- 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<string, string>)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<string, string>)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<string, string>)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<string, string>)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<string> _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;
- }
- /// <summary>
- /// 特殊功能键检测
- /// 当用户同时按下Ctrl + Alt + T, 则Toggle原始数据的显示
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
-
- }
- }
- }
- }
|