RecipeEditorControlViewModel.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Xml;
  19. using Aitex.UI.RecipeEditor.Core;
  20. using Aitex.UI.RecipeEditor.View;
  21. using ExcelLibrary.SpreadSheet;
  22. using Microsoft.Win32;
  23. using Venus_Core;
  24. using Xceed.Wpf.DataGrid;
  25. namespace Aitex.UI.RecipeEditor
  26. {
  27. public class RecipeEditorControlViewModel : INotifyPropertyChanged
  28. {
  29. private ObservableCollection<RecipeModel> m_RecipeModelObservableCollection = new ObservableCollection<RecipeModel>();
  30. public ObservableCollection<RecipeModel> RecipeModelObservableCollection
  31. {
  32. get { return m_RecipeModelObservableCollection; }
  33. set { m_RecipeModelObservableCollection = value; InvokePropertyChanged("RecipeModelObservableCollection"); }
  34. }
  35. private Recipe m_currentRecipe;
  36. public Recipe CurrentRecipe
  37. {
  38. get { return m_currentRecipe; }
  39. set { m_currentRecipe = value; InvokePropertyChanged("CurrentRecipe"); }
  40. }
  41. private Visibility m_DataGridVisibility=Visibility.Collapsed;
  42. public Visibility DataGridVisibility
  43. {
  44. get { return m_DataGridVisibility; }
  45. set { m_DataGridVisibility = value; InvokePropertyChanged("DataGridVisibility"); }
  46. }
  47. public StackPanel DataGridStackPanel { get; set; }
  48. public ICommand AddStepCommand { get; set; }
  49. public ICommand DeleteStepCommand { get; set; }
  50. public List<Tuple<string, string, ObservableCollection<RecipeRow>, Guid>> UndoList { get; set; }
  51. public List<Tuple<string, string, ObservableCollection<RecipeRow>, Guid>> RedoList { get; set; }
  52. public bool IsUndoEnabled { get { return UndoList != null && UndoList.Count > 0; } }
  53. public bool IsRedoEnabled { get { return RedoList != null && RedoList.Count > 0; } }
  54. public ObservableCollection<RecipeRow> RecipeRows { get; set; }
  55. private ObservableCollection<RecipeRow> _previousRecipeRows { get; set; }
  56. private string _valueBeforeEditing { get; set; }
  57. private string _currentRecipeVariationName = string.Empty;
  58. private SmartCellData _currentEditingCellData { get; set; }
  59. public List<Tuple<int/*row no*/, int/*col no*/, string/*var name*/, string/*reason*/>> Errors { get; set; }
  60. public List<Tuple<string, string>> CreateRecipeList { get; set; }
  61. public List<Tuple<string, string>> OpenRecipeList { get; set; }
  62. public DataGrid DataGridControl { get; set; }
  63. public int CurrentRunningStepNo { get; set; }
  64. public Visibility SingleAppElementVisibility { get; set; }
  65. public Visibility RecipeInfoTextVisibility { get; set; }
  66. public ICommand ShowDetailedErrInfoCommand { get; set; }
  67. public ICommand EditRecipeInfoCommand { get; set; }
  68. //public ICommand CellContentChangedCommand { get; set; }
  69. public ICommand RightClickCommand { get; set; }
  70. public ICommand OpenLocalRecipeCommand { get; set; }
  71. public ICommand ExpandGroupCommand { get; set; }
  72. public ICommand CollapseGroupCommand { get; set; }
  73. public ICommand SaveRecipeCommand { get; set; }
  74. public ICommand RecipeHelpDocCommand { get; set; }
  75. public ICommand RecipeExport2ExcelCommand { get; set; }
  76. public ICommand ToggleHideSameCommand { get; set; }
  77. public ICommand UndoCommand { get; set; }
  78. public ICommand RedoCommand { get; set; }
  79. public ICommand SmartCellEditorLoadedCommand { get; set; }
  80. public ICommand SmartCellEditorUnloadedCommand { get; set; }
  81. public RecipeHead RecipeHead { get; set; }
  82. public bool IsBusy { get; set; }
  83. public bool IsHideSameContent { get; set; }
  84. public string RecipeInfo { get; set; }
  85. public HashSet<string> MaskedTechNames { get; set; }
  86. public HashSet<string> MaskedCatalogNames { get; set; }
  87. public event EventHandler OnSaveRecipeFile;
  88. public event EventHandler OnLocalRecipeOpened;
  89. //public event EventHandler OnLoadRecipeContent;
  90. public bool IsRecipeModified { get; set; }
  91. //public string UserName { set; get; }
  92. //private List<Tuple<string, string, string>> SourceDilteInjectRestrictions { get; set; }
  93. //private Dictionary<string, string> _preDefinedRecipeVars { get; set; }
  94. //private List<Tuple<string/*varName*/, string/*check condition*/, string/*message*/>> _validationRules { get; set; }
  95. //private List<string> _checkVariables { get; set; }
  96. //private Dictionary<string, int> RowVarNameDic { get; set; }
  97. //private Dictionary<int, List<SmartCellData>> _copiedColumnDatas = new Dictionary<int, List<SmartCellData>>();
  98. //private Dictionary<string/*CatalogName*/, Dictionary<string/*GroupName*/, List<RecipeVariableDefine/*StepList*/>>> _recipeReadingFormat;
  99. //private Dictionary<string/*ModuleName*/, List<string>/*each item's control name*/> _recipeSavingFormat;
  100. //private string _currentRecipeFormatContent;
  101. List<SolidColorBrush> solidColorBrushes = new List<SolidColorBrush>();
  102. public bool IsBarcodeEnabled
  103. {
  104. get; set;
  105. }
  106. public Visibility IsBarcodeVisibility
  107. {
  108. get
  109. {
  110. return IsBarcodeEnabled ? Visibility.Visible : Visibility.Hidden;
  111. }
  112. }
  113. private string _defaultEndPointValue { get; set; }
  114. /// <summary>
  115. /// class construction
  116. /// </summary>
  117. public RecipeEditorControlViewModel()
  118. {
  119. //UndoList = new List<Tuple<string, string, ObservableCollection<RecipeRow>, Guid>>();
  120. //RedoList = new List<Tuple<string, string, ObservableCollection<RecipeRow>, Guid>>();
  121. //if(SingleAppElementVisibility
  122. CreateRecipeList = new List<Tuple<string, string>>();
  123. OpenRecipeList = new List<Tuple<string, string>>();
  124. //foreach (var item in GetDefinedRecipeVariations())
  125. //{
  126. // CreateRecipeList.Add(new Tuple<string, string>("配置:" + item, item));
  127. // OpenRecipeList.Add(new Tuple<string, string>("转换为配置:" + item, item));
  128. //}
  129. RecipeInfoTextVisibility = Visibility.Collapsed;
  130. SingleAppElementVisibility = Visibility.Collapsed;
  131. //RightClickCommand = new DelegatedCommand((o) => true, (o) => RightClickAction());
  132. SaveRecipeCommand = new DelegatedCommand((o) => true, (o) => SaveRecipeFile());
  133. OpenLocalRecipeCommand = new DelegatedCommand((o) => true, (o) => OpenLocalRecipe());
  134. //ShowDetailedErrInfoCommand = new DelegatedCommand((o) => true, (o) => ShowDetailedErrInformation());
  135. //EditRecipeInfoCommand = new DelegatedCommand((o) => true, (o) => EditRecipeInformation());
  136. AddStepCommand = new DelegatedCommand((o) => true, (o) => OnAddStep());
  137. DeleteStepCommand = new DelegatedCommand((o) => true, (o) => OnDeleteStep());
  138. solidColorBrushes.Add(new SolidColorBrush(Colors.Aqua));
  139. solidColorBrushes.Add(new SolidColorBrush(Colors.Aquamarine));
  140. solidColorBrushes.Add(new SolidColorBrush(Colors.LawnGreen));
  141. solidColorBrushes.Add(new SolidColorBrush(Colors.LightYellow));
  142. solidColorBrushes.Add(new SolidColorBrush(Colors.DarkOrange));
  143. solidColorBrushes.Add(new SolidColorBrush(Colors.MistyRose));
  144. solidColorBrushes.Add(new SolidColorBrush(Colors.BurlyWood));
  145. solidColorBrushes.Add(new SolidColorBrush(Colors.Gainsboro));
  146. solidColorBrushes.Add(new SolidColorBrush(Colors.Beige));
  147. solidColorBrushes.Add(new SolidColorBrush(Colors.Yellow));
  148. solidColorBrushes.Add(new SolidColorBrush(Colors.Magenta));
  149. }
  150. private void OnAddStep()
  151. {
  152. if (CurrentRecipe != null)
  153. {
  154. RecipeStep recipeStep = new RecipeStep();
  155. recipeStep.LstUnit.Add(new PressureUnitByValveMode());
  156. recipeStep.LstUnit.Add(new TCPUnit());
  157. recipeStep.LstUnit.Add(new BiasUnit());
  158. recipeStep.LstUnit.Add(new GasControlUnit());
  159. recipeStep.LstUnit.Add(new ESCHVUnit());
  160. recipeStep.LstUnit.Add(new ProcessKitUnit());
  161. CurrentRecipe.Steps.Add(recipeStep);
  162. var item = RecipeStepToGrid(recipeStep);
  163. DataGridStackPanel.Children.Add(item.Item1);
  164. }
  165. }
  166. private void OnDeleteStep()
  167. {
  168. if (CurrentRecipe != null)
  169. {
  170. CurrentRecipe.Steps.RemoveAt(CurrentRecipe.Steps.Count - 1);
  171. DataGridStackPanel.Children.RemoveAt(DataGridStackPanel.Children.Count - 1);
  172. }
  173. }
  174. /// <summary>
  175. /// Open local recipe operation
  176. /// </summary>
  177. /// <param name="recipeDefineFilePath"></param>
  178. public void OpenLocalRecipe(string recipeVariationName = "")
  179. {
  180. var dlg = new OpenFileDialog();
  181. dlg.DefaultExt = ".rcp";
  182. dlg.Filter = "Recipe File (.rcp)|*.rcp|All (*.*)|*.*";
  183. if (dlg.ShowDialog() == true)
  184. {
  185. try
  186. {
  187. XmlDocument rcpDoc = new XmlDocument();
  188. rcpDoc.Load(dlg.FileName);
  189. string recipeVariation = recipeVariationName;
  190. if (string.IsNullOrEmpty(recipeVariation))
  191. {
  192. var root = rcpDoc.SelectSingleNode("/TableRecipeData") as XmlElement;
  193. recipeVariation = root.Attributes["RecipeVersion"].Value;
  194. }
  195. var startupPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  196. string dir = System.IO.Path.GetDirectoryName(startupPath);
  197. List<Aitex.Controls.RecipeCompare.Item> map = CustomXmlSerializer.Deserialize<List<Aitex.Controls.RecipeCompare.Item>>(new System.IO.FileInfo(System.IO.Path.Combine(dir, "map.xml")));
  198. Func<Aitex.Controls.RecipeCompare.Item, bool> predicate = item => item.Name.Equals(recipeVariation);
  199. System.IO.FileInfo fi = new System.IO.FileInfo(string.Format("{0}\\config\\{1}.xml", dir, map.Any(predicate) ? map.Single(predicate).No : recipeVariation));
  200. if (fi.Exists)
  201. {
  202. XmlDocument doc = new XmlDocument();
  203. doc.Load(fi.FullName);
  204. string formatXml = doc.SelectSingleNode("/Aitex/TableRecipeFormat").OuterXml;
  205. string recipeXml = rcpDoc.SelectSingleNode("/TableRecipeData").OuterXml;
  206. //LoadRecipe(formatXml, recipeXml);
  207. if (OnLocalRecipeOpened != null)
  208. OnLocalRecipeOpened.Invoke(new Tuple<string, string>(recipeVariation, dlg.FileName), null);
  209. }
  210. else
  211. {
  212. MessageBox.Show(string.Format("工艺程序版本{0}的描述文件不存在,无法正确识别该版本的工艺程序文件。", recipeVariation), "工艺程序打开失败",
  213. MessageBoxButton.OK, MessageBoxImage.Error);
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. System.Diagnostics.Debug.WriteLine(ex.Message);
  219. MessageBox.Show(string.Format("open recipe {0} failed,please confirm the recipe file is exist and valid。\r\n{1}", dlg.FileName, ex.Message), "Open recipe failed",
  220. MessageBoxButton.OK, MessageBoxImage.Error);
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// Save recipe file
  226. /// </summary>
  227. private void SaveRecipeFile()
  228. {
  229. try
  230. {
  231. if (Errors != null && Errors.Count > 0)
  232. {
  233. var ret = MessageBox.Show(string.Format("Recipe have {0} error,continue save?", Errors.Count), "Save", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
  234. if (ret != MessageBoxResult.Yes)
  235. return;
  236. }
  237. if (OnSaveRecipeFile != null)
  238. {
  239. IsRecipeModified = false;
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. System.Diagnostics.Debug.WriteLine(ex.Message);
  245. MessageBox.Show("save recipe failed!\r\n\r\n" + ex.Message, "Save", MessageBoxButton.OK, MessageBoxImage.Warning);
  246. }
  247. }
  248. /// <summary>
  249. /// Parse recipe content with recipe format
  250. /// </summary>
  251. /// <param name="recipeFormat"></param>
  252. /// <param name="recipeContent"></param>
  253. /// <param name="selectedStepNo"></param>
  254. public void LoadRecipe(Recipe recipe)
  255. {
  256. DataGridStackPanel.Children.Clear();
  257. CurrentRecipe =recipe;
  258. recipe.Steps.ToList().ForEach(x =>
  259. {
  260. var tupleValue = RecipeStepToGrid(x);
  261. DataGridStackPanel.Children.Add(tupleValue.Item1);
  262. });
  263. }
  264. private Tuple<DataGrid,List<int>> RecipeStepToDataGrid(RecipeStep recipeStep)
  265. {
  266. DataGrid dataGrid = new DataGrid();
  267. List<int> ints = new List<int>();
  268. DataTable dataTable = new DataTable();
  269. dataTable.Columns.Add("Key");
  270. dataTable.Columns.Add("Value");
  271. dataGrid.CanUserAddRows = false;
  272. dataGrid.AutoGenerateColumns = true;
  273. dataGrid.CellEditEnding += DataGrid_CellEditEnding;
  274. //dataGrid.Margin = new Thickness(10, 0, 0, 0);
  275. //dataGrid.IsReadOnly = true;
  276. dataGrid.SelectedCellsChanged += DataGrid_SelectedCellsChanged;
  277. dataGrid.SelectionUnit = DataGridSelectionUnit.Cell;
  278. //dataGrid.EnableRowVirtualization = false;
  279. //dataGrid.IsReadOnly = true;
  280. dataGrid.SetValue(System.Windows.Controls.VirtualizingStackPanel.IsVirtualizingProperty, false);
  281. Type recipeType = recipeStep.GetType();
  282. foreach (PropertyInfo propertyInfo in recipeType.GetProperties())
  283. {
  284. if (propertyInfo.Name != "LstUnit")
  285. {
  286. dataTable.Rows.Add(propertyInfo.Name, propertyInfo.GetValue(recipeStep));
  287. }
  288. }
  289. ints.Add(dataTable.Rows.Count);
  290. recipeStep.LstUnit.ToList().ForEach(k =>
  291. {
  292. //List<dynamic> dynamics = new List<dynamic>();
  293. Type type = k.GetType();
  294. ints.Add(type.GetProperties().Length);
  295. foreach (PropertyInfo propertyInfo in type.GetProperties())
  296. {
  297. var name = propertyInfo.Name;
  298. var value = propertyInfo.GetValue(k);
  299. //dynamicModel.PropertyName = name;
  300. //dynamicModel.Property = value;
  301. dataTable.Rows.Add(name, value);
  302. }
  303. });
  304. dataGrid.ItemsSource = dataTable.DefaultView;
  305. //});
  306. return new Tuple<DataGrid, List<int>>(dataGrid, ints);
  307. }
  308. private Tuple<Grid, List<int>> RecipeStepToGrid(RecipeStep recipeStep)
  309. {
  310. Grid grid = new Grid();
  311. List<int> ints = new List<int>();
  312. ColumnDefinition col1 = new ColumnDefinition();
  313. ColumnDefinition col2 = new ColumnDefinition();
  314. grid.ColumnDefinitions.Add(col1);
  315. grid.ColumnDefinitions.Add(col2);
  316. GridOptions.SetShowBorder(grid, true);
  317. GridOptions.SetLineBrush(grid, Brushes.White);
  318. GridOptions.SetLineThickness(grid, 1);
  319. grid.Margin = new Thickness(15, 0, 0, 0);
  320. Type recipeType2 = recipeStep.GetType();
  321. int i = 0;
  322. foreach (PropertyInfo propertyInfo in recipeType2.GetProperties())
  323. {
  324. if (propertyInfo.Name != "LstUnit")
  325. {
  326. RowDefinition row1 = new RowDefinition();
  327. grid.RowDefinitions.Add(row1);
  328. var item = propertyInfo.PropertyType.ToString();
  329. switch (item)
  330. {
  331. case "System.Int32":
  332. TextBox textBox = new TextBox();
  333. Binding binding = new Binding()
  334. {
  335. Source = recipeStep, // 数据源
  336. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  337. Mode = BindingMode.TwoWay, // 绑定模式
  338. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  339. };
  340. textBox.SetBinding(TextBox.TextProperty, binding);
  341. grid.Children.Add(textBox);
  342. Grid.SetRow(textBox, i);
  343. Grid.SetColumn(textBox, 1);
  344. break;
  345. case "System.Boolean":
  346. CheckBox checkBox = new CheckBox();
  347. Binding binding2 = new Binding()
  348. {
  349. Source = recipeStep, // 数据源
  350. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  351. Mode = BindingMode.TwoWay, // 绑定模式
  352. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  353. };
  354. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding2);
  355. grid.Children.Add(checkBox);
  356. Grid.SetRow(checkBox, i);
  357. Grid.SetColumn(checkBox, 1);
  358. break;
  359. default:
  360. ComboBox comboBox = new ComboBox();
  361. Binding binding3 = new Binding()
  362. {
  363. Source = recipeStep, // 数据源
  364. Path = new PropertyPath(propertyInfo.Name), // 需绑定的数据源属性名
  365. Mode = BindingMode.TwoWay, // 绑定模式
  366. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  367. };
  368. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding3);
  369. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  370. grid.Children.Add(comboBox);
  371. Grid.SetRow(comboBox, i);
  372. Grid.SetColumn(comboBox, 1);
  373. break;
  374. }
  375. TextBlock textBlock = new TextBlock();
  376. textBlock.Text = propertyInfo.Name;
  377. grid.Children.Add(textBlock);
  378. Grid.SetRow(textBlock, i);
  379. Grid.SetColumn(textBlock, 0);
  380. i++;
  381. }
  382. }
  383. ints.Add(recipeType2.GetProperties().Length);
  384. int k = 0;
  385. recipeStep.LstUnit.ToList().ForEach(x =>
  386. {
  387. Type type = x.GetType();
  388. foreach (PropertyInfo propertyInfo in type.GetProperties())
  389. {
  390. RowDefinition row1 = new RowDefinition();
  391. grid.RowDefinitions.Add(row1);
  392. var item = propertyInfo.PropertyType.ToString();
  393. if (propertyInfo.Name == "UnitName")
  394. {
  395. TextBlock textBlock1= new TextBlock();
  396. Binding binding = new Binding()
  397. {
  398. Source = recipeStep, // 数据源
  399. Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  400. Mode = BindingMode.TwoWay, // 绑定模式
  401. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  402. };
  403. textBlock1.SetBinding(TextBlock.TextProperty, binding);
  404. grid.Children.Add(textBlock1);
  405. Grid.SetRow(textBlock1, i);
  406. Grid.SetColumn(textBlock1, 1);
  407. }
  408. else
  409. {
  410. switch (item)
  411. {
  412. case "System.Int32":
  413. TextBox textBox = new TextBox();
  414. Binding binding = new Binding()
  415. {
  416. Source = recipeStep, // 数据源
  417. Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  418. Mode = BindingMode.TwoWay, // 绑定模式
  419. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  420. };
  421. textBox.SetBinding(TextBox.TextProperty, binding);
  422. grid.Children.Add(textBox);
  423. Grid.SetRow(textBox, i);
  424. Grid.SetColumn(textBox, 1);
  425. break;
  426. case "System.Boolean":
  427. CheckBox checkBox = new CheckBox();
  428. Binding binding2 = new Binding()
  429. {
  430. Source = recipeStep, // 数据源
  431. Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  432. Mode = BindingMode.TwoWay, // 绑定模式
  433. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  434. };
  435. checkBox.SetBinding(CheckBox.IsCheckedProperty, binding2);
  436. grid.Children.Add(checkBox);
  437. Grid.SetRow(checkBox, i);
  438. Grid.SetColumn(checkBox, 1);
  439. break;
  440. default:
  441. ComboBox comboBox = new ComboBox();
  442. string path= propertyInfo.Name;
  443. Binding binding3 = new Binding()
  444. {
  445. Source = recipeStep.LstUnit[k], // 数据源
  446. Path = new PropertyPath(path), // 需绑定的数据源属性名
  447. Mode = BindingMode.TwoWay, // 绑定模式
  448. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  449. };
  450. comboBox.SetBinding(ComboBox.SelectedItemProperty, binding3);
  451. ItemsControlHelper.SetEnumValuesToItemsSource(comboBox, true);
  452. grid.Children.Add(comboBox);
  453. Grid.SetRow(comboBox, i);
  454. Grid.SetColumn(comboBox, 1);
  455. break;
  456. }
  457. }
  458. //TextBox textBox = new TextBox();
  459. ////textBox.Text = propertyInfo.GetValue(x).ToString();
  460. //Binding binding = new Binding()
  461. //{
  462. // Source = recipeStep, // 数据源
  463. // Path = new PropertyPath($"LstUnit[{k}]." + propertyInfo.Name), // 需绑定的数据源属性名
  464. // Mode = BindingMode.TwoWay, // 绑定模式
  465. // UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged //触发器
  466. //};
  467. //textBox.SetBinding(TextBox.TextProperty, binding);
  468. //grid.Children.Add(textBox);
  469. TextBlock textBlock = new TextBlock();
  470. textBlock.Text = propertyInfo.Name;
  471. grid.Children.Add(textBlock);
  472. Grid.SetRow(textBlock, i);
  473. Grid.SetColumn(textBlock, 0);
  474. i++;
  475. }
  476. ints.Add(type.GetProperties().Length);
  477. k++;
  478. });
  479. return new Tuple<Grid, List<int>>(grid, ints);
  480. }
  481. private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
  482. {
  483. //string prop1 = (e.Row.Item as DataRowView).Row[1].ToString();
  484. string prop1=(e.Row.Item as DataRowView).Row[0].ToString();
  485. string newValue = (e.EditingElement as TextBox).Text;
  486. }
  487. private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
  488. {
  489. foreach (DataGridCellInfo cell in e.AddedCells)
  490. {
  491. if (cell.Column.Header.ToString() == "Key")
  492. {
  493. (sender as DataGrid).SelectedCells.Remove(cell);
  494. }
  495. }
  496. }
  497. //private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
  498. //{
  499. // var dg = sender as DataGrid;
  500. // foreach (DataGridCellInfo info in dg.SelectedCells)
  501. // {
  502. // FrameworkElement element = info.Column.GetCellContent(info.Item);
  503. // //((TextBlock)info.Column.GetCellContent(info.Item)).Text = "123";
  504. // }
  505. //}
  506. private void ChangeDataGridRowColor(DataGrid dataGrid,List<int> ints)
  507. {
  508. int count = 0;
  509. for (int i = 0; i < ints.Count; i++)
  510. {
  511. for (int j = 0; j < ints[i]; j++)
  512. {
  513. dataGrid.UpdateLayout();
  514. int index = count + j;
  515. DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  516. row.Background = solidColorBrushes[i];
  517. }
  518. count += ints[i];
  519. }
  520. }
  521. private void ChangeGridRowColor(Grid grid, List<int> ints)
  522. {
  523. int count = 0;
  524. for (int i = 0; i < ints.Count; i++)
  525. {
  526. for (int j = 0; j < ints[i]; j++)
  527. {
  528. //dataGrid.UpdateLayout();
  529. int index = count + j;
  530. //DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGrid.Items[index]) as DataGridRow;
  531. //grid.RowDefinitions[index]
  532. }
  533. count += ints[i];
  534. }
  535. }
  536. /// <summary>
  537. /// calc recipe total time
  538. /// </summary>
  539. private void CalcRecipeTime()
  540. {
  541. int timeStepRowId = -1;
  542. int loopStepRowId = -1;
  543. for (int i = 0; i < RecipeRows.Count; i++)
  544. {
  545. if (RecipeRows[i].TechnicalName == "Time")
  546. timeStepRowId = i;
  547. if (RecipeRows[i].TechnicalName == "Loop")
  548. loopStepRowId = i;
  549. if (loopStepRowId != -1 && timeStepRowId != -1)
  550. break;
  551. }
  552. TimeSpan tspan = new TimeSpan();
  553. for (int stepNo = 0; stepNo < RecipeRows[timeStepRowId].RecipeItems.Count; stepNo++)
  554. {
  555. string loopStr = RecipeRows[loopStepRowId].RecipeItems[stepNo].Value;
  556. bool isLoopStart = Regex.IsMatch(loopStr, @"^Loop\x20\d+$");
  557. if (isLoopStart)
  558. {
  559. int loopNum = int.Parse(loopStr.ToLower().Replace("loop", "").Replace(" ", ""));
  560. TimeSpan ts = new TimeSpan();
  561. for (int innerStepNo = stepNo; innerStepNo < RecipeRows[timeStepRowId].RecipeItems.Count; innerStepNo++)
  562. {
  563. loopStr = RecipeRows[loopStepRowId].RecipeItems[innerStepNo].Value;
  564. stepNo = innerStepNo;
  565. string timeDuration = RecipeRows[timeStepRowId].RecipeItems[innerStepNo].Value;
  566. string[] timeArr = timeDuration.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  567. if (timeArr.Length == 3)
  568. {
  569. int h, mi, s;
  570. if (int.TryParse(timeArr[0], out h) && int.TryParse(timeArr[1], out mi) && int.TryParse(timeArr[2], out s))
  571. {
  572. var tt = new TimeSpan(h, mi, s);
  573. ts += tt;
  574. }
  575. }
  576. bool isLoopEnd = Regex.IsMatch(loopStr, @"^Loop End$");
  577. if (isLoopEnd)
  578. {
  579. tspan += new TimeSpan(ts.Ticks * loopNum);
  580. break;
  581. }
  582. }
  583. }
  584. else
  585. {
  586. string timeDuration = RecipeRows[timeStepRowId].RecipeItems[stepNo].Value;
  587. string[] timeArr = timeDuration.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  588. if (timeArr.Length == 3)
  589. {
  590. int h, mi, s;
  591. if (int.TryParse(timeArr[0], out h) && int.TryParse(timeArr[1], out mi) && int.TryParse(timeArr[2], out s))
  592. {
  593. var ts = new TimeSpan(h, mi, s);
  594. tspan += ts;
  595. }
  596. }
  597. }
  598. }
  599. RecipeInfo = string.Format("共{0}步,总时间{1}:{2}:{3}", RecipeRows[0].RecipeItems.Count, (int)tspan.TotalHours, tspan.Minutes.ToString("00"), tspan.Seconds.ToString("00"));
  600. InvokePropertyChanged("RecipeInfo");
  601. }
  602. #region INotifyPropertyChanged
  603. public event PropertyChangedEventHandler PropertyChanged;
  604. public void InvokePropertyChanged(string propertyName)
  605. {
  606. if (PropertyChanged != null)
  607. {
  608. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  609. }
  610. }
  611. #endregion INotifyPropertyChanged
  612. }
  613. public class RecipeRow
  614. {
  615. private ObservableCollection<SmartCellData> _recipeItems = new ObservableCollection<SmartCellData>();
  616. public RecipeRow(params SmartCellData[] vars)
  617. {
  618. foreach (var var in vars)
  619. _recipeItems.Add(var);
  620. }
  621. public string CatalogName { get; set; }
  622. public string FriendlyName { get; set; }
  623. public string TechnicalName { get; set; }
  624. public ObservableCollection<SmartCellData> RecipeItems
  625. {
  626. get { return _recipeItems; }
  627. set { _recipeItems = value; }
  628. }
  629. }
  630. /// <summary>
  631. /// Recipe head
  632. /// </summary>
  633. public class RecipeHead
  634. {
  635. public string RecipeVariation { get; set; }
  636. public string CreationTime { get; set; }
  637. public string LastRevisionTime { get; set; }
  638. public string CreatedBy { get; set; }
  639. public string LastModifiedBy { get; set; }
  640. public string PressureMode { get; set; }
  641. public string Description { get; set; }
  642. public string BasePressure
  643. {
  644. get; set;
  645. }
  646. public string PumpDownLimit
  647. {
  648. get; set;
  649. }
  650. public string PurgeActive
  651. {
  652. get; set;
  653. }
  654. public string NotToPurgeOrVent
  655. {
  656. get; set;
  657. }
  658. public string Barcode
  659. {
  660. get; set;
  661. }
  662. public string SubstrateTemp
  663. {
  664. get; set;
  665. }
  666. public string PumpingPinState
  667. {
  668. get; set;
  669. }
  670. public string VentingPinState
  671. {
  672. get; set;
  673. }
  674. public string PinDownPressure
  675. {
  676. get; set;
  677. }
  678. }
  679. public class EndPointConfigItem : INotifyPropertyChanged
  680. {
  681. public event PropertyChangedEventHandler PropertyChanged;
  682. public void InvokePropertyChanged(string propertyName)
  683. {
  684. if (PropertyChanged != null)
  685. {
  686. PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
  687. }
  688. }
  689. public string ExposureTime { get; set; }
  690. public string WaveLengthA { get; set; }
  691. public string BinningA { get; set; }
  692. public string WaveLengthB { get; set; }
  693. public string BinningB { get; set; }
  694. public string WaveLengthC { get; set; }
  695. public string BinningC { get; set; }
  696. public string WaveLengthD { get; set; }
  697. public string BinningD { get; set; }
  698. public string Fd { get; set; }
  699. public string PrefilterTime { get; set; }
  700. public string PostfilterTime { get; set; }
  701. public string AlgorithmType { get; set; }
  702. public string Criteria { get; set; }
  703. public string DelayTime { get; set; }
  704. public string ValidationTime { get; set; }
  705. public string ValidationValue { get; set; }
  706. public string TimeWindow { get; set; }
  707. public string MinimalTime { get; set; }
  708. public string PostponeTime { get; set; }
  709. public bool Control { get; set; }
  710. public bool Normalization { get; set; }
  711. public bool EnablePostponePercent { get; set; }
  712. public bool EnableCriterialPercent { get; set; }
  713. public bool EnableEventTrigger { get; set; }
  714. public bool IsFaultIfNoTrigger { get; set; }
  715. public string ToValue()
  716. {
  717. return
  718. $@"ExposureTime={ExposureTime};WaveLengthA={WaveLengthA};BinningA={BinningA};WaveLengthB={WaveLengthB};" +
  719. $@"BinningB={BinningB};WaveLengthC={WaveLengthC};BinningC={BinningC};WaveLengthD={WaveLengthD};BinningD={BinningD};Fd={Fd};" +
  720. $@"PrefilterTime={PrefilterTime};PostfilterTime={PostfilterTime};AlgorithmType={AlgorithmType};Criteria={Criteria};DelayTime={DelayTime};ValidationTime={ValidationTime};" +
  721. $@"ValidationValue={ValidationValue};TimeWindow={TimeWindow};MinimalTime={MinimalTime};PostponeTime={PostponeTime};Control={Control};Normalization={Normalization};" +
  722. $@"EnablePostponePercent={EnablePostponePercent};EnableCriterialPercent={EnableCriterialPercent};EnableEventTrigger={EnableEventTrigger};IsFaultIfNoTrigger={IsFaultIfNoTrigger};"
  723. ;
  724. }
  725. public void SetValue(string config)
  726. {
  727. if (string.IsNullOrEmpty(config))
  728. return;
  729. string[] items = config.Split(';');
  730. foreach (var item in items)
  731. {
  732. if (string.IsNullOrEmpty(item))
  733. continue;
  734. string[] pairs = item.Split('=');
  735. if (pairs.Length != 2)
  736. continue;
  737. Parallel.ForEach(this.GetType().GetProperties(),
  738. property =>
  739. {
  740. PropertyInfo pi = (PropertyInfo) property;
  741. if (pi.Name == pairs[0])
  742. {
  743. try
  744. {
  745. var convertedValue = Convert.ChangeType(pairs[1], pi.PropertyType);
  746. var originValue = Convert.ChangeType(pi.GetValue(this, null), pi.PropertyType);
  747. if (originValue != convertedValue)
  748. {
  749. pi.SetValue(this, convertedValue, null);
  750. }
  751. }
  752. catch (Exception)
  753. {
  754. }
  755. }
  756. });
  757. }
  758. }
  759. }
  760. }