Skip to content

Commit

Permalink
feat: Improved UI responsiveness during file compression
Browse files Browse the repository at this point in the history
* Implemented threading for background file compression
* Enabled UI element disabling during compression process
  • Loading branch information
fan-ziqi committed Apr 27, 2024
1 parent bb333d0 commit 087ab6f
Showing 1 changed file with 51 additions and 10 deletions.
61 changes: 51 additions & 10 deletions stl_compresser_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,85 @@
from tkinter import filedialog
from tkinter import ttk
import trimesh
import threading

def choose_files(entry_widget):
# Function to choose input files
paths = filedialog.askopenfilenames(filetypes=[("STL files", "*.stl")])
if paths:
entry_widget.delete(0, tk.END)
entry_widget.insert(0, ";".join(paths))

def choose_output_folder(entry_widget):
# Function to choose output folder
path = filedialog.askdirectory()
if path:
entry_widget.delete(0, tk.END)
entry_widget.insert(0, path)

def compress_stl(input_paths, output_path, target_triangles, progress_bar, progress_label):
def compress_stl(input_paths, output_path, target_triangles, progress_bar, progress_label, on_complete):
# Function to compress STL files
files = input_paths.split(";")

total_files = len(files)
for i, file_path in enumerate(files):
progress = int((i + 1) / total_files * 100)
progress_bar["value"] = progress
progress_label["text"] = f"Progress: {i+1}/{total_files} ({progress}%)"
window.update_idletasks()
progress_per_file = 100 / total_files

for i, file_path in enumerate(files):
mesh = trimesh.load_mesh(file_path)
simplified_mesh = mesh.simplify_quadric_decimation(target_triangles)
output_file_path = os.path.join(output_path, os.path.basename(file_path))
simplified_mesh.export(output_file_path)

# Update progress bar
progress = int((i + 1) * progress_per_file)
progress_label["text"] = f"Progress: {i + 1}/{total_files} ({progress}%)"

# Update progress bar and interface after processing each file
progress_bar["value"] = progress
window.update_idletasks()

# Call the callback function after compression is complete
on_complete()

def compress():
# Disable buttons, entry, and slider
compress_button.config(state=tk.DISABLED)
choose_button.config(state=tk.DISABLED)
output_button.config(state=tk.DISABLED)
slider.config(state=tk.DISABLED)
input_entry.config(state=tk.DISABLED)
output_entry.config(state=tk.DISABLED)

input_paths = input_entry.get()
output_path = output_entry.get()
target_triangles = int(slider.get())

progress_bar["value"] = 0
progress_label["text"] = "Progress: 0/0 (0%)"
compress_stl(input_paths, output_path, target_triangles, progress_bar, progress_label)
progress_bar["value"] = 100
progress_label["text"] = "Progress: Complete"

# Define callback function after compression thread finishes
def on_compress_finished():
# Enable buttons, entry, and slider
compress_button.config(state=tk.NORMAL)
choose_button.config(state=tk.NORMAL)
output_button.config(state=tk.NORMAL)
slider.config(state=tk.NORMAL)
input_entry.config(state=tk.NORMAL)
output_entry.config(state=tk.NORMAL)

# Create a thread to perform compression operation
compress_thread = threading.Thread(target=compress_stl,
args=(input_paths, output_path, target_triangles, progress_bar, progress_label,
on_compress_finished))

# Start the thread
compress_thread.start()

window = tk.Tk()
window.title("STL Compresser | Made by github@fan-ziqi")
window.title("STL Compressor v1.1 | Made by github@fan-ziqi")
window.resizable(False, False) # Disable resizing

# Input selection
input_label = tk.Label(window, text="Select Input File(s):")
input_label.pack()
input_frame = tk.Frame(window)
Expand All @@ -56,6 +93,7 @@ def compress():
choose_button = tk.Button(input_frame, text="Browse", command=lambda: choose_files(input_entry))
choose_button.pack(side=tk.LEFT)

# Output selection
output_label = tk.Label(window, text="Select Output Folder:")
output_label.pack()
output_frame = tk.Frame(window)
Expand All @@ -67,13 +105,15 @@ def compress():
output_button = tk.Button(output_frame, text="Browse", command=lambda: choose_output_folder(output_entry))
output_button.pack(side=tk.LEFT)

# Slider for selecting target triangles
slider_label = tk.Label(window, text="Select Target Triangles (100-10000):")
slider_label.pack()

slider = tk.Scale(window, from_=100, to=10000, orient=tk.HORIZONTAL, length=300)
slider.set(1000)
slider.pack()

# Progress bar and label
progress_frame = tk.Frame(window)
progress_frame.pack()

Expand All @@ -83,6 +123,7 @@ def compress():
progress_bar = ttk.Progressbar(progress_frame, length=300, mode='determinate')
progress_bar.pack()

# Compression button
compress_button = tk.Button(window, text="Compress", command=compress)
compress_button.pack()

Expand Down

0 comments on commit 087ab6f

Please sign in to comment.