From 0405ed0dc986919b474da52f9f1b3ce9df4b051b Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sun, 2 Sep 2018 04:58:48 +0200 Subject: [PATCH 1/4] Fix whitespace issues (according to PEP8) for gcodeParser.py The issues were reported by flake8. This changeset contains strictly whitespace changes: git diff -w --ignore-blank-lines (empty output) --- gcodeParser.py | 233 +++++++++++++++++++++++++------------------------ 1 file changed, 118 insertions(+), 115 deletions(-) diff --git a/gcodeParser.py b/gcodeParser.py index 45b6133..e0b3d27 100755 --- a/gcodeParser.py +++ b/gcodeParser.py @@ -3,11 +3,12 @@ import math import re + class GcodeParser: - + def __init__(self): self.model = GcodeModel(self) - + def parseFile(self, path): # read the gcode file with open(path, 'r') as f: @@ -21,10 +22,10 @@ def parseFile(self, path): self.line = line.rstrip() # parse a line self.parseLine() - + self.model.postProcess() return self.model - + def parseLine(self): # strip comments: ## first handle round brackets @@ -38,20 +39,20 @@ def parseLine(self): if idx >= 0: self.warn("Stripping unterminated round-bracket comment") command = command[0:idx].strip() - + # TODO strip logical line number & checksum - + # code is fist word, then args comm = command.split(None, 1) - code = comm[0] if (len(comm)>0) else None - args = comm[1] if (len(comm)>1) else None - + code = comm[0] if (len(comm) > 0) else None + args = comm[1] if (len(comm) > 1) else None + if code: - if hasattr(self, "parse_"+code): - getattr(self, "parse_"+code)(args) + if hasattr(self, "parse_" + code): + getattr(self, "parse_" + code)(args) else: - self.warn("Unknown code '%s'"%code) - + self.warn("Unknown code '%s'" % code) + def parseArgs(self, args): dic = {} if args: @@ -69,68 +70,69 @@ def parse_G0(self, args): # G0: Rapid move # same as a controlled move for us (& reprap FW) self.parse_G1(args, "G0") - + def parse_G1(self, args, type="G1"): # G1: Controlled move self.model.do_G1(self.parseArgs(args), type) - + def parse_G20(self, args): # G20: Set Units to Inches self.error("Unsupported & incompatible: G20: Set Units to Inches") - + def parse_G21(self, args): # G21: Set Units to Millimeters # Default, nothing to do pass - + def parse_G28(self, args): # G28: Move to Origin self.model.do_G28(self.parseArgs(args)) - + def parse_G90(self, args): # G90: Set to Absolute Positioning self.model.setRelative(False) - + def parse_G91(self, args): # G91: Set to Relative Positioning self.model.setRelative(True) - + def parse_G92(self, args): # G92: Set Position self.model.do_G92(self.parseArgs(args)) - + def warn(self, msg): print "[WARN] Line %d: %s (Text:'%s')" % (self.lineNb, msg, self.line) - + def error(self, msg): print "[ERROR] Line %d: %s (Text:'%s')" % (self.lineNb, msg, self.line) raise Exception("[ERROR] Line %d: %s (Text:'%s')" % (self.lineNb, msg, self.line)) + class BBox(object): - + def __init__(self, coords): self.xmin = self.xmax = coords["X"] self.ymin = self.ymax = coords["Y"] self.zmin = self.zmax = coords["Z"] - + def dx(self): return self.xmax - self.xmin - + def dy(self): return self.ymax - self.ymin - + def dz(self): return self.zmax - self.zmin - + def cx(self): - return (self.xmax + self.xmin)/2 - + return (self.xmax + self.xmin) / 2 + def cy(self): - return (self.ymax + self.ymin)/2 - + return (self.ymax + self.ymin) / 2 + def cz(self): - return (self.zmax + self.zmin)/2 - + return (self.zmax + self.zmin) / 2 + def extend(self, coords): self.xmin = min(self.xmin, coords["X"]) self.xmax = max(self.xmax, coords["X"]) @@ -138,25 +140,26 @@ def extend(self, coords): self.ymax = max(self.ymax, coords["Y"]) self.zmin = min(self.zmin, coords["Z"]) self.zmax = max(self.zmax, coords["Z"]) - + + class GcodeModel: - + def __init__(self, parser): # save parser for messages self.parser = parser # latest coordinates & extrusion relative to offset, feedrate self.relative = { - "X":0.0, - "Y":0.0, - "Z":0.0, - "F":0.0, - "E":0.0} + "X": 0.0, + "Y": 0.0, + "Z": 0.0, + "F": 0.0, + "E": 0.0} # offsets for relative coordinates and position reset (G92) self.offset = { - "X":0.0, - "Y":0.0, - "Z":0.0, - "E":0.0} + "X": 0.0, + "Y": 0.0, + "Z": 0.0, + "E": 0.0} # if true, args for move (G1) are given relatively (default: absolute) self.isRelative = False # the segments @@ -165,7 +168,7 @@ def __init__(self, parser): self.distance = None self.extrudate = None self.bbox = None - + def do_G1(self, args, type): # G0/G1: Rapid/Controlled move # clone previous coords @@ -178,7 +181,7 @@ def do_G1(self, args, type): else: coords[axis] = args[axis] else: - self.warn("Unknown axis '%s'"%axis) + self.warn("Unknown axis '%s'" % axis) # build segment absolute = { "X": self.offset["X"] + coords["X"], @@ -195,18 +198,18 @@ def do_G1(self, args, type): self.addSegment(seg) # update model coords self.relative = coords - + def do_G28(self, args): # G28: Move to Origin self.warn("G28 unimplemented") - + def do_G92(self, args): # G92: Set Position # this changes the current coords, without moving, so do not generate a segment - + # no axes mentioned == all axes to 0 if not len(args.keys()): - args = {"X":0.0, "Y":0.0, "Z":0.0, "E":0.0} + args = {"X": 0.0, "Y": 0.0, "Z": 0.0, "E": 0.0} # update specified axes for axis in args.keys(): if self.offset.has_key(axis): @@ -214,91 +217,88 @@ def do_G92(self, args): self.offset[axis] += self.relative[axis] - args[axis] self.relative[axis] = args[axis] else: - self.warn("Unknown axis '%s'"%axis) + self.warn("Unknown axis '%s'" % axis) def setRelative(self, isRelative): self.isRelative = isRelative - + def addSegment(self, segment): self.segments.append(segment) #print segment - + def warn(self, msg): self.parser.warn(msg) - + def error(self, msg): self.parser.error(msg) - - + def classifySegments(self): # apply intelligence, to classify segments - + # start model at 0 coords = { - "X":0.0, - "Y":0.0, - "Z":0.0, - "F":0.0, - "E":0.0} - + "X": 0.0, + "Y": 0.0, + "Z": 0.0, + "F": 0.0, + "E": 0.0} + # first layer at Z=0 currentLayerIdx = 0 currentLayerZ = 0 - + for seg in self.segments: # default style is fly (move, no extrusion) style = "fly" - + # no horizontal movement, but extruder movement: retraction/refill if ( (seg.coords["X"] == coords["X"]) and (seg.coords["Y"] == coords["Y"]) and - (seg.coords["E"] != coords["E"]) ): + (seg.coords["E"] != coords["E"])): style = "retract" if (seg.coords["E"] < coords["E"]) else "restore" - + # some horizontal movement, and positive extruder movement: extrusion if ( - ( (seg.coords["X"] != coords["X"]) or (seg.coords["Y"] != coords["Y"]) ) and - (seg.coords["E"] > coords["E"]) ): + ((seg.coords["X"] != coords["X"]) or (seg.coords["Y"] != coords["Y"])) and + (seg.coords["E"] > coords["E"])): style = "extrude" - + # positive extruder movement in a different Z signals a layer change for this segment if ( (seg.coords["E"] > coords["E"]) and - (seg.coords["Z"] != currentLayerZ) ): + (seg.coords["Z"] != currentLayerZ)): currentLayerZ = seg.coords["Z"] currentLayerIdx += 1 - + # set style and layer in segment seg.style = style seg.layerIdx = currentLayerIdx - - + #print coords #print seg.coords #print "%s (%s | %s)"%(style, str(seg.coords), seg.line) #print - + # execute segment coords = seg.coords - - + def splitLayers(self): # split segments into previously detected layers - + # start model at 0 coords = { - "X":0.0, - "Y":0.0, - "Z":0.0, - "F":0.0, - "E":0.0} - + "X": 0.0, + "Y": 0.0, + "Z": 0.0, + "F": 0.0, + "E": 0.0} + # init layer store self.layers = [] - + currentLayerIdx = -1 - + # for all segments for seg in self.segments: # next layer @@ -307,22 +307,22 @@ def splitLayers(self): layer.start = coords self.layers.append(layer) currentLayerIdx = seg.layerIdx - + layer.segments.append(seg) - + # execute segment coords = seg.coords - - self.topLayer = len(self.layers)-1 - + + self.topLayer = len(self.layers) - 1 + def calcMetrics(self): # init distances and extrudate self.distance = 0 self.extrudate = 0 - + # init model bbox self.bbox = None - + # extender helper def extend(bbox, coords): if bbox is None: @@ -330,52 +330,53 @@ def extend(bbox, coords): else: bbox.extend(coords) return bbox - + # for all layers for layer in self.layers: # start at layer start coords = layer.start - + # init distances and extrudate layer.distance = 0 layer.extrudate = 0 - + # include start point self.bbox = extend(self.bbox, coords) - + # for all segments for seg in layer.segments: # calc XYZ distance - d = (seg.coords["X"]-coords["X"])**2 - d += (seg.coords["Y"]-coords["Y"])**2 - d += (seg.coords["Z"]-coords["Z"])**2 + d = (seg.coords["X"] - coords["X"]) ** 2 + d += (seg.coords["Y"] - coords["Y"]) ** 2 + d += (seg.coords["Z"] - coords["Z"]) ** 2 seg.distance = math.sqrt(d) - + # calc extrudate - seg.extrudate = (seg.coords["E"]-coords["E"]) - + seg.extrudate = (seg.coords["E"] - coords["E"]) + # accumulate layer metrics layer.distance += seg.distance layer.extrudate += seg.extrudate - + # execute segment coords = seg.coords - + # include end point extend(self.bbox, coords) - + # accumulate total metrics self.distance += layer.distance self.extrudate += layer.extrudate - + def postProcess(self): self.classifySegments() self.splitLayers() self.calcMetrics() def __str__(self): - return ""%(len(self.segments), len(self.layers), self.distance, self.extrudate, self.bbox) - + return "" % (len(self.segments), len(self.layers), self.distance, self.extrudate, self.bbox) + + class Segment: def __init__(self, type, coords, lineNb, line): self.type = type @@ -386,20 +387,22 @@ def __init__(self, type, coords, lineNb, line): self.layerIdx = None self.distance = None self.extrudate = None + def __str__(self): - return ""%(self.type, self.lineNb, self.style, self.layerIdx, self.distance, self.extrudate) - + return "" % (self.type, self.lineNb, self.style, self.layerIdx, self.distance, self.extrudate) + + class Layer: def __init__(self, Z): self.Z = Z self.segments = [] self.distance = None self.extrudate = None - + def __str__(self): - return ""%(self.Z, len(self.segments), self.distance, self.extrudate) - - + return "" % (self.Z, len(self.segments), self.distance, self.extrudate) + + if __name__ == '__main__': path = "test.gcode" From 94d83cce876fb16ab835bb6e78d7eb48501e1357 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sun, 2 Sep 2018 05:48:10 +0200 Subject: [PATCH 2/4] Fix whitespace issues (according to PEP8) for yagv This changeset contains only whitespace changes. --- yagv | 279 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 129 insertions(+), 150 deletions(-) diff --git a/yagv b/yagv index 932e98f..ca9e89f 100755 --- a/yagv +++ b/yagv @@ -17,16 +17,16 @@ from gcodeParser import * import os.path import time + class App: def __init__(self): self.RX = 0.0 self.RZ = 0.0 self.zoom = 1.0 - + def main(self): - #### MAIN CODE #### - print "Yet Another GCode Viewer v%s"%YAGV_VERSION + print "Yet Another GCode Viewer v%s" % YAGV_VERSION import sys if len(sys.argv) > 1: @@ -38,41 +38,39 @@ class App: script_dir = os.path.dirname(script_path) # default to hana path = os.path.join(script_dir, "data/hana_swimsuit_fv_solid_v1.gcode") - + self.load(path) - + # default to the middle layer - self.layerIdx = len(self.model.layers)/2 + self.layerIdx = len(self.model.layers) / 2 - self.window = MyWindow(self, caption="Yet Another GCode Viewer v%s"%YAGV_VERSION, resizable=True) + self.window = MyWindow(self, caption="Yet Another GCode Viewer v%s" % YAGV_VERSION, resizable=True) # debug: log all events # self.window.push_handlers(pyglet.window.event.WindowEventLogger()) - pyglet.app.run() def reload(self): self.load(self.path) - + def load(self, path): - - print "loading file %s ..."%repr(path) + print "loading file %s ..." % repr(path) t1 = time.time() - + print - print "Parsing '%s'..."%path + print "Parsing '%s'..." % path print - + self.path = path parser = GcodeParser() self.model = parser.parseFile(path) print - print "Done! %s"%self.model + print "Done! %s" % self.model print - + # render the model print "rendering vertices..." self.renderVertices() @@ -83,19 +81,18 @@ class App: print "generating graphics..." self.generateGraphics() print "Done" - + t2 = time.time() - print "loaded file in %0.3f ms" % ((t2-t1)*1000.0, ) - + print "loaded file in %0.3f ms" % ((t2 - t1) * 1000.0, ) + def renderVertices(self): t1 = time.time() - + self.vertices = [] - + for layer in self.model.layers: - layer_vertices = [] - + x = layer.start["X"] y = layer.start["Y"] z = layer.start["Z"] @@ -109,28 +106,27 @@ class App: layer_vertices.append(x) layer_vertices.append(y) layer_vertices.append(z) - + self.vertices.append(layer_vertices) - + t2 = time.time() - print "end renderColors in %0.3f ms" % ((t2-t1)*1000.0, ) - + print "end renderColors in %0.3f ms" % ((t2 - t1) * 1000.0, ) + def renderIndexedColors(self): t1 = time.time() # pre-render segments to colors in the index styleToColoridx = { - "extrude" : 0, - "fly" : 1, - "retract" : 2, - "restore" : 3 - } - + "extrude": 0, + "fly": 1, + "retract": 2, + "restore": 3} + # all the styles for all layers self.vertex_indexed_colors = [] - + # for all layers for layer in self.model.layers: - + # index for this layer layer_vertex_indexed_colors = [] for seg in layer.segments: @@ -138,94 +134,89 @@ class App: styleCol = styleToColoridx[seg.style] # append color twice (once per end) layer_vertex_indexed_colors.extend((styleCol, styleCol)) - + # append layer to all layers self.vertex_indexed_colors.append(layer_vertex_indexed_colors) t2 = time.time() - print "end renderIndexedColors in %0.3f ms" % ((t2-t1)*1000.0, ) - + print "end renderIndexedColors in %0.3f ms" % ((t2 - t1) * 1000.0, ) + def renderColors(self): t1 = time.time() - - self.vertex_colors = [[],[],[]] - + + self.vertex_colors = [[], [], []] + # render color index to real colors colorMap = [ - # 0: old layer - [[255,255,255, 40],[255, 0, 0, 20],[ 0, 255, 0, 32],[ 0, 0, 255, 32]], # extrude, fly, retract, restore - # 1: current layer - [[255,255,255,255],[255, 0, 0,128],[ 0, 255, 0,128],[ 0, 0, 255,128]], # extrude, fly, retract, restore - # 2: limbo layer - [[255,255,255, 10],[255, 0, 0, 10],[ 0, 255, 0, 10],[ 0, 0, 255, 10]] # extrude, fly, retract, restore - ] - + # 0: old layer + [[255, 255, 255, 40], [255, 0, 0, 20], [0, 255, 0, 32], [0, 0, 255, 32]], # extrude, fly, retract, restore + # 1: current layer + [[255, 255, 255, 255], [255, 0, 0, 128], [0, 255, 0, 128], [0, 0, 255, 128]], # extrude, fly, retract, restore + # 2: limbo layer + [[255, 255, 255, 10], [255, 0, 0, 10], [0, 255, 0, 10], [0, 0, 255, 10]] # extrude, fly, retract, restore + ] + # for all 3 types for display_type in xrange(3): - type_color_map = colorMap[display_type] - + # for all preindexed layer colors for indexes in self.vertex_indexed_colors: - # render color indexes to colors colors = map(lambda e: type_color_map[e], indexes) # flatten color values fcolors = [] map(fcolors.extend, colors) - + # push colors to vertex list self.vertex_colors[display_type].append(fcolors) - + t2 = time.time() - print "end renderColors in %0.3f ms" % ((t2-t1)*1000.0, ) - + print "end renderColors in %0.3f ms" % ((t2 - t1) * 1000.0, ) + def generateGraphics(self): t1 = time.time() - + self.graphics_old = [] self.graphics_current = [] self.graphics_limbo = [] - + for layer_idx in xrange(len(self.vertices)): - nb_layer_vertices = len(self.vertices[layer_idx])/3 + nb_layer_vertices = len(self.vertices[layer_idx]) / 3 vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices, ('v3f/static', self.vertices[layer_idx]), ('c4B/static', self.vertex_colors[0][layer_idx]) ) self.graphics_old.append(vertex_list) - + vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices, ('v3f/static', self.vertices[layer_idx]), ('c4B/static', self.vertex_colors[1][layer_idx]) ) self.graphics_current.append(vertex_list) - + vertex_list = pyglet.graphics.vertex_list(nb_layer_vertices, ('v3f/static', self.vertices[layer_idx]), ('c4B/static', self.vertex_colors[2][layer_idx]) ) self.graphics_limbo.append(vertex_list) # print nb_layer_vertices, len(self.vertices[layer_idx]), len(self.colors[0][layer_idx]) - + t2 = time.time() - print "end generateGraphics in %0.3f ms" % ((t2-t1)*1000.0, ) - - + print "end generateGraphics in %0.3f ms" % ((t2 - t1) * 1000.0, ) + def rotate_drag_start(self, x, y, button, modifiers): self.rotateDragStartRX = self.RX self.rotateDragStartRZ = self.RZ self.rotateDragStartX = x self.rotateDragStartY = y - def rotate_drag_do(self, x, y, dx, dy, buttons, modifiers): # deltas deltaX = x - self.rotateDragStartX deltaY = y - self.rotateDragStartY # rotate! - self.RZ = self.rotateDragStartRZ + deltaX/5.0 # mouse X bound to model Z - self.RX = self.rotateDragStartRX + deltaY/5.0 # mouse Y bound to model X - + self.RZ = self.rotateDragStartRZ + deltaX / 5.0 # mouse X bound to model Z + self.RX = self.rotateDragStartRX + deltaY / 5.0 # mouse Y bound to model X def rotate_drag_end(self, x, y, button, modifiers): self.rotateDragStartRX = None @@ -233,36 +224,33 @@ class App: self.rotateDragStartX = None self.rotateDragStartY = None - def layer_drag_start(self, x, y, button, modifiers): self.layerDragStartLayer = self.layerIdx self.layerDragStartX = x self.layerDragStartY = y - def layer_drag_do(self, x, y, dx, dy, buttons, modifiers): # sum x & y delta = x - self.layerDragStartX + y - self.layerDragStartY # new theoretical layer - self.layerIdx = int(self.layerDragStartLayer + delta/5) + self.layerIdx = int(self.layerDragStartLayer + delta / 5) # clamp layer to 0-max self.layerIdx = max(min(self.layerIdx, self.model.topLayer), 0) - - self.window.layerLabel.text = "layer %d"%self.layerIdx - + + self.window.layerLabel.text = "layer %d" % self.layerIdx + # # clamp layer to 0-max, with origin slip # if (self.layerIdx < 0): # self.layerIdx = 0 # self.layerDragStartLayer = 0 # self.layerDragStartX = x # self.layerDragStartY = y - # if (self.layerIdx > len(self.model.layers)-1): - # self.layerIdx = len(self.model.layers)-1 - # self.layerDragStartLayer = len(self.model.layers)-1 + # if (self.layerIdx > len(self.model.layers) - 1): + # self.layerIdx = len(self.model.layers) - 1 + # self.layerDragStartLayer = len(self.model.layers) - 1 # self.layerDragStartX = x # self.layerDragStartY = y - def layer_drag_end(self, x, y, button, modifiers): self.layerDragStartLayer = None self.layerDragStartX = None @@ -276,10 +264,10 @@ class MyWindow(pyglet.window.Window): pyglet.window.Window.__init__(self, **kwargs) self.app = app self.hud() - + # hud info def hud(self): - + # HUD labels self.blLabels = [] self.brLabels = [] @@ -288,14 +276,13 @@ class MyWindow(pyglet.window.Window): # help self.helpText = [ - "Ctrl-R to reload file", - "Right-click & drag (any direction) to change layer", - "Scroll to zoom", - "Left-click & drag to rotate view"] + "Ctrl-R to reload file", + "Right-click & drag (any direction) to change layer", + "Scroll to zoom", + "Left-click & drag to rotate view"] for txt in self.helpText: self.blLabels.append( - pyglet.text.Label( txt, - font_size=12) ) + pyglet.text.Label(txt, font_size=12)) # statistics ## model stats @@ -303,8 +290,8 @@ class MyWindow(pyglet.window.Window): font_size=12, anchor_y='top') filename = os.path.basename(self.app.path) - self.statsLabel.text = "%s: %d segments, %d layers."%(filename, len(self.app.model.segments), len(self.app.model.layers)) - + self.statsLabel.text = "%s: %d segments, %d layers." % (filename, len(self.app.model.segments), len(self.app.model.layers)) + ## fps counter self.fpsLabel = pyglet.text.Label( "", font_size=12, @@ -314,55 +301,52 @@ class MyWindow(pyglet.window.Window): # status ## current Layer - self.layerLabel = pyglet.text.Label( "layer %d"%self.app.layerIdx, + self.layerLabel = pyglet.text.Label( "layer %d" % self.app.layerIdx, font_size=12, anchor_x='right', anchor_y='top') self.trLabels.append(self.layerLabel) # layout the labels in the window's corners self.placeLabels(self.width, self.height) - - + # events def on_resize(self, width, height): glViewport(0, 0, width, height) self.placeLabels(width, height) #self.render(width, height) - + return pyglet.event.EVENT_HANDLED def on_mouse_press(self, x, y, button, modifiers): - #print "on_mouse_press(x=%d, y=%d, button=%s, modifiers=%s)"%(x, y, button, modifiers) + #print "on_mouse_press(x=%d, y=%d, button=%s, modifiers=%s)" % (x, y, button, modifiers) if button & mouse.LEFT: self.app.rotate_drag_start(x, y, button, modifiers) - + if button & mouse.RIGHT: self.app.layer_drag_start(x, y, button, modifiers) - def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): - #print "on_mouse_drag(x=%d, y=%d, dx=%d, dy=%d, buttons=%s, modifiers=%s)"%(x, y, dx, dy, buttons, modifiers) + #print "on_mouse_drag(x=%d, y=%d, dx=%d, dy=%d, buttons=%s, modifiers=%s)" % (x, y, dx, dy, buttons, modifiers) if buttons & mouse.LEFT: self.app.rotate_drag_do(x, y, dx, dy, buttons, modifiers) - + if buttons & mouse.RIGHT: self.app.layer_drag_do(x, y, dx, dy, buttons, modifiers) - def on_mouse_release(self, x, y, button, modifiers): - #print "on_mouse_release(x=%d, y=%d, button=%s, modifiers=%s)"%(x, y, button, modifiers) + #print "on_mouse_release(x=%d, y=%d, button=%s, modifiers=%s)" % (x, y, button, modifiers) if button & mouse.LEFT: self.app.rotate_drag_end(x, y, button, modifiers) - + if button & mouse.RIGHT: self.app.layer_drag_end(x, y, button, modifiers) def on_key_release(self, symbol, modifiers): - print "pressed key: %s, mod: %s"%(symbol, modifiers) - #print "pressed key: %s, mod: %s"%(pyglet.window.key.R, pyglet.window.key.MOD_CTRL) - if symbol==pyglet.window.key.R and modifiers & pyglet.window.key.MOD_CTRL: + print "pressed key: %s, mod: %s" % (symbol, modifiers) + #print "pressed key: %s, mod: %s" % (pyglet.window.key.R, pyglet.window.key.MOD_CTRL) + if symbol == pyglet.window.key.R and modifiers & pyglet.window.key.MOD_CTRL: self.app.reload() - + def placeLabels(self, width, height): x = 5 y = 5 @@ -370,21 +354,21 @@ class MyWindow(pyglet.window.Window): label.x = x label.y = y y += 20 - + x = width - 5 y = 5 for label in self.brLabels: label.x = x label.y = y y += 20 - + x = 5 y = height - 5 for label in self.tlLabels: label.x = x label.y = y y -= 20 - + x = width - 5 y = height - 5 for label in self.trLabels: @@ -392,45 +376,44 @@ class MyWindow(pyglet.window.Window): label.y = y y -= 20 - def on_mouse_scroll(self, x, y, dx, dy): # zoom on mouse scroll delta = dx + dy if delta == 0: return - z = 1.2 if delta>0 else 1/1.2 + z = 1.2 if delta > 0 else 1 / 1.2 self.app.zoom = max(1.0, self.app.zoom * z) #print 'mouse scroll:', `x, y, dx, dy`, `z, self.app.zoom` def on_draw(self): #print "draw" - + # Clear buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - + # setup projection glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65, self.width / float(self.height), 0.1, 1000) - + # setup camera glMatrixMode(GL_MODELVIEW) glLoadIdentity() - gluLookAt(0,1.5,2,0,0,0,0,1,0) - + gluLookAt(0, 1.5, 2, 0, 0, 0, 0, 1, 0) + # enable alpha blending glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) - + # rotate axes to match reprap style - glRotated(-90, 1,0,0) + glRotated(-90, 1, 0, 0) # user rotate model - glRotated(-self.app.RX, 1,0,0) - glRotated(self.app.RZ, 0,0,1) - + glRotated(-self.app.RX, 1, 0, 0) + glRotated(self.app.RZ, 0, 0, 1) + # Todo check this - glTranslated(0,0,-0.5) - + glTranslated(0, 0, -0.5) + # fit & user zoom model max_width = max( self.app.model.bbox.dx(), @@ -439,57 +422,52 @@ class MyWindow(pyglet.window.Window): ) scale = self.app.zoom / max_width glScaled(scale, scale, scale) - + glTranslated(-self.app.model.bbox.cx(), -self.app.model.bbox.cy(), -self.app.model.bbox.cz()) - - - + # draw axes glBegin(GL_LINES) - glColor3f(1,0,0) - glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(1,0,0); glVertex3f(1,0.1,0) - glVertex3f(1,0,0); glVertex3f(self.app.model.bbox.xmax,0,0) - glColor3f(0,1,0) - glVertex3f(0,0,0); glVertex3f(0,1,0); glVertex3f(0,1,0); glVertex3f(0,1,0.1) - glVertex3f(0,1,0); glVertex3f(0,self.app.model.bbox.ymax,0) - glColor3f(0,0,1) - glVertex3f(0,0,0); glVertex3f(0,0,1); glVertex3f(0,0,1); glVertex3f(0.1,0,1) - glVertex3f(0,0,1); glVertex3f(0,0,self.app.model.bbox.zmax) + glColor3f(1, 0, 0) + glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 0.1, 0) + glVertex3f(1, 0, 0); glVertex3f(self.app.model.bbox.xmax, 0, 0) + glColor3f(0, 1, 0) + glVertex3f(0, 0, 0); glVertex3f(0, 1, 0); glVertex3f(0, 1, 0); glVertex3f(0, 1, 0.1) + glVertex3f(0, 1, 0); glVertex3f(0, self.app.model.bbox.ymax, 0) + glColor3f(0, 0, 1) + glVertex3f(0, 0, 0); glVertex3f(0, 0, 1); glVertex3f(0, 0, 1); glVertex3f(0.1, 0, 1) + glVertex3f(0, 0, 1); glVertex3f(0, 0, self.app.model.bbox.zmax) glEnd() - - + glLineWidth(1) # Draw the model layers # lower layers for graphic in self.app.graphics_old[0:self.app.layerIdx]: graphic.draw(GL_LINES) - + glLineWidth(2) - + # highlighted layer graphic = self.app.graphics_current[self.app.layerIdx] graphic.draw(GL_LINES) - + glLineWidth(1) # limbo layers - for graphic in self.app.graphics_limbo[self.app.layerIdx+1:]: + for graphic in self.app.graphics_limbo[self.app.layerIdx + 1:]: graphic.draw(GL_LINES) - - + # disable depth for HUD glDisable(GL_DEPTH_TEST) glDepthMask(0) - - #Set your camera up for 2d, draw 2d scene - + + # Set your camera up for 2d, draw 2d scene glMatrixMode(GL_PROJECTION) glLoadIdentity(); glOrtho(0, self.width, 0, self.height, -1, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() - - self.fpsLabel.text = "%d fps"%int(round(pyglet.clock.get_fps())) - + + self.fpsLabel.text = "%d fps" % int(round(pyglet.clock.get_fps())) + for label in self.blLabels: label.draw() for label in self.brLabels: @@ -498,9 +476,10 @@ class MyWindow(pyglet.window.Window): label.draw() for label in self.trLabels: label.draw() - + # reenable depth for next model display glEnable(GL_DEPTH_TEST) glDepthMask(1) + App().main() From 431c0fad7a2449539604041e4d9e230ca2524531 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sun, 2 Sep 2018 05:01:08 +0200 Subject: [PATCH 3/4] Add Makefile for style checks Simply style checks via "make lint". --- Makefile | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bf79a2a --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +.PHONY: help + @echo "Available targets:" + @echo " lint" + @echo " help" + + +.PHONY: lint +lint: + python -m flake8 . From 500339ca6de1a5b43c9eed480dfd641b06eaa52f Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sun, 2 Sep 2018 05:01:39 +0200 Subject: [PATCH 4/4] Add travis tests for style issues --- .travis.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2f94e59 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: python +python: + - "2.7" + +addons: + apt: + packages: + - python-flake8 + +script: + # TODO: fix these remaining style check issues + - flake8 --ignore=W191,W601,E126,E261,E265,E266,E501 . + - flake8 --ignore=F401,F403,F405,W191,E126,E128,E265,E266,E402,E501,E702,E703 yagv