Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
-
import win32com.client
|
| 11 |
-
|
| 12 |
-
# Open SolidWorks
|
| 13 |
-
swApp = win32com.client.Dispatch("SldWorks.Application")
|
| 14 |
-
swApp.Visible = True
|
| 15 |
-
|
| 16 |
-
# Open the CAD file
|
| 17 |
-
model = swApp.OpenDoc6(cad_file_path, 1, 0, "", None, None)
|
| 18 |
-
|
| 19 |
-
# Save as STEP format
|
| 20 |
-
step_file_path = cad_file_path.replace(".sldprt", ".step")
|
| 21 |
-
success = model.Extension.SaveAs(step_file_path, 0, 1, None, None, None)
|
| 22 |
-
|
| 23 |
-
# Close the model
|
| 24 |
-
swApp.CloseDoc(model.GetTitle())
|
| 25 |
-
swApp.Quit()
|
| 26 |
-
|
| 27 |
-
if success:
|
| 28 |
-
return step_file_path
|
| 29 |
-
else:
|
| 30 |
-
raise Exception("Failed to convert CAD file to STEP format using SolidWorks.")
|
| 31 |
-
|
| 32 |
-
def run_ansys_simulation(step_file_path):
|
| 33 |
-
"""
|
| 34 |
-
Runs a simulation in ANSYS using the provided STEP file.
|
| 35 |
-
Requires ANSYS to be installed and accessible via command-line or API.
|
| 36 |
-
"""
|
| 37 |
-
# Example APDL script
|
| 38 |
-
apdl_script = f"""
|
| 39 |
-
/PREP7
|
| 40 |
-
/IMPORT,STEP,{step_file_path}
|
| 41 |
-
/SOLU
|
| 42 |
-
! Apply loads and boundary conditions here
|
| 43 |
-
SOLVE
|
| 44 |
-
FINISH
|
| 45 |
-
/POST1
|
| 46 |
-
! Post-process results
|
| 47 |
-
"""
|
| 48 |
-
|
| 49 |
-
# Save the APDL script to a temporary file
|
| 50 |
-
apdl_script_path = "simulation.inp"
|
| 51 |
-
with open(apdl_script_path, "w") as f:
|
| 52 |
-
f.write(apdl_script)
|
| 53 |
-
|
| 54 |
-
# Run ANSYS using the APDL script
|
| 55 |
-
ansys_path = "C:\\Program Files\\ANSYS Inc\\v201\\ansys\\bin\\winx64\\ansys201.exe" # Replace with your ANSYS installation path
|
| 56 |
-
command = f'"{ansys_path}" -b -i {apdl_script_path} -o simulation_output.txt'
|
| 57 |
-
|
| 58 |
-
try:
|
| 59 |
-
subprocess.run(command, shell=True, check=True)
|
| 60 |
-
with open("simulation_output.txt", "r") as f:
|
| 61 |
-
simulation_results = f.read()
|
| 62 |
-
return simulation_results
|
| 63 |
-
except Exception as e:
|
| 64 |
-
raise Exception(f"Failed to run ANSYS simulation: {e}")
|
| 65 |
-
|
| 66 |
-
def process_cad_and_generate_gcode_with_simulation(cad_file):
|
| 67 |
-
"""
|
| 68 |
-
Processes the uploaded CAD file, converts it to STEP using SolidWorks,
|
| 69 |
-
runs ANSYS simulation, and generates G-code for CNC simulation.
|
| 70 |
-
"""
|
| 71 |
-
# Check if CAD file is provided
|
| 72 |
-
if not cad_file:
|
| 73 |
-
return "No file provided. Please upload a valid CAD file.", None, None
|
| 74 |
-
|
| 75 |
-
# Save the uploaded CAD file to a temporary location
|
| 76 |
-
cad_file_path = cad_file.name
|
| 77 |
-
with open(cad_file_path, "wb") as f:
|
| 78 |
-
f.write(cad_file.read())
|
| 79 |
-
|
| 80 |
-
# Process CAD file with SolidWorks
|
| 81 |
-
step_file_path = process_cad_with_solidworks(cad_file_path)
|
| 82 |
-
|
| 83 |
-
# Run ANSYS simulation
|
| 84 |
-
simulation_results = run_ansys_simulation(step_file_path)
|
| 85 |
-
|
| 86 |
-
# Generate G-code
|
| 87 |
gcode = []
|
| 88 |
gcode.append("G21 ; Set units to millimeters")
|
| 89 |
gcode.append("G90 ; Absolute positioning")
|
| 90 |
gcode.append("G28 U0 W0 ; Return to home position")
|
| 91 |
-
gcode.append("M3 S1000 ; Start spindle at 1000 RPM")
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
gcode.append("G01 X0
|
| 96 |
-
gcode.append("G02
|
| 97 |
-
gcode.append("G01
|
| 98 |
-
gcode.append("
|
| 99 |
-
gcode.append("
|
| 100 |
-
gcode.append("G03 X1.125 Z2.75 R0.125 ; Arc (R=0.125)")
|
| 101 |
-
gcode.append("G01 X1.25 Z3 ; Linear cut to next step")
|
| 102 |
-
gcode.append("G01 X2 Z3 ; Straight cut")
|
| 103 |
-
gcode.append("G02 X2.75 Z4.125 R0.5 ; Arc (R=0.5)")
|
| 104 |
-
gcode.append("G01 X3 Z5 ; Linear move to final step")
|
| 105 |
-
gcode.append("G00 X100 Z100 ; Rapid move to safe position")
|
| 106 |
gcode.append("M5 ; Stop spindle")
|
| 107 |
gcode.append("M30 ; End of program")
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
inputs=gr.File(label="Upload CAD File (SolidWorks or STEP)"),
|
| 120 |
-
outputs=[
|
| 121 |
-
gr.Text(label="Processing Status"),
|
| 122 |
-
gr.Text(label="Simulation Results"),
|
| 123 |
-
gr.File(label="Download Generated G-Code")
|
| 124 |
],
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
)
|
| 129 |
|
| 130 |
-
# Launch the
|
| 131 |
-
|
| 132 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# G-code Generator Function
|
| 4 |
+
def generate_gcode(length, radius1, radius2, feed_rate, depth):
|
| 5 |
"""
|
| 6 |
+
Generates G-code for a turning operation with arcs and linear moves.
|
| 7 |
+
Inputs:
|
| 8 |
+
- length: Length of the part to machine
|
| 9 |
+
- radius1: Radius of first arc (R = 0.25)
|
| 10 |
+
- radius2: Radius of second arc (R = 0.125)
|
| 11 |
+
- feed_rate: Feed rate for the CNC machine
|
| 12 |
+
- depth: Cutting depth
|
| 13 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
gcode = []
|
| 15 |
gcode.append("G21 ; Set units to millimeters")
|
| 16 |
gcode.append("G90 ; Absolute positioning")
|
| 17 |
gcode.append("G28 U0 W0 ; Return to home position")
|
| 18 |
+
gcode.append(f"M3 S1000 ; Start spindle at 1000 RPM")
|
| 19 |
+
gcode.append(f"G00 X0 Z{depth} ; Rapid move to start position")
|
| 20 |
+
|
| 21 |
+
# Simulate moves based on reference image
|
| 22 |
+
gcode.append(f"G01 X0 Z-{length} F{feed_rate} ; Linear move")
|
| 23 |
+
gcode.append(f"G02 X{radius1} Z-{length / 2} R{radius1} ; Arc with R={radius1}")
|
| 24 |
+
gcode.append(f"G01 X{radius2} Z-{(3 * length) / 4} ; Linear move to smaller radius")
|
| 25 |
+
gcode.append(f"G03 X{radius2 + 0.5} Z-{length} R{radius2} ; Arc with R={radius2}")
|
| 26 |
+
gcode.append("G00 Z5 ; Retract tool")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
gcode.append("M5 ; Stop spindle")
|
| 28 |
gcode.append("M30 ; End of program")
|
| 29 |
+
return "\n".join(gcode)
|
| 30 |
+
|
| 31 |
+
# Gradio Interface
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=generate_gcode,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Slider(2, 10, step=0.5, label="Length of Part (mm)", value=5),
|
| 36 |
+
gr.Slider(0.1, 1.0, step=0.05, label="First Arc Radius (mm)", value=0.25),
|
| 37 |
+
gr.Slider(0.1, 1.0, step=0.05, label="Second Arc Radius (mm)", value=0.125),
|
| 38 |
+
gr.Slider(50, 500, step=50, label="Feed Rate (mm/min)", value=100),
|
| 39 |
+
gr.Slider(0.5, 5, step=0.5, label="Cutting Depth (mm)", value=3.125),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
],
|
| 41 |
+
outputs=gr.Textbox(label="Generated G-Code"),
|
| 42 |
+
title="CNC G-code Generator",
|
| 43 |
+
description="Generate G-code for CNC turning operations. Customize inputs for toolpath and cutting parameters."
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# Launch the interface
|
| 47 |
+
iface.launch(share=True)
|
|
|