Commit d4e01201 authored by Guillaume Seguin's avatar Guillaume Seguin

Heavily speedup 2D viewer loading speed by using gcoder correctly (#412)

parent 6f9a56ba
...@@ -380,7 +380,7 @@ class Gviz(wx.Panel): ...@@ -380,7 +380,7 @@ class Gviz(wx.Panel):
def addfile(self, gcode): def addfile(self, gcode):
self.clear() self.clear()
self.add_parsed_gcodes(gcode.lines) self.add_parsed_gcodes(gcode)
max_layers = len(self.layers) max_layers = len(self.layers)
if hasattr(self.parent, "layerslider"): if hasattr(self.parent, "layerslider"):
self.parent.layerslider.SetRange(0, max_layers - 1) self.parent.layerslider.SetRange(0, max_layers - 1)
...@@ -390,27 +390,35 @@ class Gviz(wx.Panel): ...@@ -390,27 +390,35 @@ class Gviz(wx.Panel):
# the reason addgcode is not factored as a add_parsed_gcodes([gline]) is # 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 # 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 # 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, gcode):
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]
for gline in lines: for layer_idx, layer in enumerate(gcode.all_layers):
if gline.command not in ["G0", "G1", "G2", "G3"]: has_move = False
for gline in layer:
if gline.is_move:
has_move = True
break
if not has_move:
continue
self.lines[layer.z] = []
self.pens[layer.z] = []
self.arcs[layer.z] = []
self.arcpens[layer.z] = []
self.layers.append(layer.z)
for gline in layer:
if not gline.is_move:
continue continue
target = self.lastpos[:] target = self.lastpos[:]
target[0] = gline.current_x
target[1] = gline.current_y
target[2] = gline.current_z
target[5] = 0.0 target[5] = 0.0
target[6] = 0.0 target[6] = 0.0
if gline.relative:
if gline.x != None: target[0] += gline.x
if gline.y != None: target[1] += gline.y
if gline.z != None: target[2] += gline.z
else:
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: if gline.e != None:
if gline.relative_e: if gline.relative_e:
target[3] += gline.e target[3] += gline.e
...@@ -419,19 +427,12 @@ class Gviz(wx.Panel): ...@@ -419,19 +427,12 @@ class Gviz(wx.Panel):
if gline.f != None: target[4] = gline.f if gline.f != None: target[4] = gline.f
if gline.i != None: target[5] = gline.i if gline.i != None: target[5] = gline.i
if gline.j != None: target[6] = gline.j if gline.j != None: target[6] = gline.j
z = target[2]
if z not in self.layers:
self.lines[z] = []
self.pens[z] = []
self.arcs[z] = []
self.arcpens[z] = []
self.layers.append(z)
start_pos = self.lastpos[:] start_pos = self.lastpos[:]
if gline.command in ["G0", "G1"]: if gline.command in ["G0", "G1"]:
self.lines[z].append((_x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1]))) self.lines[layer.z].append((_x(start_pos[0]), _y(start_pos[1]), _x(target[0]), _y(target[1])))
self.pens[z].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen) self.pens[layer.z].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen)
elif gline.command in ["G2", "G3"]: elif gline.command in ["G2", "G3"]:
# startpos, endpos, arc center # startpos, endpos, arc center
arc = [_x(start_pos[0]), _y(start_pos[1]), arc = [_x(start_pos[0]), _y(start_pos[1]),
...@@ -440,8 +441,8 @@ class Gviz(wx.Panel): ...@@ -440,8 +441,8 @@ class Gviz(wx.Panel):
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[layer.z].append(arc)
self.arcpens[z].append(self.arcpen) self.arcpens[layer.z].append(self.arcpen)
self.lastpos = target self.lastpos = target
self.dirty = 1 self.dirty = 1
......
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