Commit b367fa2a authored by Guillaume Seguin's avatar Guillaume Seguin

Cleanup gcoder.py indent

parent b3a673bc
...@@ -19,340 +19,340 @@ import re ...@@ -19,340 +19,340 @@ import re
import math import math
def deltalen(a,b): def deltalen(a,b):
d = object() d = object()
d.x = b.x - a.x d.x = b.x - a.x
d.y = b.y - a.y d.y = b.y - a.y
d.z = b.z - a.z d.z = b.z - a.z
return math.sqrt((d.x*d.x)+(d.y*d.y)+(d.z*d.z)) return math.sqrt((d.x*d.x)+(d.y*d.y)+(d.z*d.z))
class Line(object): class Line(object):
def __init__(self,l): def __init__(self,l):
self._x = None self._x = None
self._y = None self._y = None
self._z = None self._z = None
self.e = None self.e = None
self.f = 0 self.f = 0
self.regex = re.compile("[-]?\d+[.]?\d*") self.regex = re.compile("[-]?\d+[.]?\d*")
self.raw = l.upper().lstrip() self.raw = l.upper().lstrip()
self.imperial = False self.imperial = False
self.relative = False self.relative = False
self.relative_e = False self.relative_e = False
if ";" in self.raw: if ";" in self.raw:
self.raw = self.raw.split(";")[0] self.raw = self.raw.split(";")[0]
self._parse_coordinates() self._parse_coordinates()
def _to_mm(self,v): def _to_mm(self,v):
if v and self.imperial: if v and self.imperial:
return v*25.4 return v*25.4
return v return v
def _getx(self): def _getx(self):
return self._to_mm(self._x) return self._to_mm(self._x)
def _setx(self,v): def _setx(self,v):
self._x = v self._x = v
def _gety(self): def _gety(self):
return self._to_mm(self._y) return self._to_mm(self._y)
def _sety(self,v): def _sety(self,v):
self._y = v self._y = v
def _getz(self): def _getz(self):
return self._to_mm(self._z) return self._to_mm(self._z)
def _setz(self,v): def _setz(self,v):
self._z = v self._z = v
def _gete(self): def _gete(self):
return self._to_mm(self._e) return self._to_mm(self._e)
def _sete(self,v): def _sete(self,v):
self._e = v self._e = v
x = property(_getx,_setx) x = property(_getx,_setx)
y = property(_gety,_sety) y = property(_gety,_sety)
z = property(_getz,_setz) z = property(_getz,_setz)
e = property(_gete,_sete) e = property(_gete,_sete)
def command(self): def command(self):
try: try:
return self.raw.split(" ")[0] return self.raw.split(" ")[0]
except: except:
return "" return ""
def _get_float(self,which): def _get_float(self,which):
try: try:
return float(self.regex.findall(self.raw.split(which)[1])[0]) return float(self.regex.findall(self.raw.split(which)[1])[0])
except: except:
return None return None
def _parse_coordinates(self): def _parse_coordinates(self):
try: try:
if "X" in self.raw: if "X" in self.raw:
self._x = self._get_float("X") self._x = self._get_float("X")
except: except:
pass pass
try: try:
if "Y" in self.raw: if "Y" in self.raw:
self._y = self._get_float("Y") self._y = self._get_float("Y")
except: except:
pass pass
try: try:
if "Z" in self.raw: if "Z" in self.raw:
self._z = self._get_float("Z") self._z = self._get_float("Z")
except: except:
pass pass
try: try:
if "E" in self.raw: if "E" in self.raw:
self.e = self._get_float("E") self.e = self._get_float("E")
except: except:
pass pass
try: try:
if "F" in self.raw: if "F" in self.raw:
self.f = self._get_float("F") self.f = self._get_float("F")
except: except:
pass pass
def is_move(self): def is_move(self):
return self.command() and ("G1" in self.raw or "G0" in self.raw) return self.command() and ("G1" in self.raw or "G0" in self.raw)
def __str__(self): def __str__(self):
return self.raw return self.raw
class Layer(object): class Layer(object):
def __init__(self,lines): def __init__(self,lines):
self.lines = lines self.lines = lines
def measure(self): def measure(self):
xmin = 999999999 xmin = 999999999
ymin = 999999999 ymin = 999999999
zmin = 0 zmin = 0
xmax = -999999999 xmax = -999999999
ymax = -999999999 ymax = -999999999
zmax = -999999999 zmax = -999999999
relative = False relative = False
relative_e = False relative_e = False
current_x = 0 current_x = 0
current_y = 0 current_y = 0
current_z = 0 current_z = 0
for line in self.lines: for line in self.lines:
if line.command() == "G92": if line.command() == "G92":
current_x = line.x or current_x current_x = line.x or current_x
current_y = line.y or current_y current_y = line.y or current_y
current_z = line.z or current_z current_z = line.z or current_z
if line.is_move(): if line.is_move():
x = line.x x = line.x
y = line.y y = line.y
z = line.z z = line.z
if line.relative: if line.relative:
x = current_x + (x or 0) x = current_x + (x or 0)
y = current_y + (y or 0) y = current_y + (y or 0)
z = current_z + (z or 0) z = current_z + (z or 0)
if x and line.e: if x and line.e:
if x < xmin: if x < xmin:
xmin = x xmin = x
if x > xmax: if x > xmax:
xmax = x xmax = x
if y and line.e: if y and line.e:
if y < ymin: if y < ymin:
ymin = y ymin = y
if y > ymax: if y > ymax:
ymax = y ymax = y
if z: if z:
if z < zmin: if z < zmin:
zmin = z zmin = z
if z > zmax: if z > zmax:
zmax = z zmax = z
current_x = x or current_x current_x = x or current_x
current_y = y or current_y current_y = y or current_y
current_z = z or current_z current_z = z or current_z
return ( (xmin,xmax),(ymin,ymax),(zmin,zmax) ) return ( (xmin,xmax),(ymin,ymax),(zmin,zmax) )
class GCode(object): class GCode(object):
def __init__(self,data): def __init__(self,data):
self.lines = [Line(i) for i in data] self.lines = [Line(i) for i in data]
self._preprocess() self._preprocess()
self._create_layers() self._create_layers()
def _preprocess(self): def _preprocess(self):
#checks for G20, G21, G90 and G91, sets imperial and relative flags #checks for G20, G21, G90 and G91, sets imperial and relative flags
imperial = False imperial = False
relative = False relative = False
relative_e = False relative_e = False
for line in self.lines: for line in self.lines:
if line.command() == "G20": if line.command() == "G20":
imperial = True imperial = True
elif line.command() == "G21": elif line.command() == "G21":
imperial = False imperial = False
elif line.command() == "G90": elif line.command() == "G90":
relative = False relative = False
relative_e = False relative_e = False
elif line.command() == "G91": elif line.command() == "G91":
relative = True relative = True
relative_e = True relative_e = True
elif line.command() == "M82": elif line.command() == "M82":
relative_e = False relative_e = False
elif line.command() == "M83": elif line.command() == "M83":
relative_e = True relative_e = True
elif line.is_move(): elif line.is_move():
line.imperial = imperial line.imperial = imperial
line.relative = relative line.relative = relative
line.relative_e = relative_e line.relative_e = relative_e
def _create_layers(self): def _create_layers(self):
self.layers = [] self.layers = []
prev_z = None prev_z = None
cur_z = 0 cur_z = 0
cur_lines = [] cur_lines = []
layer_index = [] layer_index = []
temp_layers = {} temp_layers = {}
for line in self.lines: for line in self.lines:
if line.command() == "G92" and line.z != None: if line.command() == "G92" and line.z != None:
cur_z = line.z cur_z = line.z
elif line.is_move(): elif line.is_move():
if line.z != None: if line.z != None:
if line.relative: if line.relative:
cur_z += line.z cur_z += line.z
else: else:
cur_z = line.z cur_z = line.z
if cur_z != prev_z: if cur_z != prev_z:
old_lines = temp_layers.pop(prev_z,[]) old_lines = temp_layers.pop(prev_z,[])
old_lines += cur_lines old_lines += cur_lines
temp_layers[prev_z] = old_lines temp_layers[prev_z] = old_lines
if not prev_z in layer_index: if not prev_z in layer_index:
layer_index.append(prev_z) layer_index.append(prev_z)
cur_lines = [] cur_lines = []
cur_lines.append(line) cur_lines.append(line)
prev_z = cur_z prev_z = cur_z
old_lines = temp_layers.pop(prev_z,[]) old_lines = temp_layers.pop(prev_z,[])
old_lines += cur_lines old_lines += cur_lines
temp_layers[prev_z] = old_lines temp_layers[prev_z] = old_lines
if not prev_z in layer_index: if not prev_z in layer_index:
layer_index.append(prev_z) layer_index.append(prev_z)
layer_index.sort() layer_index.sort()
for idx in layer_index: for idx in layer_index:
cur_lines = temp_layers[idx] cur_lines = temp_layers[idx]
has_movement = False has_movement = False
for l in cur_lines: for l in cur_lines:
if l.is_move() and l.e != None: if l.is_move() and l.e != None:
has_movement = True has_movement = True
break break
if has_movement: if has_movement:
self.layers.append(Layer(cur_lines)) self.layers.append(Layer(cur_lines))
def num_layers(self): def num_layers(self):
return len(self.layers) return len(self.layers)
def measure(self): def measure(self):
xmin = 999999999 xmin = 999999999
ymin = 999999999 ymin = 999999999
zmin = 0 zmin = 0
xmax = -999999999 xmax = -999999999
ymax = -999999999 ymax = -999999999
zmax = -999999999 zmax = -999999999
for l in self.layers: for l in self.layers:
xd,yd,zd = l.measure() xd,yd,zd = l.measure()
if xd[0] < xmin: if xd[0] < xmin:
xmin = xd[0] xmin = xd[0]
if xd[1] > xmax: if xd[1] > xmax:
xmax = xd[1] xmax = xd[1]
if yd[0] < ymin: if yd[0] < ymin:
ymin = yd[0] ymin = yd[0]
if yd[1] > ymax: if yd[1] > ymax:
ymax = yd[1] ymax = yd[1]
if zd[0] < zmin: if zd[0] < zmin:
zmin = zd[0] zmin = zd[0]
if zd[1] > zmax: if zd[1] > zmax:
zmax = zd[1] zmax = zd[1]
self.xmin = xmin self.xmin = xmin
self.xmax = xmax self.xmax = xmax
self.ymin = ymin self.ymin = ymin
self.ymax = ymax self.ymax = ymax
self.zmin = zmin self.zmin = zmin
self.zmax = zmax self.zmax = zmax
self.width = xmax - xmin self.width = xmax - xmin
self.depth = ymax - ymin self.depth = ymax - ymin
self.height = zmax - zmin self.height = zmax - zmin
def filament_length(self): def filament_length(self):
total_e = 0 total_e = 0
cur_e = 0 cur_e = 0
for line in self.lines: for line in self.lines:
if line.command() == "G92": if line.command() == "G92":
if line.e != None: if line.e != None:
total_e += cur_e total_e += cur_e
cur_e = line.e cur_e = line.e
elif line.is_move() and line.e: elif line.is_move() and line.e:
if line.relative_e: if line.relative_e:
cur_e += line.e cur_e += line.e
else: else:
cur_e = line.e cur_e = line.e
return total_e return total_e
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "usage: %s filename.gcode" % sys.argv[0] print "usage: %s filename.gcode" % sys.argv[0]
return return
# d = [i.replace("\n","") for i in open(sys.argv[1])] # d = [i.replace("\n","") for i in open(sys.argv[1])]
# gcode = GCode(d) # gcode = GCode(d)
gcode = GCode(list(open(sys.argv[1]))) gcode = GCode(list(open(sys.argv[1])))
gcode.measure() gcode.measure()
print "Dimensions:" print "Dimensions:"
print "\tX: %0.02f - %0.02f (%0.02f)" % (gcode.xmin,gcode.xmax,gcode.width) print "\tX: %0.02f - %0.02f (%0.02f)" % (gcode.xmin,gcode.xmax,gcode.width)
print "\tY: %0.02f - %0.02f (%0.02f)" % (gcode.ymin,gcode.ymax,gcode.depth) print "\tY: %0.02f - %0.02f (%0.02f)" % (gcode.ymin,gcode.ymax,gcode.depth)
print "\tZ: %0.02f - %0.02f (%0.02f)" % (gcode.zmin,gcode.zmax,gcode.height) print "\tZ: %0.02f - %0.02f (%0.02f)" % (gcode.zmin,gcode.zmax,gcode.height)
print "Filament used: %0.02fmm" % gcode.filament_length() print "Filament used: %0.02fmm" % gcode.filament_length()
print "Number of layers: %d" % gcode.num_layers() print "Number of layers: %d" % gcode.num_layers()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment