Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
gcode
|
| 16 |
-
gcode
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
gcode
|
| 22 |
-
gcode
|
| 23 |
-
gcode
|
| 24 |
-
gcode
|
| 25 |
-
gcode
|
| 26 |
-
gcode
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Gradio Interface
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from matplotlib.patches import Rectangle, Ellipse
|
| 5 |
+
|
| 6 |
+
# Function for basic G-code generation (simple circles and rectangles)
|
| 7 |
+
def generate_gcode(shape_type, size):
|
| 8 |
+
gcode = []
|
| 9 |
+
|
| 10 |
+
if shape_type == "Circle":
|
| 11 |
+
# Generate a simple circle G-code (assuming radius as size)
|
| 12 |
+
gcode.append(f"G17 G21 G90") # Select XY plane, units in mm, absolute positioning
|
| 13 |
+
gcode.append(f"G0 Z5.0") # Move tool to safe height
|
| 14 |
+
gcode.append(f"G0 X0 Y0") # Move to start position
|
| 15 |
+
gcode.append(f"G2 X0 Y0 I{size} J0") # Circle in clockwise direction
|
| 16 |
+
gcode.append(f"G0 Z5.0") # Return to safe height
|
| 17 |
+
|
| 18 |
+
elif shape_type == "Rectangle":
|
| 19 |
+
# Generate a simple rectangle G-code (using size for length and width)
|
| 20 |
+
width, height = size, size / 2
|
| 21 |
+
gcode.append(f"G17 G21 G90") # Select XY plane, units in mm, absolute positioning
|
| 22 |
+
gcode.append(f"G0 Z5.0") # Move tool to safe height
|
| 23 |
+
gcode.append(f"G0 X0 Y0") # Move to start position
|
| 24 |
+
gcode.append(f"G1 X{width} Y0") # Move to the right side of the rectangle
|
| 25 |
+
gcode.append(f"G1 X{width} Y{height}") # Move upwards
|
| 26 |
+
gcode.append(f"G1 X0 Y{height}") # Move to the left side
|
| 27 |
+
gcode.append(f"G1 X0 Y0") # Close the rectangle
|
| 28 |
+
gcode.append(f"G0 Z5.0") # Return to safe height
|
| 29 |
+
|
| 30 |
+
return "\n".join(gcode)
|
| 31 |
+
|
| 32 |
+
# Function for 2D Visualization
|
| 33 |
+
def visualize_2d(shape_type, size):
|
| 34 |
+
fig, ax = plt.subplots()
|
| 35 |
+
|
| 36 |
+
if shape_type == "Circle":
|
| 37 |
+
# Visualize a circle
|
| 38 |
+
circle = Ellipse((0, 0), width=2*size, height=2*size, edgecolor='blue', facecolor='none')
|
| 39 |
+
ax.add_patch(circle)
|
| 40 |
+
|
| 41 |
+
elif shape_type == "Rectangle":
|
| 42 |
+
# Visualize a rectangle
|
| 43 |
+
width, height = size, size / 2
|
| 44 |
+
rect = Rectangle((-width/2, -height/2), width, height, linewidth=2, edgecolor='green', facecolor='none')
|
| 45 |
+
ax.add_patch(rect)
|
| 46 |
+
|
| 47 |
+
ax.set_xlim(-size*1.5, size*1.5)
|
| 48 |
+
ax.set_ylim(-size*1.5, size*1.5)
|
| 49 |
+
ax.set_aspect('equal', 'box')
|
| 50 |
+
plt.grid(True)
|
| 51 |
+
plt.title(f"2D Visualization: {shape_type}")
|
| 52 |
+
return fig
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
+
def interface(shape_type, size):
|
| 56 |
+
# Generate G-code and visualization
|
| 57 |
+
gcode = generate_gcode(shape_type, size)
|
| 58 |
+
fig = visualize_2d(shape_type, size)
|
| 59 |
+
return gcode, fig
|
| 60 |
+
|
| 61 |
+
# Define Gradio Interface
|
| 62 |
+
gr.Interface(
|
| 63 |
+
fn=interface,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Dropdown(choices=["Circle", "Rectangle"], label="Shape Type"),
|
| 66 |
+
gr.Slider(minimum=1, maximum=100, step=0.1, label="Size")
|
| 67 |
+
],
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.Textbox(label="G-code Output", lines=20),
|
| 70 |
+
gr.Plot(label="2D Visualization")
|
| 71 |
+
],
|
| 72 |
+
live=True
|
| 73 |
+
).launch()
|