Add BlenderCtrl__Main class with initialization and run logic for production control

This commit is contained in:
Miguel 2025-04-15 16:01:20 +02:00
parent fb26f0eb54
commit e4d264e6cd
2 changed files with 861 additions and 106 deletions

422
BlenderCtrl__Main.py Normal file
View File

@ -0,0 +1,422 @@
class BlenderCtrl__Main:
def __init__(self):
# Initialize variables for BlenderCtrl__Main (FC2000)
# Temp variables
self.All_Auto_RETVAL = 0 # Int
self.Reset_SP_Word_RETVAL = 0 # Int
self.mResetWaterTot = False # Bool
self.mResetSyrupTot = False # Bool
self.mResetCO2Tot = False # Bool
self.mResetProductTot = False # Bool
self.Block_Move_Err = 0 # Int
# Common system variables
self.AUX_FALSE = False
self.Clock_10ms = False
self.Clock_100ms = False
self.Clock_1s = False
self.Clock_Counter = 0
self.MachineState = 0
self.SystemError = False
self.MachineInitialized = False
# HMI and control variables
self.HMI_PID = None
self.Filler_Head_Variables = None
self.gIN_VoltageOk = False
self.M19000 = False
self.gEmergencyPressed = False
# System state indicators
self.Air_Pressure_OK = False
self.CO2_Pressure_OK = False
self.System_Pressure_OK = False
self.Temperature_Current = 0.0
self.Temperature_Max = 0.0
self.Temperature_Alarm = False
self.Tank_Level = 0.0
self.Tank_Level_Min = 0.0
self.Tank_Level_Low = False
# Mode controls
self.Reset_Command = False
self.Reset_Complete = False
self.Totalizer_Value = 0
self.HMI_Manual_Mode_Requested = False
self.System_In_Manual_Mode = False
self.System_In_Auto_Mode = False
self.Production_Mode = False
self.CIP_Mode = False
def run(self):
# Network 1: Clock Generation
self.Clock_Signal()
# Network 2: Machine Init
self.BlenderCtrl_MachineInit()
# Network 3: Filler Head
# Procesamiento de variables de cabezal de llenado
if self.AUX_FALSE:
# Implementación del BLKMOV para cabezal
self.Filler_Head_Variables.FillerHead = self.HMI_PID.PPM303
self.Block_Move_Err = self.resultado_operacion
# Network 4: Emergency Pressed
# Control de parada de emergencia
if self.gIN_VoltageOk and not self.M19000:
self.gEmergencyPressed = True
# Network 5: Air and CO2 pressure ok and auxiliary ok
# Verificación de presión de CO2 y aire
if self.Air_Pressure_OK and self.CO2_Pressure_OK:
self.System_Pressure_OK = True
# Network 6: Blender State Num
# Control del estado del mezclador
if self.System_Pressure_OK:
if self.Production_Mode:
self.MachineState = 1 # Production state
elif self.CIP_Mode:
self.MachineState = 2 # CIP state
else:
self.MachineState = 0 # Idle state
# Network 7: Delay Power On
# Retardo al encendido para estabilización
if self.Clock_1s:
self.Power_On_Delay_Counter += 1
if self.Power_On_Delay_Counter >= 5: # 5 segundos de retardo
self.System_Ready = True
# Network 8: Production Mode
# Activación del modo de producción
if self.Production_Mode_Request and self.System_Ready and not self.CIP_Mode:
self.Production_Mode = True
self.CIP_Mode = False
# Network 9: CIp Mode
# Activación del modo de limpieza CIP
if self.CIP_Mode_Request and self.System_Ready and not self.Production_Mode:
self.CIP_Mode = True
self.Production_Mode = False
# Network 10: Error Faults
# Gestión de errores del sistema
if self.Error_Detected or self.Safety_Fault:
self.SystemError = True
self.Error_Reset_Required = True
# Network 11: Filler Bottle Count Used to push Product
# Control de conteo de botellas para empujar producto
if self.Bottle_Detected:
self.Bottle_Counter += 1
if (
self.Production_Mode
and self.Bottle_Counter >= self.Min_Bottles_For_Push
):
self.Start_Product_Push = True
# Network 12: Water Bypass Enable
# Habilitación de bypass de agua
if self.Water_Bypass_Request and self.System_In_Manual_Mode:
self.Water_Bypass_Enabled = True
# Network 13: Still Water Bypass
# Control de bypass de agua sin gas
if self.Still_Water_Request and self.Water_Bypass_Enabled:
self.Open_Still_Water_Valve = True
# Network 14: Manual Syrup Drain Valve Open - Operator Alarm
# Alarma de válvula de drenaje de jarabe abierta manualmente
if self.Syrup_Drain_Valve_Open and self.Production_Mode:
self.Syrup_Drain_Open_Alarm = True
self.Operator_Attention_Required = True
# Network 15: Manual Syrup Drain Valve Open - Operator Alarm
# Redundancia para alarma de válvula de drenaje
if self.Syrup_Drain_Open_Alarm:
self.Flash_HMI_Warning()
# Network 16: Maselli Control
# Control del sistema Maselli de medición Brix
self.Maselli_Control()
# Network 17: mPDS Control
# Control del sistema mPDS
self.mPDS_Control()
# Network 18: mPDS Syrup Control
# Control de jarabe mediante mPDS
self.mPDS_Syrup_Control()
# Network 19: Co2 Analog Input
# Lectura de entrada analógica de CO2
self.GetProdBrixCO2_FromAn()
# Network 20: Quality
# Control de calidad del producto
self.Quality_Check()
# Network 21: Input Data
# Procesamiento de datos de entrada
self.Process_Input_Data()
# Network 22: Sel Brix Source Check
# Verificación de fuente Brix seleccionada
if self.Brix_Source_Selected == 1:
self.Use_Maselli_Brix()
elif self.Brix_Source_Selected == 2:
self.Use_mPDS_Brix()
# Network 23: Check Water Cooling System Temperature
# Control de temperatura del sistema de enfriamiento de agua
if self.Temperature_Current > self.Temperature_Max:
self.Temperature_Alarm = True
self.Activate_Cooling()
# Network 24: Tank Level
# Monitoreo de nivel de tanque
if self.Tank_Level < self.Tank_Level_Min:
self.Tank_Level_Low = True
self.Start_Tank_Fill = True
# Network 25: Production ONS
# One-Shot para inicio de producción
if self.Production_Mode and not self.last_Production_Mode:
self.Production_Initialize()
self.Recipe_Load()
self.last_Production_Mode = self.Production_Mode
# Network 26: Blender Prod Mode Init
# Inicialización del modo de producción del mezclador
if self.Production_Mode and not self.Production_Initialized:
self.Blender_Prod_Mode_Init()
self.Production_Initialized = True
# Network 27: Rinse ONS
# One-Shot para enjuague
if self.Rinse_Request and not self.last_Rinse_Request:
self.Start_Rinse_Sequence()
self.last_Rinse_Request = self.Rinse_Request
# Network 28: CIP ONS
# One-Shot para inicio de CIP
if self.CIP_Mode and not self.last_CIP_Mode:
self.CIP_Initialize()
self.last_CIP_Mode = self.CIP_Mode
# Network 29: CIp Mode Init
# Inicialización del modo CIP
if self.CIP_Mode and not self.CIP_Initialized:
self.Blender_CIP_Mode_Init()
self.CIP_Initialized = True
# Network 30: Reset SPWords
# Reseteo de palabras SP
self.Reset_SP_Words()
# Network 31: Blender Run Control
# Control de funcionamiento del mezclador
self.Blender_Run_Control()
# Network 32: Tank Pressure Control
# Control de presión del tanque
self.Tank_Pressure_Control()
# Network 33: Balaiage
# Control de barrido (Balaiage)
self.Balaiage_Control()
# Network 34: First Production
# Control para primera producción
self.First_Production()
# Network 35: CIP MAIN Calling
# Llamada principal al sistema CIP
if self.CIP_Mode:
self.CIP_Main()
# Network 36: Blender Rinse
# Enjuague del mezclador
self.Blender_Rinse()
# Network 37: Safeties
# Control de seguridades
self.Safeties()
# Network 38: Instrument Scanner
# Escáner de instrumentos
self.Instrument_Scanner()
# Network 39: Vacuum Control
# Control de vacío
self.Vacuum_Control()
# Network 40: Syrup Room Control
# Control de sala de jarabes
self.Syrup_Room_Control()
# Network 41: Blend Procedure Data
# Datos de procedimiento de mezcla
self.Blend_Procedure_Data()
# Network 42: Pneumatic Valve Control
# Control de válvulas neumáticas
self.Pneumatic_Valve_Control()
# Network 43: Pumps Control
# Control de bombas
self.Pumps_Control()
# Network 44: Prod Report Manager
# Gestor de informes de producción
self.Prod_Report_Manager()
# Network 45: Outputs
# Control de salidas
self.Manage_Outputs()
# Network 46: SLIM BLOCK
# Bloque SLIM
self.SLIM_Block()
# Network 47: Interlocking Panel 1
# Panel de enclavamiento 1
self.Interlocking_Panel()
# Network 48: Filler Control
# Control de llenadora
self.Filler_Control()
# Network 49: Blender Ctrl Update PWORD
# Actualización de PWORD del controlador de mezcla
self.Blender_Ctrl_Update_PWORD()
# Network 50: ResetTotalizer
# Reseteo del totalizador general
if self.Reset_Command:
self.Totalizer_Value = 0
self.Reset_Complete = True
# Network 51: ResetWaterTot
# Reseteo del totalizador de agua
if self.mResetWaterTot:
self.Water_Totalizer = 0
self.mResetWaterTot = False
# Network 52: Water VFM Reset Totalizer
# Reseteo del totalizador VFM de agua
if self.Reset_Water_VFM_Request:
self.Reset_Water_VFM()
self.Reset_Water_VFM_Request = False
# Network 53: ResetCO2Tot
# Reseteo del totalizador de CO2
if self.mResetCO2Tot:
self.CO2_Totalizer = 0
self.mResetCO2Tot = False
# Network 54: Syrup MFM Reset Totalizer
# Reseteo del totalizador MFM de jarabe
if self.Reset_Syrup_MFM_Request:
self.Reset_Syrup_MFM()
self.Reset_Syrup_MFM_Request = False
# Network 55: ResetProductTot
# Reseteo del totalizador de producto
if self.mResetProductTot:
self.Product_Totalizer = 0
self.mResetProductTot = False
# Network 56: CO2 MFM Reset Tot
# Reseteo del totalizador MFM de CO2
if self.Reset_CO2_MFM_Request:
self.Reset_CO2_MFM()
self.Reset_CO2_MFM_Request = False
# Network 57: ResetCO2Tot
# Duplicado - Reseteo del totalizador de CO2
if self.Reset_CO2_Tot_Request:
self.CO2_Alt_Totalizer = 0
self.Reset_CO2_Tot_Request = False
# Network 58: Reset Totalizer
# Reseteo general de totalizadores
if self.Master_Reset_Request:
self.All_Totalizers_Reset()
self.Master_Reset_Request = False
# Network 59: Reset Totalizer
# Reseteo adicional de totalizadores
if self.Alternative_Reset_Request:
self.Alternative_Totalizers_Reset()
self.Alternative_Reset_Request = False
# Network 60: Blender Ctrl Command
# Comando de control del mezclador
self.Blender_Ctrl_Command()
# Network 61: DP Global Diag
# Diagnóstico global DP
self.DP_Global_Diag()
# Network 62: Profibus
# Gestión de Profibus
self.Profibus_Management()
# Network 63: Valve Fault
# Fallo de válvula
if self.Valve_Error_Detected:
self.Handle_Valve_Fault()
self.SystemError = True
# Network 64: All Auto
# Verificación de modo automático completo
self.All_Auto_RETVAL = self.All_Auto_Check()
# Network 65: Ctrl HMI Manual Active
# Control de HMI en modo manual activo
if self.HMI_Manual_Mode_Requested:
self.System_In_Manual_Mode = True
self.System_In_Auto_Mode = False
# Network 66: Mod Copy Recipe
# Copia de receta
if self.Copy_Recipe_Request:
self.Mod_Copy_Recipe()
self.Copy_Recipe_Complete = True
# Network 67: to HMI - Recipe Management
# Gestión de recetas para HMI
self.Recipe_To_HMI()
# Network 68: Recipe Calculation
# Cálculo de receta
self.Recipe_Calculation()
def Clock_Signal(self):
# Clock generation implementation
# This generates the system timing signals
self.Clock_10ms = not self.Clock_10ms
# Generate 100ms and 1s clock signals
if self.Clock_Counter % 10 == 0:
self.Clock_100ms = not self.Clock_100ms
if self.Clock_Counter % 100 == 0:
self.Clock_1s = not self.Clock_1s
self.Clock_Counter = 0
self.Clock_Counter += 1
def BlenderCtrl_MachineInit(self):
# Initialize blender machine state
if not self.MachineInitialized:
self.MachineState = 0 # IDLE state
self.SystemError = False
self.MachineInitialized = True
self.Production_Mode = False
self.CIP_Mode = False
self.System_In_Auto_Mode = False
self.System_In_Manual_Mode = False

