111 lines
4.1 KiB
C#
111 lines
4.1 KiB
C#
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<string, string> _patternDescriptions = new Dictionary<string, string>
|
|
{
|
|
{ "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)" },
|
|
{ "LETRASNUMEROSESPACIOS", "Format: ABC...123... (letters, numbers and spaces allowed)" },
|
|
{ "Numero", "Numeric value" }
|
|
};
|
|
|
|
public static List<string> GetPatternList()
|
|
{
|
|
return new List<string>(_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),
|
|
"LETRASNUMEROSESPACIOS" => ApplyLetrasNumerosEspaciosPattern(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 ApplyLetrasNumerosEspaciosPattern(string text)
|
|
{
|
|
// Keep only letters, numbers and spaces
|
|
var cleanedText = new string(text.Where(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)).ToArray());
|
|
|
|
// Convert to uppercase
|
|
return cleanedText.ToUpper();
|
|
}
|
|
|
|
private static string ApplyNumberPattern(string text)
|
|
{
|
|
var match = Regex.Match(text, @"-?\d+\.?\d*");
|
|
return match.Success ? match.Value : "0";
|
|
}
|
|
}
|
|
|
|
public class TagPatternItemsSource<T> : IItemsSource
|
|
{
|
|
public ItemCollection GetValues()
|
|
{
|
|
ItemCollection items = new ItemCollection();
|
|
foreach (string pattern in TagPattern.GetPatternList())
|
|
{
|
|
items.Add(pattern);
|
|
}
|
|
return items;
|
|
}
|
|
}
|
|
}
|