Incorperated rsi code parser

This commit is contained in:
Adam 2025-03-26 21:03:41 +00:00
parent 9587d27805
commit ffc40b2f88

69
src/RSIPI/krl_parser.py Normal file
View File

@ -0,0 +1,69 @@
import csv
import re
from collections import OrderedDict
class KRLParser:
"""Parses KUKA KRL .src and .dat files, extracting Cartesian coordinates into CSV."""
def __init__(self, src_file, dat_file):
self.src_file = src_file
self.dat_file = dat_file
self.positions = OrderedDict()
def parse_src(self):
"""Parses .src file and collects all positional references."""
pattern = re.compile(r"PDAT_ACT=PPDAT(\d+)")
with open(self.src_file, 'r') as file:
for line in file:
match = pattern.search(line)
if match:
pos_ref = f"PPDAT{match.group(1)}"
self.positions[pos_ref] = {}
def parse_dat(self):
"""Parses .dat file extracting all Cartesian coordinates."""
pos_pattern = re.compile(
r"DECL E6POS (XP\d+)=\{X ([^,]+),Y ([^,]+),Z ([^,]+),A ([^,]+),B ([^,]+),C ([^,]+),S ([^,]+),T ([^,]+)")
with open(self.dat_file, 'r') as file:
for line in file:
match = pos_pattern.search(line)
if match:
pos_name = match.group(1)
coords = {
'X': float(match.group(2)),
'Y': float(match.group(3)),
'Z': float(match.group(4)),
'A': float(match.group(5)),
'B': float(match.group(6)),
'C': float(match.group(7)),
'S': int(match.group(8)),
'T': int(match.group(9)),
}
if pos_name in self.positions:
self.positions[pos_name] = coords
def export_csv(self, output_file):
"""Exports the parsed positions to CSV."""
fieldnames = ["Sequence", "PosRef", "X", "Y", "Z", "A", "B", "C", "S", "T"]
with open(output_file, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for idx, (pos_ref, coords) in enumerate(self.positions.items()):
if coords:
writer.writerow({
"Sequence": idx,
"PosRef": pos_ref,
**coords
})
# Example usage:
if __name__ == "__main__":
parser = KRLParser("path/to/file.src", "path/to/file.dat")
parser.parse_src()
parser.parse_dat()
parser.export_csv("path/to/output.csv")