Files
AIO_3D_Print_Local_Screen/refer/GcodeParser.py
2026-05-09 16:36:21 +08:00

45 lines
1.0 KiB
Python

import re
def load_gcode_vertices(path):
vertices = []
x = 0
y = 0
z = 0
with open(path, "r", encoding="utf-8", errors="ignore") as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith("G0") or line.startswith("G1"):
old_x = x
old_y = y
old_z = z
mx = re.search(r"X([-0-9.]+)", line)
my = re.search(r"Y([-0-9.]+)", line)
mz = re.search(r"Z([-0-9.]+)", line)
if mx:
x = float(mx.group(1))
if my:
y = float(my.group(1))
if mz:
z = float(mz.group(1))
vertices.append({
"x1": old_x,
"y1": old_y,
"z1": old_z,
"x2": x,
"y2": y,
"z2": z,
})
return vertices