Refactor wire element handling and improve MOVE enabling logic in SCL parser

This commit is contained in:
Miguel 2025-04-15 16:45:50 +02:00
parent 0f1d00da1b
commit e4a15be080
1 changed files with 62 additions and 43 deletions

View File

@ -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 "<Powerrail" in wire_str
):
is_enabled = True
debug_print(
f"Found MOVE {move_uid} enabled by powerrail using string search",
debug,
)
break
if not is_enabled:
debug_print("MOVE is not directly enabled, skipping", debug)
continue
@ -435,7 +452,8 @@ def parse_siemens_lad_to_scl(xml_file, debug=True):
if source_uid:
source_access = parts.find(f"Access[@UId='{source_uid}']") or (
parts.find(
f"flg:Access[@UId='{source_uid}']", namespaces=flgnet_ns
f"flg:Access[@UId='{source_uid}']",
namespaces=flgnet_ns,
)
if flgnet_ns
else None
@ -596,7 +614,8 @@ def parse_siemens_lad_to_scl(xml_file, debug=True):
if dest_uid:
dest_access = parts.find(f"Access[@UId='{dest_uid}']") or (
parts.find(
f"flg:Access[@UId='{dest_uid}']", namespaces=flgnet_ns
f"flg:Access[@UId='{dest_uid}']",
namespaces=flgnet_ns,
)
if flgnet_ns
else None