data_process_ui/app/script/load_measure_data.py
2026-01-12 09:21:42 +08:00

124 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#导入测量的轨道数据
import re
from PyQt6.QtWidgets import QFileDialog
import color
def get_filename(filename = None):
if not filename:
if hasattr(get_filename, "filename"):
path_hint = os.path.dirname(get_filename.filename)
else:
path_hint = ""
filename, _ = QFileDialog.getOpenFileName(window, "从DAT文件导入数据", path_hint, "DAT数据文件(*.dat *.DAT)")
if not filename:
return
if len(filename) < 4 or filename[-4:].lower() != ".dat":
filename += ".dat"
filename = filename.replace("\\", "\\\\")
filename = filename.replace("\"", "\\\"")
return filename
def load_measure_data(file_name):
with open(file_name, "r") as fobj:
raw = fobj.readlines()
flag = False
r1 = []
for line in raw:
line = line.strip()
match = re.match(r"For\s+M5\|Adr\s+", line)
if match is None:
raise RuntimeError("Unrecognized data: {line}")
m = match.group(0)
pos = len(m)
line = line[pos:]
match = re.match(r"\d+\|([0-9A-Z]+)\s+", line)
if match is None:
raise RuntimeError("Unrecognized data: {line}")
m = match.group(0)
pos = len(m)
line = line[pos:]
match = re.match(r"[0-9a-zA-Z\.\-]+", line)
if match is None:
raise RuntimeError("Unrecognized data: {line}")
label = match.group(0)
pos = len(label)
line = line[pos:]
match = re.match(r"KZ\d*", label)
if not match is None:
label = match.group(0)
pos = len(label)
line = line[pos:]
#print("控制点", label, line)
continue
if label == "Intermediate":
flag = True
continue
if label == "End":
flag = False
continue
if not flag:
continue
match = re.match(r"[A-Z]*\d+[\.\-]\d+", label)
if match is None:
continue
data = line.split("|")
data = data[3]
match = re.match(r"[0-9a-zA-Z]+\s+(\d+\.\d+)", data)
if match is None:
raise RuntimeError(f"找不到所需数据: {data}")
data = match.group(1)
#print("轨道点", label, data)
r1.append([label, data])
r2 = []
for label, data in r1:
match = re.match(r"([A-Z]*)(\d+)[\.\-](\d+)", label)
l = match.group(1)
d1 = int(match.group(2))
d2 = int(match.group(3))
d3 = float(data)
r2.append([l, d1, d2, d3])
r2.sort(key = lambda x: (x[1], x[2]))
# for line in r2:
# print(f"{line[0]}{line[1]}.{line[2]} {line[3]}")
return r2
filename = get_filename()
if filename:
#轨道数据表
sheet_measure = sheet("measure_data")
data = load_measure_data(filename)
sheet_measure.clear_content()
with sheet_measure:
for line in data:
label = "%s%d.%d" % tuple(line[:3])
row = line[1] - 1
flag = line[2] > 1
if line[2] > 2:
print(f"{color.yellow}警告{color.nc}:编号{label}第2个数字大于2当作右轨处理")
data = line[3]
if flag:
cell(row, 2).value = label
cell(row, 3).value = data
else:
cell(row, 0).value = label
cell(row, 1).value = data
sheet_measure.fit_content()