File size: 4,186 Bytes
146c59e 37fcde6 db1ada9 37fcde6 db1ada9 37fcde6 db1ada9 37fcde6 db1ada9 37fcde6 f81a344 |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import gradio as gr
import matplotlib.pyplot as plt
import plotly.graph_objects as go
import tempfile
import numpy as np
import os
# Function to simulate G-code generation
def generate_gcode(length, diameter, step_file):
try:
# Mock G-code generation as a string
gcode = f"(Generated G-code for Toolpath)\n"
gcode += f"G21 ; Set units to mm\n"
gcode += f"G00 X0 Y0 Z0 ; Move to start\n"
gcode += f"G01 X{length} Y0 Z0 F1000 ; Move along X-axis\n"
gcode += f"G01 X{length} Y{diameter} Z0 ; Move to end point\n"
gcode += f"G00 X0 Y0 Z0 ; Return to origin\n"
gcode += f"M30 ; End of program"
# Save the G-code to a file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".nc")
with open(temp_file.name, "w") as f:
f.write(gcode)
print("G-code file generated successfully")
return temp_file.name # Return file path
except Exception as e:
print(f"Error in G-code Generation: {str(e)}")
return None
# Function to generate 2D Visualization with Graphs
def generate_2d_plot(length, diameter):
try:
# Create 2D Rectangle and Overlay a Graph
fig, ax = plt.subplots()
ax.plot([0, length, length, 0, 0], [0, 0, diameter, diameter, 0], color='blue', linewidth=2, label="Toolpath")
# Example graph (sinusoidal curve)
x = np.linspace(0, length, 100)
y = diameter / 2 * np.sin(2 * np.pi * x / length)
ax.plot(x, y, color='red', linestyle='--', label="Graph Overlay")
ax.set_title("2D Visualization with Graphs")
ax.set_xlabel("Length (mm)")
ax.set_ylabel("Diameter (mm)")
ax.legend()
# Save the figure to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
plt.savefig(temp_file.name, format='png')
plt.close(fig)
return temp_file.name # Return the file path
except Exception as e:
print(f"Error in 2D Plot: {str(e)}")
return None
# Function to generate 3D Visualization
def generate_3d_plot(length, diameter):
try:
t = np.linspace(0, 2 * np.pi, 50)
z = np.linspace(0, length, 20)
T, Z = np.meshgrid(t, z)
X = diameter * np.cos(T)
Y = diameter * np.sin(T)
fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z, colorscale='Viridis')])
fig.update_layout(
title="3D Visualization of Toolpath",
scene=dict(
xaxis_title="X (mm)",
yaxis_title="Y (mm)",
zaxis_title="Z (mm)"
)
)
return fig
except Exception as e:
print(f"Error in 3D Plot: {str(e)}")
return None
# Main function to handle all actions
def app_interface(length, diameter, step_file):
# Process STEP file (Placeholder logic)
step_file_path = None
if step_file is not None:
step_file_path = step_file.name
print(f"Uploaded STEP file: {step_file_path}")
# Generate G-code
gcode_file = generate_gcode(length, diameter, step_file_path)
# Generate Visualizations
file_path_2d = generate_2d_plot(length, diameter)
fig_3d = generate_3d_plot(length, diameter)
return file_path_2d, fig_3d, gcode_file
# Define Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## CNC Toolpath Generator with 2D/3D Visualization and G-code Generation")
with gr.Row():
length_input = gr.Number(label="Length (mm)", value=100)
diameter_input = gr.Number(label="Diameter (mm)", value=50)
step_file_input = gr.File(label="Upload STEP File")
submit_button = gr.Button("Submit")
with gr.Row():
output_2d = gr.Image(label="2D Visualization with Graphs")
output_3d = gr.Plot(label="3D Visualization")
gcode_output = gr.File(label="G-code File")
submit_button.click(
app_interface,
inputs=[length_input, diameter_input, step_file_input],
outputs=[output_2d, output_3d, gcode_output]
)
# Launch the app
if __name__ == "__main__":
demo.launch()
|