import subprocess
import re

def run_openscad(tooldiam: float, side:float, thickness:float, width:float, length:float, number:float, zdown: list, input_file: str, output_file: str):
    with open(output_file, 'w') as f:
        f.write(f'({input_file})\n') # Add input file name at the beginning
        f.write('G90\nT13 M06 S11000\nM03\nG04 P1\nF250\n') # Add specified lines at the beginning
        for z in zdown:
            temp_output_file = f'temp_output_{str(z).replace(".", "_")}.svg' 
            args = ['C:\Program Files\OpenSCAD\openscad.exe', '-o', temp_output_file, f'-Dtooldiam={tooldiam}', f'-Dside={side}', f'-Dthickness={thickness}', f'-Dwidth={width}', f'-Dlength={length}', f'-Dnumber={number}', f'-Dzdown={z}', input_file]
            subprocess.run(args)
            with open(temp_output_file, 'r') as temp_f:
                content = temp_f.read()
            content = re.sub(r'<.*?>', '', content)
            content = re.sub(r'\n.*?>\n', '', content) # Remove lines that begin with < and end with > without leaving a blank line
            content = re.sub(r'OpenSCAD Model\n', '', content)
            content = re.sub(r'<path d="', '', content)
            content = re.sub(r'M', f'\nG00 Z2\nG00', content) # Replace M with G00 Z2 G00
            content = re.sub(r'L', '\nG01', content)
            content = re.sub(r'(?<!\()\bz\b(?!\))', '\nG00 Z2', content)
            content = re.sub(r'G00\s+([\d.-]+),([\d.-]+)', lambda m: f'G00 X{m.group(1)} Y{-float(m.group(2))}\nG01 Z-{z}', content) # Negate the value after the Y and remove the negative sign if the value is already negative
            content = re.sub(r'G01\s+([\d.-]+),([\d.-]+)', lambda m: f'G01 X{m.group(1)} Y{-float(m.group(2))}', content) # Negate the value after the Y and remove the negative sign if the value is already negative
            lines = content.split('\n')
            lines = [line for line in lines if line.strip()] # Remove empty lines
            f.write(f'(zdown = {z})\n(tooldiam = {tooldiam})\nG00 Z2\n')
            f.write('\n'.join(lines))
            f.write('\n\n') # Add a blank line between each iteration
        f.write('M05\nM02\n') # Add specified lines at the end


import tkinter as tk

def run_script():
   
    tooldiam = float(tooldiam_entry.get())
    side = float(sides_entry.get())
    thickness = float(thickness_entry.get())
    width = float(width_entry.get())
    length = float(length_entry.get())
    number = float(number_entry.get())
    zdown = list(map(float, zdown_entry.get().split(',')))
    input_file = input_file_entry.get()
    output_file = output_file_entry.get()
	
    run_openscad(tooldiam, side, thickness, width, length, number, zdown,  input_file, output_file)

from tkinter import font
import tkinter as tk


root = tk.Tk()
default_font = font.nametofont("TkDefaultFont")
default_font.configure(size=14)

root.option_add("*Entry.Font", "Helvetica 14")

frame = tk.Frame(root,bd=8,relief="sunken")
frame.pack(padx=5, pady=5)

tooldiam_label = tk.Label(frame, text="Tool Diameter")
tooldiam_label.grid(row=0,column=0,padx=5,pady=5)
tooldiam_entry = tk.Entry(frame, width=10)
tooldiam_entry.grid(row=1,column=0,padx=5,pady=5)
tooldiam_entry.insert(0, "6.3")

sides_label = tk.Label(frame, text="number of sides (3=both)",font=("TkDefaultFont",10))
sides_label.grid(row=0,column=1,padx=5,pady=5)
sides_entry = tk.Entry(frame, width=10)
sides_entry.grid(row=1,column=1,padx=5,pady=5)
sides_entry.insert(0, "3")

thickness_label = tk.Label(frame, text="Thickness")
thickness_label.grid(row=2,column=0,padx=5,pady=5)
thickness_entry = tk.Entry(frame, width=10)
thickness_entry.grid(row=3,column=0,padx=5,pady=5)
thickness_entry.insert(0, "14")

width_label = tk.Label(frame, text="Width")
width_label.grid(row=2,column=1,padx=5,pady=5)
width_entry = tk.Entry(frame, width=10)
width_entry.grid(row=3,column=1,padx=5,pady=5)
width_entry.insert(0, "45")

length_label = tk.Label(frame, text="Length (inside)")
length_label.grid(row=4,column=0,padx=5,pady=5)
length_entry = tk.Entry(frame, width=10)
length_entry.grid(row=5,column=0,padx=5,pady=5)
length_entry.insert(0, "15")

number_label = tk.Label(frame, text="Number of fingers")
number_label.grid(row=4,column=1,padx=5,pady=5)
number_entry = tk.Entry(frame, width=2)
number_entry.grid(row=5,column=1,padx=5,pady=5)
number_entry.insert(0, "4")

zdown_label = tk.Label(frame, text="Z Down (comma separated)")
zdown_label.grid(row=6,columnspan=2,padx=5,pady=5)
zdown_entry = tk.Entry(frame, width=40)
zdown_entry.grid(row=7,columnspan=2,padx=5,pady=5)
zdown_entry.insert(0, "0.5, 1, 1.5, 2, 2.5, 3, 3.5, 8, 14")

input_file_label = tk.Label(frame, text="Input File")
input_file_label.grid(row=8,columnspan=2,padx=5,pady=5)
input_file_entry = tk.Entry(frame, width=40)
input_file_entry.grid(row=9,columnspan=2,padx=5,pady=5)
input_file_entry.insert(0, "P:\Docs\openscad\lump2.scad")

output_file_label = tk.Label(frame, text="Output File")
output_file_label.grid(row=10,columnspan=2,padx=5,pady=5)
output_file_entry = tk.Entry(frame, width=40)
output_file_entry.grid(row=11,columnspan=2,padx=5,pady=5)
output_file_entry.insert(0, "P:\docs\openscad\lumpxxx.dnc")

run_button = tk.Button(frame, text="Run Script", command=run_script)
run_button.grid(row=12,column=1,padx=5,pady=5)

root.mainloop()


