karthikmn commited on
Commit
f1b3def
·
verified ·
1 Parent(s): a26f366

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}
23
+ /mesh
24
+ esize, 10
25
+ vmesh, all
26
+ allsel, all
27
+ /solu
28
+ solve
29
+ /exit
30
+ """
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
43
+ ax.plot(
44
+ [vertices[start][0], vertices[end][0]],
45
+ [vertices[start][1], vertices[end][1]],
46
+ color="black",
47
+ linewidth=2
48
+ )
49
+ ax.set_title("2D Projection (Top View)")
50
+ ax.set_xlabel("X-axis")
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],
96
+ outputs=[cad_file, apdl_file, cad_2d_image, cad_3d_image]
97
+ )
98
+
99
+ if __name__ == "__main__":
100
+ app.launch()