import gradio as gr import os # Directories TEMPLATE_DIR = "simulations/templates" OUTPUT_DIR = "simulations/outputs" ASSETS_DIR = "assets" # Ensure output directory exists os.makedirs(OUTPUT_DIR, exist_ok=True) # Function to generate CAD files def generate_cad(length, width, height, material): cad_content = f"CAD File: Box with dimensions {length}x{width}x{height} mm using {material}." output_path = os.path.join(OUTPUT_DIR, "generated_cad.txt") with open(output_path, "w") as f: f.write(cad_content) return f"CAD file saved at: {output_path}" # Function to set up ANSYS simulation def setup_simulation(load, temperature, simulation_type): template_path = os.path.join(TEMPLATE_DIR, "apdl_template.txt") try: with open(template_path, "r") as f: apdl_template = f.read() except FileNotFoundError: return "APDL template not found. Ensure 'apdl_template.txt' exists in the templates folder." apdl_script = apdl_template.format( load=load, temperature=temperature, simulation_type=simulation_type ) output_path = os.path.join(OUTPUT_DIR, "simulation_script.inp") with open(output_path, "w") as f: f.write(apdl_script) return f"Simulation script saved at: {output_path}" # Function to optimize G-Code def optimize_gcode(toolpath_file, speed): if not os.path.exists(toolpath_file): return f"Toolpath file '{toolpath_file}' not found." optimized_content = f"Optimized toolpath for speed {speed} mm/s." output_path = os.path.join(OUTPUT_DIR, "optimized_gcode.txt") with open(output_path, "w") as f: f.write(optimized_content) return f"Optimized G-Code saved at: {output_path}" # Gradio GUI with gr.Blocks() as gui: gr.Markdown("# CNC and ANSYS Workflow Automation") with gr.Tab("Parameter Input"): length = gr.Number(label="Length (mm)") width = gr.Number(label="Width (mm)") height = gr.Number(label="Height (mm)") material = gr.Textbox(label="Material") generate_cad_btn = gr.Button("Generate CAD File") cad_output = gr.Textbox(label="CAD Output") generate_cad_btn.click(generate_cad, [length, width, height, material], cad_output) with gr.Tab("Simulation Setup"): load = gr.Number(label="Load (N)") temperature = gr.Number(label="Temperature (°C)") simulation_type = gr.Radio(["Structural", "Thermal", "Acoustic"], label="Simulation Type") setup_sim_btn = gr.Button("Setup Simulation") simulation_output = gr.Textbox(label="Simulation Script Output") setup_sim_btn.click(setup_simulation, [load, temperature, simulation_type], simulation_output) with gr.Tab("G-Code Optimization"): toolpath_file = gr.Textbox(label="Toolpath File (e.g., toolpath.nc)") speed = gr.Number(label="Cutting Speed (mm/s)") optimize_btn = gr.Button("Optimize G-Code") gcode_output = gr.Textbox(label="Optimized G-Code Output") optimize_btn.click(optimize_gcode, [toolpath_file, speed], gcode_output) gui.launch()