ignition_scripts/COMPARE_SVG_DWG/compare_dwg_vs_svg.py
2026-01-20 08:19:45 +04:00

121 lines
2.7 KiB
Python

import csv
import xml.etree.ElementTree as ET
from pathlib import Path
import re
# -----------------------------
# CONFIG
# -----------------------------
DWG_TXT_PATH = Path("AMZ_TPA8_SYSDL_MCM01.txt")
SVG_PATH = Path("MCM01.svg")
# SVG suffixes that mean "same device"
SVG_SUFFIXES = (
"_ASSEMBLY",
"_END",
"_LINE",
"_OUT",
"_RECT",
"_CIRCLE",
"_TRIBOTLEFT",
"_TRITOPRIGHT",
)
# -----------------------------
# NORMALIZATION
# -----------------------------
def normalize_name(name: str) -> str:
if not name:
return ""
n = name.strip().upper()
# ---- VFD1 (DWG) vs VFD (SVG)
n = re.sub(r"_VFD1$", "_VFD", n)
# ---- Strip SVG part suffixes
for suffix in SVG_SUFFIXES:
if n.endswith(suffix):
n = n[: -len(suffix)]
break
return n
# -----------------------------
# LOAD DWG
# -----------------------------
def load_dwg_devices(path: Path) -> set[str]:
devices = set()
with path.open("r", encoding="utf-8", errors="ignore") as f:
reader = csv.DictReader(f, delimiter="\t")
if "P_TAG1" not in reader.fieldnames:
raise RuntimeError(
f"P_TAG1 column not found. Found: {reader.fieldnames}"
)
for row in reader:
raw = row.get("P_TAG1", "")
if raw and raw != "<>":
devices.add(normalize_name(raw))
return devices
# -----------------------------
# LOAD SVG
# -----------------------------
def load_svg_devices(path: Path) -> set[str]:
devices = set()
tree = ET.parse(path)
root = tree.getroot()
for elem in root.iter():
elem_id = elem.attrib.get("id")
if elem_id:
devices.add(normalize_name(elem_id))
return devices
# -----------------------------
# MAIN
# -----------------------------
dwg_devices = load_dwg_devices(DWG_TXT_PATH)
svg_devices = load_svg_devices(SVG_PATH)
missing_in_svg = sorted(dwg_devices - svg_devices)
extra_in_svg = sorted(svg_devices - dwg_devices)
# -----------------------------
# REPORT
# -----------------------------
print("====== DWG vs SVG DEVICE CHECK (NORMALIZED) ======")
print(f"DWG logical devices: {len(dwg_devices)}")
print(f"SVG logical devices: {len(svg_devices)}")
print()
# ---- EXTRA FIRST
if extra_in_svg:
print(f"⚠ Extra SVG devices ({len(extra_in_svg)}):")
for d in extra_in_svg:
print(f" - {d}")
else:
print("✅ No extra SVG devices")
print()
# ---- MISSING LAST
if missing_in_svg:
print(f"❌ Missing in SVG ({len(missing_in_svg)}):")
for d in missing_in_svg:
print(f" - {d}")
else:
print("✅ No missing DWG devices in SVG")
print("=================================================")