Spaces:
Running
Running
File size: 3,044 Bytes
a5278c7 ade98ea d5a5f32 3c0c1d7 d5a5f32 a5278c7 246540a 89f400b ade98ea 89f400b ade98ea 89f400b ade98ea a5278c7 d5a5f32 89f400b d5a5f32 3c0c1d7 a5278c7 3c0c1d7 583de60 3c0c1d7 583de60 3c0c1d7 583de60 3c0c1d7 d5a5f32 8cc4369 d5a5f32 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import os
import trimesh
import gradio as gr
import matplotlib.pyplot as plt
import pyvista as pv
from pyvista import Plotter
# Install and start Xvfb
os.system("apt-get update && apt-get install -y xvfb libgl1-mesa-glx")
pv.start_xvfb()
def generate_cad_and_apdl(length, width, height):
"""
Generate a 3D CAD model (box only) using Trimesh and output APDL script.
"""
cad_model = trimesh.creation.box(extents=(length, width, height))
cad_path = "/tmp/demo_model.stl"
cad_model.export(cad_path)
apdl_script = f"""
/prep7
block, 0, {length}, 0, {width}, 0, {height}
/mesh
esize, 10
vmesh, all
allsel, all
/solu
solve
/exit
"""
apdl_path = "/tmp/demo_model_apdl.txt"
with open(apdl_path, "w") as f:
f.write(apdl_script)
return cad_model, cad_path, apdl_path
def visualize_2d(cad_model):
vertices = cad_model.vertices
edges = cad_model.edges
fig, ax = plt.subplots(figsize=(6, 6))
for edge in edges:
start, end = edge
ax.plot(
[vertices[start][0], vertices[end][0]],
[vertices[start][1], vertices[end][1]],
color="black",
linewidth=2
)
ax.set_title("2D Projection (Top View)")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.grid(True)
ax.set_aspect('equal')
image_path = "/tmp/cad_2d_visualization.png"
plt.savefig(image_path)
plt.close(fig)
return image_path
def visualize_3d(cad_model):
vertices = cad_model.vertices
faces = cad_model.faces
faces_pv = []
for face in faces:
faces_pv.append(len(face))
faces_pv.extend(face)
mesh = pv.PolyData(vertices, faces_pv)
plotter = Plotter(off_screen=True)
plotter.add_mesh(mesh, color="lightblue", show_edges=True)
plotter.set_background("white")
plotter.view_isometric()
image_path = "/tmp/cad_3d_visualization.png"
plotter.screenshot(image_path)
plotter.close()
return image_path
def create_files_and_visualizations(length, width, height):
cad_model, cad_file, apdl_file = generate_cad_and_apdl(length, width, height)
cad_2d_path = visualize_2d(cad_model)
cad_3d_path = visualize_3d(cad_model)
return cad_file, apdl_file, cad_2d_path, cad_3d_path
with gr.Blocks() as app:
with gr.Row():
length = gr.Number(label="Length (mm)", value=100, precision=2)
width = gr.Number(label="Width (mm)", value=50, precision=2)
height = gr.Number(label="Height (mm)", value=20, precision=2)
submit_button = gr.Button("Submit")
with gr.Row():
cad_file = gr.File(label="Download CAD Model (STL)")
apdl_file = gr.File(label="Download APDL Script")
cad_2d_image = gr.Image(label="2D Visualization (Top View)")
cad_3d_image = gr.Image(label="3D Visualization")
submit_button.click(
fn=create_files_and_visualizations,
inputs=[length, width, height],
outputs=[cad_file, apdl_file, cad_2d_image, cad_3d_image]
)
if __name__ == "__main__":
app.launch()
|