karthikmn commited on
Commit
a5278c7
·
verified ·
1 Parent(s): 246540a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -48
app.py CHANGED
@@ -1,25 +1,22 @@
 
1
  import trimesh
2
  import gradio as gr
3
  import matplotlib.pyplot as plt
4
  import pyvista as pv
5
  from pyvista import Plotter
6
 
7
- # Enable headless rendering
 
8
  pv.start_xvfb()
9
 
10
-
11
  def generate_cad_and_apdl(length, width, height):
12
  """
13
  Generate a 3D CAD model (box only) using Trimesh and output APDL script.
14
  """
15
- # 1. Create a CAD model as a box
16
  cad_model = trimesh.creation.box(extents=(length, width, height))
17
-
18
- # Save the CAD model as an STL file
19
  cad_path = "/tmp/demo_model.stl"
20
  cad_model.export(cad_path)
21
-
22
- # 2. Generate APDL Script (basic block)
23
  apdl_script = f"""
24
  /prep7
25
  block, 0, {length}, 0, {width}, 0, {height}
@@ -34,19 +31,12 @@ solve
34
  apdl_path = "/tmp/demo_model_apdl.txt"
35
  with open(apdl_path, "w") as f:
36
  f.write(apdl_script)
37
-
38
- return cad_model, cad_path, apdl_path
39
 
 
40
 
41
  def visualize_2d(cad_model):
42
- """
43
- Generate a 2D visualization of the CAD model using Matplotlib.
44
- """
45
- # Extract vertices and edges
46
  vertices = cad_model.vertices
47
  edges = cad_model.edges
48
-
49
- # Plot the edges in 2D
50
  fig, ax = plt.subplots(figsize=(6, 6))
51
  for edge in edges:
52
  start, end = edge
@@ -61,78 +51,45 @@ def visualize_2d(cad_model):
61
  ax.set_ylabel("Y-axis")
62
  ax.grid(True)
63
  ax.set_aspect('equal')
64
-
65
- # Save the figure to a file
66
  image_path = "/tmp/cad_2d_visualization.png"
67
  plt.savefig(image_path)
68
  plt.close(fig)
69
-
70
  return image_path
71
 
72
-
73
  def visualize_3d(cad_model):
74
- """
75
- Generate a 3D visualization of the CAD model using PyVista.
76
- """
77
- # Convert Trimesh model to PyVista
78
  vertices = cad_model.vertices
79
  faces = cad_model.faces
80
  faces_pv = []
81
  for face in faces:
82
  faces_pv.append(len(face))
83
  faces_pv.extend(face)
84
-
85
  mesh = pv.PolyData(vertices, faces_pv)
86
-
87
- # Create a PyVista plotter
88
  plotter = Plotter(off_screen=True)
89
  plotter.add_mesh(mesh, color="lightblue", show_edges=True)
90
  plotter.set_background("white")
91
  plotter.view_isometric()
92
-
93
- # Save the visualization as an image
94
  image_path = "/tmp/cad_3d_visualization.png"
95
  plotter.screenshot(image_path)
96
  plotter.close()
97
-
98
  return image_path
99
 
100
-
101
  def create_files_and_visualizations(length, width, height):
102
- """
103
- Generate CAD and APDL files, and visualize the model in 2D and 3D.
104
- """
105
- # Generate CAD and APDL files
106
  cad_model, cad_file, apdl_file = generate_cad_and_apdl(length, width, height)
107
-
108
- # Generate 2D visualization
109
  cad_2d_path = visualize_2d(cad_model)
110
-
111
- # Generate 3D visualization
112
  cad_3d_path = visualize_3d(cad_model)
113
-
114
  return cad_file, apdl_file, cad_2d_path, cad_3d_path
115
 
116
-
117
- # Gradio interface
118
  with gr.Blocks() as app:
119
- # Input fields
120
  with gr.Row():
121
  length = gr.Number(label="Length (mm)", value=100, precision=2)
122
  width = gr.Number(label="Width (mm)", value=50, precision=2)
123
  height = gr.Number(label="Height (mm)", value=20, precision=2)
124
-
125
- # Submit button
126
  submit_button = gr.Button("Submit")
127
-
128
- # Output fields
129
  with gr.Row():
130
  cad_file = gr.File(label="Download CAD Model (STL)")
131
  apdl_file = gr.File(label="Download APDL Script")
132
  cad_2d_image = gr.Image(label="2D Visualization (Top View)")
133
  cad_3d_image = gr.Image(label="3D Visualization")
134
-
135
- # Button click event
136
  submit_button.click(
137
  fn=create_files_and_visualizations,
138
  inputs=[length, width, height],
 
1
+ import os
2
  import trimesh
3
  import gradio as gr
4
  import matplotlib.pyplot as plt
5
  import pyvista as pv
6
  from pyvista import Plotter
7
 
8
+ # Install and start Xvfb
9
+ os.system("apt-get update && apt-get install -y xvfb libgl1-mesa-glx")
10
  pv.start_xvfb()
11
 
 
12
  def generate_cad_and_apdl(length, width, height):
13
  """
