SequenceData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using Caliburn.Micro.Core;
  2. using Prism.Mvvm;
  3. using RecipeEditorLib.DGExtension.CustomColumn;
  4. using RecipeEditorLib.RecipeModel.Params;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Xml;
  12. namespace CyberX8_MainPages.Sequence
  13. {
  14. public class SequenceData : BindableBase
  15. {
  16. public SequenceData()
  17. {
  18. this.Steps = new ObservableCollection<ObservableCollection<Param>>();
  19. }
  20. public ObservableCollection<ObservableCollection<Param>> Steps { get; set; }
  21. public ObservableCollection<Param> CreateStep(ObservableCollection<EditorDataGridTemplateColumnBase> _columns, XmlNode step = null)
  22. {
  23. PositionParam posParam = null;
  24. string posValue = string.Empty;
  25. ObservableCollection<Param> Params = new ObservableCollection<Param>();
  26. foreach (EditorDataGridTemplateColumnBase col in _columns)
  27. {
  28. Param parm = null;
  29. string value = string.Empty;
  30. if (!(col is ExpanderColumn) && step != null && !(col is StepColumn))
  31. {
  32. if (step.Attributes[col.ControlName] != null)
  33. value = step.Attributes[col.ControlName].Value;
  34. }
  35. if (col is StepColumn)
  36. {
  37. parm = new StepParam()
  38. {
  39. Name = col.ControlName
  40. };
  41. }
  42. else if (col is TextBoxColumn)
  43. {
  44. parm = new StringParam()
  45. {
  46. Name = col.ControlName,
  47. Value = value,
  48. IsEnabled = !col.IsReadOnly
  49. };
  50. }
  51. else if (col is NumColumn)
  52. {
  53. if (!string.IsNullOrEmpty(value))
  54. parm = new IntParam() { Name = col.ControlName, Value = int.Parse(value), IsEnabled = !col.IsReadOnly };
  55. else
  56. parm = new IntParam() { Name = col.ControlName, Value = 0, IsEnabled = !col.IsReadOnly };
  57. }
  58. else if (col is ComboxColumn)
  59. {
  60. parm = new ComboxParam()
  61. {
  62. Name = col.ControlName,
  63. Value = value,
  64. Options = ((ComboxColumn)col).Options,
  65. IsEnabled = !col.IsReadOnly
  66. };
  67. }
  68. else if (col is PositionColumn)
  69. {
  70. posParam = new PositionParam() { Name = col.ControlName, Options = ((PositionColumn)col).Options, IsEnabled = !col.IsReadOnly };
  71. parm = posParam;
  72. posValue = value;
  73. }
  74. else if (col is ExpanderColumn)
  75. {
  76. parm = new ExpanderParam() { };
  77. }
  78. else if (col is RecipeSelectColumn)
  79. {
  80. //string path = ((RecipeSelectColumn) col).RecipeProcessType;
  81. //if (!string.IsNullOrEmpty(path) && value.StartsWith(path))
  82. //{
  83. // value = value.Substring(path.Length + 1);
  84. //}
  85. parm = new PathFileParam()
  86. {
  87. Name = col.ControlName,
  88. Value = value,
  89. IsEnabled = !col.IsReadOnly,
  90. PrefixPath = ((RecipeSelectColumn)col).RecipeProcessType
  91. };
  92. }
  93. else if (col is MultipleSelectColumn)
  94. {
  95. parm = new MultipleSelectParam((MultipleSelectColumn)col) { Name = col.ControlName, IsEnabled = !col.IsReadOnly };
  96. string[] opts = value.Split(',');
  97. ((MultipleSelectParam)parm).Options.Apply(opt => {
  98. for (var index = 0; index < opts.Length; index++)
  99. {
  100. if (opt.ControlName == opts[index])
  101. {
  102. opt.IsChecked = true;
  103. break;
  104. }
  105. }
  106. });
  107. parm.IsSaved = true;
  108. }
  109. parm.Parent = Params;
  110. Params.Add(parm);
  111. }
  112. posParam.Value = posValue;
  113. return Params;
  114. }
  115. public ObservableCollection<Param> CloneStep(ObservableCollection<EditorDataGridTemplateColumnBase> _columns, ObservableCollection<Param> _sourceParams)
  116. {
  117. ObservableCollection<Param> targetParams = this.CreateStep(_columns);
  118. PositionParam posParam = null;
  119. string posValue = string.Empty;
  120. for (var index = 0; index < _sourceParams.Count; index++)
  121. {
  122. if (_sourceParams[index] is PositionParam)
  123. {
  124. posParam = (PositionParam)targetParams[index];
  125. posValue = ((PositionParam)_sourceParams[index]).Value;
  126. }
  127. else if (_sourceParams[index] is StringParam)
  128. {
  129. ((StringParam)targetParams[index]).Value = ((StringParam)_sourceParams[index]).Value;
  130. }
  131. else if (_sourceParams[index] is PathFileParam)
  132. {
  133. ((PathFileParam)targetParams[index]).Value = ((PathFileParam)_sourceParams[index]).Value;
  134. ((PathFileParam)targetParams[index]).PrefixPath = ((PathFileParam)_sourceParams[index]).PrefixPath;
  135. }
  136. else if (_sourceParams[index] is IntParam)
  137. {
  138. ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value;
  139. }
  140. else if (_sourceParams[index] is ComboxParam)
  141. {
  142. ((ComboxParam)targetParams[index]).Value = ((ComboxParam)_sourceParams[index]).Value;
  143. }
  144. else if (_sourceParams[index] is BoolParam)
  145. {
  146. ((BoolParam)targetParams[index]).Value = ((BoolParam)_sourceParams[index]).Value;
  147. }
  148. else if (_sourceParams[index] is IntParam)
  149. {
  150. ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value;
  151. }
  152. else if (_sourceParams[index] is DoubleParam)
  153. {
  154. ((DoubleParam)targetParams[index]).Value = ((DoubleParam)_sourceParams[index]).Value;
  155. }
  156. targetParams[index].Parent = targetParams;
  157. }
  158. posParam.Value = posValue;
  159. return targetParams;
  160. }
  161. public void LoadSteps(ObservableCollection<EditorDataGridTemplateColumnBase> _columns, XmlNodeList _steps)
  162. {
  163. foreach (XmlNode _step in _steps)
  164. {
  165. this.Steps.Add(this.CreateStep(_columns, _step));
  166. }
  167. }
  168. public void Load(ObservableCollection<EditorDataGridTemplateColumnBase> _columns, XmlDocument doc)
  169. {
  170. XmlNode node = doc.SelectSingleNode("Aitex/TableSequenceData");
  171. if (node.Attributes["CreatedBy"] != null)
  172. this.Creator = node.Attributes["CreatedBy"].Value;
  173. if (node.Attributes["CreationTime"] != null)
  174. this.CreateTime = DateTime.Parse(node.Attributes["CreationTime"].Value);
  175. if (node.Attributes["LastRevisedBy"] != null)
  176. this.Revisor = node.Attributes["LastRevisedBy"].Value;
  177. if (node.Attributes["LastRevisionTime"] != null)
  178. this.ReviseTime = DateTime.Parse(node.Attributes["LastRevisionTime"].Value);
  179. if (node.Attributes["Description"] != null)
  180. this.Description = node.Attributes["Description"].Value;
  181. foreach (XmlNode _step in doc.SelectNodes("Aitex/TableSequenceData/Step"))
  182. {
  183. this.Steps.Add(this.CreateStep(_columns, _step));
  184. }
  185. }
  186. public string ToXml()
  187. {
  188. StringBuilder builder = new StringBuilder();
  189. builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  190. builder.Append(string.Format("<Aitex><TableSequenceData CreatedBy=\"{0}\" CreationTime=\"{1}\" LastRevisedBy=\"{2}\" LastRevisionTime=\"{3}\" Description=\"{4}\" >", this.Creator, this.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), this.Revisor, this.ReviseTime.ToString("yyyy-MM-dd HH:mm:ss"), this.Description));
  191. foreach (ObservableCollection<Param> parameters in Steps)
  192. {
  193. builder.Append("<Step ");
  194. foreach (Param parameter in parameters)
  195. {
  196. if (parameter.Visible == System.Windows.Visibility.Visible)
  197. {
  198. if (parameter is IntParam)
  199. builder.Append(parameter.Name + "=\"" + ((IntParam)parameter).Value + "\" ");
  200. else if (parameter is DoubleParam)
  201. builder.Append(parameter.Name + "=\"" + ((DoubleParam)parameter).Value + "\" ");
  202. else if (parameter is StringParam)
  203. builder.Append(parameter.Name + "=\"" + ((StringParam)parameter).Value + "\" ");
  204. else if (parameter is PathFileParam)
  205. builder.Append(parameter.Name + "=\"" + ((PathFileParam)parameter).Value + "\" ");
  206. else if (parameter is ComboxParam)
  207. builder.Append(parameter.Name + "=\"" + ((ComboxParam)parameter).Value + "\" ");
  208. else if (parameter is PositionParam)
  209. builder.Append(parameter.Name + "=\"" + ((PositionParam)parameter).Value + "\" ");
  210. else if (parameter is BoolParam)
  211. builder.Append(parameter.Name + "=\"" + ((BoolParam)parameter).Value + "\" ");
  212. else if (parameter is MultipleSelectParam)
  213. {
  214. List<string> selected = new List<string>();
  215. ((MultipleSelectParam)parameter).Options.Apply(
  216. opt =>
  217. {
  218. if (opt.IsChecked)
  219. selected.Add(opt.ControlName);
  220. }
  221. );
  222. builder.Append(parameter.Name + "=\"" + string.Join(",", selected) + "\" ");
  223. }
  224. }
  225. }
  226. builder.Append("/>");
  227. }
  228. builder.Append("</TableSequenceData></Aitex>");
  229. return builder.ToString();
  230. }
  231. private string name;
  232. public string Name
  233. {
  234. get { return this.name; }
  235. set { SetProperty(ref name, value); }
  236. }
  237. private string creator;
  238. public string Creator
  239. {
  240. get { return this.creator; }
  241. set { SetProperty(ref creator, value); }
  242. }
  243. private DateTime createTime;
  244. public DateTime CreateTime
  245. {
  246. get { return this.createTime; }
  247. set { SetProperty(ref createTime, value); }
  248. }
  249. private string description;
  250. public string Description
  251. {
  252. get { return this.description; }
  253. set { SetProperty(ref description, value); }
  254. }
  255. private string devisor;
  256. public string Revisor
  257. {
  258. get { return this.devisor; }
  259. set { SetProperty(ref devisor, value); }
  260. }
  261. private DateTime deviseTime;
  262. public DateTime ReviseTime
  263. {
  264. get { return this.deviseTime; }
  265. set { SetProperty(ref deviseTime, value); }
  266. }
  267. }
  268. }