File size: 3,156 Bytes
6d9bd32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()