using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Xml; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using MECF.Framework.UI.Client.CenterViews.Configs.Roles; using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig; using MECF.Framework.UI.Client.ClientBase; using RecipeEditorLib.DGExtension.CustomColumn; using RecipeEditorLib.RecipeModel.Params; namespace MECF.Framework.UI.Client.CenterViews.Editors.Recipe { public class RecipeFormatBuilder { public ObservableCollection RecipeInfos { get; set; } public ObservableCollection StepInfos { get; set; } public ObservableCollection ContentInfos { get; set; } public ObservableCollection Columns { get; set; } public ObservableCollection Configs { get; set; } public ObservableCollection OesConfig { get; set; } public ObservableCollection VatConfig { get; set; } public ObservableCollection BrandConfig { get; set; } public ObservableCollection FineTuningConfig { get; set; } public string RecipeChamberType { get; set; } public string RecipeVersion { get; set; } /// /// 去除小数点、箭头、括号(含括号里的内容)、横杠 /// /// /// private string strAbc(string DisplayName) { string displayname = DisplayName; if (Regex.IsMatch(displayname.Substring(0, 1), @"^[+-]?\d*[.]?\d*$")) { displayname = displayname.Remove(0, displayname.IndexOf(".") + 1);//去除序号 } displayname = displayname.Trim().Replace(" ","").Replace(".", "").Replace("->", "_").Replace("-", ""); if (displayname.Contains("(") && displayname.Contains(")")) { displayname=displayname.Remove(displayname.IndexOf("("), displayname.IndexOf(")") - displayname.IndexOf("(") + 1); } return displayname; } public double GetDoubleValueByName(string module, string name) { string value = SystemConfigProvider.Instance.GetValueByName(module, name); double returnValue = 0; if (String.IsNullOrEmpty(value)) { if (name.EndsWith(".Default")) { returnValue = 0; } else if (name.EndsWith(".Min")) { returnValue = 0; } else if (name.EndsWith(".Max")) { returnValue = 10000; } } else { returnValue = double.Parse(value); } return returnValue; } public string GetStringValueByName(string module, string name) { return SystemConfigProvider.Instance.GetValueByName(module, name); } private RecipeProvider recipeProvider = new RecipeProvider(); //获取RecipInfo的Name信息 public ObservableCollection GetRecipeColumnName(string path, string module = "PM1", bool defaultCellEnable = true) { var str = recipeProvider.GetRecipeFormatXml(path); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(str); XmlNode nodeRoot = doc.SelectSingleNode("TableRecipeFormat"); RecipeChamberType = nodeRoot.Attributes["RecipeChamberType"].Value; RecipeVersion = nodeRoot.Attributes["RecipeVersion"].Value; } catch (Exception ex) { LOG.Write(ex); return null; } RecipeInfos = new ObservableCollection(); XmlNodeList nodes = doc.SelectNodes("TableRecipeFormat/Catalog/Group"); foreach (XmlNode node in nodes) { XmlNodeList childNodes = node.SelectNodes("Step"); foreach (XmlNode step in childNodes) { RecipeInfos.Add(new RecipeInfo() { ID = step.Attributes["ControlName"].Value, Name = step.Attributes["DisplayName"].Value }); } } return RecipeInfos; } //获取StepInfo的No信息 public ObservableCollection GetRecipeStepNo() { StepInfos = new ObservableCollection(); for (int i = 0; i < 50; i++) { StepInfos.Add(new StepInfo() { ID = (i + 1).ToString(), Name = "Step" + (i + 1).ToString() }); } return StepInfos; } //获取ContentInfo的Name信息 public ObservableCollection GetContentName(string path, string module = "PM1", bool defaultCellEnable = true) { var str = recipeProvider.GetRecipeFormatXml(path); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(str); XmlNode nodeRoot = doc.SelectSingleNode("TableRecipeFormat"); RecipeChamberType = nodeRoot.Attributes["RecipeChamberType"].Value; RecipeVersion = nodeRoot.Attributes["RecipeVersion"].Value; } catch (Exception ex) { LOG.Write(ex); return null; } ContentInfos = new ObservableCollection(); XmlNodeList nodesCatalog = doc.SelectNodes("TableRecipeFormat/Catalog"); foreach (XmlNode catalog in nodesCatalog) { string catalogName = catalog.Attributes["DisplayName"].Value; XmlNodeList nodeGroups = catalog.SelectNodes("Group"); foreach (XmlNode group in nodeGroups) { string groupName = group.Attributes["DisplayName"].Value; XmlNodeList nodeContents = group.SelectNodes("Content"); foreach (XmlNode content in nodeContents) { ContentInfos.Add(new ContentInfo() { Name = catalogName + "." + groupName + "." + content.Attributes["DisplayName"].Value }); } } } return ContentInfos; } public ObservableCollection Build(string path, string module = "PM1", bool defaultCellEnable = true, string roleName = "管理员") { var str = recipeProvider.GetRecipeFormatXml(path); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(str); XmlNode nodeRoot = doc.SelectSingleNode("TableRecipeFormat"); RecipeChamberType = nodeRoot.Attributes["RecipeChamberType"].Value; RecipeVersion = nodeRoot.Attributes["RecipeVersion"].Value; } catch (Exception ex) { LOG.Write(ex); return null; } //初始化RoleManager RoleManager roleManager = new RoleManager(); roleManager.Initialize(); //得到当前登录的RoleItem RoleItem roleItem = roleManager.GetRoleByName(roleName); MenuPermission menuPermission = new MenuPermission(); menuPermission.ParsePermission(roleItem.Role.MenuPermission); var columns = new ObservableCollection(); EditorDataGridTemplateColumnBase col = null; XmlNodeList nodes = doc.SelectNodes("TableRecipeFormat/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", UnitName = "", ControlName = "StepNo", StringCellTemplate = "TemplateStep", StringHeaderTemplate = "ParamTemplate", IsEnable = defaultCellEnable, }; if (menuPermission.MenuPermissionDictionary.ContainsKey(col.DisplayName.Replace(" ", "")) && menuPermission.MenuPermissionDictionary[col.DisplayName.Replace(" ", "")] == MenuPermissionEnum.MP_NONE) { col.Visibility = Visibility.Hidden; } 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, UnitName = step.Attributes["UnitName"].Value, Default = GetStringValueByName("PM", $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Default"), 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), IsEnable = defaultCellEnable, }; //columns.Add(col); break; case "ReadOnly": col = new TextBoxColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, UnitName = step.Attributes["UnitName"].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), IsEnable = defaultCellEnable, }; //columns.Add(col); break; case "NumInput": col = new NumColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, UnitName = step.Attributes["UnitName"].Value, InputMode = step.Attributes["InputMode"].Value, //Minimun = double.Parse(step.Attributes["Min"].Value), Minimun = GetDoubleValueByName("PM", $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Min"), //Maximun = double.Parse(step.Attributes["Max"].Value), Maximun = GetDoubleValueByName("PM", $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Max"), 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), IsEnable = defaultCellEnable, }; //columns.Add(col); break; case "DoubleInput": //MFC和PC的最大值读取MFC的设定 string displayText = new Regex(@"\(M\d+\)").Match(step.Attributes["DisplayName"].Value).Value; string maxConfig = $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Max"; string minConfig = $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Min"; if (displayText.Contains("M")) { maxConfig = $"PM.{module}.MFC.Mfc{displayText.Replace("(M", "").Replace(")", "")}.N2Scale"; } else { displayText = new Regex(@"\(PC\d+\)").Match(step.Attributes["DisplayName"].Value).Value; if (displayText.Contains("PC")) { maxConfig = $"PM.{module}.PC.PC{displayText.Replace("(PC", "").Replace(")", "")}.Scale"; } } col = new DoubleColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, UnitName = step.Attributes["UnitName"].Value, InputMode = step.Attributes["InputMode"].Value, Maximun = GetDoubleValueByName("PM", maxConfig), Minimun = GetDoubleValueByName("PM", minConfig), Default = GetStringValueByName("PM", $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Default"), 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), IsEnable = defaultCellEnable, }; if(displayText.Contains("M")) { ((DoubleColumn)col).Minimun = ((DoubleColumn)col).Maximun * GetDoubleValueByName("PM", $"PM.{module}.MFC.MinScale")/100.0 ; } //columns.Add(col); break; case "DoubleTextInput": //MFC和PC的最大值读取MFC的设定 displayText = new Regex(@"\(M\d+\)").Match(step.Attributes["DisplayName"].Value).Value; maxConfig = $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Max"; minConfig = $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Min"; if (displayText.Contains("M")) { maxConfig = $"PM.{module}.MFC.Mfc{displayText.Replace("(M", "").Replace(")", "")}.N2Scale"; } else { displayText = new Regex(@"\(PC\d+\)").Match(step.Attributes["DisplayName"].Value).Value; if (displayText.Contains("PC")) { maxConfig = $"PM.{module}.PC.PC{displayText.Replace("(PC", "").Replace(")", "")}.Scale"; } } col = new DoubleColumn() { ModuleName = step.Attributes["ModuleName"].Value, ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, UnitName = step.Attributes["UnitName"].Value, InputMode = step.Attributes["InputMode"].Value, Minimun = GetDoubleValueByName("PM", minConfig), Maximun = GetDoubleValueByName("PM", maxConfig), Default = "Hold", StringCellTemplate = "TemplateNumberText", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), IsEnable = defaultCellEnable, }; //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 : "", Default = GetStringValueByName("PM", $"PM.{module}.RecipeConfig.{strAbc(node.Attributes["DisplayName"].Value)}.{strAbc(step.Attributes["DisplayName"].Value)}.Default"), ControlName = step.Attributes["ControlName"].Value, DisplayName = step.Attributes["DisplayName"].Value, UnitName = step.Attributes["UnitName"].Value, StringCellTemplate = "TemplateComboxEx", StringHeaderTemplate = "ParamTemplate", EnableConfig = step.Attributes["EnableConfig"] != null && Convert.ToBoolean(step.Attributes["EnableConfig"].Value), EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), IsEnable = defaultCellEnable, }; 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 = "TemplateComboxEx", StringHeaderTemplate = "ParamTemplate", EnableTolerance = step.Attributes["EnableTolerance"] != null && Convert.ToBoolean(step.Attributes["EnableTolerance"].Value), IsEnable = defaultCellEnable, }; 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, UnitName = step.Attributes["UnitName"].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), IsEnable = defaultCellEnable, }; //columns.Add(col); break; } if (menuPermission.MenuPermissionDictionary.ContainsKey(col.DisplayName.Replace(" ", ""))) { if (menuPermission.MenuPermissionDictionary[col.DisplayName.Replace(" ", "")] == MenuPermissionEnum.MP_NONE) { col.Visibility = Visibility.Hidden; } } columns.Add(col); if (defaultCellEnable && path.EndsWith("Process") && menuPermission.MenuPermissionDictionary.ContainsKey(col.DisplayName.Replace(" ", ""))) { SetCellEnable(col, menuPermission.MenuPermissionDictionary[col.DisplayName.Replace(" ", "")]); } } } Columns = columns; var configs = new ObservableCollection(); nodes = doc.SelectNodes("TableRecipeFormat/ProcessConfig/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; } } } Configs = configs; BrandConfig = GetConfig(doc.SelectNodes("TableRecipeFormat/BrandConfig/Configs")); OesConfig = GetConfig(doc.SelectNodes("TableRecipeFormat/OesConfig/Configs")); VatConfig = GetConfig(doc.SelectNodes("TableRecipeFormat/VatConfig/Configs")); FineTuningConfig = GetConfig(doc.SelectNodes("TableRecipeFormat/FineTuningConfig/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); }); } public static void SetCellEnable(EditorDataGridTemplateColumnBase col, MenuPermissionEnum permissionEnum) { if (permissionEnum == MenuPermissionEnum.MP_NONE) { col.Feedback = (p) => { p.IsEnabled = false; p.Visible = Visibility.Hidden; }; } else if (permissionEnum == MenuPermissionEnum.MP_READ) { col.Feedback = (p) => { p.IsEnabled = false; p.Visible = Visibility.Visible; }; } else { col.Feedback = (p) => { p.IsEnabled = true; p.Visible = Visibility.Visible; }; } } } }