From 8356c6d2f57ad0197b967e228a4041534f5060ad Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 24 Mar 2025 16:05:29 +0100 Subject: [PATCH] Primera Version --- .gitignore | 363 +++++++++++++++++++++++++++++++ App.xaml | 11 + App.xaml.cs | 88 ++++++++ AssemblyInfo.cs | 10 + Models/S7Block.cs | 161 ++++++++++++++ Models/S7Object.cs | 120 +++++++++++ Models/S7Project.cs | 96 +++++++++ Models/S7Symbol.cs | 74 +++++++ Parsers/DbfParser.cs | 79 +++++++ Parsers/S7ProjectParser.cs | 370 ++++++++++++++++++++++++++++++++ S7Explorer.csproj | 19 ++ S7Explorer.sln | 25 +++ ViewModels/MainViewModel.cs | 199 +++++++++++++++++ ViewModels/S7ObjectViewModel.cs | 12 ++ Views/MainWindow.xaml | 67 ++++++ Views/MainWindow.xaml.cs | 35 +++ 16 files changed, 1729 insertions(+) create mode 100644 .gitignore create mode 100644 App.xaml create mode 100644 App.xaml.cs create mode 100644 AssemblyInfo.cs create mode 100644 Models/S7Block.cs create mode 100644 Models/S7Object.cs create mode 100644 Models/S7Project.cs create mode 100644 Models/S7Symbol.cs create mode 100644 Parsers/DbfParser.cs create mode 100644 Parsers/S7ProjectParser.cs create mode 100644 S7Explorer.csproj create mode 100644 S7Explorer.sln create mode 100644 ViewModels/MainViewModel.cs create mode 100644 ViewModels/S7ObjectViewModel.cs create mode 100644 Views/MainWindow.xaml create mode 100644 Views/MainWindow.xaml.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9491a2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,363 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/App.xaml b/App.xaml new file mode 100644 index 0000000..02a4e92 --- /dev/null +++ b/App.xaml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/App.xaml.cs b/App.xaml.cs new file mode 100644 index 0000000..26ad7db --- /dev/null +++ b/App.xaml.cs @@ -0,0 +1,88 @@ +using System; +using System.IO; +using System.Windows; +using System.Windows.Media.Imaging; + +namespace S7Explorer +{ + public partial class App : Application + { + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + // Asegurarnos de que existe la carpeta Resources para los íconos + EnsureResourcesExist(); + + // Configurar manejo de excepciones no controladas + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + Current.DispatcherUnhandledException += Current_DispatcherUnhandledException; + } + + private void EnsureResourcesExist() + { + try + { + // Directorio de recursos + string resourcesDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources"); + if (!Directory.Exists(resourcesDir)) + { + Directory.CreateDirectory(resourcesDir); + } + + // Crear íconos básicos si no existen + CreateDefaultIconIfNotExists(resourcesDir, "project.png"); + CreateDefaultIconIfNotExists(resourcesDir, "device.png"); + CreateDefaultIconIfNotExists(resourcesDir, "folder.png"); + CreateDefaultIconIfNotExists(resourcesDir, "db.png"); + CreateDefaultIconIfNotExists(resourcesDir, "fb.png"); + CreateDefaultIconIfNotExists(resourcesDir, "fc.png"); + CreateDefaultIconIfNotExists(resourcesDir, "ob.png"); + CreateDefaultIconIfNotExists(resourcesDir, "symbol.png"); + CreateDefaultIconIfNotExists(resourcesDir, "default.png"); + } + catch (Exception) + { + // Ignorar errores en la creación de recursos - no son críticos + } + } + + private void CreateDefaultIconIfNotExists(string directory, string filename) + { + string filePath = Path.Combine(directory, filename); + if (!File.Exists(filePath)) + { + // Crear un ícono simple - en una aplicación real, incluirías recursos reales + BitmapSource bmp = BitmapSource.Create(16, 16, 96, 96, System.Windows.Media.PixelFormats.Bgr32, null, new byte[16 * 16 * 4], 16 * 4); + + using (var fileStream = new FileStream(filePath, FileMode.Create)) + { + BitmapEncoder encoder = new PngBitmapEncoder(); + encoder.Frames.Add(BitmapFrame.Create(bmp)); + encoder.Save(fileStream); + } + } + } + + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + HandleException(e.ExceptionObject as Exception); + } + + private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + HandleException(e.Exception); + e.Handled = true; + } + + private void HandleException(Exception ex) + { + if (ex == null) return; + + MessageBox.Show($"Ha ocurrido un error inesperado: {ex.Message}\n\nDetalles: {ex.StackTrace}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + + // Aquí podrías añadir registro de errores + } + } +} \ No newline at end of file diff --git a/AssemblyInfo.cs b/AssemblyInfo.cs new file mode 100644 index 0000000..cc29e7f --- /dev/null +++ b/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/Models/S7Block.cs b/Models/S7Block.cs new file mode 100644 index 0000000..0cda6a1 --- /dev/null +++ b/Models/S7Block.cs @@ -0,0 +1,161 @@ +using System; +using System.ComponentModel; + +namespace S7Explorer.Models +{ + public class S7Block : S7Object + { + private string _authorName; + private string _family; + private string _version; + private DateTime? _modified; + private int _size; + + [DisplayName("Autor")] + public string AuthorName + { + get => _authorName; + set + { + if (_authorName != value) + { + _authorName = value; + OnPropertyChanged(nameof(AuthorName)); + } + } + } + + [DisplayName("Familia")] + public string Family + { + get => _family; + set + { + if (_family != value) + { + _family = value; + OnPropertyChanged(nameof(Family)); + } + } + } + + [DisplayName("Versión")] + public string Version + { + get => _version; + set + { + if (_version != value) + { + _version = value; + OnPropertyChanged(nameof(Version)); + } + } + } + + [DisplayName("Modificado")] + public DateTime? Modified + { + get => _modified; + set + { + if (_modified != value) + { + _modified = value; + OnPropertyChanged(nameof(Modified)); + } + } + } + + [DisplayName("Tamaño (bytes)")] + public int Size + { + get => _size; + set + { + if (_size != value) + { + _size = value; + OnPropertyChanged(nameof(Size)); + } + } + } + + public S7Block() + { + // Establece valores predeterminados específicos para bloques + } + } + + public class S7DataBlock : S7Block + { + private bool _instanceDb; + + [DisplayName("Es DB de Instancia")] + public bool IsInstanceDb + { + get => _instanceDb; + set + { + if (_instanceDb != value) + { + _instanceDb = value; + OnPropertyChanged(nameof(IsInstanceDb)); + } + } + } + + public S7DataBlock() + { + ObjectType = S7ObjectType.DataBlock; + } + } + + public class S7FunctionBlock : S7Block + { + private string _language; + + [DisplayName("Lenguaje")] + public string Language + { + get => _language; + set + { + if (_language != value) + { + _language = value; + OnPropertyChanged(nameof(Language)); + } + } + } + + public S7FunctionBlock() + { + ObjectType = S7ObjectType.FunctionBlock; + } + } + + public class S7Function : S7Block + { + private string _returnType; + + [DisplayName("Tipo de Retorno")] + public string ReturnType + { + get => _returnType; + set + { + if (_returnType != value) + { + _returnType = value; + OnPropertyChanged(nameof(ReturnType)); + } + } + } + + public S7Function() + { + ObjectType = S7ObjectType.Function; + } + } +} \ No newline at end of file diff --git a/Models/S7Object.cs b/Models/S7Object.cs new file mode 100644 index 0000000..9a0810c --- /dev/null +++ b/Models/S7Object.cs @@ -0,0 +1,120 @@ +using System.Collections.ObjectModel; +using System.ComponentModel; +using Newtonsoft.Json; + +namespace S7Explorer.Models +{ + public enum S7ObjectType + { + Project, + Device, + Folder, + DataBlock, + FunctionBlock, + Function, + Organization, + Symbol + } + + public class S7Object : INotifyPropertyChanged + { + private string _name; + private string _description; + private bool _isExpanded; + + public string Id { get; set; } + public string Number { get; set; } + + [DisplayName("Nombre")] + public string Name + { + get => _name; + set + { + if (_name != value) + { + _name = value; + OnPropertyChanged(nameof(Name)); + } + } + } + + [DisplayName("Descripción")] + public string Description + { + get => _description; + set + { + if (_description != value) + { + _description = value; + OnPropertyChanged(nameof(Description)); + } + } + } + + [Browsable(false)] + public S7ObjectType ObjectType { get; set; } + + [Browsable(false)] + public string IconSource => GetIconPath(); + + [Browsable(false)] + [JsonIgnore] + public S7Object Parent { get; set; } + + [Browsable(false)] + public ObservableCollection Children { get; set; } = new ObservableCollection(); + + [Browsable(false)] + public bool IsExpanded + { + get => _isExpanded; + set + { + if (_isExpanded != value) + { + _isExpanded = value; + OnPropertyChanged(nameof(IsExpanded)); + } + } + } + + // Para búsquedas de texto + [Browsable(false)] + public bool ContainsText(string searchText) + { + if (string.IsNullOrWhiteSpace(searchText)) + return false; + + searchText = searchText.ToLowerInvariant(); + + return Name?.ToLowerInvariant().Contains(searchText) == true || + Description?.ToLowerInvariant().Contains(searchText) == true || + Number?.ToLowerInvariant().Contains(searchText) == true; + } + + private string GetIconPath() + { + return ObjectType switch + { + S7ObjectType.Project => "/Resources/project.png", + S7ObjectType.Device => "/Resources/device.png", + S7ObjectType.Folder => "/Resources/folder.png", + S7ObjectType.DataBlock => "/Resources/db.png", + S7ObjectType.FunctionBlock => "/Resources/fb.png", + S7ObjectType.Function => "/Resources/fc.png", + S7ObjectType.Organization => "/Resources/ob.png", + S7ObjectType.Symbol => "/Resources/symbol.png", + _ => "/Resources/default.png" + }; + } + + public event PropertyChangedEventHandler PropertyChanged; + + protected virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} \ No newline at end of file diff --git a/Models/S7Project.cs b/Models/S7Project.cs new file mode 100644 index 0000000..5a2d6fd --- /dev/null +++ b/Models/S7Project.cs @@ -0,0 +1,96 @@ +using System; +using System.ComponentModel; +using System.IO; + +namespace S7Explorer.Models +{ + public class S7Project : S7Object + { + private string _filePath; + private string _version; + private DateTime _created; + private DateTime _lastModified; + + [DisplayName("Ruta de Archivo")] + public string FilePath + { + get => _filePath; + set + { + if (_filePath != value) + { + _filePath = value; + OnPropertyChanged(nameof(FilePath)); + } + } + } + + [DisplayName("Versión STEP7")] + public string Version + { + get => _version; + set + { + if (_version != value) + { + _version = value; + OnPropertyChanged(nameof(Version)); + } + } + } + + [DisplayName("Creado")] + public DateTime Created + { + get => _created; + set + { + if (_created != value) + { + _created = value; + OnPropertyChanged(nameof(Created)); + } + } + } + + [DisplayName("Última Modificación")] + public DateTime LastModified + { + get => _lastModified; + set + { + if (_lastModified != value) + { + _lastModified = value; + OnPropertyChanged(nameof(LastModified)); + } + } + } + + // Directorio base del proyecto (donde están las carpetas ombstx, YDBs, etc.) + [Browsable(false)] + public string ProjectDirectory => Path.GetDirectoryName(FilePath); + + public S7Project() + { + ObjectType = S7ObjectType.Project; + Created = DateTime.Now; + LastModified = DateTime.Now; + } + + // Inicializa un proyecto a partir de un archivo .s7p + public S7Project(string filePath) : this() + { + FilePath = filePath; + Name = Path.GetFileNameWithoutExtension(filePath); + + // Trata de obtener fechas del archivo + if (File.Exists(filePath)) + { + var fileInfo = new FileInfo(filePath); + Created = fileInfo.CreationTime; + LastModified = fileInfo.LastWriteTime; + } + } + } +} \ No newline at end of file diff --git a/Models/S7Symbol.cs b/Models/S7Symbol.cs new file mode 100644 index 0000000..6544126 --- /dev/null +++ b/Models/S7Symbol.cs @@ -0,0 +1,74 @@ +using System.ComponentModel; + +namespace S7Explorer.Models +{ + public class S7Symbol : S7Object + { + private string _address; + private string _dataType; + private string _comment; + + [DisplayName("Dirección")] + public string Address + { + get => _address; + set + { + if (_address != value) + { + _address = value; + OnPropertyChanged(nameof(Address)); + } + } + } + + [DisplayName("Tipo de Datos")] + public string DataType + { + get => _dataType; + set + { + if (_dataType != value) + { + _dataType = value; + OnPropertyChanged(nameof(DataType)); + } + } + } + + [DisplayName("Comentario")] + public string Comment + { + get => _comment; + set + { + if (_comment != value) + { + _comment = value; + OnPropertyChanged(nameof(Comment)); + } + } + } + + public S7Symbol() + { + ObjectType = S7ObjectType.Symbol; + } + + // Sobrescribe el método de búsqueda de texto para incluir dirección y tipo de datos + public new bool ContainsText(string searchText) + { + if (base.ContainsText(searchText)) + return true; + + if (string.IsNullOrWhiteSpace(searchText)) + return false; + + searchText = searchText.ToLowerInvariant(); + + return Address?.ToLowerInvariant().Contains(searchText) == true || + DataType?.ToLowerInvariant().Contains(searchText) == true || + Comment?.ToLowerInvariant().Contains(searchText) == true; + } + } +} \ No newline at end of file diff --git a/Parsers/DbfParser.cs b/Parsers/DbfParser.cs new file mode 100644 index 0000000..3472b08 --- /dev/null +++ b/Parsers/DbfParser.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using NDbfReader; + +namespace S7Explorer.Parsers +{ + public class DbfParser + { + // Lee campos específicos de un archivo DBF + public static List> ReadDbfFile(string filePath, IEnumerable fieldNames) + { + var result = new List>(); + + if (!File.Exists(filePath)) + throw new FileNotFoundException($"No se encontró el archivo DBF: {filePath}"); + + try + { + // Abrir tabla DBF con codificación específica + using var stream = File.OpenRead(filePath); + using var table = Table.Open(stream); + + // Crear lector usando la API correcta + var reader = table.OpenReader(); + + while (reader.Read()) + { + var record = new Dictionary(); + + foreach (var fieldName in fieldNames) + { + // Obtener valor y convertir a string si no es null + var value = reader.GetValue(fieldName); + record[fieldName] = value?.ToString() ?? string.Empty; + + // Manejamos específicamente bytes para campos como "MC5CODE" + // que pueden contener datos binarios codificados como CP1252 + if (value is byte[] byteValue) + { + record[fieldName] = Encoding.GetEncoding(1252).GetString(byteValue); + } + } + + result.Add(record); + } + } + catch (Exception ex) + { + throw new Exception($"Error al leer el archivo DBF {filePath}: {ex.Message}", ex); + } + + return result; + } + + // Convierte un string que representa un número a un entero opcional + public static int? StringToInt(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return null; + + if (int.TryParse(value, out int result)) + return result; + + return null; + } + + // Convierte códigos Windows-1252 a UTF-8 para manejar caracteres especiales en STEP7 + public static string ConvertCP1252ToUtf8(string input) + { + if (string.IsNullOrEmpty(input)) + return string.Empty; + + byte[] bytes = Encoding.GetEncoding(1252).GetBytes(input); + return Encoding.UTF8.GetString(bytes); + } + } +} \ No newline at end of file diff --git a/Parsers/S7ProjectParser.cs b/Parsers/S7ProjectParser.cs new file mode 100644 index 0000000..ef598b5 --- /dev/null +++ b/Parsers/S7ProjectParser.cs @@ -0,0 +1,370 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using S7Explorer.Models; + +namespace S7Explorer.Parsers +{ + public class S7ProjectParser + { + private readonly string _projectFilePath; + private readonly string _projectDirectory; + + public S7ProjectParser(string projectFilePath) + { + _projectFilePath = projectFilePath; + _projectDirectory = Path.GetDirectoryName(projectFilePath); + + if (string.IsNullOrEmpty(_projectDirectory)) + throw new ArgumentException("No se pudo determinar el directorio del proyecto"); + } + + public async Task ParseProjectAsync() + { + return await Task.Run(() => + { + // Crear objeto de proyecto + var project = new S7Project(_projectFilePath); + + try + { + // Estructura básica del proyecto + var devicesFolder = new S7Object + { + Name = "Dispositivos", + ObjectType = S7ObjectType.Folder, + Parent = project + }; + project.Children.Add(devicesFolder); + + // Parsear dispositivos + ParseDevices(devicesFolder); + + // Para uso en el futuro: más carpetas de alto nivel + var sharedFolder = new S7Object + { + Name = "Datos Globales", + ObjectType = S7ObjectType.Folder, + Parent = project + }; + project.Children.Add(sharedFolder); + + return project; + } + catch (Exception ex) + { + // En caso de error, al menos retornamos un proyecto básico con el error + var errorObject = new S7Object + { + Name = "Error al parsear proyecto", + Description = ex.Message, + ObjectType = S7ObjectType.Folder, + Parent = project + }; + project.Children.Add(errorObject); + + return project; + } + }); + } + + private void ParseDevices(S7Object devicesFolder) + { + try + { + // Obtener lista de dispositivos a partir del archivo de información + var deviceIdInfos = ParseDeviceIdInfos(); + + foreach (var deviceInfo in deviceIdInfos) + { + var device = new S7Object + { + Name = deviceInfo.Name, + ObjectType = S7ObjectType.Device, + Parent = devicesFolder + }; + devicesFolder.Children.Add(device); + + // Crear carpetas para cada tipo de bloque + var dbFolder = CreateBlockFolder(device, "Bloques de datos (DB)"); + var fbFolder = CreateBlockFolder(device, "Bloques de función (FB)"); + var fcFolder = CreateBlockFolder(device, "Funciones (FC)"); + var obFolder = CreateBlockFolder(device, "Bloques de organización (OB)"); + var symbolsFolder = CreateBlockFolder(device, "Tabla de símbolos"); + + // Parsear bloques y símbolos si se dispone de IDs + if (deviceInfo.SymbolListId.HasValue) + { + ParseSymbols(symbolsFolder, deviceInfo.SymbolListId.Value); + } + + if (deviceInfo.SubblockListId.HasValue) + { + ParseBlocks(dbFolder, fbFolder, fcFolder, obFolder, deviceInfo.SubblockListId.Value); + } + } + } + catch (Exception ex) + { + // Si falla el parseo, añadimos un nodo de error pero seguimos con el resto + var errorNode = new S7Object + { + Name = "Error al parsear dispositivos", + Description = ex.Message, + ObjectType = S7ObjectType.Folder, + Parent = devicesFolder + }; + devicesFolder.Children.Add(errorNode); + } + } + + private S7Object CreateBlockFolder(S7Object parent, string name) + { + var folder = new S7Object + { + Name = name, + ObjectType = S7ObjectType.Folder, + Parent = parent + }; + parent.Children.Add(folder); + return folder; + } + + private List ParseDeviceIdInfos() + { + var result = new List(); + + // Esto es una simplificación - en una implementación real + // necesitarías parsear los archivos reales como en el proyecto C++ + var s7resoffPath = Path.Combine(_projectDirectory, "hrs", "S7RESOFF.DBF"); + + if (File.Exists(s7resoffPath)) + { + try + { + // Leer la tabla de dispositivos + var records = DbfParser.ReadDbfFile(s7resoffPath, new[] { "ID", "NAME", "RSRVD4_L" }); + + foreach (var record in records) + { + var device = new DeviceIdInfo + { + Name = DbfParser.ConvertCP1252ToUtf8(record["NAME"]), + }; + + // Procesamiento simplificado para IDs de Subblock y Symbol List + device.SubblockListId = DbfParser.StringToInt(record["RSRVD4_L"]); + + // NOTA: En una implementación completa, tendrías que leer linkhrs.lnk + // para obtener SubblockListId y SymbolListId reales + + result.Add(device); + } + } + catch (Exception) + { + // Error de parseo - ignorar y continuar + } + } + + // Si no se encuentran dispositivos, crear uno de ejemplo + if (result.Count == 0) + { + result.Add(new DeviceIdInfo + { + Name = "Dispositivo de ejemplo" + }); + } + + return result; + } + + private void ParseSymbols(S7Object symbolsFolder, int symbolListId) + { + // Esta es una implementación de muestra + // En una versión completa, leerías los archivos YDBs reales + + // Crear algunos símbolos de ejemplo + var symbols = new List + { + new S7Symbol { + Name = "Motor_Start", + Address = "I0.0", + DataType = "BOOL", + Comment = "Pulsador de inicio del motor" + }, + new S7Symbol { + Name = "Motor_Stop", + Address = "I0.1", + DataType = "BOOL", + Comment = "Pulsador de parada del motor" + }, + new S7Symbol { + Name = "Motor_Running", + Address = "Q0.0", + DataType = "BOOL", + Comment = "Motor en marcha" + }, + new S7Symbol { + Name = "Temperature", + Address = "IW64", + DataType = "INT", + Comment = "Temperatura del proceso" + }, + }; + + foreach (var symbol in symbols) + { + symbol.Parent = symbolsFolder; + symbolsFolder.Children.Add(symbol); + } + } + + private void ParseBlocks(S7Object dbFolder, S7Object fbFolder, S7Object fcFolder, S7Object obFolder, int subblockListId) + { + // Esta es una implementación de muestra + // En una versión completa, leerías los archivos SUBBLK.DBF reales + + // Crear algunos bloques de ejemplo + AddSampleDataBlocks(dbFolder); + AddSampleFunctionBlocks(fbFolder); + AddSampleFunctions(fcFolder); + AddSampleOrgBlocks(obFolder); + } + + private void AddSampleDataBlocks(S7Object dbFolder) + { + var dbs = new List + { + new S7DataBlock { + Name = "Datos_proceso", + Number = "DB1", + Size = 124, + IsInstanceDb = false, + Description = "Datos de proceso", + Modified = DateTime.Now.AddDays(-5) + }, + new S7DataBlock { + Name = "Parámetros", + Number = "DB2", + Size = 234, + IsInstanceDb = false, + Description = "Parámetros de configuración", + Modified = DateTime.Now.AddDays(-10) + }, + new S7DataBlock { + Name = "Motor_Inst", + Number = "DB10", + Size = 86, + IsInstanceDb = true, + Description = "Instancia de FB1", + Modified = DateTime.Now.AddDays(-2) + } + }; + + foreach (var db in dbs) + { + db.Parent = dbFolder; + dbFolder.Children.Add(db); + } + } + + private void AddSampleFunctionBlocks(S7Object fbFolder) + { + var fbs = new List + { + new S7FunctionBlock { + Name = "Motor_Control", + Number = "FB1", + Size = 328, + Language = "SCL", + Description = "Control de motor", + Modified = DateTime.Now.AddDays(-15) + }, + new S7FunctionBlock { + Name = "PID_Control", + Number = "FB2", + Size = 512, + Language = "SCL", + Description = "Controlador PID", + Modified = DateTime.Now.AddDays(-20) + } + }; + + foreach (var fb in fbs) + { + fb.Parent = fbFolder; + fbFolder.Children.Add(fb); + } + } + + private void AddSampleFunctions(S7Object fcFolder) + { + var fcs = new List + { + new S7Function { + Name = "Calc_Setpoint", + Number = "FC1", + Size = 124, + ReturnType = "REAL", + Description = "Cálculo de punto de consigna", + Modified = DateTime.Now.AddDays(-8) + }, + new S7Function { + Name = "Scale_Analog", + Number = "FC2", + Size = 68, + ReturnType = "REAL", + Description = "Escalado de valor analógico", + Modified = DateTime.Now.AddDays(-12) + } + }; + + foreach (var fc in fcs) + { + fc.Parent = fcFolder; + fcFolder.Children.Add(fc); + } + } + + private void AddSampleOrgBlocks(S7Object obFolder) + { + var obs = new List + { + new S7Block { + Name = "Main", + Number = "OB1", + Size = 256, + ObjectType = S7ObjectType.Organization, + Description = "Ciclo principal", + Modified = DateTime.Now.AddDays(-1) + }, + new S7Block { + Name = "Clock_100ms", + Number = "OB35", + Size = 124, + ObjectType = S7ObjectType.Organization, + Description = "Interrupción cíclica 100ms", + Modified = DateTime.Now.AddDays(-7) + } + }; + + foreach (var ob in obs) + { + ob.Parent = obFolder; + obFolder.Children.Add(ob); + } + } + + // Clase interna para la información de los dispositivos + private class DeviceIdInfo + { + public string Name { get; set; } + public int? SubblockListId { get; set; } + public int? SymbolListId { get; set; } + } + } +} \ No newline at end of file diff --git a/S7Explorer.csproj b/S7Explorer.csproj new file mode 100644 index 0000000..e2c67a4 --- /dev/null +++ b/S7Explorer.csproj @@ -0,0 +1,19 @@ + + + + WinExe + net8.0-windows + enable + enable + true + + + + + + + + + + + diff --git a/S7Explorer.sln b/S7Explorer.sln new file mode 100644 index 0000000..f44808e --- /dev/null +++ b/S7Explorer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35806.99 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S7Explorer", "S7Explorer.csproj", "{365DA498-EF12-4D2F-A8EA-200E47DFC0EA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {365DA498-EF12-4D2F-A8EA-200E47DFC0EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {365DA498-EF12-4D2F-A8EA-200E47DFC0EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {365DA498-EF12-4D2F-A8EA-200E47DFC0EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {365DA498-EF12-4D2F-A8EA-200E47DFC0EA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FD8C3796-EDDD-4A9E-A149-CD9F4DEB68EE} + EndGlobalSection +EndGlobal diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs new file mode 100644 index 0000000..aeb6e00 --- /dev/null +++ b/ViewModels/MainViewModel.cs @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using Ookii.Dialogs.Wpf; +using S7Explorer.Models; +using S7Explorer.Parsers; + +namespace S7Explorer.ViewModels +{ + public class MainViewModel : ObservableObject + { + private string _searchText; + private object _selectedObject; + private object _selectedTreeItem; + private S7Project _currentProject; + private ObservableCollection _projectStructure; + private string _projectInfo; + private bool _isLoading; + + public string SearchText + { + get => _searchText; + set => SetProperty(ref _searchText, value); + } + + public object SelectedObject + { + get => _selectedObject; + set => SetProperty(ref _selectedObject, value); + } + + public object SelectedTreeItem + { + get => _selectedTreeItem; + set + { + if (SetProperty(ref _selectedTreeItem, value)) + { + // Actualizar el objeto seleccionado para PropertyGrid + SelectedObject = _selectedTreeItem; + } + } + } + + public ObservableCollection ProjectStructure + { + get => _projectStructure; + set => SetProperty(ref _projectStructure, value); + } + + public string ProjectInfo + { + get => _projectInfo; + set => SetProperty(ref _projectInfo, value); + } + + public bool IsLoading + { + get => _isLoading; + set => SetProperty(ref _isLoading, value); + } + + public IRelayCommand OpenProjectCommand { get; } + public IRelayCommand SearchCommand { get; } + + public MainViewModel() + { + ProjectStructure = new ObservableCollection(); + OpenProjectCommand = new RelayCommand(OpenProject); + SearchCommand = new RelayCommand(SearchInProject); + ProjectInfo = "No hay proyecto abierto"; + } + + private void OpenProject() + { + var dialog = new VistaOpenFileDialog + { + Title = "Seleccionar archivo de proyecto STEP7", + Filter = "Proyectos STEP7 (*.s7p)|*.s7p|Todos los archivos (*.*)|*.*", + CheckFileExists = true + }; + + if (dialog.ShowDialog() == true) + { + LoadProjectAsync(dialog.FileName); + } + } + + private async void LoadProjectAsync(string filePath) + { + try + { + IsLoading = true; + ProjectInfo = "Cargando proyecto..."; + + // Reiniciar estado + ProjectStructure.Clear(); + _currentProject = null; + + // Parsear proyecto + var parser = new S7ProjectParser(filePath); + _currentProject = await parser.ParseProjectAsync(); + + // Actualizar UI + ProjectStructure.Add(_currentProject); + _currentProject.IsExpanded = true; + + // Actualizar info + ProjectInfo = $"Proyecto: {Path.GetFileNameWithoutExtension(filePath)}"; + } + catch (Exception ex) + { + MessageBox.Show($"Error al cargar el proyecto: {ex.Message}", "Error", + MessageBoxButton.OK, MessageBoxImage.Error); + ProjectInfo = "Error al cargar el proyecto"; + } + finally + { + IsLoading = false; + } + } + + private void SearchInProject() + { + if (_currentProject == null || string.IsNullOrWhiteSpace(SearchText)) + return; + + try + { + // Recopilar todos los objetos en el proyecto + var allObjects = GetAllObjects(_currentProject); + + // Buscar objetos que coincidan con el texto + var matchingObjects = allObjects.Where(o => o.ContainsText(SearchText)).ToList(); + + if (matchingObjects.Count == 0) + { + MessageBox.Show($"No se encontraron coincidencias para: {SearchText}", + "Búsqueda", MessageBoxButton.OK, MessageBoxImage.Information); + return; + } + + // Seleccionar el primer objeto coincidente y expandir su ruta + var firstMatch = matchingObjects.First(); + SelectAndExpandToObject(firstMatch); + + // Informar al usuario + if (matchingObjects.Count > 1) + { + MessageBox.Show($"Se encontraron {matchingObjects.Count} coincidencias. " + + "Mostrando la primera coincidencia.", + "Búsqueda", MessageBoxButton.OK, MessageBoxImage.Information); + } + } + catch (Exception ex) + { + MessageBox.Show($"Error durante la búsqueda: {ex.Message}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + + private IEnumerable GetAllObjects(S7Object root) + { + // Devolver el objeto raíz + yield return root; + + // Recursivamente devolver todos los hijos + foreach (var child in root.Children) + { + foreach (var obj in GetAllObjects(child)) + { + yield return obj; + } + } + } + + private void SelectAndExpandToObject(S7Object obj) + { + // Expandir todos los nodos padres hasta llegar al objeto + var parent = obj.Parent; + while (parent != null) + { + parent.IsExpanded = true; + parent = parent.Parent; + } + + // Seleccionar el objeto + SelectedTreeItem = obj; + } + } +} \ No newline at end of file diff --git a/ViewModels/S7ObjectViewModel.cs b/ViewModels/S7ObjectViewModel.cs new file mode 100644 index 0000000..37b79ef --- /dev/null +++ b/ViewModels/S7ObjectViewModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace S7Explorer.ViewModels +{ + class S7ObjectViewModel + { + } +} diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml new file mode 100644 index 0000000..cce7603 --- /dev/null +++ b/Views/MainWindow.xaml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + +