-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw copy 3.py
206 lines (167 loc) · 8.25 KB
/
draw copy 3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox, filedialog
import serial # Import the serial module
# Declare global variables
canvas = None
serial_port = None
def setup_serial():
global serial_port
serial_port = serial.Serial('COM3', 115200, timeout=1)
print("Serial port opened on COM3 with baud rate 115200.")
setup_serial() # Call the setup_serial function to initialize the serial port
def send_lines_to_engraver():
global canvas
lines = canvas.find_withtag("line") # Get all items tagged with 'line'
gcode_commands = []
for line in lines:
x1, y1, x2, y2 = canvas.coords(line) # Get coordinates of each line
gcode_commands.append(f"G1 X{x1} Y{y1} S100") # Assuming laser power is set to 100 (adjust as needed)
gcode_commands.append(f"G1 X{x2} Y{y2} S0") # Turn off laser at the end of the line
send_command(gcode_commands)
def send_command(gcode_commands):
global serial_port
for command in gcode_commands:
serial_port.write((command + "\n").encode())
serial_port.flush()
def save_gcode():
global canvas
lines = canvas.find_withtag("line")
canvas_height = canvas.winfo_height() # Get canvas height
gcode_lines = []
for line in lines:
x1, y1, x2, y2 = canvas.coords(line)
# Adjust y-coordinate to match desired position relative to bottom-left corner
y1 = canvas_height - y1
y2 = canvas_height - y2
# Add feedrate (speed) and laser power (S100 for on, S0 for off)
gcode_lines.append(f"G1 X{x1} Y{y1} F3000 S100") # Turn laser on
gcode_lines.append(f"G1 X{x2} Y{y2} F2000 S0") # Turn laser off
file_path = filedialog.asksaveasfilename(defaultextension=".gcode",
filetypes=[("G-code Files", "*.gcode")])
if file_path:
with open(file_path, 'w') as f:
f.write("\n".join(gcode_lines))
messagebox.showinfo("G-code Export", "G-code saved successfully.")
def cut():
global serial_port
file_path = filedialog.askopenfilename(filetypes=[("G-code Files", "*.gcode")])
if file_path:
print("Reading G-code file:", file_path)
with open(file_path, 'r') as f:
gcode_commands = f.readlines()
print("Sending G-code commands:")
for command in gcode_commands:
print(command.strip())
send_command([command.strip() for command in gcode_commands]) # Sending each command individually
def home_laser():
global serial_port
print("Sending homing command...")
# Send homing command along with the current position as 0,0
send_command(["G28 F2000 X0 Y0"])
print("Homing command sent.")
def main():
global canvas, serial_port
root = tk.Tk()
root.title("Draw Cut Application")
root.geometry("1600x1200") # Adjusted window size for better layout
# Menu frame for inputs at the top of the window
menu_frame = tk.Frame(root)
menu_frame.pack(side='top', fill='x', expand=False)
# Variables for machine and material dimensions with default values
machine_width = tk.IntVar(value=410)
machine_height = tk.IntVar(value=860)
# Canvas setup; dimensions will be updated via a function
canvas = tk.Canvas(root, bg='white')
canvas.pack(fill='both', expand=True)
# Function to update canvas size based on machine dimensions
def update_canvas():
mw, mh = machine_width.get(), machine_height.get()
canvas.config(width=mw, height=mh)
canvas.delete("all") # Clear previous drawings
# Draw the maximum cutting area
canvas.create_rectangle(2.5, 2.5, mw - 2.5, mh - 2.5, outline="blue", width=1)
# Function to clear all lines from the canvas
def clear_lines():
canvas.delete("line") # Assumes all lines are tagged with 'line' when created
# Machine width and height entries
machine_width_label = ttk.Label(menu_frame, text="Machine Width (mm):")
machine_width_label.pack(side='left', padx=5)
machine_width_entry = ttk.Entry(menu_frame, textvariable=machine_width, width=7)
machine_width_entry.pack(side='left', padx=5)
machine_height_label = ttk.Label(menu_frame, text="Machine Height (mm):")
machine_height_label.pack(side='left', padx=5)
machine_height_entry = ttk.Entry(menu_frame, textvariable=machine_height, width=7)
machine_height_entry.pack(side='left', padx=5)
confirm_machine_button = ttk.Button(menu_frame, text="Set Machine Size", command=update_canvas)
confirm_machine_button.pack(side='left', padx=5)
material_x_label = ttk.Label(menu_frame, text="Material Width (mm):")
material_x_label.pack(side='left', padx=5)
material_x_entry = ttk.Entry(menu_frame, width=7)
material_x_entry.pack(side='left', padx=5)
material_y_label = ttk.Label(menu_frame, text="Material Height (mm):")
material_y_label.pack(side='left', padx=5)
material_y_entry = ttk.Entry(menu_frame, width=7)
material_y_entry.pack(side='left', padx=5)
confirm_button = ttk.Button(menu_frame, text="Set Material", command=lambda: draw_material())
confirm_button.pack(side='left', padx=5)
draw_line_button = ttk
draw_line_button = ttk.Button(menu_frame, text="Enable Draw Line", command=lambda: toggle_line_drawing())
draw_line_button.pack(side='left', padx=5)
# Additional button to clear lines
clear_lines_button = ttk.Button(menu_frame, text="Clear Lines", command=clear_lines)
clear_lines_button.pack(side='left', padx=5)
# Additional button to clear lines
gcode_button = ttk.Button(menu_frame, text="save g_code", command=save_gcode)
gcode_button.pack(side='left', padx=5)
# Additional button to clear lines
cut_button = ttk.Button(menu_frame, text="Cut", command=cut)
cut_button.pack(side='left', padx=5)
# Button for homing
home_button = ttk.Button(menu_frame, text="Home", command=home_laser)
home_button.pack(side='left', padx=5)
# Functions to handle drawing and material settings
line_start = None
line_drawing_enabled = False # To track if line drawing mode is enabled
def handle_left_click(event):
nonlocal line_start
if line_drawing_enabled:
if line_start is None:
line_start = (event.x, event.y)
canvas.create_oval(event.x-2, event.y-2, event.x+2, event.y+2, fill="green")
else:
# Tagging lines with 'line' for easy removal
canvas.create_line(line_start[0], line_start[1], event.x, event.y, fill="green", width=2, tags="line")
line_start = (event.x, event.y)
def handle_right_click(event):
nonlocal line_start
line_start = None
def draw_material():
canvas.delete("material_area")
try:
material_width = int(material_x_entry.get())
material_height = int(material_y_entry.get())
if 0 < material_width <= machine_width.get() and 0 < material_height <= machine_height.get():
x1 = 2.5
y1 = machine_height.get() - material_height - 2.5
canvas.create_rectangle(x1, y1, x1 + material_width, y1 + material_height, outline="red", width=2, fill="light grey", tags="material_area")
else:
messagebox.showerror("Error", "Material dimensions must fit within the specified machine dimensions.")
except ValueError:
messagebox.showerror("Error", "Please enter valid integer dimensions.")
def toggle_line_drawing():
nonlocal line_drawing_enabled
if line_drawing_enabled:
canvas.unbind("<Button-1>")
canvas.unbind("<Button-3>")
draw_line_button.config(text="Enable Draw Line")
else:
canvas.bind("<Button-1>", handle_left_click)
canvas.bind("<Button-3>", handle_right_click)
draw_line_button.config(text="Disable Draw Line")
line_drawing_enabled = not line_drawing_enabled
# Initialize the canvas with the default machine size
update_canvas()
root.mainloop()
if __name__ == "__main__":
main()