Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,64 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
-
import plotly.graph_objects as go
|
| 4 |
-
import tempfile
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
try:
|
| 12 |
-
# Mock G-code generation as a string
|
| 13 |
-
gcode = f"(Generated G-code for Toolpath)\n"
|
| 14 |
-
gcode += f"G21 ; Set units to mm\n"
|
| 15 |
-
gcode += f"G00 X0 Y0 Z0 ; Move to start\n"
|
| 16 |
-
gcode += f"G01 X{length} Y0 Z0 F1000 ; Move along X-axis\n"
|
| 17 |
-
gcode += f"G01 X{length} Y{diameter} Z0 ; Move to end point\n"
|
| 18 |
-
gcode += f"G00 X0 Y0 Z0 ; Return to origin\n"
|
| 19 |
-
gcode += f"M30 ; End of program"
|
| 20 |
-
|
| 21 |
-
# Save the G-code to a file
|
| 22 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".nc")
|
| 23 |
-
with open(temp_file.name, "w") as f:
|
| 24 |
-
f.write(gcode)
|
| 25 |
-
|
| 26 |
-
print("G-code file generated successfully")
|
| 27 |
-
return temp_file.name # Return file path
|
| 28 |
|
| 29 |
-
|
| 30 |
-
print(f"Error in G-code Generation: {str(e)}")
|
| 31 |
-
return None
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Function to generate 2D Visualization with Graphs
|
| 35 |
-
def generate_2d_plot(length, diameter):
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
ax.set_ylabel("Diameter (mm)")
|
| 49 |
-
ax.legend()
|
| 50 |
-
|
| 51 |
-
# Save the figure to a temporary file
|
| 52 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 53 |
-
plt.savefig(temp_file.name, format='png')
|
| 54 |
-
plt.close(fig)
|
| 55 |
-
return temp_file.name # Return the file path
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 59 |
-
return None
|
| 60 |
-
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
t = np.linspace(0, 2 * np.pi, 50)
|
| 66 |
-
z = np.linspace(0, length, 20)
|
| 67 |
-
T, Z = np.meshgrid(t, z)
|
| 68 |
-
X = diameter * np.cos(T)
|
| 69 |
-
Y = diameter * np.sin(T)
|
| 70 |
-
|
| 71 |
-
fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z, colorscale='Viridis')])
|
| 72 |
-
fig.update_layout(
|
| 73 |
-
title="3D Visualization of Toolpath",
|
| 74 |
-
scene=dict(
|
| 75 |
-
xaxis_title="X (mm)",
|
| 76 |
-
yaxis_title="Y (mm)",
|
| 77 |
-
zaxis_title="Z (mm)"
|
| 78 |
-
)
|
| 79 |
-
)
|
| 80 |
-
return fig
|
| 81 |
-
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print(f"Error in 3D Plot: {str(e)}")
|
| 84 |
-
return None
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# Main function to handle all actions
|
| 88 |
-
def app_interface(length, diameter, step_file):
|
| 89 |
-
# Process STEP file (Placeholder logic)
|
| 90 |
-
step_file_path = None
|
| 91 |
-
if step_file is not None:
|
| 92 |
-
step_file_path = step_file.name
|
| 93 |
-
print(f"Uploaded STEP file: {step_file_path}")
|
| 94 |
-
|
| 95 |
-
# Generate G-code
|
| 96 |
-
gcode_file = generate_gcode(length, diameter, step_file_path)
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
with gr.Blocks() as demo:
|
| 107 |
-
gr.Markdown("## CNC Toolpath Generator with 2D/3D Visualization and G-code Generation")
|
| 108 |
-
|
| 109 |
-
with gr.Row():
|
| 110 |
-
length_input = gr.Number(label="Length (mm)", value=100)
|
| 111 |
-
diameter_input = gr.Number(label="Diameter (mm)", value=50)
|
| 112 |
-
step_file_input = gr.File(label="Upload STEP File")
|
| 113 |
-
|
| 114 |
-
submit_button = gr.Button("Submit")
|
| 115 |
-
|
| 116 |
-
with gr.Row():
|
| 117 |
-
output_2d = gr.Image(label="2D Visualization with Graphs")
|
| 118 |
-
output_3d = gr.Plot(label="3D Visualization")
|
| 119 |
-
|
| 120 |
-
gcode_output = gr.File(label="G-code File")
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
inputs=[length_input, diameter_input, step_file_input],
|
| 125 |
-
outputs=[output_2d, output_3d, gcode_output]
|
| 126 |
-
)
|
| 127 |
|
| 128 |
-
# Launch the app
|
| 129 |
-
|
| 130 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cadquery as cq
|
| 3 |
+
from cadquery import exporters
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
def process_step_file(step_file):
|
| 9 |
+
# Save the uploaded file temporarily
|
| 10 |
+
step_path = step_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Load STEP file using CadQuery
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
+
model = cq.importers.importStep(step_path)
|
| 15 |
+
# Export 2D projection to SVG for visualization
|
| 16 |
+
svg_path = "projection.svg"
|
| 17 |
+
exporters.export(model, svg_path)
|
| 18 |
|
| 19 |
+
# Generate G-code (dummy example for simplicity)
|
| 20 |
+
gcode = "G21 ; Set units to millimeters\n"
|
| 21 |
+
gcode += "G17 ; Select XY plane\n"
|
| 22 |
+
gcode += "G90 ; Absolute positioning\n"
|
| 23 |
+
gcode += "G0 Z5 ; Move Z axis up\n"
|
| 24 |
+
gcode += "G0 X0 Y0 ; Move to origin\n"
|
| 25 |
+
gcode += "G1 Z-1 F100 ; Start cutting\n"
|
| 26 |
+
gcode += "G1 X10 Y0 F200 ; Linear move\n"
|
| 27 |
+
gcode += "G1 X10 Y10 ; Linear move\n"
|
| 28 |
+
gcode += "G1 X0 Y10 ; Linear move\n"
|
| 29 |
+
gcode += "G1 X0 Y0 ; Return to origin\n"
|
| 30 |
+
gcode += "G0 Z5 ; Move Z axis up\n"
|
| 31 |
+
gcode += "M30 ; End of program"
|
| 32 |
+
|
| 33 |
+
# 2D Visualization
|
| 34 |
+
fig, ax = plt.subplots()
|
| 35 |
+
ax.plot([0, 10, 10, 0, 0], [0, 0, 10, 10, 0], marker="o") # Simple 2D Graph example
|
| 36 |
+
ax.set_title("2D Visualization (Placeholder)")
|
| 37 |
+
ax.set_xlabel("X Axis")
|
| 38 |
+
ax.set_ylabel("Y Axis")
|
| 39 |
+
plt_path = "2d_visualization.png"
|
| 40 |
+
plt.savefig(plt_path)
|
| 41 |
|
| 42 |
+
# Return results
|
| 43 |
+
return plt_path, svg_path, gcode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
return f"Error: {str(e)}", None, None
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Gradio Interface
|
| 48 |
+
with gr.Blocks() as app:
|
| 49 |
+
gr.Markdown("# STEP File Processor: G-code Generator & Visualizer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Upload Section
|
| 52 |
+
step_input = gr.File(label="Upload STEP File (.step)")
|
| 53 |
+
submit_btn = gr.Button("Submit")
|
| 54 |
|
| 55 |
+
# Outputs
|
| 56 |
+
gcode_output = gr.Textbox(label="Generated G-code")
|
| 57 |
+
visualization_2d = gr.Image(label="2D Visualization")
|
| 58 |
+
visualization_3d = gr.HTML(label="3D Projection (SVG)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Events
|
| 61 |
+
submit_btn.click(process_step_file, inputs=step_input, outputs=[visualization_2d, visualization_3d, gcode_output])
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# Launch the Gradio app
|
| 64 |
+
app.launch(share=True)
|
|
|