using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Xml; using Aitex.Core.RT.Log; using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig; using MECF.Framework.UI.Client.CenterViews.Parameter; using RecipeEditorLib.DGExtension.CustomColumn; using RecipeEditorLib.RecipeModel.Params; namespace MECF.Framework.UI.Client.CenterViews.Editors.Recipe { public class ParameterFormatBuilder { public ObservableCollection Columns { get; set; } public ObservableCollection Configs { get; set; } public string ChamberType { get; set; } public string Version { get; set; } private int _tableNumber; public int TableNumber { get; set; } private ParameterProvider parameterProvider = new ParameterProvider(); public ObservableCollection Build(string path) { var str = parameterProvider.GetParameterFormatXml(path); string configstr = ""; if (path == "PM1") { configstr = "Process"; } else { configstr = path; } XmlDocument doc = new XmlDocument(); try { doc.LoadXml(str); XmlNode nodeRoot = doc.SelectSingleNode("TableParameterFormat"); ChamberType = nodeRoot.Attributes["ChamberType"].Value; Version = nodeRoot.Attributes["Version"].Value; if (nodeRoot.Attributes["TableNumber"] != null) { TableNumber = Convert.ToInt32(nodeRoot.Attributes["TableNumber"].Value); } } catch (Exception ex) { LOG.Write(ex); return null; } var columns = new ObservableCollection(); EditorDataGridTemplateColumnBase col = null; XmlNodeList nodes = doc.SelectNodes("TableParameterFormat/Catalog/Group"); foreach (XmlNode node in nodes) { columns.Add(new ExpanderColumn() { DisplayName = node.Attributes["DisplayName"].Value, StringCellTemplate = "TemplateExpander", StringHeaderTemplate = "ParamExpander" }); XmlNodeList childNodes = node.SelectNodes("Step"); foreach (XmlNode step in childNodes) { //step number if (step.Attributes["ControlName"].Value == "StepNo") { col = new StepColumn() { DisplayName = "Step", ControlName = "StepNo", StringCellTemplate = "TemplateStep", StringHeaderTemplate = "ParamTemplate" }; columns.Add(col); continue; } switch (step.Attributes["InputType"].Value) { case "TextInput": col = new TextBoxColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, StringCellTemplate = "TemplateText", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; columns.Add(col); break; case "ReadOnly": col = new TextBoxColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, StringCellTemplate = "TemplateText", StringHeaderTemplate = "ParamTemplate", IsReadOnly = true, EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; columns.Add(col); break; case "NumInput": col = new NumColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, InputMode = step.Attributes["InputMode"].Value, Minimun = double.Parse(step.Attributes["Min"].Value), Maximun = double.Parse(step.Attributes["Max"].Value), StringCellTemplate = "TemplateNumber", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; columns.Add(col); break; case "DoubleInput": col = new DoubleColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, InputMode = step.Attributes["InputMode"].Value, Minimun = double.Parse(step.Attributes["Min"].Value), Maximun = double.Parse(step.Attributes["Max"].Value), StringCellTemplate = "TemplateNumber", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; columns.Add(col); break; case "EditableSelection": case "ReadOnlySelection": col = new ComboxColumn() { IsReadOnly = step.Attributes["InputType"].Value == "ReadOnlySelection", ModuleName = step.Attributes["ModuleName"].Value, Default = step.Attributes["Default"] != null ? step.Attributes["Default"].Value : "", ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, StringCellTemplate = "TemplateCombox", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; XmlNodeList items = step.SelectNodes("Item"); foreach (XmlNode item in items) { ComboxColumn.Option opt = new ComboxColumn.Option(); opt.ControlName = item.Attributes["ControlName"].Value; opt.DisplayName = item.Attributes["DisplayName"].Value; ((ComboxColumn)col).Options.Add(opt); } columns.Add(col); break; case "LoopSelection": col = new LoopComboxColumn() { IsReadOnly = false, ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, StringCellTemplate = "TemplateCombox", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; XmlNodeList options = step.SelectNodes("Item"); foreach (XmlNode item in options) { LoopComboxColumn.Option opt = new LoopComboxColumn.Option(); opt.ControlName = item.Attributes["ControlName"].Value; opt.DisplayName = item.Attributes["DisplayName"].Value; ((LoopComboxColumn)col).Options.Add(opt); } columns.Add(col); break; case "PopSetting": col = new PopSettingColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, StringCellTemplate = "TemplatePopSetting", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), }; columns.Add(col); break; } } } Columns = columns; var configs = new ObservableCollection(); nodes = doc.SelectNodes($"TableParameterFormat/Config/Configs"); foreach (XmlNode node in nodes) { XmlNodeList childNodes = node.SelectNodes("Config"); foreach (XmlNode configNode in childNodes) { switch (configNode.Attributes["InputType"].Value) { case "DoubleInput": var config = new DoubleParam() { Name = configNode.Attributes["ControlName"].Value, Value = configNode.Attributes["Default"].Value, DisplayName = configNode.Attributes["DisplayName"].Value, EnableConfig = configNode.Attributes["EnableConfig"] != null && Convert.ToBoolean(configNode.Attributes["EnableConfig"].Value), EnableTolerance = configNode.Attributes["EnableTolerance"] != null && Convert.ToBoolean(configNode.Attributes["EnableTolerance"].Value), }; if (double.TryParse(configNode.Attributes["Max"].Value, out double max)) { (config as DoubleParam).Maximun = max; } if (double.TryParse(configNode.Attributes["Min"].Value, out double min)) { (config as DoubleParam).Minimun = min; } configs.Add(config); break; case "NumInput": var numConfig = new NumParam() { Name = configNode.Attributes["ControlName"].Value, Value = int.Parse(configNode.Attributes["Default"].Value), DisplayName = configNode.Attributes["DisplayName"].Value, EnableConfig = configNode.Attributes["EnableConfig"] != null && Convert.ToBoolean(configNode.Attributes["EnableConfig"].Value), EnableTolerance = configNode.Attributes["EnableTolerance"] != null && Convert.ToBoolean(configNode.Attributes["EnableTolerance"].Value), }; if (double.TryParse(configNode.Attributes["Max"].Value, out max)) { (numConfig as NumParam).Maximun = max; } if (double.TryParse(configNode.Attributes["Min"].Value, out min)) { (numConfig as NumParam).Minimun = min; } configs.Add(numConfig); break; case "TextInput": var strConfig = new StringParam() { Name = configNode.Attributes["ControlName"].Value, Value = configNode.Attributes["Default"].Value, DisplayName = configNode.Attributes["DisplayName"].Value, EnableConfig = configNode.Attributes["EnableConfig"] != null && Convert.ToBoolean(configNode.Attributes["EnableConfig"].Value), EnableTolerance = configNode.Attributes["EnableTolerance"] != null && Convert.ToBoolean(configNode.Attributes["EnableTolerance"].Value), }; configs.Add(strConfig); break; } } } Configs = configs; return Columns; } private ObservableCollection GetConfig(XmlNodeList nodes) { var configs = new ObservableCollection(); foreach (XmlNode node in nodes) { XmlNodeList childNodes = node.SelectNodes("Config"); foreach (XmlNode configNode in childNodes) { switch (configNode.Attributes["InputType"].Value) { case "TextInput": var text = new StringParam() { Name = configNode.Attributes["ControlName"].Value, Value = configNode.Attributes["Default"].Value, DisplayName = configNode.Attributes["DisplayName"].Value, }; configs.Add(text); break; case "DoubleInput": var config = new DoubleParam() { Name = configNode.Attributes["ControlName"].Value, Value = configNode.Attributes["Default"].Value, DisplayName = configNode.Attributes["DisplayName"].Value, }; if (double.TryParse(configNode.Attributes["Max"].Value, out double max)) { (config as DoubleParam).Maximun = max; } if (double.TryParse(configNode.Attributes["Min"].Value, out double min)) { (config as DoubleParam).Minimun = min; } configs.Add(config); break; case "ReadOnlySelection": var col = new ComboxParam() { Name = configNode.Attributes["ControlName"].Value, DisplayName = configNode.Attributes["DisplayName"].Value, Value = configNode.Attributes["Default"] != null ? configNode.Attributes["Default"].Value : "", Options = new ObservableCollection(), IsEditable = configNode.Attributes["InputType"].Value == "ReadOnlySelection", EnableTolerance = configNode.Attributes["EnableTolerance"] != null && Convert.ToBoolean(configNode.Attributes["EnableTolerance"].Value), }; XmlNodeList items = configNode.SelectNodes("Item"); foreach (XmlNode item in items) { ComboxColumn.Option opt = new ComboxColumn.Option(); opt.ControlName = item.Attributes["ControlName"].Value; opt.DisplayName = item.Attributes["DisplayName"].Value; col.Options.Add(opt); } col.Value = !string.IsNullOrEmpty(col.Value) ? col.Value : (col.Options.Count > 0 ? col.Options[0].ControlName : ""); configs.Add(col); break; } } } return configs; } public static void ApplyTemplate(UserControl uc, ObservableCollection columns) { columns.ToList().ForEach(col => { col.CellTemplate = (DataTemplate)uc.FindResource(col.StringCellTemplate); col.HeaderTemplate = (DataTemplate)uc.FindResource(col.StringHeaderTemplate); }); } } }