jithenderchoudary commited on
Commit
057eff8
·
verified ·
1 Parent(s): cb6ab23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -34
app.py CHANGED
@@ -3,41 +3,71 @@ import matplotlib.pyplot as plt
3
  import plotly.graph_objects as go
4
  import tempfile
5
  import numpy as np
 
6
 
7
 
8
- # Function to generate 2D Visualization using Matplotlib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def generate_2d_plot(length, diameter):
10
  try:
11
- # Create a simple 2D rectangular plot
12
  fig, ax = plt.subplots()
13
- ax.plot([0, length, length, 0, 0], [0, 0, diameter, diameter, 0], color='blue', linewidth=2)
14
- ax.set_title("2D Visualization of Toolpath")
 
 
 
 
 
 
15
  ax.set_xlabel("Length (mm)")
16
  ax.set_ylabel("Diameter (mm)")
17
- ax.set_aspect('equal')
18
 
19
  # Save the figure to a temporary file
20
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
21
  plt.savefig(temp_file.name, format='png')
22
- plt.close(fig) # Close the figure to free memory
23
  return temp_file.name # Return the file path
24
-
25
  except Exception as e:
26
  print(f"Error in 2D Plot: {str(e)}")
27
  return None
28
 
29
 
30
- # Function to generate 3D Visualization using Plotly
31
  def generate_3d_plot(length, diameter):
32
  try:
33
- # Create a simple 3D visualization (e.g., a cylinder representing the toolpath)
34
  t = np.linspace(0, 2 * np.pi, 50)
35
  z = np.linspace(0, length, 20)
36
  T, Z = np.meshgrid(t, z)
37
  X = diameter * np.cos(T)
38
  Y = diameter * np.sin(T)
39
 
40
- # Create the 3D figure
41
  fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z, colorscale='Viridis')])
42
  fig.update_layout(
43
  title="3D Visualization of Toolpath",
@@ -47,35 +77,53 @@ def generate_3d_plot(length, diameter):
47
  zaxis_title="Z (mm)"
48
  )
49
  )
50
- return fig # Return the Plotly figure
51
-
52
  except Exception as e:
53
  print(f"Error in 3D Plot: {str(e)}")
54
  return None
55
 
56
 
57
- # Main application function
58
- def app_interface(length, diameter):
59
- # Generate 2D and 3D visualizations
60
- file_path = generate_2d_plot(length, diameter)
61
- fig = generate_3d_plot(length, diameter)
62
- return file_path, fig
63
-
64
-
65
- # Define the Gradio interface
66
- demo = gr.Interface(
67
- fn=app_interface,
68
- inputs=[
69
- gr.Number(label="Length (mm)", value=100), # Default length
70
- gr.Number(label="Diameter (mm)", value=50) # Default diameter
71
- ],
72
- outputs=[
73
- gr.Image(label="2D Visualization"), # 2D Image Output
74
- gr.Plot(label="3D Visualization") # 3D Plot Output
75
- ],
76
- title="CNC Toolpath Visualizer",
77
- description="Enter the length and diameter to generate 2D and 3D toolpath visualizations."
78
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Launch the app
81
  if __name__ == "__main__":
 
3
  import plotly.graph_objects as go
4
  import tempfile
5
  import numpy as np
6
+ import os
7
 
8
 
9
+ # Function to simulate G-code generation
10
+ def generate_gcode(length, diameter, step_file):
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
+ except Exception as e:
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
+ # Create 2D Rectangle and Overlay a Graph
38
  fig, ax = plt.subplots()
39
+ ax.plot([0, length, length, 0, 0], [0, 0, diameter, diameter, 0], color='blue', linewidth=2, label="Toolpath")
40
+
41
+ # Example graph (sinusoidal curve)
42
+ x = np.linspace(0, length, 100)
43
+ y = diameter / 2 * np.sin(2 * np.pi * x / length)
44
+ ax.plot(x, y, color='red', linestyle='--', label="Graph Overlay")
45
+
46
+ ax.set_title("2D Visualization with Graphs")
47
  ax.set_xlabel("Length (mm)")
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
  print(f"Error in 2D Plot: {str(e)}")
59
  return None
60
 
61
 
62
+ # Function to generate 3D Visualization
63
  def generate_3d_plot(length, diameter):
64
  try:
 
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",
 
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
+ # Generate Visualizations
99
+ file_path_2d = generate_2d_plot(length, diameter)
100
+ fig_3d = generate_3d_plot(length, diameter)
101
+
102
+ return file_path_2d, fig_3d, gcode_file
103
+
104
+
105
+ # Define Gradio interface
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
+ submit_button.click(
123
+ app_interface,
124
+ inputs=[length_input, diameter_input, step_file_input],
125
+ outputs=[output_2d, output_3d, gcode_output]
126
+ )
127
 
128
  # Launch the app
129
  if __name__ == "__main__":