From 14a099c3e4063401fff5c7cdd05f2ced6b0e4c78 Mon Sep 17 00:00:00 2001 From: localhorst Date: Thu, 16 Jun 2022 12:59:31 +0200 Subject: [PATCH] added scrolling and zoom in simulator --- gcode_simulator.py | 103 +++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/gcode_simulator.py b/gcode_simulator.py index 2d31a45..c114ad3 100755 --- a/gcode_simulator.py +++ b/gcode_simulator.py @@ -1,16 +1,14 @@ -#!/usr/bin/python - -from tkinter import Tk, Canvas, Frame, BOTH, EventType, ALL +import tkinter as tk import argparse gcode_file_path = " " bedsizex = 0 bedsizey = 0 scale_factor = 1.0 -x_offset = 24.0 -y_offset = 24.0 +x_offset = 48.0 +y_offset = 48.0 laser_on = False -last_position = {"x": x_offset, "y": 720-y_offset} #set to initial start at home pos +last_position = {"x": 0.0, "y": 0.0} global canvas debug = True @@ -40,7 +38,7 @@ def plot_paths(canvas): laser_on = True if(line.startswith('G1')): x = float(line[(int(line.find('X'))+1):(int(line.find('Y')-1))]) * (scale_factor*1) + x_offset - y = (720-y_offset) - (float(line[(int(line.find('Y'))+1):(int(line.find(';')-1))])* (scale_factor*1)) + y = (bedsizey * scale_factor-y_offset) - (float(line[(int(line.find('Y'))+1):(int(line.find(';')-1))])* (scale_factor*1)) if(debug): print("[decoder] movement to " + str(x) + " " + str(y)) @@ -51,44 +49,75 @@ def plot_paths(canvas): last_position["x"] = x last_position["y"] = y - -class SimulatorView(Frame): - def __init__(self): - global canvas - super().__init__() - canvas = Canvas(self) - #canvas.bind("", lambda event: canvas.scan_dragto(event.x, event.y, gain=1)) - self.initUI() - - def initUI(self): +class Example(tk.Frame): + def __init__(self, root): + tk.Frame.__init__(self, root) + self.canvas = tk.Canvas(self, width=400, height=400, background="bisque") + self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview) + self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview) + self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set) + self.canvas.configure(scrollregion=(0,0,1000,1000)) self.master.title("gcode simulator") - self.pack(fill=BOTH, expand=1) - canvas.create_line(x_offset, y_offset, x_offset, (720-y_offset), width=2, fill='black') - canvas.create_line(1280-x_offset, y_offset, 1280-x_offset, (720-y_offset), width=2, fill='black') - canvas.create_line(x_offset, y_offset, 1280-x_offset, y_offset, width=2, fill='black') - canvas.create_line(x_offset, (720-y_offset), 1280-x_offset, (720-y_offset), width=2, fill='black') - plot_paths(canvas) - canvas.pack(fill=BOTH, expand=1) + self.xsb.grid(row=1, column=0, sticky="ew") + self.ysb.grid(row=0, column=1, sticky="ns") + self.canvas.grid(row=0, column=0, sticky="nsew") + self.grid_rowconfigure(0, weight=1) + self.grid_columnconfigure(0, weight=1) + actual_bedsize_x = bedsizex * scale_factor + actual_bedsize_y = bedsizey * scale_factor -def main(): + self.canvas.create_line(x_offset, y_offset, x_offset, (actual_bedsize_y-y_offset), width=2, fill='black') + self.canvas.create_line(actual_bedsize_x-x_offset, y_offset, actual_bedsize_x-x_offset, (actual_bedsize_y-y_offset), width=2, fill='black') + self.canvas.create_line(x_offset, y_offset, actual_bedsize_x-x_offset, y_offset, width=2, fill='black') + self.canvas.create_line(x_offset, (actual_bedsize_y-y_offset), actual_bedsize_x-x_offset, (actual_bedsize_y-y_offset), width=2, fill='black') + + plot_paths(self.canvas) + + # This is what enables using the mouse: + self.canvas.bind("", self.move_start) + self.canvas.bind("", self.move_move) + #linux scroll + self.canvas.bind("", self.zoomerP) + self.canvas.bind("", self.zoomerM) + #windows scroll + self.canvas.bind("",self.zoomer) + + #move + def move_start(self, event): + self.canvas.scan_mark(event.x, event.y) + def move_move(self, event): + self.canvas.scan_dragto(event.x, event.y, gain=1) + + #windows zoom + def zoomer(self,event): + if (event.delta > 0): + self.canvas.scale("all", event.x, event.y, 1.1, 1.1) + elif (event.delta < 0): + self.canvas.scale("all", event.x, event.y, 0.9, 0.9) + self.canvas.configure(scrollregion = self.canvas.bbox("all")) + + #linux zoom + def zoomerP(self,event): + self.canvas.scale("all", event.x, event.y, 1.1, 1.1) + self.canvas.configure(scrollregion = self.canvas.bbox("all")) + def zoomerM(self,event): + self.canvas.scale("all", event.x, event.y, 0.9, 0.9) + self.canvas.configure(scrollregion = self.canvas.bbox("all")) + +if __name__ == "__main__": parse_arguments() if(bedsizex < bedsizey): - scale_factor = float(720/bedsizey) + scale_factor = float(720/bedsizey)*0.8 else: - scale_factor = float(1280/bedsizex) + scale_factor = float(1280/bedsizex)*0.8 print("scale factor: " + str(scale_factor)) + last_position = {"x": x_offset, "y": bedsizey * scale_factor-y_offset} #set to initial start at home pos - #plot_paths(0) - - root = Tk() - ex = SimulatorView() - root.geometry(str(1280+int(x_offset))+"x"+str(720+int(y_offset))+"+300+300") - root.mainloop() - - -if __name__ == '__main__': - main() + root = tk.Tk() + Example(root).pack(fill="both", expand=True) + root.geometry(str(int(bedsizex*scale_factor+x_offset))+"x"+str(int(bedsizey*scale_factor+y_offset))+"+300+300") + root.mainloop() \ No newline at end of file