RecipeViewModel.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.View.Common;
  4. using MECF.Framework.Common.DataCenter;
  5. using Microsoft.VisualBasic;
  6. using Newtonsoft.Json;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Media;
  18. using System.Xml;
  19. using Venus_Core;
  20. using Venus_Core.Attributes;
  21. using Venus_MainPages.PMs;
  22. using Venus_MainPages.Unity;
  23. using Venus_MainPages.Views;
  24. using Venus_Themes.UserControls;
  25. using WPF.Themes.UserControls;
  26. namespace Venus_MainPages.ViewModels
  27. {
  28. internal class RecipeViewModel : BindableBase
  29. {
  30. #region 私有字段
  31. public string ModuleName = "PMA";
  32. private string m_CurrentRecipeName;
  33. private UiRecipeManager m_uiRecipeManager = new UiRecipeManager();
  34. private RecipeView recipeView;
  35. private TreeView treeViewRcpList;
  36. private Recipe m_currentRecipe;
  37. private string m_recipeType;
  38. private bool firstLoad=true;
  39. private WrapPanel headWrapPanel;
  40. private StackPanel bodyStackPanel;
  41. private List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>()
  42. {
  43. new SolidColorBrush(Colors.Coral),
  44. new SolidColorBrush(Colors.Cyan),
  45. new SolidColorBrush(Colors.Honeydew)
  46. };
  47. private Grid currentRecipeGrid;
  48. private int copyIndex = -1;
  49. #endregion
  50. #region 属性
  51. public string CurrentRecipeName
  52. {
  53. get { return m_CurrentRecipeName; }
  54. set { SetProperty(ref m_CurrentRecipeName, value); }
  55. }
  56. public Recipe CurrentRecipe
  57. {
  58. get { return m_currentRecipe; }
  59. set
  60. {
  61. m_currentRecipe = value;
  62. RecipeType = m_currentRecipe.Header.Type.ToString();
  63. }
  64. }
  65. public string RecipeType
  66. {
  67. get { return m_recipeType; }
  68. set { SetProperty(ref m_recipeType, value); }
  69. }
  70. #endregion
  71. #region 命令
  72. private DelegateCommand<Object> _MouseRightButtonDownCommand;
  73. public DelegateCommand<Object> MouseRightButtonDownCommand =>
  74. _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand<Object>(OnMouseRightButtonDown));
  75. private DelegateCommand<Object> _LoadedCommand;
  76. public DelegateCommand<Object> LoadedCommand =>
  77. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand<Object>(OnLoaded));
  78. private DelegateCommand _SaveRecipeCommand;
  79. public DelegateCommand SaveRecipeCommand =>
  80. _SaveRecipeCommand ?? (_SaveRecipeCommand = new DelegateCommand(OnSaveRecipe));
  81. private DelegateCommand _AddStepCommand;
  82. public DelegateCommand AddStepCommand =>
  83. _AddStepCommand ?? (_AddStepCommand = new DelegateCommand(OnAddStep));
  84. private DelegateCommand _DeleteStepCommand;
  85. public DelegateCommand DeleteStepCommand =>
  86. _DeleteStepCommand ?? (_DeleteStepCommand = new DelegateCommand(OnDeleteStep));
  87. #endregion
  88. #region 构造函数
  89. public RecipeViewModel()
  90. {
  91. }
  92. #endregion
  93. #region 命令方法
  94. private void OnAddStep()
  95. {
  96. if (CurrentRecipe != null)
  97. {
  98. var index = currentRecipeGrid.ColumnDefinitions.Count;
  99. RecipeStep recipeStep;
  100. if (copyIndex == -1)
  101. {
  102. recipeStep = new RecipeStep();
  103. recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
  104. recipeStep.LstUnit.Add(new TCPUnit());
  105. recipeStep.LstUnit.Add(new BiasUnit());
  106. recipeStep.LstUnit.Add(new GasControlUnit());
  107. recipeStep.LstUnit.Add(new ESCHVUnit());
  108. recipeStep.LstUnit.Add(new ProcessKitUnit());
  109. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  110. }
  111. else
  112. {
  113. var t = JsonConvert.SerializeObject(CurrentRecipe);
  114. recipeStep = Recipe.Load(t).Steps[copyIndex - 1];
  115. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  116. }
  117. RecipeStepToGridColumn(recipeStep, currentRecipeGrid, index, true);
  118. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  119. {
  120. CurrentRecipe.Steps[i - 1].StepNo = i;
  121. }
  122. }
  123. }
  124. private void OnDeleteStep()
  125. {
  126. if (CurrentRecipe != null)
  127. {
  128. OnDeleteStep(CurrentRecipe.Steps.Count - 1);
  129. }
  130. }
  131. private void OnSaveRecipe()
  132. {
  133. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  134. }
  135. private void OnLoaded(Object myrecipeView)
  136. {
  137. if (firstLoad == true)
  138. {
  139. firstLoad = false;
  140. recipeView = myrecipeView as RecipeView;
  141. treeViewRcpList = recipeView.treeViewRcpList;
  142. headWrapPanel = recipeView.headWrapPanel;
  143. bodyStackPanel = recipeView.bodyStackPanel;
  144. UpdateRecipeFileList();
  145. treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged;
  146. }
  147. }
  148. TreeViewFileItem selectedItem;
  149. private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  150. {
  151. selectedItem = e.NewValue as TreeViewFileItem;
  152. if (selectedItem == null)
  153. return;
  154. try
  155. {
  156. CurrentRecipeName = selectedItem.FileName;
  157. string xmlRecipeData = m_uiRecipeManager.LoadRecipe(ModuleName, selectedItem.FileName);
  158. if (xmlRecipeData == "")
  159. {
  160. treeViewRcpList.Items.Remove(selectedItem);
  161. return;
  162. }
  163. CurrentRecipe = Recipe.Load(xmlRecipeData);
  164. LoadHeadWrapPanel(headWrapPanel, CurrentRecipe);
  165. LoadRecipe(CurrentRecipe);
  166. }
  167. catch (Exception ex)
  168. {
  169. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  170. }
  171. e.Handled = true;
  172. }
  173. public bool SaveRecipe(string recipeName, string recipeContent)
  174. {
  175. return m_uiRecipeManager.SaveRecipe(ModuleName, recipeName, recipeContent);
  176. }
  177. private void OnMouseRightButtonDown(Object treeView)
  178. {
  179. //treeViewRcpList = treeView as TreeView;
  180. treeViewRcpList.ContextMenu = new ContextMenu() { Background = new SolidColorBrush(Colors.AliceBlue)};
  181. MenuItem menuItem = new MenuItem();
  182. menuItem = new MenuItem();
  183. menuItem.Tag = "\\";
  184. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  185. menuItem.Header = "New Recipe";
  186. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  187. menuItem = new MenuItem();
  188. menuItem.Tag = "\\";
  189. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  190. menuItem.Header = "Delete Recipe";
  191. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  192. menuItem = new MenuItem();
  193. menuItem.Tag = "\\";
  194. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAsRecipe);
  195. menuItem.Header = "Save As Recipe";
  196. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  197. menuItem = new MenuItem();
  198. menuItem.Tag = "\\";
  199. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  200. menuItem.Header = "Rename";
  201. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  202. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  203. }
  204. private void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  205. {
  206. MenuItem mit = sender as MenuItem;
  207. string folderName = mit.Tag as string;
  208. PerformCreateRecipe(folderName);
  209. }
  210. private void PerformCreateRecipe(string folderName)
  211. {
  212. try
  213. {
  214. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  215. {
  216. Owner = Application.Current.MainWindow
  217. };
  218. if (dlg.ShowDialog() == true)
  219. {
  220. var recipeName = folderName + "\\" + dlg.InputText;
  221. RecipeType type = (RecipeType)Enum.Parse(typeof(RecipeType), dlg.SelectedType);
  222. string newRecipe =RecipeUnity.CreateRecipe(type, dlg.InputText);
  223. //string recipeContent = m_uiRecipeManager.GetRecipeTemplate(ModuleName);
  224. //m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName));
  225. if (SaveAsRecipe(recipeName, newRecipe))
  226. {
  227. //UpdateRecipeFileList();
  228. //SelectRecipe(recipeName);
  229. treeViewRcpList.Items.Add(new TreeViewFileItem(dlg.InputText));
  230. }
  231. else
  232. {
  233. WPFMessageBox.ShowError("Error");
  234. }
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. LOG.WriteExeption(ex);
  240. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  241. }
  242. }
  243. private void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  244. {
  245. if (CurrentRecipe == null)
  246. {
  247. return;
  248. }
  249. if (WPFMessageBox.ShowQuestion($"Delete {CurrentRecipeName}?","删除后无法恢复!!!") == MessageBoxResult.Yes)
  250. {
  251. MenuItem mit = sender as MenuItem;
  252. //string recipename = mit.Header.ToString();
  253. m_uiRecipeManager.DeleteRecipe(ModuleName, CurrentRecipeName);
  254. //PerformCreateRecipe(folderName);
  255. treeViewRcpList.Items.Remove(selectedItem);
  256. }
  257. }
  258. private void menuItem_MouseClick_SaveAsRecipe(object sender, RoutedEventArgs e)
  259. {
  260. if (CurrentRecipe == null)
  261. {
  262. return;
  263. }
  264. var newName = Interaction.InputBox(" ", "Save As Recipe", CurrentRecipeName, -1, -1);
  265. if (newName != CurrentRecipeName && newName != "")
  266. {
  267. var newRecipe = CurrentRecipe;
  268. newRecipe.Header.Name = newName;
  269. newRecipe.Header.CreateTime = DateTime.Now.ToString();
  270. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  271. File.WriteAllText(newrecipePath, RecipeUnity.RecipeToString(newRecipe));
  272. UpdateRecipeFileList();
  273. }
  274. }
  275. private void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  276. {
  277. if (CurrentRecipe == null)
  278. {
  279. return;
  280. }
  281. var newName= Interaction.InputBox(" ", "Rename Recipe", CurrentRecipeName, -1, -1);
  282. if (newName != CurrentRecipeName && newName!="")
  283. {
  284. var oldrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, CurrentRecipeName + ".rcp");
  285. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  286. CurrentRecipe.Header.Name = newName;
  287. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  288. File.Move(oldrecipePath, newrecipePath);
  289. UpdateRecipeFileList();
  290. }
  291. }
  292. public bool SaveAsRecipe(string recipeName, string recipeContent)
  293. {
  294. return m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, recipeContent);
  295. }
  296. public string GetXmlRecipeList()
  297. {
  298. return m_uiRecipeManager.GetXmlRecipeList(ModuleName, true) ?? "";
  299. }
  300. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  301. {
  302. foreach (XmlElement ele in curElementNode.ChildNodes)
  303. {
  304. if (ele.Name == "File")
  305. {
  306. string fileName = ele.Attributes["Name"].Value;
  307. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  308. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  309. item.Tag = ele.Attributes["Name"].Value;
  310. //item.ToolTip = fileName;
  311. itemsControl.Items.Add(item);
  312. }
  313. else if (ele.Name == "Folder")
  314. {
  315. string folderName = ele.Attributes["Name"].Value;
  316. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  317. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  318. item.Tag = ele.Attributes["Name"].Value;
  319. CreateTreeViewItems(ele, item);
  320. item.IsExpanded = false;
  321. itemsControl.Items.Add(item);
  322. }
  323. }
  324. }
  325. private void LoadHeadWrapPanel(WrapPanel HeadWrapPanel, Recipe recipe)
  326. {
  327. HeadWrapPanel.Children.Clear();
  328. Type type = recipe.Header.GetType();
  329. foreach (var propertyInfo in type.GetProperties())
  330. {
  331. TextBlock textBlock = new TextBlock();
  332. textBlock.FontSize = 15;
  333. //textBlock.Width = 100;
  334. textBlock.Text = propertyInfo.Name + ":";
  335. textBlock.Margin = new Thickness(15, 0, 0, 0);
  336. textBlock.VerticalAlignment = VerticalAlignment.Center;
  337. HeadWrapPanel.Children.Add(textBlock);
  338. Binding binding = new Binding()
  339. {
  340. Source = recipe.Header, // 数据源
  341. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  342. Mode = BindingMode.TwoWay, // 绑定模式
  343. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  344. };
  345. var propertyTypeName = propertyInfo.PropertyType.Name;
  346. var propertyInfoName = propertyInfo.Name;
  347. Control control = new Control();
  348. switch (propertyTypeName)
  349. {
  350. case "Int32":
  351. case "String":
  352. control = new TextBox();
  353. control.Margin = new Thickness(1, 0, 0, 0);
  354. control.BorderThickness = new Thickness(0, 0, 0, 1);
  355. //control.BorderBrush = Brushes.Black;
  356. control.FontSize = 15;
  357. control.Foreground = Brushes.Green;
  358. control.Background = Brushes.Transparent;
  359. control.VerticalAlignment = VerticalAlignment.Center;
  360. control.MinWidth = 15;
  361. control.SetBinding(TextBox.TextProperty, binding);
  362. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  363. if (objAttrs.Length > 0)
  364. {
  365. (control as TextBox).IsReadOnly = true;
  366. }
  367. break;
  368. case "Boolean":
  369. control = new CheckBox();
  370. control.SetBinding(CheckBox.IsCheckedProperty, binding);
  371. break;
  372. default:
  373. control = new ComboBox();
  374. control.Height = 23;
  375. control.SetBinding(ComboBox.SelectedItemProperty, binding);
  376. ItemsControlHelper.SetEnumValuesToItemsSource(control, true);
  377. break;
  378. }
  379. HeadWrapPanel.Children.Add(control);
  380. }
  381. }
  382. private void MenuItemLeftInsert_Click(object sender, RoutedEventArgs e)
  383. {
  384. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  385. int insertIndex = Convert.ToInt32(t.Text);
  386. OnAddStep(insertIndex);
  387. }
  388. private void MenuItemRightInsert_Click(object sender, RoutedEventArgs e)
  389. {
  390. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  391. int insertIndex = Convert.ToInt32(t.Text);
  392. OnAddStep(insertIndex + 1);
  393. }
  394. private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
  395. {
  396. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  397. int deleteIndex = Convert.ToInt32(t.Text);
  398. OnDeleteStep(deleteIndex - 1);
  399. }
  400. private void MenuItemCopy_Click(object sender, RoutedEventArgs e)
  401. {
  402. var t = ((sender as MenuItem).Parent as ContextMenu).PlacementTarget as TextBox;
  403. copyIndex = Convert.ToInt32(t.Text);
  404. }
  405. #endregion
  406. #region 私有方法
  407. private void RecipeStepToGridColumn(RecipeStep recipeStep, Grid grid, int index, bool isInsert)
  408. {
  409. int location = 1;
  410. if (isInsert == true)
  411. {
  412. location -= 1;
  413. }
  414. Type recipeType = recipeStep.GetType();
  415. int cycleCount = 1;
  416. if (index == 0)
  417. {
  418. cycleCount = 2;
  419. }
  420. for (int j = 0; j < cycleCount; j++)
  421. {
  422. int i = 0;
  423. ColumnDefinition col1 = new ColumnDefinition();
  424. //col1.MinWidth = 50;
  425. grid.ColumnDefinitions.Insert(index, col1);
  426. Binding stepcheckbinding = null;
  427. //grid.MinWidth = 200;
  428. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  429. {
  430. string propertyInfoName = propertyInfo.Name;
  431. string propertyTypeName = propertyInfo.PropertyType.Name;
  432. if (propertyInfoName != "LstUnit")
  433. {
  434. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  435. {
  436. RowDefinition row1 = new RowDefinition();
  437. grid.RowDefinitions.Add(row1);
  438. }
  439. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  440. {
  441. TextBox textBlock = new TextBox();
  442. textBlock.IsReadOnly = true;
  443. textBlock.Text = propertyInfoName;
  444. grid.Children.Add(textBlock);
  445. textBlock.Background = new SolidColorBrush(Colors.BurlyWood);
  446. Grid.SetRow(textBlock, i);
  447. Grid.SetColumn(textBlock, 0);
  448. }
  449. else
  450. {
  451. Binding binding = new Binding()
  452. {
  453. Source = recipeStep, // 数据源
  454. Path = new PropertyPath(propertyInfoName), // 需绑定的数据源属性名
  455. Mode = BindingMode.TwoWay, // 绑定模式
  456. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  457. };
  458. switch (propertyTypeName)
  459. {
  460. case "Int32":
  461. case "String":
  462. TextBox textBox = new TextBox();
  463. if (stepcheckbinding == null)
  464. {
  465. textBox.IsEnabled = true;
  466. }
  467. else
  468. {
  469. textBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  470. }
  471. textBox.SetBinding(TextBox.TextProperty, binding);
  472. grid.Children.Add(textBox);
  473. Grid.SetRow(textBox, i);
  474. Grid.SetColumn(textBox, index + location);
  475. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  476. if (objAttrs.Length > 0)
  477. {
  478. textBox.IsReadOnly = true;
  479. ContextMenu contextmenu = new ContextMenu();
  480. textBox.ContextMenu = contextmenu;
  481. MenuItem menuItemDelete = new MenuItem();
  482. menuItemDelete.Header = "删除";
  483. menuItemDelete.Click += MenuItemDelete_Click;
  484. MenuItem menuItemLeftInsert = new MenuItem();
  485. menuItemLeftInsert.Header = "左插入";
  486. menuItemLeftInsert.Click += MenuItemLeftInsert_Click;
  487. MenuItem menuItemRightInsert = new MenuItem();
  488. menuItemRightInsert.Header = "右插入";
  489. menuItemRightInsert.Click += MenuItemRightInsert_Click;
  490. MenuItem menuItemCopy = new MenuItem();
  491. menuItemCopy.Tag = textBox.Text;
  492. menuItemCopy.Header = "复制";
  493. menuItemCopy.Click += MenuItemCopy_Click;
  494. contextmenu.Items.Add(menuItemCopy);
  495. contextmenu.Items.Add(menuItemDelete);
  496. contextmenu.Items.Add(menuItemLeftInsert);
  497. contextmenu.Items.Add(menuItemRightInsert);
  498. }
  499. break;
  500. case "Boolean":
  501. CheckBox checkBox = new CheckBox();
  502. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  503. grid.Children.Add(checkBox);
  504. Grid.SetRow(checkBox, i);
  505. Grid.SetColumn(checkBox, index + location);
  506. if (stepcheckbinding == null)
  507. {
  508. stepcheckbinding = new Binding
  509. {
  510. Source = checkBox, // 数据源
  511. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  512. Mode = BindingMode.TwoWay, // 绑定模式
  513. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  514. };
  515. }
  516. else
  517. {
  518. if (stepcheckbinding == null)
  519. {
  520. checkBox.IsEnabled = true;
  521. }
  522. else
  523. {
  524. checkBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  525. }
  526. }
  527. break;
  528. default:
  529. ComboBox comboBox = new ComboBox();
  530. comboBox.Style = (Style)recipeView.FindResource("customeComboBoxStyle");
  531. comboBox.Background = new SolidColorBrush(Colors.Black);
  532. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  533. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  534. grid.Children.Add(comboBox);
  535. Grid.SetRow(comboBox, i);
  536. Grid.SetColumn(comboBox, index + location);
  537. if (stepcheckbinding == null)
  538. {
  539. comboBox.IsEnabled = true;
  540. }
  541. else
  542. {
  543. comboBox.SetBinding(TextBox.IsEnabledProperty, stepcheckbinding);
  544. }
  545. break;
  546. }
  547. }
  548. i++;
  549. }
  550. }
  551. int k = 0;
  552. recipeStep.LstUnit.ToList().ForEach(x =>
  553. {
  554. Type unitType = x.GetType();
  555. Binding checkbinding = null;
  556. foreach (PropertyInfo propertyInfo in unitType.GetProperties())
  557. {
  558. if (index == 0 && grid.ColumnDefinitions.Count == 1)
  559. {
  560. RowDefinition row1 = new RowDefinition();
  561. grid.RowDefinitions.Add(row1);
  562. }
  563. if (grid.ColumnDefinitions.Count == 1 && j == 0)
  564. {
  565. TextBox textBlock = new TextBox();
  566. textBlock.IsReadOnly = true;
  567. switch (propertyInfo.Name)
  568. {
  569. case "Gas1":
  570. var data1 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas1");
  571. textBlock.Text = $"{propertyInfo.Name}({data1.DisplayName},{data1.Scale})";
  572. break;
  573. case "Gas2":
  574. var data2 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas2");
  575. textBlock.Text = $"{propertyInfo.Name}({data2.DisplayName},{data2.Scale})";
  576. break;
  577. case "Gas3":
  578. var data3 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas3");
  579. textBlock.Text = $"{propertyInfo.Name}({data3.DisplayName},{data3.Scale})";
  580. break;
  581. case "Gas4":
  582. var data4 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas4");
  583. textBlock.Text = $"{propertyInfo.Name}({data4.DisplayName},{data4.Scale})";
  584. break;
  585. case "Gas5":
  586. var data5 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas5");
  587. textBlock.Text = $"{propertyInfo.Name}({data5.DisplayName},{data5.Scale})";
  588. break;
  589. case "Gas6":
  590. var data6 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas6");
  591. textBlock.Text = $"{propertyInfo.Name}({data6.DisplayName},{data6.Scale})";
  592. break;
  593. case "Gas7":
  594. var data7 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas7");
  595. textBlock.Text = $"{propertyInfo.Name}({data7.DisplayName},{data7.Scale})";
  596. break;
  597. case "Gas8":
  598. var data8 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas8");
  599. textBlock.Text = $"{propertyInfo.Name}({data8.DisplayName},{data8.Scale})";
  600. break;
  601. default:
  602. textBlock.Text = propertyInfo.Name;
  603. break;
  604. }
  605. textBlock.Background = solidColorBrushes[k % 3];
  606. grid.Children.Add(textBlock);
  607. Grid.SetRow(textBlock, i);
  608. Grid.SetColumn(textBlock, 0);
  609. }
  610. else
  611. {
  612. Binding binding = new Binding()
  613. {
  614. Source = recipeStep.LstUnit[k], // 数据源
  615. //Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  616. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  617. Mode = BindingMode.TwoWay, // 绑定模式
  618. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  619. };
  620. var item = propertyInfo.PropertyType.Name;
  621. if (propertyInfo.Name == "UnitName")
  622. {
  623. TextBox textBlock1 = new TextBox();
  624. textBlock1.Text = propertyInfo.GetValue(x).ToString();
  625. textBlock1.IsReadOnly = true;
  626. grid.Children.Add(textBlock1);
  627. Grid.SetRow(textBlock1, i);
  628. Grid.SetColumn(textBlock1, index + location);
  629. }
  630. else
  631. {
  632. switch (item)
  633. {
  634. case "Int32":
  635. case "String":
  636. TextBox textBox = new TextBox();
  637. if (checkbinding == null)
  638. {
  639. textBox.IsEnabled = true;
  640. }
  641. else
  642. {
  643. textBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  644. }
  645. switch (propertyInfo.Name)
  646. {
  647. case "Gas1":
  648. var data1 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas1");
  649. TextBoxMaxValue.SetMaxValue(textBox, (int)data1.Scale);
  650. break;
  651. case "Gas2":
  652. var data2 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas2");
  653. TextBoxMaxValue.SetMaxValue(textBox, (int)data2.Scale);
  654. break;
  655. case "Gas3":
  656. var data3 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas3");
  657. break;
  658. case "Gas4":
  659. var data4 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas4");
  660. break;
  661. case "Gas5":
  662. var data5 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas5");
  663. break;
  664. case "Gas6":
  665. var data6 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas6");
  666. break;
  667. case "Gas7":
  668. var data7 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas7");
  669. break;
  670. case "Gas8":
  671. var data8 = (AITMfcData)QueryDataClient.Instance.Service.GetData($"{ModuleName}.MfcGas8");
  672. break;
  673. }
  674. textBox.SetBinding(TextBox.TextProperty, binding);
  675. grid.Children.Add(textBox);
  676. Grid.SetRow(textBox, i);
  677. Grid.SetColumn(textBox, index + location);
  678. object[] objAttrs = propertyInfo.GetCustomAttributes(typeof(IsOnlyReadAttribute), true);
  679. if (objAttrs.Length > 0)
  680. {
  681. textBox.IsReadOnly = true;
  682. }
  683. break;
  684. case "Boolean":
  685. CheckBox checkBox = new CheckBox();
  686. if (checkbinding == null)
  687. {
  688. checkbinding = new Binding
  689. {
  690. Source = checkBox, // 数据源
  691. Path = new PropertyPath("IsChecked"), // 需绑定的数据源属性名
  692. Mode = BindingMode.TwoWay, // 绑定模式
  693. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  694. };
  695. }
  696. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
  697. grid.Children.Add(checkBox);
  698. Grid.SetRow(checkBox, i);
  699. Grid.SetColumn(checkBox, index + location);
  700. break;
  701. default:
  702. ComboBox comboBox = new ComboBox();
  703. comboBox.Style = (Style)recipeView.FindResource("customeComboBoxStyle");
  704. if (checkbinding == null)
  705. {
  706. comboBox.IsEnabled = true;
  707. }
  708. else
  709. {
  710. comboBox.SetBinding(TextBox.IsEnabledProperty, checkbinding);
  711. }
  712. comboBox.Background = Brushes.White;
  713. string path = propertyInfo.Name;
  714. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding);
  715. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  716. grid.Children.Add(comboBox);
  717. Grid.SetRow(comboBox, i);
  718. Grid.SetColumn(comboBox, index + location);
  719. break;
  720. }
  721. }
  722. }
  723. i++;
  724. }
  725. k++;
  726. });
  727. //index++;
  728. }
  729. }
  730. private void UpdateRecipeFileList()
  731. {
  732. XmlDocument doc = new XmlDocument();
  733. doc.LoadXml(GetXmlRecipeList());
  734. treeViewRcpList.Items.Clear();
  735. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  736. }
  737. private void LoadRecipe(Recipe recipe)
  738. {
  739. copyIndex = -1;
  740. bodyStackPanel.Children.Clear();
  741. //CurrentRecipe = null;
  742. GC.Collect(); // This should pick up the control removed at a previous MouseDown
  743. GC.WaitForPendingFinalizers(); // Doesn't help either
  744. GC.Collect();
  745. GC.WaitForPendingFinalizers(); // Doesn't help either
  746. CurrentRecipe = recipe;
  747. Grid grid = new Grid();
  748. grid.Margin = new Thickness(15);
  749. //index = 0;
  750. for (int i = 0; i < recipe.Steps.Count; i++)
  751. {
  752. RecipeStepToGridColumn(recipe.Steps[i], grid, i, false);
  753. }
  754. bodyStackPanel.Children.Add(grid);
  755. currentRecipeGrid = grid;
  756. }
  757. private void OnAddStep(int index)
  758. {
  759. if (CurrentRecipe != null)
  760. {
  761. RecipeStep recipeStep;
  762. if (copyIndex == -1)
  763. {
  764. recipeStep = new RecipeStep();
  765. recipeStep.LstUnit.Add(new PressureByPressureModeUnit());
  766. recipeStep.LstUnit.Add(new TCPUnit());
  767. recipeStep.LstUnit.Add(new BiasUnit());
  768. recipeStep.LstUnit.Add(new GasControlUnit());
  769. recipeStep.LstUnit.Add(new ESCHVUnit());
  770. recipeStep.LstUnit.Add(new ProcessKitUnit());
  771. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  772. }
  773. else
  774. {
  775. var t = JsonConvert.SerializeObject(CurrentRecipe);
  776. recipeStep = Recipe.Load(t).Steps[copyIndex - 1];
  777. CurrentRecipe.Steps.Insert(index - 1, recipeStep);
  778. }
  779. RecipeStepToGridColumn(recipeStep, currentRecipeGrid, index, true);
  780. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  781. {
  782. CurrentRecipe.Steps[i - 1].StepNo = i;
  783. }
  784. }
  785. }
  786. private void OnDeleteStep(int index)
  787. {
  788. if (CurrentRecipe != null && CurrentRecipe.Steps.Count > 1)
  789. {
  790. CurrentRecipe.Steps.RemoveAt(index);
  791. currentRecipeGrid.Children.RemoveRange(currentRecipeGrid.RowDefinitions.Count * (index + 1), currentRecipeGrid.RowDefinitions.Count);
  792. currentRecipeGrid.ColumnDefinitions.RemoveAt(index);
  793. for (int i = 1; i <= CurrentRecipe.Steps.Count; i++)
  794. {
  795. CurrentRecipe.Steps[i - 1].StepNo = i;
  796. }
  797. }
  798. }
  799. #endregion
  800. }
  801. }