From e4a15be0802ef681d79199e2893683742ccb8804 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 15 Apr 2025 16:45:50 +0200 Subject: [PATCH] Refactor wire element handling and improve MOVE enabling logic in SCL parser --- LadderToPython.py | 105 +++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/LadderToPython.py b/LadderToPython.py index ba823b1..cde0701 100644 --- a/LadderToPython.py +++ b/LadderToPython.py @@ -157,7 +157,7 @@ def parse_siemens_lad_to_scl(xml_file, debug=True): debug_print(f"No FlgNet found for network {i+1}", debug) continue - # Get FlgNet namespace - THIS IS THE KEY FIX + # Get FlgNet namespace flgnet_ns = {} if flg_net.nsmap: debug_print(f"FlgNet has its own namespace: {flg_net.nsmap}", debug) @@ -208,6 +208,18 @@ def parse_siemens_lad_to_scl(xml_file, debug=True): debug_print(f"Successfully found Parts and Wires elements", debug) + # Get all wire elements + wire_elements = [] + wire_elements.extend(wires.findall("Wire")) + if flgnet_ns: + wire_elements.extend(wires.findall("flg:Wire", namespaces=flgnet_ns)) + + # Try XPath with any namespace + if not wire_elements: + for child in wires: + if child.tag.endswith("Wire"): + wire_elements.append(child) + # Process function calls (CALL instructions) call_elements = [] # Try both direct and with namespace @@ -252,19 +264,6 @@ def parse_siemens_lad_to_scl(xml_file, debug=True): # Check if this call is connected to a powerrail (unconditional execution) is_enabled = False - wire_elements = [] - wire_elements.extend(wires.findall("Wire")) - if flgnet_ns: - wire_elements.extend( - wires.findall("flg:Wire", namespaces=flgnet_ns) - ) - - # Try XPath with any namespace - if not wire_elements: - for child in wires: - if child.tag.endswith("Wire"): - wire_elements.append(child) - for wire in wire_elements: # Check if wire has a powerrail source powerrail = wire.find("Powerrail") or ( @@ -338,38 +337,32 @@ def parse_siemens_lad_to_scl(xml_file, debug=True): # Check if MOVE is directly enabled is_enabled = False - for wire in wire_elements: - powerrail = wire.find("Powerrail") or ( - wire.find("flg:Powerrail", namespaces=flgnet_ns) - if flgnet_ns - else None - ) - # Try XPath with any namespace - if powerrail is None: - for child in wire: - if child.tag.endswith("Powerrail"): - powerrail = child - break + # Debug all wires to find the one connected to this move + for wire_idx, wire in enumerate(wire_elements): + debug_print(f"Checking wire {wire_idx} for MOVE {move_uid}", debug) + + # Check for powerrail + powerrail = None + for child in wire: + if child.tag.endswith("Powerrail"): + powerrail = child + debug_print(f" Found powerrail in wire {wire_idx}", debug) + break if powerrail is None: continue - name_con = wire.find(f"NameCon[@UId='{move_uid}'][@Name='en']") or ( - wire.find( - f"flg:NameCon[@UId='{move_uid}'][@Name='en']", - namespaces=flgnet_ns, - ) - if flgnet_ns - else None - ) - - # Try XPath with any namespace - if name_con is None: - for child in wire: + # Check for connection to this move + name_con = None + for child in wire: + if child.tag.endswith("NameCon"): + debug_print( + f" Found NameCon: UId={child.get('UId')}, Name={child.get('Name')}", + debug, + ) if ( - child.tag.endswith("NameCon") - and child.get("UId") == move_uid + child.get("UId") == move_uid and child.get("Name") == "en" ): name_con = child @@ -377,9 +370,33 @@ def parse_siemens_lad_to_scl(xml_file, debug=True): if name_con is not None: is_enabled = True - debug_print("MOVE is directly enabled by powerrail", debug) + debug_print( + f"MOVE with UId {move_uid} is directly enabled by powerrail in wire {wire_idx}", + debug, + ) break + if not is_enabled: + debug_print( + f"MOVE with UId {move_uid} is not directly enabled, trying alternative search", + debug, + ) + + # Try a more direct XML search + for wire in wire_elements: + wire_str = etree.tostring(wire).decode("utf-8") + if ( + f'UId="{move_uid}"' in wire_str + and 'Name="en"' in wire_str + and "