14
  Generate a 3D CAD model (box only) using Trimesh and output APDL script.
15
  """
 
16
  cad_model = trimesh.creation.box(extents=(length, width, height))
 
 
17
  cad_path = "/tmp/demo_model.stl"
18
  cad_model.export(cad_path)
19
+
 
20
  apdl_script = f"""
21
  /prep7
22
  block, 0, {length}, 0, {width}, 0, {height}
 
31
  apdl_path = "/tmp/demo_model_apdl.txt"
32
  with open(apdl_path, "w") as f:
33
  f.write(apdl_script)
 
 
34
 
35
+ return cad_model, cad_path, apdl_path
36
 
37
  def visualize_2d(cad_model):
 
 
 
 
38
  vertices = cad_model.vertices
39
  edges = cad_model.edges
 
 
40
  fig, ax = plt.subplots(figsize=(6, 6))
41
  for edge in edges:
42
  start, end = edge
 
51
  ax.set_ylabel("Y-axis")
52
  ax.grid(True)
53
  ax.set_aspect('equal')
 
 
54
  image_path = "/tmp/cad_2d_visualization.png"
55
  plt.savefig(image_path)
56
  plt.close(fig)
 
57
  return image_path
58
 
 
59
  def visualize_3d(cad_model):
 
 
 
 
60
  vertices = cad_model.vertices
61
  faces = cad_model.faces
62
  faces_pv = []
63
  for face in faces:
64
  faces_pv.append(len(face))
65
  faces_pv.extend(face)
 
66
  mesh = pv.PolyData(vertices, faces_pv)
 
 
67
  plotter = Plotter(off_screen=True)
68
  plotter.add_mesh(mesh, color="lightblue", show_edges=True)
69
  plotter.set_background("white")
70
  plotter.view_isometric()
 
 
71
  image_path = "/tmp/cad_3d_visualization.png"
72
  plotter.screenshot(image_path)
73
  plotter.close()
 
74
  return image_path
75
 
 
76
  def create_files_and_visualizations(length, width, height):
 
 
 
 
77
  cad_model, cad_file, apdl_file = generate_cad_and_apdl(length, width, height)
 
 
78
  cad_2d_path = visualize_2d(cad_model)
 
 
79
  cad_3d_path = visualize_3d(cad_model)
 
80
  return cad_file, apdl_file, cad_2d_path, cad_3d_path
81
 
 
 
82
  with gr.Blocks() as app:
 
83
  with gr.Row():
84
  length = gr.Number(label="Length (mm)", value=100, precision=2)
85
  width = gr.Number(label="Width (mm)", value=50, precision=2)
86
  height = gr.Number(label="Height (mm)", value=20, precision=2)
 
 
87
  submit_button = gr.Button("Submit")
 
 
88
  with gr.Row():
89
  cad_file = gr.File(label="Download CAD Model (STL)")
90
  apdl_file = gr.File(label="Download APDL Script")
91
  cad_2d_image = gr.Image(label="2D Visualization (Top View)")
92
  cad_3d_image = gr.Image(label="3D Visualization")
 
 
93
  submit_button.click(
94
  fn=create_files_and_visualizations,
95
  inputs=[length, width, height],