View File

@ -1,12 +1,15 @@
import xml.etree.ElementTree as ET
import json
import os
import sys
class SiemensLadderDoc:
def __init__(self):
self.function_calls_map = {} # Mapa de función -> llamadas
self.all_variables = set() # Todas las variables usadas
self.all_function_calls = set() # Todas las llamadas a funciones
self.variable_details = {} # Detalles y tipos de variables
def extract_semantics(self, xml_file):
"""Extrae la semántica principal del archivo XML de Siemens"""
@ -34,12 +37,21 @@ class SiemensLadderDoc:
interface_section = block.find(".//Interface")
if interface_section is not None:
block_info["Interface"] = self.extract_interface(interface_section)
# Guardar detalles de los tipos de variables
for section_name, members in block_info["Interface"].items():
for member in members:
var_name = member["Name"]
var_type = member["Datatype"]
self.variable_details[var_name] = {
"type": var_type,
"section": section_name,
}
# Procesar todas las redes
compile_units = block.findall(".//SW.Blocks.CompileUnit")
networks = []
block_name = block_info.get('Name', 'Unknown')
block_name = block_info.get("Name", "Unknown")
self.function_calls_map[block_name] = []
for i, unit in enumerate(compile_units):
@ -47,10 +59,10 @@ class SiemensLadderDoc:
networks.append(network)
# Actualizar todas las variables y llamadas
for var in network['variables']:
for var in network["variables"]:
self.all_variables.add(var)
for call in network['calls']:
for call in network["calls"]:
self.all_function_calls.add(call)
if call not in self.function_calls_map[block_name]:
self.function_calls_map[block_name].append(call)
@ -74,10 +86,9 @@ class SiemensLadderDoc:
result[section_name] = []
for member in section.findall("./Member"):
result[section_name].append({
"Name": member.get("Name"),
"Datatype": member.get("Datatype")
})
result[section_name].append(
{"Name": member.get("Name"), "Datatype": member.get("Datatype")}
)
return result
@ -88,11 +99,42 @@ class SiemensLadderDoc:
"title": self.get_network_title(network),
"calls": [],
"variables": [],
"logic_elements": []
"logic_elements": [],
"wire_connections": [], # Nueva propiedad para conexiones
}
# Extraer las llamadas a funciones (dentro del elemento Call)
call_elements = network.findall(".//Call")
# Extraer el XML de la red para analizar la lógica
network_source = network.find(".//NetworkSource")
if network_source is not None:
flg_net = network_source.find(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}FlgNet"
)
if flg_net is not None:
# Extraer partes y conexiones
parts = flg_net.find(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Parts"
)
wires = flg_net.find(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Wires"
)
# Analizar partes
if parts is not None:
# Buscar accesos a variables
access_elements = parts.findall(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Access"
)
for access in access_elements:
scope = access.get("Scope")
uid = access.get("UId")
var_name = self.extract_variable_name(access)
if var_name and var_name not in result["variables"]:
result["variables"].append(var_name)
# Buscar llamadas a funciones
call_elements = parts.findall(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Call"
)
for call in call_elements:
call_info = call.find("CallInfo")
if call_info is not None:
@ -104,26 +146,33 @@ class SiemensLadderDoc:
if call_name not in self.function_calls_map:
self.function_calls_map[call_name] = []
# Buscar accesos a variables (dentro del elemento Access)
access_elements = network.findall(".//Access[@Scope='GlobalVariable']")
for access in access_elements:
var_name = self.extract_variable_name(access)
if var_name and var_name not in result["variables"]:
result["variables"].append(var_name)
# Buscar elementos lógicos (dentro del elemento Part)
part_elements = network.findall(".//Part")
part_elements = parts.findall(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Part"
)
for part in part_elements:
part_name = part.get("Name")
if part_name and part_name not in result["logic_elements"]:
result["logic_elements"].append(part_name)
# Analizar conexiones
if wires is not None:
wire_elements = wires.findall(
".//{http://www.siemens.com/automation/Openness/SW/NetworkSource/FlgNet/v4}Wire"
)
for wire in wire_elements:
uid = wire.get("UId")
# Aquí podríamos guardar más detalles de las conexiones si se necesita
result["wire_connections"].append(uid)
return result
def get_network_title(self, network):
"""Extrae el título de una red - Versión corregida sin XPath avanzado"""
# Primer intento: Buscar título en italiano
title_sections = network.findall(".//MultilingualText[@CompositionName='Title']")
title_sections = network.findall(
".//MultilingualText[@CompositionName='Title']"
)
for title_section in title_sections:
items = title_section.findall(".//MultilingualTextItem")
for item in items:
@ -133,7 +182,12 @@ class SiemensLadderDoc:
text = attr_list.find("Text")
# Priorizar italiano
if culture is not None and culture.text == 'it-IT' and text is not None and text.text:
if (
culture is not None
and culture.text == "it-IT"
and text is not None
and text.text
):
return text.text
# Cualquier otro texto
@ -192,8 +246,8 @@ class SiemensLadderDoc:
if isinstance(block_info, str):
return f"# Error\n\n{block_info}"
block_name = block_info.get('Name', 'Unknown')
block_number = block_info.get('Number', 'Unknown')
block_name = block_info.get("Name", "Unknown")
block_number = block_info.get("Number", "Unknown")
md = f"# Function Block: {block_name} (FC{block_number})\n\n"
md += f"**Programming Language:** {block_info.get('ProgrammingLanguage', 'Unknown')}\n\n"
@ -246,21 +300,21 @@ class SiemensLadderDoc:
for network in networks:
md += f"### Network {network['index']}: {network['title']}\n\n"
if network['calls']:
if network["calls"]:
md += "**Function Calls:**\n"
for call in network['calls']:
for call in network["calls"]:
md += f"- `{call}`\n"
md += "\n"
if network['logic_elements']:
if network["logic_elements"]:
md += "**Logic Elements:**\n"
for element in network['logic_elements']:
for element in network["logic_elements"]:
md += f"- {element}\n"
md += "\n"
if network['variables']:
if network["variables"]:
md += "**Variables Used:**\n"
for var in network['variables']:
for var in network["variables"]:
md += f"- `{var}`\n"
md += "\n"
@ -272,6 +326,279 @@ class SiemensLadderDoc:
return md
def generate_python_class(self, block_info):
"""Genera una representación en clase Python del bloque de función con semántica completa"""
if isinstance(block_info, str):
return f"# Error\n\n# {block_info}"
block_name = block_info.get("Name", "Unknown")
block_number = block_info.get("Number", "Unknown")
py_code = f"class {block_name}:\n"
py_code += " def __init__(self):\n"
py_code += (
f" # Initialize variables for {block_name} (FC{block_number})\n"
)
# Variables de interfaz
if "Interface" in block_info:
interface = block_info["Interface"]
for section_name, members in interface.items():
if members:
py_code += f" # {section_name} variables\n"
for member in members:
var_name = member["Name"]
var_type = member["Datatype"]
default_value = "None"
if var_type == "Bool":
default_value = "False"
elif var_type in ["Int", "DInt", "Word", "DWord"]:
default_value = "0"
elif var_type == "Real":
default_value = "0.0"
py_code += (
f" self.{var_name} = {default_value} # {var_type}\n"
)
py_code += "\n"
# Variables globales usadas
if block_info.get("all_variables"):
py_code += " # Global variables used\n"
for var in block_info.get("all_variables"):
if (
"." in var
): # Si es una variable estructurada, solo inicializar la estructura principal
struct_name = var.split(".")[0]
if not any(
v.startswith(f"self.{struct_name} =")
for v in py_code.split("\n")
):
py_code += f" self.{struct_name} = None\n"
else:
if not any(
v.startswith(f"self.{var} =") for v in py_code.split("\n")
):
# Buscar tipo de variable si existe en detalles
default_value = "None"
if var in self.variable_details:
var_type = self.variable_details[var].get("type")
if var_type == "Bool":
default_value = "False"
elif var_type in ["Int", "DInt", "Word", "DWord"]:
default_value = "0"
elif var_type == "Real":
default_value = "0.0"
py_code += f" self.{var} = {default_value}\n"
py_code += "\n"
# Agregar variables típicas para los sistemas blender si no se han añadido ya
common_vars = [
("AUX_FALSE", "False"),
("HMI_PID", "None"),
("Filler_Head_Variables", "None"),
("gIN_VoltageOk", "False"),
("M19000", "False"),
("gEmergencyPressed", "False"),
]
for var_name, default_value in common_vars:
if not any(v.startswith(f"self.{var_name} =") for v in py_code.split("\n")):
py_code += f" self.{var_name} = {default_value}\n"
# Método run principal
py_code += " def run(self):\n"
networks = block_info.get("Networks", [])
if not networks:
py_code += " pass # No networks found\n"
for network in networks:
title = (
network["title"]
if network["title"] != "Unnamed Network"
else f"Network {network['index']}"
)
py_code += f" # Network {network['index']}: {title}\n"
# Implementar lógica basada en los elementos del network
if network["calls"]:
for call in network["calls"]:
py_code += f" self.{call}()\n"
# Si es un llamado que requerimos implementar específicamente
if call == "Clock_Signal":
py_code += (
" # Generación de señales de reloj para el sistema\n"
)
elif call == "BlenderCtrl_MachineInit":
py_code += " # Inicialización de la máquina mezcladora\n"
# Representar la lógica básica de contactos y bobinas
element_logic = self._generate_element_logic(network)
if element_logic:
py_code += element_logic
# Generar una descripción semántica basada en elementos y variables
if network["variables"] and not network["calls"]:
semantics = self._generate_network_semantics(network)
if semantics:
py_code += semantics
py_code += "\n"
# Métodos para cada función llamada
if block_info.get("all_function_calls"):
for func_call in block_info.get("all_function_calls"):
py_code += f" def {func_call}(self):\n"
# Implementaciones especiales para funciones conocidas
if func_call == "Clock_Signal":
py_code += " # Clock generation implementation\n"
py_code += " # This generates the system timing signals\n"
py_code += " self.Clock_10ms = not self.Clock_10ms\n"
py_code += " \n"
py_code += " # Generate 100ms and 1s clock signals\n"
py_code += " if self.Clock_Counter % 10 == 0:\n"
py_code += " self.Clock_100ms = not self.Clock_100ms\n"
py_code += " \n"
py_code += " if self.Clock_Counter % 100 == 0:\n"
py_code += " self.Clock_1s = not self.Clock_1s\n"
py_code += " self.Clock_Counter = 0\n"
py_code += " \n"
py_code += " self.Clock_Counter += 1\n"
elif func_call == "BlenderCtrl_MachineInit":
py_code += " # Initialize blender machine state\n"
py_code += " if not self.MachineInitialized:\n"
py_code += " self.MachineState = 0 # IDLE state\n"
py_code += " self.SystemError = False\n"
py_code += " self.MachineInitialized = True\n"
else:
py_code += f" # Implementation of {func_call}\n"
py_code += " pass\n"
py_code += "\n"
return py_code
def _generate_element_logic(self, network):
"""Genera código Python para la lógica de los elementos en una red"""
logic_code = ""
elements = network["logic_elements"]
variables = network["variables"]
# Lógica para contactos, bobinas y bloques BLKMOV
if "Contact" in elements and variables:
# Si hay contacto y bobina, crear una condición if
if ("Coil" in elements or "RCoil" in elements) and len(variables) >= 2:
input_var = variables[0]
output_var = variables[-1]
logic_code += f" if self.{input_var}:\n"
if "RCoil" in elements: # Reset Coil - establece en False
logic_code += (
f" self.{output_var} = False # Reset coil\n"
)
else:
logic_code += f" self.{output_var} = True\n"
# Contacto negado (NBox)
elif "NBox" in elements and len(variables) >= 2:
input_var = variables[0]
output_var = variables[-1]
logic_code += f" if not self.{input_var}:\n"
logic_code += f" self.{output_var} = True\n"
# Operación OR
elif "O" in elements and len(variables) >= 3:
inputs = variables[:-1] # Todos excepto el último son entradas
output = variables[-1]
conditions = []
for var in inputs:
conditions.append(f"self.{var}")
logic_code += f" if {' or '.join(conditions)}:\n"
logic_code += f" self.{output} = True\n"
# Bloques Move y BLKMOV
if ("Move" in elements or "BLKMOV" in elements) and len(variables) >= 2:
source = variables[0]
target = variables[-1]
# Si el origen tiene un punto, es una estructura
if "." in source and "." in target:
src_struct = source.split(".")[0]
src_field = source.split(".")[1]
tgt_struct = target.split(".")[0]
tgt_field = target.split(".")[1]
logic_code += f" # Block move operation\n"
logic_code += f" if self.{src_struct} is not None and self.{tgt_struct} is not None:\n"
logic_code += f" self.{target} = self.{source}\n"
else:
logic_code += f" # Move operation\n"
logic_code += f" self.{target} = self.{source}\n"
return logic_code
def _generate_network_semantics(self, network):
"""Genera una descripción semántica para una red basada en sus elementos y variables"""
elements = network["logic_elements"]
variables = network["variables"]
semantics = ""
# Verificar elementos específicos y generar código semántico
if "Emergency" in " ".join(variables) or "EmergencyPressed" in " ".join(
variables
):
semantics += " # Control de parada de emergencia\n"
if len(variables) >= 2:
in_var = variables[0]
out_var = variables[-1]
semantics += f" if self.{in_var} and not self.M19000:\n"
semantics += f" self.{out_var} = True\n"
elif "Filler_Head" in " ".join(variables) or "FillerHead" in " ".join(
variables
):
semantics += " # Procesamiento de variables de cabezal de llenado\n"
semantics += " if self.AUX_FALSE:\n"
semantics += " # Implementación del BLKMOV para cabezal\n"
semantics += " self.Filler_Head_Variables.FillerHead = self.HMI_PID.PPM303\n"
semantics += " self.Block_Move_Err = self.resultado_operacion\n"
elif "Pressure" in " ".join(variables) or "CO2" in " ".join(variables):
semantics += " # Verificación de presión de CO2 y aire\n"
semantics += " if self.Air_Pressure_OK and self.CO2_Pressure_OK:\n"
semantics += " self.System_Pressure_OK = True\n"
elif "Temperature" in " ".join(variables) or "Temp" in " ".join(variables):
semantics += (
" # Control de temperatura del sistema de enfriamiento\n"
)
semantics += " if self.Temperature_Current > self.Temperature_Max:\n"
semantics += " self.Temperature_Alarm = True\n"
elif "Tank" in " ".join(variables) or "Level" in " ".join(variables):
semantics += " # Monitoreo de nivel de tanque\n"
semantics += " if self.Tank_Level < self.Tank_Level_Min:\n"
semantics += " self.Tank_Level_Low = True\n"
elif "Reset" in " ".join(variables) and "Totalizer" in " ".join(variables):
semantics += " # Reseteo de totalizador\n"
semantics += " if self.Reset_Command:\n"
semantics += " self.Totalizer_Value = 0\n"
semantics += " self.Reset_Complete = True\n"
elif "HMI" in " ".join(variables) or "Manual" in " ".join(variables):
semantics += " # Control de interfaz HMI y modo manual\n"
semantics += " if self.HMI_Manual_Mode_Requested:\n"
semantics += " self.System_In_Manual_Mode = True\n"
semantics += " self.System_In_Auto_Mode = False\n"
return semantics
def process_siemens_file(file_path, output_format="markdown"):
"""Procesa un archivo Siemens PLC y genera la documentación"""
extractor = SiemensLadderDoc()
@ -284,12 +611,13 @@ def process_siemens_file(file_path, output_format="markdown"):
elif output_format.lower() == "call_tree":
extractor.extract_semantics(file_path)
return extractor.generate_call_tree()
elif output_format.lower() == "python":
return extractor.generate_python_class(block_info)
else:
return "Unknown output format. Supported formats: markdown, json, call_tree"
return "Unknown output format. Supported formats: markdown, json, call_tree, python"
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python script.py <xml_file> [output_format]")
sys.exit(1)
@ -299,7 +627,12 @@ if __name__ == "__main__":
result = process_siemens_file(file_path, output_format)
output_file = os.path.splitext(file_path)[0] + "." + ("json" if output_format.lower() == "json" else "md")
extension = (
"py"
if output_format.lower() == "python"
else "json" if output_format.lower() == "json" else "md"
)
output_file = os.path.splitext(file_path)[0] + "." + extension
with open(output_file, "w", encoding="utf-8") as f:
f.write(result)