81 lines
2.8 KiB
C#
81 lines
2.8 KiB
C#
|
|
using System;
|
|
using System.IO;
|
|
using Siemens.Engineering;
|
|
using Siemens.Engineering.HW.Features;
|
|
|
|
class SimaticSDExporter
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length < 2)
|
|
{
|
|
Console.WriteLine("Uso: SimaticSDExporter.exe <proyecto.ap20> <directorio_export>");
|
|
return;
|
|
}
|
|
|
|
string projectPath = args[0];
|
|
string exportDir = args[1];
|
|
|
|
TiaPortal portal = null;
|
|
Project project = null;
|
|
|
|
try
|
|
{
|
|
// Abrir TIA Portal V20
|
|
portal = TiaPortal.Open(TiaPortalMode.WithUserInterface);
|
|
Console.WriteLine("✅ TIA Portal V20 abierto");
|
|
|
|
// Abrir proyecto
|
|
project = portal.Projects.Open(projectPath);
|
|
Console.WriteLine($"✅ Proyecto abierto: {project.Name}");
|
|
|
|
// Buscar PLCs
|
|
foreach (Device device in project.Devices)
|
|
{
|
|
foreach (DeviceItem deviceItem in device.DeviceItems)
|
|
{
|
|
if (deviceItem.GetService<SoftwareContainer>() != null)
|
|
{
|
|
var sw = deviceItem.GetService<SoftwareContainer>().Software;
|
|
Console.WriteLine($"Procesando PLC: {deviceItem.Name}");
|
|
|
|
// Exportar bloques en SIMATIC SD
|
|
foreach (var block in sw.BlockGroup.Blocks)
|
|
{
|
|
try
|
|
{
|
|
string blockExportDir = Path.Combine(exportDir, deviceItem.Name, "SIMATIC_SD");
|
|
Directory.CreateDirectory(blockExportDir);
|
|
|
|
// ¡AQUÍ ESTÁ EL MÉTODO SIMATIC SD!
|
|
block.ExportAsDocuments(
|
|
new DirectoryInfo(blockExportDir),
|
|
block.Name
|
|
);
|
|
|
|
Console.WriteLine($"✅ {block.Name} exportado en SIMATIC SD");
|
|
}
|
|
catch (Exception blockEx)
|
|
{
|
|
Console.WriteLine($"❌ Error con {block.Name}: {blockEx.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("🎉 Exportación SIMATIC SD completada");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"❌ Error: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
project?.Close();
|
|
portal?.Close();
|
|
}
|
|
}
|
|
}
|