Commit a49b4871 authored by Guillaume Seguin's avatar Guillaume Seguin

Start cleaning up addgcode()

There is a lot of code duplication between addgcode() and add_parsed_gcode,
which would probably need to be cleaned up.
parent 251bda7d
...@@ -301,6 +301,10 @@ class gviz(wx.Panel): ...@@ -301,6 +301,10 @@ class gviz(wx.Panel):
self.clear() self.clear()
self.add_parsed_gcodes(gcode.lines) self.add_parsed_gcodes(gcode.lines)
# FIXME : there's code duplication going on there, we should factor it (but
# the reason addgcode is not factored as a add_parsed_gcodes([gline]) is
# because when loading a file there's no hilight, so it simply lets us not
# do the if hilight: all the time for nothing when loading a lot of lines
def add_parsed_gcodes(self, lines): def add_parsed_gcodes(self, lines):
def _y(y): def _y(y):
return self.build_dimensions[1] - (y - self.build_dimensions[4]) return self.build_dimensions[1] - (y - self.build_dimensions[4])
...@@ -332,99 +336,92 @@ class gviz(wx.Panel): ...@@ -332,99 +336,92 @@ class gviz(wx.Panel):
start_pos = self.lastpos[:] start_pos = self.lastpos[:]
if gline.command in ["G0", "G1"]: if gline.command in ["G0", "G1"]:
line = [ _x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1]) ] self.lines[z].append((_x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1])))
self.lines[z].append(line)
self.pens[z].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen) self.pens[z].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen)
self.lastpos = target elif gline.command in ["G2", "G3"]:
# startpos, endpos, arc center
if gline.command in ["G2", "G3"]:
arc = [_x(start_pos[0]), _y(start_pos[1]), arc = [_x(start_pos[0]), _y(start_pos[1]),
_x(target[0]), _y(target[1]), _x(target[0]), _y(target[1]),
_x(start_pos[0] + target[5]), _y(start_pos[1] + target[6]) ] # center _x(start_pos[0] + target[5]), _y(start_pos[1] + target[6])]
# FIXME : verify this works : why not reverse endpoints 4, 5 # FIXME : verify this works : why not reverse endpoints 4, 5
if gline.command == "G2": # clockwise, reverse endpoints if gline.command == "G2": # clockwise, reverse endpoints
arc[0], arc[1], arc[2], arc[3] = arc[2], arc[3], arc[0], arc[1] arc[0], arc[1], arc[2], arc[3] = arc[2], arc[3], arc[0], arc[1]
self.arcs[z].append(arc) self.arcs[z].append(arc)
self.arcpens[z].append(self.arcpen) self.arcpens[z].append(self.arcpen)
self.lastpos = target self.lastpos = target
self.dirty = 1 self.dirty = 1
self.Refresh()
def addgcode(self, gcode = "M105", hilight = 0): def addgcode(self, gcode = "M105", hilight = 0):
gcode = gcode.split("*")[0] gcode = gcode.split("*")[0]
gcode = gcode.split(";")[0] gcode = gcode.split(";")[0]
gcode = gcode.lower().strip().split() gcode = gcode.lower().strip()
if not gcode: if not gcode:
return return
if gcode[0][0] == 'n': # not sure what this check is for ? gline = gcoder.Line(gcode)
gcode.pop(0) gline.parse_coordinates(False)
# FIXME by iXce : I commented this code out as I have no clue what it does,
def _readgcode(): # but it should be looked at
target = self.lastpos[:] #if gcode[0][0] == 'n': # not sure what this check is for ?
target[5]=0.0 # gcode.pop(0)
target[6]=0.0
if hilight:
target = self.hilightpos[:]
for i in gcode:
if i[0]=="x":
target[0]=float(i[1:])
elif i[0]=="y":
target[1]=float(i[1:])
elif i[0]=="z":
target[2]=float(i[1:])
elif i[0]=="e":
target[3]=float(i[1:])
elif i[0]=="f":
target[4]=float(i[1:])
elif i[0]=="i":
target[5]=float(i[1:])
elif i[0]=="j":
target[6]=float(i[1:])
if not hilight:
if not target[2] in self.lines.keys():
self.lines[target[2]]=[]
self.pens[target[2]]=[]
self.arcs[target[2]]=[]
self.arcpens[target[2]]=[]
self.layers+=[target[2]]
return target
def _y(y): def _y(y):
return self.build_dimensions[1]-(y-self.build_dimensions[4]) return self.build_dimensions[1] - (y - self.build_dimensions[4])
def _x(x): def _x(x):
return x-self.build_dimensions[3] return x - self.build_dimensions[3]
start_pos = self.hilightpos[:] if hilight else self.lastpos[:] start_pos = self.hilightpos[:] if hilight else self.lastpos[:]
if gcode[0] in [ "g0", "g1" ]: if gline.command not in ["G0", "G1", "G2", "G3"]:
target = _readgcode() return
line = [ _x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1]) ]
target = self.hilightpos[:] if hilight else self.lastpos[:]
target[5] = 0.0
target[6] = 0.0
if gline.x != None: target[0] = gline.x
if gline.y != None: target[1] = gline.y
if gline.z != None: target[2] = gline.z
if gline.e != None: target[3] = gline.e
if gline.f != None: target[4] = gline.f
if gline.i != None: target[5] = gline.i
if gline.j != None: target[6] = gline.j
z = target[2]
if not hilight and z not in self.layers:
self.lines[z] = []
self.pens[z] = []
self.arcs[z] = []
self.arcpens[z] = []
self.layers.append(z)
if gline.command in ["G0", "G1"]:
line = [_x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1])]
if not hilight: if not hilight:
self.lines[ target[2] ] += [line] self.lines[z].append((_x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1])))
self.pens[ target[2] ] += [self.mainpen if target[3] != self.lastpos[3] else self.travelpen] self.pens[z].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen)
self.lastpos = target
else: else:
self.hilight += [line] self.hilight.append(line)
self.hilightpos = target elif gline.command in ["G2", "G3"]:
self.dirty = 1 # startpos, endpos, arc center
arc = [_x(start_pos[0]), _y(start_pos[1]),
if gcode[0] in [ "g2", "g3" ]: _x(target[0]), _y(target[1]),
target = _readgcode() _x(start_pos[0] + target[5]), _y(start_pos[1] + target[6])]
arc = [] if gline.command == "G2": # clockwise, reverse endpoints
arc += [ _x(start_pos[0]), _y(start_pos[1]) ]
arc += [ _x(target[0]), _y(target[1]) ]
arc += [ _x(start_pos[0] + target[5]), _y(start_pos[1] + target[6]) ] # center
if gcode[0] == "g2": # clockwise, reverse endpoints
arc[0], arc[1], arc[2], arc[3] = arc[2], arc[3], arc[0], arc[1] arc[0], arc[1], arc[2], arc[3] = arc[2], arc[3], arc[0], arc[1]
self.arcs[z].append(arc)
self.arcpens[z].append(self.arcpen)
else:
self.hilightarcs.append(arc)
if not hilight: if not hilight:
self.arcs[ target[2] ] += [arc]
self.arcpens[ target[2] ] += [self.arcpen]
self.lastpos = target self.lastpos = target
else: else:
self.hilightarcs += [arc]
self.hilightpos = target self.hilightpos = target
self.dirty = 1 self.dirty = 1
self.Refresh()
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
......
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