using System; using System.Text.RegularExpressions; using System.Collections.Generic; using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; namespace CtrEditor.FuncionesBase { public class TagPattern { public const string DEFAULT_PATTERN = "DESCRIPCION"; private static readonly Dictionary _patternDescriptions = new Dictionary { { "Descripcion", "First letter capitalized" }, { "DESCRIPCION", "All uppercase text" }, { "Siemens IO Input", "Format: Exxxxx.y (x: 0-65535, y: 0-7)" }, { "Siemens IO Output", "Format: Axxxxx.y (x: 0-65535, y: 0-7)" }, { "LETRASNUMEROS", "Format: ABC...123... (1-10 letters + numbers)" }, { "Numero", "Numeric value" } }; public static List GetPatternList() { return new List(_patternDescriptions.Keys); } public static string ApplyPattern(string text, string pattern) { if (string.IsNullOrEmpty(text)) return text; return pattern switch { "Descripcion" => ApplyDescripcionPattern(text), "DESCRIPCION" => text.ToUpper(), "Siemens IO Input" => ApplySiemensPattern(text, "E"), "Siemens IO Output" => ApplySiemensPattern(text, "A"), "LETRASNUMEROS" => ApplyLetrasNumerosPattern(text), "Numero" => ApplyNumberPattern(text), _ => text }; } private static string ApplyDescripcionPattern(string text) { if (string.IsNullOrEmpty(text)) return text; return char.ToUpper(text[0]) + (text.Length > 1 ? text.Substring(1).ToLower() : ""); } private static string ApplySiemensPattern(string text, string prefix) { var match = Regex.Match(text, $"{prefix}?(\\d{{1,5}})\\.?(\\d)?", RegexOptions.IgnoreCase); if (match.Success) { int address = Math.Min(int.Parse(match.Groups[1].Value), 65535); int bit = match.Groups[2].Success ? Math.Min(int.Parse(match.Groups[2].Value), 7) : 0; return $"{prefix}{address}.{bit}"; } return $"{prefix}0.0"; // Default value if pattern doesn't match } private static string ApplyLetrasNumerosPattern(string text) { // Extract letters and numbers from the text var letters = new string(text.Where(c => char.IsLetter(c)).Take(10).ToArray()); var numbers = new string(text.Where(c => char.IsDigit(c)).ToArray()); // If no letters found, return "A" as default if (string.IsNullOrEmpty(letters)) letters = "A"; // If no numbers found, return "0" as default if (string.IsNullOrEmpty(numbers)) numbers = "0"; // Combine letters (uppercase) and numbers return $"{letters.ToUpper()}{numbers}"; } private static string ApplyNumberPattern(string text) { var match = Regex.Match(text, @"-?\d+\.?\d*"); return match.Success ? match.Value : "0"; } } public class TagPatternItemsSource : IItemsSource { public ItemCollection GetValues() { ItemCollection items = new ItemCollection(); foreach (string pattern in TagPattern.GetPatternList()) { items.Add(pattern); } return items; } } }