Skip to content

Instantly share code, notes, and snippets.

@luisdamed
Created April 21, 2023 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisdamed/59cfce0723614be341d1e06212236d0e to your computer and use it in GitHub Desktop.
Save luisdamed/59cfce0723614be341d1e06212236d0e to your computer and use it in GitHub Desktop.
Get the size of a 3D print job from the G-Code. Works for Verbosed G-Code from Prusa Slicer (tested on version 2.5.0+win64)
# Read the details about this on www.makerluis.com/getting-the-size-of-3d-print-from-gcode
def get_max_size(text):
pattern_xy= 'G1.X(\d*.\d*).Y(\d*.\d*).E\d*.\d*.;.perimeter'
pattern_z = 'G1.Z(\d*.\d*).F\d*.\d*.;.restore.layer.Z'
pattern_width = ';TYPE:Perimeter\n;WIDTH:(\d*.\d*).'
matches_xy = re.findall(pattern_xy, text)
x = list(list(zip(*matches_xy))[0])
y = list(list(zip(*matches_xy))[1])
x = [float(el) for el in x]
y = [float(el) for el in y]
matches_z = re.findall(pattern_z, text)
z = [float(el) for el in matches_z]
matches_width = re.findall(pattern_width, text)
widths = [float(el) for el in matches_width]
avg_width = sum(widths)/len(widths)
print(f'Avg. width: {avg_width:.2f} mm')
x_max = max(x) - min(x) + avg_width
y_max = max(y) - min(y) + avg_width
z.sort()
z_max = z[-1]
return f'Max. X dimension: {x_max:.2f} mm\n'\
f'Max. Y dimension: {y_max:.2f} mm\n'\
f'Max. Z dimension: {z_max:.2f} mm\n'\
import re
input_file = 'Shape-Box_0.2mm_PLA_MINI_25m.gcode'
with open(input_file, 'r') as f:
lines = f.readlines() # List of strings, one for each line
text_string = ''.join(lines) # Large string containing all the text
print(get_max_size(text_string))
# Uncomment the following in case you want to time this script
#import timeit
#num_tests = 100
#test_time = timeit.timeit('print(get_max_size(text_string))',
# setup='from __main__ import get_max_size, text_string',
# number=num_tests)
#print(f'Number of lines of G-Code file is: {len(lines)}')
#print(f'Average execution time is {test_time/num_tests} seconds, tested {num_tests} times')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment