RecipeViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.UI.View.Common;
  3. using Aitex.UI.RecipeEditor;
  4. using Aitex.UI.RecipeEditor.View;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.Equipment;
  7. using Microsoft.VisualBasic;
  8. using Microsoft.Win32;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Media;
  22. using System.Xml;
  23. using Venus_Core;
  24. using Venus_MainPages.PMs;
  25. using Venus_MainPages.Views;
  26. using WPF.Themes.UserControls;
  27. namespace Venus_MainPages.ViewModels
  28. {
  29. internal class RecipeViewModel : BindableBase
  30. {
  31. #region 私有字段
  32. private string m_Title;
  33. public string ModuleName = "PMA";
  34. private string m_CurrentRecipeName;
  35. private UiRecipeManager m_uiRecipeManager = new UiRecipeManager();
  36. private RecipeView recipeView;
  37. private TreeView treeViewRcpList;
  38. private RecipeEditorControl tableRecipeGrid;
  39. private Recipe m_currentRecipe;
  40. private string m_recipeType;
  41. private bool firstLoad=true;
  42. #endregion
  43. #region 属性
  44. public string Title
  45. {
  46. get { return m_Title; }
  47. set { SetProperty(ref m_Title, value); }
  48. }
  49. public string CurrentRecipeName
  50. {
  51. get { return m_CurrentRecipeName; }
  52. set { SetProperty(ref m_CurrentRecipeName, value); }
  53. }
  54. public Recipe CurrentRecipe
  55. {
  56. get { return m_currentRecipe; }
  57. set
  58. {
  59. m_currentRecipe = value;
  60. RecipeType = m_currentRecipe.Header.Type.ToString();
  61. }
  62. }
  63. public string RecipeType
  64. {
  65. get { return m_recipeType; }
  66. set { SetProperty(ref m_recipeType, value); }
  67. }
  68. #endregion
  69. #region 命令
  70. private DelegateCommand _NewCommand;
  71. public DelegateCommand NewCommand =>
  72. _NewCommand ?? (_NewCommand = new DelegateCommand(OnNew));
  73. private DelegateCommand _ReNameCommand;
  74. public DelegateCommand ReNameCommand =>
  75. _ReNameCommand ?? (_ReNameCommand = new DelegateCommand(OnReName));
  76. private DelegateCommand _DeleteCommand;
  77. public DelegateCommand DeleteCommand =>
  78. _DeleteCommand ?? (_DeleteCommand = new DelegateCommand(OnDelete));
  79. private DelegateCommand<Object> _MouseRightButtonDownCommand;
  80. public DelegateCommand<Object> MouseRightButtonDownCommand =>
  81. _MouseRightButtonDownCommand ?? (_MouseRightButtonDownCommand = new DelegateCommand<Object>(OnMouseRightButtonDown));
  82. private DelegateCommand<Object> _LoadedCommand;
  83. public DelegateCommand<Object> LoadedCommand =>
  84. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand<Object>(OnLoaded));
  85. private DelegateCommand _SaveRecipeCommand;
  86. public DelegateCommand SaveRecipeCommand =>
  87. _SaveRecipeCommand ?? (_SaveRecipeCommand = new DelegateCommand(OnSaveRecipe));
  88. private DelegateCommand _AddStepCommand;
  89. public DelegateCommand AddStepCommand =>
  90. _AddStepCommand ?? (_AddStepCommand = new DelegateCommand(OnAddStep));
  91. private DelegateCommand _DeleteStepCommand;
  92. public DelegateCommand DeleteStepCommand =>
  93. _DeleteStepCommand ?? (_DeleteStepCommand = new DelegateCommand(OnDeleteStep));
  94. //private DelegateCommand _ReLoadCommand;
  95. //public DelegateCommand ReLoadCommand =>
  96. // _ReLoadCommand ?? (_ReLoadCommand = new DelegateCommand(OnReLoad));
  97. #endregion
  98. #region 构造函数
  99. public RecipeViewModel()
  100. {
  101. Title = "配方管理";
  102. }
  103. #endregion
  104. #region 命令方法
  105. private void OnNew()
  106. {
  107. }
  108. private void OnReName()
  109. {
  110. }
  111. private void OnDelete()
  112. {
  113. }
  114. private void OnAddStep()
  115. {
  116. this.tableRecipeGrid.ControlViewModel.OnAddStep(CurrentRecipe.Steps.Count+1);
  117. }
  118. private void OnDeleteStep()
  119. {
  120. this.tableRecipeGrid.ControlViewModel.OnDeleteStep(CurrentRecipe.Steps.Count-1);
  121. }
  122. private void OnReLoad()
  123. {
  124. this.tableRecipeGrid.ControlViewModel.LoadRecipe(CurrentRecipe);
  125. }
  126. private void OnSaveRecipe()
  127. {
  128. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  129. }
  130. private void OnLoaded(Object myrecipeView)
  131. {
  132. if (firstLoad == true)
  133. {
  134. firstLoad = false;
  135. recipeView = myrecipeView as RecipeView;
  136. treeViewRcpList = recipeView.treeViewRcpList;
  137. tableRecipeGrid = recipeView.tableRecipeGrid;
  138. UpdateRecipeFileList();
  139. treeViewRcpList.SelectedItemChanged += TreeViewRcpList_SelectedItemChanged;
  140. }
  141. }
  142. TreeViewFileItem selectedItem;
  143. private void TreeViewRcpList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  144. {
  145. selectedItem = e.NewValue as TreeViewFileItem;
  146. if (selectedItem == null)
  147. return;
  148. try
  149. {
  150. CurrentRecipeName = selectedItem.FileName;
  151. string xmlRecipeData = m_uiRecipeManager.LoadRecipe(ModuleName, selectedItem.FileName);
  152. if (xmlRecipeData == "")
  153. {
  154. treeViewRcpList.Items.Remove(selectedItem);
  155. return;
  156. }
  157. CurrentRecipe = Recipe.Load(xmlRecipeData);
  158. this.tableRecipeGrid.ControlViewModel.LoadRecipe(CurrentRecipe);
  159. }
  160. catch (Exception ex)
  161. {
  162. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  163. }
  164. e.Handled = true;
  165. }
  166. /// <summary>
  167. /// Load recipe data by recipe name
  168. /// </summary>
  169. /// <param name="receipeName"></param>
  170. /// <returns></returns>
  171. public string LoadRecipe(string receipeName)
  172. {
  173. return m_uiRecipeManager.LoadRecipe(ModuleName, receipeName);
  174. }
  175. /// <summary>
  176. /// Save recipe by recipe name and recipe data
  177. /// </summary>
  178. /// <param name="recipeName"></param>
  179. /// <param name="recipe"></param>
  180. /// <returns></returns>
  181. public bool SaveRecipe(string recipeName, string recipeContent)
  182. {
  183. return m_uiRecipeManager.SaveRecipe(ModuleName, recipeName, recipeContent);
  184. }
  185. /// <summary>
  186. /// 右击TreeView,上下文菜单选择功能
  187. /// </summary>
  188. /// <param name="treeView"></param>
  189. private void OnMouseRightButtonDown(Object treeView)
  190. {
  191. //treeViewRcpList = treeView as TreeView;
  192. treeViewRcpList.ContextMenu = new ContextMenu() { Background = new SolidColorBrush(Colors.AliceBlue)};
  193. MenuItem menuItem = new MenuItem();
  194. menuItem = new MenuItem();
  195. menuItem.Tag = "\\";
  196. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_CreateRecipe);
  197. menuItem.Header = "New Recipe";
  198. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  199. menuItem = new MenuItem();
  200. menuItem.Tag = "\\";
  201. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_DeleteRecipe);
  202. menuItem.Header = "Delete Recipe";
  203. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  204. menuItem = new MenuItem();
  205. menuItem.Tag = "\\";
  206. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_SaveAsRecipe);
  207. menuItem.Header = "Save As Recipe";
  208. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  209. menuItem = new MenuItem();
  210. menuItem.Tag = "\\";
  211. menuItem.Click += new RoutedEventHandler(menuItem_MouseClick_RenameRecipe);
  212. menuItem.Header = "Rename";
  213. treeViewRcpList.ContextMenu.Items.Add(menuItem);
  214. treeViewRcpList.ContextMenu.Visibility = Visibility.Visible;
  215. }
  216. /// <summary>
  217. /// 创建新的配方
  218. /// </summary>
  219. /// <param name="sender"></param>
  220. /// <param name="e"></param>
  221. private void menuItem_MouseClick_CreateRecipe(object sender, RoutedEventArgs e)
  222. {
  223. MenuItem mit = sender as MenuItem;
  224. string folderName = mit.Tag as string;
  225. PerformCreateRecipe(folderName);
  226. }
  227. private void PerformCreateRecipe(string folderName)
  228. {
  229. try
  230. {
  231. RecipeNameInputDlg dlg = new RecipeNameInputDlg(Application.Current.Resources["GlobalLableMsgInputRecipeName"].ToString())
  232. {
  233. Owner = Application.Current.MainWindow
  234. };
  235. if (dlg.ShowDialog() == true)
  236. {
  237. var recipeName = folderName + "\\" + dlg.InputText;
  238. RecipeType type = (RecipeType)Enum.Parse(typeof(RecipeType), dlg.SelectedType);
  239. string newRecipe =RecipeUnity.CreateRecipe(type, dlg.InputText);
  240. //string recipeContent = m_uiRecipeManager.GetRecipeTemplate(ModuleName);
  241. //m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, m_uiRecipeManager.LoadRecipe("system",folderName));
  242. if (SaveAsRecipe(recipeName, newRecipe))
  243. {
  244. //UpdateRecipeFileList();
  245. //SelectRecipe(recipeName);
  246. treeViewRcpList.Items.Add(new TreeViewFileItem(dlg.InputText));
  247. }
  248. else
  249. {
  250. WPFMessageBox.ShowError("Error");
  251. }
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. LOG.WriteExeption(ex);
  257. MessageBox.Show(string.Format(Application.Current.Resources["GlobalLableMsgRecipeCreateException"].ToString(), ex.Message), Application.Current.Resources["GlobalLableTitleRecipeEditor"].ToString(), MessageBoxButton.OK, MessageBoxImage.Error);
  258. }
  259. }
  260. /// <summary>
  261. /// 删除配方
  262. /// </summary>
  263. /// <param name="sender"></param>
  264. /// <param name="e"></param>
  265. private void menuItem_MouseClick_DeleteRecipe(object sender, RoutedEventArgs e)
  266. {
  267. if (CurrentRecipe == null)
  268. {
  269. return;
  270. }
  271. if (WPFMessageBox.ShowQuestion($"Delete {CurrentRecipeName}?","删除后无法恢复!!!") == MessageBoxResult.Yes)
  272. {
  273. MenuItem mit = sender as MenuItem;
  274. //string recipename = mit.Header.ToString();
  275. m_uiRecipeManager.DeleteRecipe(ModuleName, CurrentRecipeName);
  276. //PerformCreateRecipe(folderName);
  277. treeViewRcpList.Items.Remove(selectedItem);
  278. }
  279. }
  280. /// <summary>
  281. /// 另存为配方
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private void menuItem_MouseClick_SaveAsRecipe(object sender, RoutedEventArgs e)
  286. {
  287. if (CurrentRecipe == null)
  288. {
  289. return;
  290. }
  291. //var dlg = new SaveFileDialog()
  292. //{
  293. // Title = "Save As",
  294. // DefaultExt = "rcp",
  295. // Filter = "rcp files (*.rcp)|*.rcp",
  296. //};
  297. //if (dlg.ShowDialog() == true)
  298. //{
  299. // var newRecipe = CurrentRecipe;
  300. // newRecipe.Header.Name = Path.GetFileNameWithoutExtension( dlg.FileName);
  301. // newRecipe.Header.CreateTime = DateTime.Now.ToString();
  302. // File.WriteAllText(dlg.FileName, RecipeUnity.RecipeToString(newRecipe));
  303. //}
  304. var newName = Interaction.InputBox("Save As Recipe", "", CurrentRecipeName, -1, -1);
  305. var newRecipe = CurrentRecipe;
  306. newRecipe.Header.Name = newName;
  307. newRecipe.Header.CreateTime = DateTime.Now.ToString();
  308. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  309. File.WriteAllText(newrecipePath, RecipeUnity.RecipeToString(newRecipe));
  310. UpdateRecipeFileList();
  311. }
  312. private void menuItem_MouseClick_RenameRecipe(object sender, RoutedEventArgs e)
  313. {
  314. if (CurrentRecipe == null)
  315. {
  316. return;
  317. }
  318. var newName= Interaction.InputBox("Rename Recipe", "", CurrentRecipeName, -1, -1);
  319. if (newName != CurrentRecipeName && newName!="")
  320. {
  321. var oldrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, CurrentRecipeName + ".rcp");
  322. var newrecipePath = Path.Combine(QueryDataClient.Instance.Service.GetData("GetRTPath").ToString(), "Recipes", ModuleName, newName + ".rcp");
  323. CurrentRecipe.Header.Name = newName;
  324. SaveRecipe(CurrentRecipeName, RecipeUnity.RecipeToString(CurrentRecipe));
  325. File.Move(oldrecipePath, newrecipePath);
  326. UpdateRecipeFileList();
  327. }
  328. }
  329. /// <summary>
  330. /// SaveAs recipe by recipe name and recipe data
  331. /// </summary>
  332. /// <param name="recipeName"></param>
  333. /// <param name="recipe"></param>
  334. /// <returns></returns>
  335. public bool SaveAsRecipe(string recipeName, string recipeContent)
  336. {
  337. return m_uiRecipeManager.SaveAsRecipe(ModuleName, recipeName, recipeContent);
  338. }
  339. /// <summary>
  340. /// Recipe template
  341. /// </summary>
  342. public string RecipeTemplate
  343. {
  344. get
  345. {
  346. string template = m_uiRecipeManager.GetRecipeTemplate(ModuleName);
  347. return template;
  348. }
  349. }
  350. private void UpdateRecipeFileList()
  351. {
  352. XmlDocument doc = new XmlDocument();
  353. doc.LoadXml(GetXmlRecipeList());
  354. treeViewRcpList.Items.Clear();
  355. CreateTreeViewItems(doc.DocumentElement, this.treeViewRcpList);
  356. }
  357. /// <summary>
  358. /// Get recipe list in xml format
  359. /// </summary>
  360. /// <returns></returns>
  361. public string GetXmlRecipeList()
  362. {
  363. return m_uiRecipeManager.GetXmlRecipeList(ModuleName, true) ?? "";
  364. }
  365. void CreateTreeViewItems(XmlElement curElementNode, ItemsControl itemsControl)
  366. {
  367. foreach (XmlElement ele in curElementNode.ChildNodes)
  368. {
  369. if (ele.Name == "File")
  370. {
  371. string fileName = ele.Attributes["Name"].Value;
  372. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  373. TreeViewFileItem item = new TreeViewFileItem(ele.Attributes["Name"].Value);
  374. item.Tag = ele.Attributes["Name"].Value;
  375. //item.ToolTip = fileName;
  376. itemsControl.Items.Add(item);
  377. }
  378. else if (ele.Name == "Folder")
  379. {
  380. string folderName = ele.Attributes["Name"].Value;
  381. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  382. TreeViewFolderItem item = new TreeViewFolderItem(folderName);
  383. item.Tag = ele.Attributes["Name"].Value;
  384. CreateTreeViewItems(ele, item);
  385. item.IsExpanded = false;
  386. itemsControl.Items.Add(item);
  387. }
  388. }
  389. }
  390. #endregion
  391. }
  392. }