DataSelectFrame.xaml.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Aitex.UI.Charting.ViewModel;
  15. using System.Xml;
  16. using Aitex.UI.Charting.Model;
  17. namespace Aitex.UI.Charting.View
  18. {
  19. /// <summary>
  20. /// Interaction logic for DataSelectFrame.xaml
  21. /// </summary>
  22. public partial class DataSelectFrame : UserControl
  23. {
  24. public DataSelectFrame()
  25. {
  26. InitializeComponent();
  27. Loaded += new RoutedEventHandler(DataSelectFrame_Loaded);
  28. _viewModel = new DataSelectFrameViewModel();
  29. DataContext = _viewModel;
  30. }
  31. DataSelectFrameViewModel _viewModel;
  32. bool _hideRawData = true;
  33. void DataSelectFrame_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. //_viewModel = new DataSelectFrameViewModel();
  36. //DataContext = _viewModel;
  37. }
  38. /// <summary>
  39. /// 由于Combobox控件不支持ICommand接口,因此此处仍旧采用message handling方式处理
  40. /// </summary>
  41. /// <param name="sender"></param>
  42. /// <param name="e"></param>
  43. private void DataSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
  44. {
  45. string dataSourceName = string.Empty;
  46. if (e.AddedItems != null && e.AddedItems.Count > 0)
  47. {
  48. dataSourceName = (string)e.AddedItems[0];
  49. _viewModel.DataSourceComboxItemSelectChangeCommand.Execute(dataSourceName);
  50. }
  51. UpdateTreeView(dataSourceName);
  52. }
  53. /// <summary>
  54. /// 更新数据源
  55. /// </summary>
  56. /// <param name="dataSourceName"></param>
  57. public void UpdateTreeView(string dataSourceName)
  58. {
  59. if (!string.IsNullOrEmpty(dataSourceName) && _viewModel.CommonViewModel.DataSources.Keys.Contains(dataSourceName))
  60. {
  61. var dataSource = _viewModel.CommonViewModel.DataSources[dataSourceName];
  62. System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  63. var root = doc.CreateElement("Aitex");
  64. doc.AppendChild(root);
  65. var sortNode = doc.CreateElement("GroupData");
  66. //root.AppendChild(rawNode);
  67. root.AppendChild(sortNode);
  68. foreach (var dataName1 in dataSource.Datas.Keys)
  69. {
  70. //添加分类的节点
  71. string dataName = ConvertToWaferDisplayIndex(dataName1);
  72. string[] arr = dataName.Split('.');
  73. if (_hideRawData && arr.Length > 2)
  74. {
  75. if (arr[1] == "DIs" || arr[1] == "DOs" || arr[1] == "AIs" || arr[1] == "AOs")
  76. continue;
  77. }
  78. var newSortNode = doc.SelectSingleNode(string.Format("/Aitex/GroupData/Item[@NodeName='{0}']", arr[0])) as XmlElement;
  79. if (newSortNode == null)
  80. {
  81. newSortNode = doc.CreateElement("Item");
  82. newSortNode.SetAttribute("NodeName", arr[0]);
  83. sortNode.AppendChild(newSortNode);
  84. }
  85. GenerateXmlNode(doc, newSortNode, arr.ToList(), 1, dataSource.Title, dataName);
  86. }
  87. //create tree view
  88. treeViewDataName.Items.Clear();
  89. var treeViewNodSort = new TreeViewItem() { Header = "GroupData" };
  90. treeViewDataName.Items.Add(treeViewNodSort);
  91. if (doc.DocumentElement.ChildNodes[0].ChildNodes.Count > 0)
  92. GenerateTreeViewNode(doc.DocumentElement.ChildNodes[0] as XmlElement, treeViewNodSort);
  93. SortTreeView(treeViewNodSort);
  94. }
  95. else
  96. {
  97. if (combBoxDataSourceList.Items.Count > 0)
  98. {
  99. combBoxDataSourceList.SelectedIndex = 0;
  100. }
  101. else
  102. {
  103. treeViewDataName.Items.Clear();
  104. }
  105. }
  106. //默认展开TreeView控件
  107. if (treeViewDataName.Items.Count > 0)
  108. {
  109. var tvi = treeViewDataName.Items[0] as TreeViewItem;
  110. tvi.IsExpanded = true;
  111. if (tvi.Items.Count < 10)
  112. {
  113. foreach (TreeViewItem item in tvi.Items)
  114. {
  115. item.IsExpanded = true;
  116. }
  117. }
  118. }
  119. }
  120. XmlElement GenerateXmlNode(XmlDocument doc, XmlElement node, List<string> varPath, int varId, string dataSourceName, string dataVarName)
  121. {
  122. if (varId >= varPath.Count)
  123. {
  124. node.SetAttribute("SourceName", dataSourceName);
  125. node.SetAttribute("VarName", dataVarName);
  126. return node;
  127. }
  128. var newNode = node.SelectSingleNode(string.Format("Item[@NodeName='{0}']", varPath[varId])) as XmlElement;
  129. if (newNode == null)
  130. {
  131. newNode = doc.CreateElement("Item");
  132. newNode.SetAttribute("NodeName", varPath[varId]);
  133. node.AppendChild(newNode);
  134. }
  135. return GenerateXmlNode(doc, newNode, varPath, varId + 1, dataSourceName, dataVarName);
  136. }
  137. TreeViewItem GenerateTreeViewNode(XmlElement node, TreeViewItem item)
  138. {
  139. if (node.ChildNodes.Count > 0)
  140. {
  141. foreach (XmlElement childNode in node.ChildNodes)
  142. {
  143. var newNode = new TreeViewItem() { Header = childNode.Attributes["NodeName"].Value };
  144. GenerateTreeViewNode(childNode, newNode);
  145. item.Items.Add(newNode);
  146. }
  147. var attNode = node.Attributes["NodeName"];
  148. if (attNode != null && attNode.Value == "VirtualDevice")
  149. {
  150. item.ContextMenu = new ContextMenu();
  151. var mi0 = new MenuItem() { Header = "PM.VirtualDevice" };
  152. mi0.Background = Brushes.Gray;
  153. mi0.Foreground = Brushes.White;
  154. mi0.IsEnabled = false;
  155. item.ContextMenu.Items.Add(mi0);
  156. var mi = new MenuItem() { Header = "增加自定义变量" };
  157. mi.Click += new RoutedEventHandler(add_Virtual_Device_Click);
  158. mi.Background = Brushes.White;
  159. //mi.Tag = item.Tag;
  160. item.ContextMenu.Items.Add(mi);
  161. }
  162. }
  163. else
  164. {
  165. string sourceName = node.Attributes["SourceName"].Value;
  166. string varName = node.Attributes["VarName"].Value;
  167. if (varName == "PM.VirtualDevice")
  168. {
  169. //item.Tag = new Tuple<string, string, string>(sourceName, varName, "function(x,y,z)"); //_vm.DataSources[sourceName].Data[varName];
  170. item.ContextMenu = new ContextMenu();
  171. var mi0 = new MenuItem() { Header = varName };
  172. mi0.Background = Brushes.Gray;
  173. mi0.Foreground = Brushes.White;
  174. mi0.IsEnabled = false;
  175. item.ContextMenu.Items.Add(mi0);
  176. var mi = new MenuItem() { Header = "增加自定义变量" };
  177. mi.Click += new RoutedEventHandler(add_Virtual_Device_Click);
  178. mi.Background = Brushes.White;
  179. //mi.Tag = item.Tag;
  180. item.ContextMenu.Items.Add(mi);
  181. }
  182. else
  183. {
  184. item.Tag = new KeyValuePair<string, string>(sourceName, varName); //_vm.DataSources[sourceName].Data[varName];
  185. item.ContextMenu = new ContextMenu();
  186. var mi0 = new MenuItem() { Header = varName };
  187. mi0.Background = Brushes.Gray;
  188. mi0.Foreground = Brushes.White;
  189. mi0.IsEnabled = false;
  190. item.ContextMenu.Items.Add(mi0);
  191. var mi = new MenuItem() { Header = "添加到绘图区" };
  192. mi.Click += new RoutedEventHandler(mi_Click);
  193. mi.Background = Brushes.White;
  194. mi.Tag = item.Tag;
  195. item.ContextMenu.Items.Add(mi);
  196. if (String.Compare(varName, 0, "PM.VirtualDevice", 0, "PM.VirtualDevice".Length, false) == 0)
  197. {
  198. var mi2 = new MenuItem() { Header = "编辑变量" };
  199. mi2.Click += new RoutedEventHandler(edit_Virtual_Device_Click);
  200. mi2.Background = Brushes.White;
  201. mi2.Tag = item.Tag;
  202. item.ContextMenu.Items.Add(mi2);
  203. var mi3 = new MenuItem() { Header = "删除变量" };
  204. mi3.Click += new RoutedEventHandler(delete_Click);
  205. mi3.Background = Brushes.White;
  206. mi3.Tag = item.Tag;
  207. item.ContextMenu.Items.Add(mi3);
  208. }
  209. }
  210. }
  211. return item;
  212. }
  213. /// <summary>
  214. /// 对TreeView进行排序操作
  215. /// </summary>
  216. /// <param name="treeViewItem"></param>
  217. /// <returns></returns>
  218. TreeViewItem SortTreeView(TreeViewItem treeViewItem)
  219. {
  220. List<TreeViewItem> list1 = new List<TreeViewItem>();
  221. foreach (TreeViewItem child in treeViewItem.Items)
  222. {
  223. SortTreeView(child);
  224. list1.Add(child);
  225. }
  226. list1.Sort(TreeViewItemCompare);
  227. treeViewItem.Items.Clear();
  228. foreach (var item in list1)
  229. {
  230. treeViewItem.Items.Add(item);
  231. }
  232. return treeViewItem;
  233. }
  234. private static int TreeViewItemCompare(TreeViewItem x, TreeViewItem y)
  235. {
  236. string x1 = x.Header as string;
  237. string x2 = y.Header as string;
  238. return string.Compare(x1, x2);
  239. }
  240. void edit_Virtual_Device_Click(object sender, RoutedEventArgs e)
  241. {
  242. if (sender is MenuItem)
  243. {
  244. var mi = (MenuItem)sender;
  245. if (mi != null && mi.Tag != null)
  246. {
  247. var dataInfo = (KeyValuePair<string, string>)mi.Tag;
  248. string varName = dataInfo.Value;
  249. varName = varName.Replace("PM.VirtualDevice.", "");
  250. var dlg = new VirtualDeviceFunctionEditor(varName, this) { Owner = Application.Current.MainWindow };
  251. dlg.ShowDialog();
  252. }
  253. }
  254. }
  255. void add_Virtual_Device_Click(object sender, RoutedEventArgs e)
  256. {
  257. var dlg = new VirtualDeviceFunctionEditor(null, this) { Owner = Application.Current.MainWindow };
  258. dlg.ShowDialog();
  259. }
  260. void delete_Click(object sender, RoutedEventArgs e)
  261. {
  262. try
  263. {
  264. if (sender is MenuItem)
  265. {
  266. var mi = (MenuItem)sender;
  267. if (mi != null && mi.Tag != null)
  268. {
  269. var dataInfo = (KeyValuePair<string, string>)mi.Tag;
  270. string varName = dataInfo.Value;
  271. varName = varName.Replace("PM.VirtualDevice.", "");
  272. if (MessageBox.Show("确认删除变量?\n" + varName, "删除", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  273. {
  274. string sql = string.Format("DELETE FROM \"VirtualDevice\" where \"ChamberID\" ='{0}' and \"DeviceName\" = '{1}';",
  275. CommonViewModel.Instance.CurrentSelectedDataSource.ChamberName, varName);
  276. Charting.ViewModel.ChartingBaseViewModel.ExecuteNonQuery(sql);
  277. ViewModel.CommonViewModel.Instance.CurrentSelectedDataSource.Datas.Remove(dataInfo.Value);
  278. UpdateTreeView(ViewModel.CommonViewModel.Instance.CurrentSelectedDataSourceName);
  279. }
  280. }
  281. }
  282. }
  283. catch (Exception ex)
  284. {
  285. MessageBox.Show("删除变量操作失败!\n" + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Asterisk);
  286. }
  287. }
  288. void mi_Click(object sender, RoutedEventArgs e)
  289. {
  290. if (sender is MenuItem)
  291. {
  292. var mi = (MenuItem)sender;
  293. if (mi != null && mi.Tag != null)
  294. {
  295. var dataInfo = (KeyValuePair<string, string>)mi.Tag;
  296. _viewModel.CommonViewModel.AddDataSeries(dataInfo.Key, ConvertWaferDisplayIndex(dataInfo.Value), null, Colors.White, 1, 0, 1);
  297. }
  298. }
  299. else if (sender is TreeView)
  300. {
  301. var treeNode = treeViewDataName.SelectedItem as TreeViewItem;
  302. if (treeNode != null && treeNode.Tag != null)
  303. {
  304. var dataInfo = (KeyValuePair<string, string>)treeNode.Tag;
  305. _viewModel.CommonViewModel.AddDataSeries(dataInfo.Key, ConvertWaferDisplayIndex(dataInfo.Value), null, Colors.White, 1, 0, 1);
  306. }
  307. }
  308. }
  309. private string ConvertToWaferDisplayIndex(string innerIndexString)
  310. {
  311. string str = innerIndexString;
  312. //if (source.ToLower().Contains("smart") &&
  313. // (source.ToLower().Contains("reflect") || source.ToLower().Contains("curvature") || source.ToLower().Contains("temp")))
  314. if ((innerIndexString.ToLower().Contains("smart") && innerIndexString.ToLower().Contains("reflect")) ||
  315. (innerIndexString.ToLower().Contains("curvaturea") || innerIndexString.ToLower().Contains("curvatureb") || innerIndexString.ToLower().Contains("curvaturec") || innerIndexString.ToLower().Contains("curvatured")) ||
  316. (innerIndexString.ToLower().Contains("reflecta") || innerIndexString.ToLower().Contains("reflectb") || innerIndexString.ToLower().Contains("reflectc") || innerIndexString.ToLower().Contains("reflectd")) ||
  317. (innerIndexString.ToLower().Contains("tempa") || innerIndexString.ToLower().Contains("tempb") || innerIndexString.ToLower().Contains("tempc") || innerIndexString.ToLower().Contains("tempd")))
  318. {
  319. if (!(string.IsNullOrEmpty(CommonViewModel.Instance.WaferDisplayIndex)))
  320. {
  321. string[] mSplit = innerIndexString.Split('_');
  322. if (mSplit.Length >= 2)
  323. {
  324. int innderIndex;
  325. if (int.TryParse(mSplit[mSplit.Length - 1], out innderIndex))
  326. {
  327. string[] arrDisplayIndex = CommonViewModel.Instance.WaferDisplayIndex.Split(',');
  328. if (innderIndex>=1 && innderIndex<=arrDisplayIndex.Length)
  329. {
  330. int displayIndex;
  331. if (int.TryParse(arrDisplayIndex[innderIndex-1], out displayIndex))
  332. {
  333. str = mSplit[0] + "_";
  334. for (int ii = 1; ii < mSplit.Length - 1; ii++)
  335. {
  336. str += mSplit[ii] + "_";
  337. }
  338. str += (displayIndex).ToString().PadLeft(2, '0');
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. return str;
  346. }
  347. private string ConvertWaferDisplayIndex(string source)
  348. {
  349. string str = source;
  350. //if (source.ToLower().Contains("smart") &&
  351. // (source.ToLower().Contains("reflect") || source.ToLower().Contains("curvature") || source.ToLower().Contains("temp")))
  352. if ((source.ToLower().Contains("smart") && source.ToLower().Contains("reflect")) ||
  353. (source.ToLower().Contains("curvaturea") || source.ToLower().Contains("curvatureb") || source.ToLower().Contains("curvaturec") || source.ToLower().Contains("curvatured")) ||
  354. (source.ToLower().Contains("reflecta") || source.ToLower().Contains("reflectb") || source.ToLower().Contains("reflectc") || source.ToLower().Contains("reflectd")) ||
  355. (source.ToLower().Contains("tempa") || source.ToLower().Contains("tempb") || source.ToLower().Contains("tempc") || source.ToLower().Contains("tempd")))
  356. {
  357. if (!(string.IsNullOrEmpty(CommonViewModel.Instance.WaferDisplayIndex)))
  358. {
  359. string[] mSplit = source.Split('_');
  360. if (mSplit.Length >= 2)
  361. {
  362. int index;
  363. bool result = int.TryParse(mSplit[mSplit.Length - 1], out index);
  364. if (result)
  365. {
  366. List<string> _mWaferDisplayIndex = CommonViewModel.Instance.WaferDisplayIndex.Split(',').ToList();
  367. index = _mWaferDisplayIndex.FindIndex((x) => x == (index.ToString().Trim().PadLeft(2, '0')));
  368. index = (index >= 0 ? ++index : index);
  369. if (index >= 0 && index <= _mWaferDisplayIndex.Count)
  370. {
  371. str = mSplit[0] + "_";
  372. for (int ii = 1; ii < mSplit.Length - 1; ii++)
  373. {
  374. str += mSplit[ii] + "_";
  375. }
  376. str += (index).ToString().PadLeft(2, '0');
  377. }
  378. }
  379. }
  380. }
  381. }
  382. return str;
  383. }
  384. /// <summary>
  385. /// 特殊功能键检测
  386. /// 当用户同时按下Ctrl + Alt + T, 则Toggle原始数据的显示
  387. /// </summary>
  388. /// <param name="sender"></param>
  389. /// <param name="e"></param>
  390. private void treeViewVarList_KeyDown(object sender, KeyEventArgs e)
  391. {
  392. if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) &&
  393. e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Alt) &&
  394. e.Key == Key.T)
  395. {
  396. _hideRawData = !_hideRawData;
  397. UpdateTreeView(CommonViewModel.Instance.CurrentSelectedDataSource.Title);
  398. }
  399. }
  400. }
  401. }