added scrolling and zoom in simulator
This commit is contained in:
parent
97aead6f86
commit
14a099c3e4
|
@ -1,16 +1,14 @@
|
||||||
#!/usr/bin/python
|
import tkinter as tk
|
||||||
|
|
||||||
from tkinter import Tk, Canvas, Frame, BOTH, EventType, ALL
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
gcode_file_path = " "
|
gcode_file_path = " "
|
||||||
bedsizex = 0
|
bedsizex = 0
|
||||||
bedsizey = 0
|
bedsizey = 0
|
||||||
scale_factor = 1.0
|
scale_factor = 1.0
|
||||||
x_offset = 24.0
|
x_offset = 48.0
|
||||||
y_offset = 24.0
|
y_offset = 48.0
|
||||||
laser_on = False
|
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
|
global canvas
|
||||||
debug = True
|
debug = True
|
||||||
|
|
||||||
|
@ -40,7 +38,7 @@ def plot_paths(canvas):
|
||||||
laser_on = True
|
laser_on = True
|
||||||
if(line.startswith('G1')):
|
if(line.startswith('G1')):
|
||||||
x = float(line[(int(line.find('X'))+1):(int(line.find('Y')-1))]) * (scale_factor*1) + x_offset
|
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))
|
if(debug): print("[decoder] movement to " + str(x) + " " + str(y))
|
||||||
|
@ -51,44 +49,75 @@ def plot_paths(canvas):
|
||||||
last_position["x"] = x
|
last_position["x"] = x
|
||||||
last_position["y"] = y
|
last_position["y"] = y
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SimulatorView(Frame):
|
class Example(tk.Frame):
|
||||||
def __init__(self):
|
def __init__(self, root):
|
||||||
global canvas
|
tk.Frame.__init__(self, root)
|
||||||
super().__init__()
|
self.canvas = tk.Canvas(self, width=400, height=400, background="bisque")
|
||||||
canvas = Canvas(self)
|
self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
|
||||||
#canvas.bind("<B1-Motion>", lambda event: canvas.scan_dragto(event.x, event.y, gain=1))
|
self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
|
||||||
self.initUI()
|
self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)
|
||||||
|
self.canvas.configure(scrollregion=(0,0,1000,1000))
|
||||||
def initUI(self):
|
|
||||||
self.master.title("gcode simulator")
|
self.master.title("gcode simulator")
|
||||||
self.pack(fill=BOTH, expand=1)
|
self.xsb.grid(row=1, column=0, sticky="ew")
|
||||||
canvas.create_line(x_offset, y_offset, x_offset, (720-y_offset), width=2, fill='black')
|
self.ysb.grid(row=0, column=1, sticky="ns")
|
||||||
canvas.create_line(1280-x_offset, y_offset, 1280-x_offset, (720-y_offset), width=2, fill='black')
|
self.canvas.grid(row=0, column=0, sticky="nsew")
|
||||||
canvas.create_line(x_offset, y_offset, 1280-x_offset, y_offset, width=2, fill='black')
|
self.grid_rowconfigure(0, weight=1)
|
||||||
canvas.create_line(x_offset, (720-y_offset), 1280-x_offset, (720-y_offset), width=2, fill='black')
|
self.grid_columnconfigure(0, weight=1)
|
||||||
plot_paths(canvas)
|
|
||||||
canvas.pack(fill=BOTH, expand=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("<ButtonPress-1>", self.move_start)
|
||||||
|
self.canvas.bind("<B1-Motion>", self.move_move)
|
||||||
|
#linux scroll
|
||||||
|
self.canvas.bind("<Button-4>", self.zoomerP)
|
||||||
|
self.canvas.bind("<Button-5>", self.zoomerM)
|
||||||
|
#windows scroll
|
||||||
|
self.canvas.bind("<MouseWheel>",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()
|
parse_arguments()
|
||||||
|
|
||||||
if(bedsizex < bedsizey):
|
if(bedsizex < bedsizey):
|
||||||
scale_factor = float(720/bedsizey)
|
scale_factor = float(720/bedsizey)*0.8
|
||||||
else:
|
else:
|
||||||
scale_factor = float(1280/bedsizex)
|
scale_factor = float(1280/bedsizex)*0.8
|
||||||
|
|
||||||
print("scale factor: " + str(scale_factor))
|
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.Tk()
|
||||||
|
Example(root).pack(fill="both", expand=True)
|
||||||
root = Tk()
|
root.geometry(str(int(bedsizex*scale_factor+x_offset))+"x"+str(int(bedsizey*scale_factor+y_offset))+"+300+300")
|
||||||
ex = SimulatorView()
|
root.mainloop()
|
||||||
root.geometry(str(1280+int(x_offset))+"x"+str(720+int(y_offset))+"+300+300")
|
|
||||||
root.mainloop()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Loading…
Reference in New Issue