Commit 817d4a9d authored by Guillaume Seguin's avatar Guillaume Seguin

Add "Edit layer" tool

parent 4b5456e1
images/edit.png

1010 Bytes

......@@ -248,6 +248,35 @@ class GCode(object):
self.line_idxs.insert(end_index + i, end_line + i + 1)
return commands[::-1]
def rewrite_layer(self, commands, layer_idx):
# Prepend commands in reverse order
commands = [c.strip() for c in commands[::-1] if c.strip()]
layer = self.all_layers[layer_idx]
# Find start index to append lines
# and end index to append new indices
start_index = self.layer_idxs.index(layer_idx)
for i in range(start_index, len(self.layer_idxs)):
if self.layer_idxs[i] != layer_idx:
end_index = i
break
else:
end_index = i + 1
self.layer_idxs = self.layer_idxs[:start_index] + array('I', len(commands) * [layer_idx]) + self.layer_idxs[end_index:]
self.line_idxs = self.line_idxs[:start_index] + array('I', range(len(commands))) + self.line_idxs[end_index:]
del self.lines[start_index:end_index]
del layer[:]
for i, command in enumerate(commands):
gline = Line(command)
# Split to get command
split(gline)
# Force is_move to False
gline.is_move = False
# Insert gline at beginning of layer
layer.insert(0, gline)
# Insert gline at beginning of list
self.lines.insert(start_index, gline)
return commands[::-1]
def append(self, command, store = True):
command = command.strip()
if not command:
......
......@@ -21,7 +21,7 @@ from . import gcoder
from .gl.panel import wxGLPanel
from .gl.trackball import build_rotmatrix
from .gl.libtatlin import actors
from .injectgcode import injector
from .injectgcode import injector, injector_edit
from pyglet.gl import glPushMatrix, glPopMatrix, \
glTranslatef, glRotatef, glScalef, glMultMatrixd
......@@ -95,6 +95,14 @@ class GcodeViewPanel(wxGLPanel):
else:
print _("Invalid layer for injection")
def editlayer(self):
l = self.parent.model.num_layers_to_draw
filtered = [k for k, v in self.parent.model.layer_idxs_map.iteritems() if v == l]
if filtered:
injector_edit(self.parent.model.gcode, l, filtered[0])
else:
print _("Invalid layer for edition")
def setlayercb(self, layer):
pass
......@@ -385,7 +393,7 @@ class GcodeViewFrame(GvizBaseFrame, GcodeViewLoader):
self.objects = [GCObject(self.platform), GCObject(None)]
fit_image = wx.Image(imagefile('fit.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
self.toolbar.InsertLabelTool(6, 6, " " + _("Fit to plate"), fit_image,
self.toolbar.InsertLabelTool(6, 8, " " + _("Fit to plate"), fit_image,
shortHelp = _("Fit to plate [F]"),
longHelp = '')
self.toolbar.Realize()
......@@ -400,8 +408,9 @@ class GcodeViewFrame(GvizBaseFrame, GcodeViewLoader):
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerup(), id = 3)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerdown(), id = 4)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.resetview(), id = 5)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.fit(), id = 7)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.fit(), id = 8)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.inject(), id = 6)
self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.editlayer(), id = 7)
def process_slider(self, event):
new_layer = self.layerslider.GetValue()
......
......@@ -18,7 +18,7 @@ from collections import deque
import wx
import time
from . import gcoder
from .injectgcode import injector
from .injectgcode import injector, injector_edit
from .utils import imagefile, install_locale, get_home_pos
install_locale('pronterface')
......@@ -44,7 +44,8 @@ class GvizBaseFrame(wx.Frame):
self.toolbar.AddSimpleTool(4, wx.Image(imagefile('arrow_down.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Move Down a Layer [D]"), '')
self.toolbar.AddLabelTool(5, " " + _("Reset view"), wx.Image(imagefile('reset.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelp = _("Reset view"), longHelp = '')
self.toolbar.AddSeparator()
self.toolbar.AddLabelTool(6, " " + _("Inject G-Code"), wx.Image(imagefile('inject.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelp = _("Inject G-Code"), longHelp = _("Insert code at the beginning of this layer"))
self.toolbar.AddSimpleTool(6, wx.Image(imagefile('inject.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelpString = _("Inject G-Code"), longHelpString = _("Insert code at the beginning of this layer"))
self.toolbar.AddSimpleTool(7, wx.Image(imagefile('edit.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelpString = _("Edit layer"), longHelpString = _("Edit the G-Code of this layer"))
vbox.Add(self.toolbar, 0, border = 5)
......@@ -84,6 +85,7 @@ class GvizWindow(GvizBaseFrame):
self.Bind(wx.EVT_TOOL, lambda x: self.p.layerdown(), id = 4)
self.Bind(wx.EVT_TOOL, self.resetview, id = 5)
self.Bind(wx.EVT_TOOL, lambda x: self.p.inject(), id = 6)
self.Bind(wx.EVT_TOOL, lambda x: self.p.editlayer(), id = 7)
self.initpos = None
self.p.Bind(wx.EVT_KEY_DOWN, self.key)
......@@ -205,6 +207,10 @@ class Gviz(wx.Panel):
layer = self.layers.index(self.layerindex)
injector(self.gcode, self.layerindex, layer)
def editlayer(self):
layer = self.layers.index(self.layerindex)
injector_edit(self.gcode, self.layerindex, layer)
def clearhilights(self):
self.hilight.clear()
self.hilightarcs.clear()
......
......@@ -24,7 +24,20 @@ def injector(gcode, viz_layer, layer_idx):
z = z if z is not None else 0
MacroEditor(_("Inject G-Code at layer %d (Z = %.03f)") % (viz_layer, z), "", cb, True)
def injector_edit(gcode, viz_layer, layer_idx):
cb = lambda toadd: rewritelayer(gcode, viz_layer, layer_idx, toadd)
layer = gcode.all_layers[layer_idx]
z = layer.z
z = z if z is not None else 0
lines = [line.raw for line in layer]
MacroEditor(_("Edit G-Code of layer %d (Z = %.03f)") % (viz_layer, z), lines, cb, True)
def inject(gcode, viz_layer, layer_idx, toadd):
# TODO: save modified gcode after injection ?
nlines = len(gcode.prepend_to_layer(toadd, layer_idx))
print _("Successfully injected %d lines at beginning of layer %d") % (nlines, viz_layer)
def rewritelayer(gcode, viz_layer, layer_idx, toadd):
# TODO: save modified gcode after edit ?
nlines = len(gcode.rewrite_layer(toadd, layer_idx))
print _("Successfully edited layer %d (which now contains %d lines)") % (viz_layer, nlines)
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