Skip to content

Instantly share code, notes, and snippets.

@guyzmo
Created December 17, 2016 15:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save guyzmo/1bae3460cfd782336e487e61adfe84a0 to your computer and use it in GitHub Desktop.
Save guyzmo/1bae3460cfd782336e487e61adfe84a0 to your computer and use it in GitHub Desktop.
Gcode Plotter
#!/usr/bin/env python
from __future__ import unicode_literals, print_function
import sys
import matplotlib.pyplot as plt
import csv
import re
G1XY_RE = re.compile('G[01] X(?P<X>[0-9.]*) Y(?P<Y>[0-9.]*).*')
G1Z_RE = re.compile('G[01] .*Z(?P<Z>[0-9.]*).*')
G1E_RE = re.compile('G[01] .*E(?P<E>[0-9.]*).*')
G1COLORS_RE = re.compile('G[01] .* c(?P<c>[0-9.]*) m(?P<m>[0-9.]*) y(?P<y>[0-9.]*) k(?P<k>[0-9.]*) w(?P<w>[0-9.]*)')
def getColumns(filename):
xy_axis = ([], [])
z_axis = ([],)
e_axis = ([],)
color_axis = ([], [], [], [], [])
with open(filename, 'r') as f:
for l in f:
if l.startswith('G1'):
m = G1XY_RE.match(l)
if m:
x, y = m.groups()
xy_axis[0].append(float(x))
xy_axis[1].append(float(y))
m = G1Z_RE.match(l)
if m:
z = m.groups()[0]
z_axis[0].append(float(z))
m = G1E_RE.match(l)
if m:
e = m.groups()[0]
e_axis[0].append(float(e))
m = G1COLORS_RE.match(l)
if m:
colors = m.groups()
for i in range(0,5):
color_axis[i].append(colors[i])
return xy_axis, z_axis, color_axis
def run(filename, opt):
print("Parsing file...")
g1xy, g1z, g1colors = getColumns(filename)
print("Plotting...")
if opt == 'colors':
plt.plot(g1colors[0],label="C-Axis")
plt.plot(g1colors[1],label="M-Axis")
plt.plot(g1colors[2],label="Y-Axis")
plt.plot(g1colors[3],label="K-Axis")
plt.plot(g1colors[4],label="W-Axis")
plt.show()
elif opt == 'xy':
plt.plot(g1xy[0],label="X-Axis")
plt.plot(g1xy[1],label="Y-Axis")
plt.show()
elif opt == 'z':
plt.plot(g1z[0],label="Z-Axis")
plt.show()
elif opt == 'e':
plt.plot(g1e[0],label="E-Axis")
plt.show()
else:
print("Usage: {} [xy|z|e|colors] filename".format(sys.argv[0]))
sys.exit(2)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: {} [xy|z|e|colors] filename".format(sys.argv[0]))
sys.exit(1)
run(sys.argv[2], sys.argv[1])
@mineevburyat
Copy link

Thanks for the hint!! I will use your idea using mathplot in my project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment