Untitled
unknown
plain_text
a year ago
4.4 kB
15
Indexable
def parse_gcode(file_path, status_var):
global newGcode
tcp_coords = []
z_position = 0
x_position = 0
y_position = 0
with open(file_path, 'r') as file:
lines = file.readlines()
total_lines = len(lines)
for idx, line in enumerate(lines):
line = line.strip()
status_var.set(f"Analyzing G-code: {idx + 1}/{total_lines}")
print(f"Analyzing G-code: {idx + 1}/{total_lines}")
if 'G0' in line or 'G1' in line:
x = y = None
match = re.search(r'X([-+]?[0-9]*\.?[0-9]+)', line)
if match:
x = float(match.group(1))
x_position = x
match = re.search(r'Y([-+]?[0-9]*\.?[0-9]+)', line)
if match:
y = float(match.group(1))
y_position = y
match = re.search(r'Z([-+]?[0-9]*\.?[0-9]+)', line)
if match:
z_position = float(match.group(1))
coord = [1, x or x_position, y or y_position, z_position, 180, 0, -90, 0, 0, 0]
tcp_coords.append(coord)
elif 'G2' in line or 'G3' in line:
arc_command = 2 if 'G2' in line else 3
x = y = i = j = None
match = re.search(r'X([-+]?[0-9]*\.?[0-9]+)', line)
if match:
x = float(match.group(1))
match = re.search(r'Y([-+]?[0-9]*\.?[0-9]+)', line)
if match:
y = float(match.group(1))
match = re.search(r'I([-+]?[0-9]*\.?[0-9]+)', line)
if match:
i = float(match.group(1))
match = re.search(r'J([-+]?[0-9]*\.?[0-9]+)', line)
if match:
j = float(match.group(1))
center_x = x_position + i
center_y = y_position + j
start_x = x_position
start_y = y_position
# Calculate the radius
radius = sqrt((start_x - center_x) ** 2 + (start_y - center_y) ** 2)
if x is None and y is None:
# Full circle case
arc_command = 3
end_x = start_x
end_y = start_y
# Calculate a point 90 degrees from the start point as the midpoint
mid_angle = atan2(start_y - center_y, start_x - center_x) + pi / 2
mid_x = center_x + radius * cos(mid_angle)
mid_y = center_y + radius * sin(mid_angle)
coord = [arc_command, mid_x, mid_y, z_position, 180, 0, -90, end_x, end_y, z_position, 180, 0, -90]
else:
# Calculate start and end angles
start_angle = atan2(start_y - center_y, start_x - center_x)
end_angle = atan2(y - center_y, x - center_x)
# Ensure the angles are in the correct range
if arc_command == 2: # Clockwise
if start_angle < end_angle:
start_angle += 2 * pi
else: # Counterclockwise
if end_angle < start_angle:
end_angle += 2 * pi
# Calculate the midpoint angle
mid_angle = (start_angle + end_angle) / 2
# Calculate the midpoint coordinates
mid_x = center_x + radius * cos(mid_angle)
mid_y = center_y + radius * sin(mid_angle)
coord = [arc_command, mid_x, mid_y, z_position, 180, 0, -90, x or 0, y or 0, z_position, 180, 0, -90]
tcp_coords.append(coord)
# Update the current position
if x is not None:
x_position = x
if y is not None:
y_position = y
status_var.set("G-code analysis complete.")
print("G-code analysis complete. Result:")
for coord in tcp_coords:
print(coord)
newGcode = 1
# Create a simple G-code file and export it to the root directory
with open('simplified_gcode.txt', 'w') as outfile:
for coord in tcp_coords:
outfile.write(f'{coord}\n')
return tcp_coords
Editor is loading...
Leave a Comment