simulationapps / app.py
karthikmn's picture
Update app.py
73c5836 verified
raw
history blame
1.99 kB
import os
import gradio as gr
from utils.ansys_utils import run_simulation
def generate_gcode(cad_file):
"""
Generates a simple G-Code file for a square path based on the CAD file.
Returns the file path of the generated G-Code file.
"""
try:
# Ensure the output directory exists
output_dir = "/mnt/data/"
os.makedirs(output_dir, exist_ok=True)
# Define the G-Code content
gcode_content = """
G21 ; Set units to millimeters
G17 ; Select XY plane
G90 ; Absolute positioning
G1 F1500 ; Set feed rate
G0 Z5 ; Raise Z-axis to safe height
G0 X0 Y0 ; Move to start position
G1 Z-1 F300 ; Lower Z-axis to cutting depth
G1 X10 Y0 ; Cut to the first corner
G1 X10 Y10 ; Cut to the second corner
G1 X0 Y10 ; Cut to the third corner
G1 X0 Y0 ; Return to the start position
G0 Z5 ; Raise Z-axis to safe height
M30 ; End program
"""
# Save the G-Code to a file
gcode_file = os.path.join(output_dir, "generated_gcode.nc")
with open(gcode_file, "w") as f:
f.write(gcode_content)
return gcode_file
except Exception as e:
return f"Error in G-Code generation: {e}"
def automate_workflow(cad_file):
"""
Automates the workflow:
1. Run Simulation in ANSYS
2. Generate G-Code for CNC
"""
# Step 1: Run Simulation in ANSYS
simulation_results = run_simulation(cad_file)
# Step 2: Generate G-Code
gcode_file = generate_gcode(cad_file)
return simulation_results, gcode_file
# Gradio Interface
interface = gr.Interface(
fn=automate_workflow,
inputs=[
gr.File(label="Upload CAD File (.step format)")
],
outputs=[
gr.Text(label="Simulation Results"),
gr.File(label="Generated G-Code File")
],
title="ANSYS Simulation and G-Code Generation"
)
if __name__ == "__main__":
interface.launch()