using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using Newtonsoft.Json; using S7Explorer.Parsers; namespace S7Explorer.Models { public class S7Function : S7Block { private string _returnType; private List _parameters; private string _interface; [DisplayName("Tipo de Retorno")] [Description("Tipo de dato que retorna la función")] [Category("Interfaz")] public string ReturnType { get => _returnType; set { if (_returnType != value) { _returnType = value; OnPropertyChanged(nameof(ReturnType)); } } } [Browsable(false)] [JsonIgnore] public List Parameters { get => _parameters; set { if (_parameters != value) { _parameters = value; OnPropertyChanged(nameof(Parameters)); // Actualizar la interfaz formateada cuando se cambian los parámetros UpdateFormattedInterface(); } } } [DisplayName("Interfaz")] [Description("Interfaz de la función con sus parámetros")] [Category("Interfaz")] [EditorAttribute(typeof(System.ComponentModel.Design.MultilineStringEditor), typeof(System.Drawing.Design.UITypeEditor))] public string Interface { get => _interface; set { if (_interface != value) { _interface = value; OnPropertyChanged(nameof(Interface)); } } } // Propiedades adicionales específicas de FC [DisplayName("Parámetros de Entrada")] [Description("Número de parámetros de entrada")] [Category("Estadísticas")] public int InputParameterCount => Parameters?.Count(p => p.Direction == "IN") ?? 0; [DisplayName("Parámetros de Salida")] [Description("Número de parámetros de salida")] [Category("Estadísticas")] public int OutputParameterCount => Parameters?.Count(p => p.Direction == "OUT") ?? 0; [DisplayName("Parámetros IN/OUT")] [Description("Número de parámetros de entrada/salida")] [Category("Estadísticas")] public int InOutParameterCount => Parameters?.Count(p => p.Direction == "IN/OUT") ?? 0; [DisplayName("Total Parámetros")] [Description("Número total de parámetros")] [Category("Estadísticas")] public int TotalParameterCount => Parameters?.Count ?? 0; public S7Function() { ObjectType = S7ObjectType.Function; Parameters = new List(); } /// /// Actualiza la representación formateada de la interfaz basada en los parámetros /// private void UpdateFormattedInterface() { if (Parameters == null || Parameters.Count == 0) { Interface = "// No hay parámetros definidos"; return; } var builder = new System.Text.StringBuilder(); // Agrupar por dirección var inputParams = Parameters.Where(p => p.Direction == "IN").ToList(); var outputParams = Parameters.Where(p => p.Direction == "OUT").ToList(); var inOutParams = Parameters.Where(p => p.Direction == "IN/OUT").ToList(); // Añadir tipo de retorno builder.AppendLine($"FUNCTION {Name} : {ReturnType ?? "VOID"}"); builder.AppendLine(); // Añadir parámetros de entrada if (inputParams.Any()) { builder.AppendLine("VAR_INPUT"); foreach (var param in inputParams) { builder.Append($" {param.Name} : {param.DataType}"); if (!string.IsNullOrEmpty(param.Description)) builder.Append($"; // {param.Description}"); builder.AppendLine(); } builder.AppendLine("END_VAR"); builder.AppendLine(); } // Añadir parámetros de salida if (outputParams.Any()) { builder.AppendLine("VAR_OUTPUT"); foreach (var param in outputParams) { builder.Append($" {param.Name} : {param.DataType}"); if (!string.IsNullOrEmpty(param.Description)) builder.Append($"; // {param.Description}"); builder.AppendLine(); } builder.AppendLine("END_VAR"); builder.AppendLine(); } // Añadir parámetros de entrada/salida if (inOutParams.Any()) { builder.AppendLine("VAR_IN_OUT"); foreach (var param in inOutParams) { builder.Append($" {param.Name} : {param.DataType}"); if (!string.IsNullOrEmpty(param.Description)) builder.Append($"; // {param.Description}"); builder.AppendLine(); } builder.AppendLine("END_VAR"); } Interface = builder.ToString(); } } }