jithenderchoudary commited on
Commit
7def2b3
·
verified ·
1 Parent(s): a77fc10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -54
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 OCC.Extend.DataExchange import read_step_file
5
- from OCC.Extend.ShapeFactory import make_box
6
-
7
- # Function to process the STEP file
8
- def process_step_file(step_file):
9
- try:
10
- # Load STEP file using pythonOCC
11
- step_path = step_file.name
12
- shape = read_step_file(step_path)
13
-
14
- # Generate G-code (simple mock-up)
15
- gcode = "G21 ; Set units to millimeters\n"
16
- gcode += "G17 ; Select XY plane\n"
17
- gcode += "G90 ; Absolute positioning\n"
18
- gcode += "G0 Z5 ; Move Z axis up\n"
19
- gcode += "G0 X0 Y0 ; Move to origin\n"
20
- gcode += "G1 Z-1 F100 ; Start cutting\n"
21
- gcode += "G1 X10 Y0 F200 ; Linear move\n"
22
- gcode += "G1 X10 Y10 ; Linear move\n"
23
- gcode += "G1 X0 Y10 ; Linear move\n"
24
- gcode += "G1 X0 Y0 ; Return to origin\n"
25
- gcode += "G0 Z5 ; Move Z axis up\n"
26
- gcode += "M30 ; End of program"
27
-
28
- # 2D Visualization (simple projection using matplotlib)
29
- fig, ax = plt.subplots()
30
- ax.plot([0, 10, 10, 0, 0], [0, 0, 10, 10, 0], marker="o") # Basic square for placeholder
31
- ax.set_title("2D Visualization (Placeholder)")
32
- ax.set_xlabel("X Axis")
33
- ax.set_ylabel("Y Axis")
34
- plt_path = "2d_visualization.png"
35
- plt.savefig(plt_path)
36
-
37
- # Return 2D Visualization image, G-code, and a simple 3D SVG (for demonstration)
38
- return plt_path, "Projection.svg", gcode
39
- except Exception as e:
40
- return f"Error: {str(e)}", None, None
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Gradio Interface
43
- with gr.Blocks() as app:
44
- gr.Markdown("# STEP File Processor: G-code Generator & Visualizer")
45
-
46
- # Upload Section
47
- step_input = gr.File(label="Upload STEP File (.step)")
48
- submit_btn = gr.Button("Submit")
49
-
50
- # Outputs
51
- gcode_output = gr.Textbox(label="Generated G-code")
52
- visualization_2d = gr.Image(label="2D Visualization")
53
- visualization_3d = gr.HTML(label="3D Projection (SVG)")
54
-
55
- # Events
56
- submit_btn.click(process_step_file, inputs=step_input, outputs=[visualization_2d, visualization_3d, gcode_output])
57
-
58
- # Launch the Gradio app
59
- app.launch(share=True)
 
 
 
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()