Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cadquery as cq
|
| 3 |
from cadquery import exporters
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def generate_apdl_script(file, press_force, material_json):
|
| 9 |
try:
|
| 10 |
-
# Parse material properties from JSON
|
| 11 |
material = json.loads(material_json)
|
| 12 |
elastic_modulus = material.get("elastic_modulus", 2e11)
|
| 13 |
poisson = material.get("poisson", 0.3)
|
| 14 |
|
| 15 |
-
# Get the uploaded file path
|
| 16 |
step_file_path = file.name
|
| 17 |
-
|
| 18 |
-
# Load STEP file with CadQuery
|
| 19 |
model = cq.importers.importStep(step_file_path)
|
| 20 |
-
|
| 21 |
-
# Export as STL for meshing
|
| 22 |
stl_file = step_file_path.replace(".stp", ".stl")
|
| 23 |
exporters.export(model, stl_file)
|
| 24 |
|
| 25 |
-
# Generate APDL script
|
| 26 |
apdl_script = f"""\
|
| 27 |
/PREP7
|
| 28 |
! Importing Geometry
|
|
@@ -39,12 +75,10 @@ SOLVE
|
|
| 39 |
*GET, stress, NODE, 0, S, MAX
|
| 40 |
/EXIT, SAVE
|
| 41 |
"""
|
| 42 |
-
# Save APDL script to file
|
| 43 |
apdl_file_path = step_file_path.replace(".stp", ".inp")
|
| 44 |
with open(apdl_file_path, "w") as apdl_file:
|
| 45 |
apdl_file.write(apdl_script)
|
| 46 |
|
| 47 |
-
# Return script content and file path for download
|
| 48 |
return apdl_script, apdl_file_path
|
| 49 |
except Exception as e:
|
| 50 |
return f"Error: {str(e)}", None
|
|
@@ -52,43 +86,45 @@ SOLVE
|
|
| 52 |
# Gradio App
|
| 53 |
def main():
|
| 54 |
with gr.Blocks() as app:
|
| 55 |
-
gr.Markdown("#
|
| 56 |
|
| 57 |
-
# Input Row
|
| 58 |
with gr.Row():
|
| 59 |
step_input = gr.File(label="Upload STEP File")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
force_input = gr.Number(label="Press Force (N)", value=1000)
|
| 61 |
material_input = gr.Textbox(
|
| 62 |
label="Material Properties (JSON)",
|
| 63 |
value='{"elastic_modulus": 2e11, "poisson": 0.3}'
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# Output Row
|
| 67 |
with gr.Row():
|
| 68 |
script_output = gr.Textbox(label="Generated APDL Script", interactive=False)
|
| 69 |
download_button = gr.File(label="Download Script")
|
|
|
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
# Events
|
| 77 |
def handle_download(apdl_script, apdl_file_path):
|
| 78 |
if apdl_file_path and os.path.exists(apdl_file_path):
|
| 79 |
return apdl_file_path
|
| 80 |
return None
|
| 81 |
|
| 82 |
-
|
| 83 |
fn=generate_apdl_script,
|
| 84 |
inputs=[step_input, force_input, material_input],
|
| 85 |
-
outputs=[script_output, download_button]
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
submit_button.click(
|
| 89 |
-
fn=lambda: "Submission successful!",
|
| 90 |
-
inputs=None,
|
| 91 |
-
outputs=gr.Textbox(label="Submission Status")
|
| 92 |
)
|
| 93 |
|
| 94 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cadquery as cq
|
| 3 |
from cadquery import exporters
|
| 4 |
+
import trimesh
|
| 5 |
+
import pyvista as pv
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
# 2D View Function
|
| 12 |
+
def generate_2d_view(step_file):
|
| 13 |
+
try:
|
| 14 |
+
mesh = trimesh.load(step_file.name)
|
| 15 |
+
projection = mesh.section(plane_origin=[0, 0, 0], plane_normal=[0, 0, 1])
|
| 16 |
+
projection_img = projection.to_image() if projection else None
|
| 17 |
+
|
| 18 |
+
fig, ax = plt.subplots()
|
| 19 |
+
ax.imshow(projection_img, cmap="gray")
|
| 20 |
+
ax.axis("off")
|
| 21 |
+
|
| 22 |
+
buffer = BytesIO()
|
| 23 |
+
plt.savefig(buffer, format="png")
|
| 24 |
+
buffer.seek(0)
|
| 25 |
+
return buffer
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error in generating 2D view: {e}"
|
| 28 |
+
|
| 29 |
+
# 3D View Function
|
| 30 |
+
def generate_3d_view(step_file):
|
| 31 |
+
try:
|
| 32 |
+
model = cq.importers.importStep(step_file.name)
|
| 33 |
+
stl_file = "temp_model.stl"
|
| 34 |
+
exporters.export(model, stl_file)
|
| 35 |
+
|
| 36 |
+
mesh = pv.read(stl_file)
|
| 37 |
+
plotter = pv.Plotter(off_screen=True)
|
| 38 |
+
plotter.add_mesh(mesh, color="lightblue")
|
| 39 |
+
screenshot = plotter.screenshot(asarray=True)
|
| 40 |
+
|
| 41 |
+
buffer = BytesIO()
|
| 42 |
+
plt.imshow(screenshot)
|
| 43 |
+
plt.axis("off")
|
| 44 |
+
plt.savefig(buffer, format="png")
|
| 45 |
+
buffer.seek(0)
|
| 46 |
+
return buffer
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error in generating 3D view: {e}"
|
| 49 |
+
|
| 50 |
+
# APDL Script Generation
|
| 51 |
def generate_apdl_script(file, press_force, material_json):
|
| 52 |
try:
|
|
|
|
| 53 |
material = json.loads(material_json)
|
| 54 |
elastic_modulus = material.get("elastic_modulus", 2e11)
|
| 55 |
poisson = material.get("poisson", 0.3)
|
| 56 |
|
|
|
|
| 57 |
step_file_path = file.name
|
|
|
|
|
|
|
| 58 |
model = cq.importers.importStep(step_file_path)
|
|
|
|
|
|
|
| 59 |
stl_file = step_file_path.replace(".stp", ".stl")
|
| 60 |
exporters.export(model, stl_file)
|
| 61 |
|
|
|
|
| 62 |
apdl_script = f"""\
|
| 63 |
/PREP7
|
| 64 |
! Importing Geometry
|
|
|
|
| 75 |
*GET, stress, NODE, 0, S, MAX
|
| 76 |
/EXIT, SAVE
|
| 77 |
"""
|
|
|
|
| 78 |
apdl_file_path = step_file_path.replace(".stp", ".inp")
|
| 79 |
with open(apdl_file_path, "w") as apdl_file:
|
| 80 |
apdl_file.write(apdl_script)
|
| 81 |
|
|
|
|
| 82 |
return apdl_script, apdl_file_path
|
| 83 |
except Exception as e:
|
| 84 |
return f"Error: {str(e)}", None
|
|
|
|
| 86 |
# Gradio App
|
| 87 |
def main():
|
| 88 |
with gr.Blocks() as app:
|
| 89 |
+
gr.Markdown("# Press Tool Design and APDL Script Generator")
|
| 90 |
|
|
|
|
| 91 |
with gr.Row():
|
| 92 |
step_input = gr.File(label="Upload STEP File")
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
two_d_output = gr.Image(label="2D View")
|
| 96 |
+
three_d_output = gr.Image(label="3D View")
|
| 97 |
+
generate_views_btn = gr.Button("Generate 2D & 3D Views")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
force_input = gr.Number(label="Press Force (N)", value=1000)
|
| 101 |
material_input = gr.Textbox(
|
| 102 |
label="Material Properties (JSON)",
|
| 103 |
value='{"elastic_modulus": 2e11, "poisson": 0.3}'
|
| 104 |
)
|
| 105 |
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
script_output = gr.Textbox(label="Generated APDL Script", interactive=False)
|
| 108 |
download_button = gr.File(label="Download Script")
|
| 109 |
+
generate_apdl_btn = gr.Button("Generate APDL Script")
|
| 110 |
|
| 111 |
+
# Events for Visualization
|
| 112 |
+
generate_views_btn.click(
|
| 113 |
+
fn=[generate_2d_view, generate_3d_view],
|
| 114 |
+
inputs=step_input,
|
| 115 |
+
outputs=[two_d_output, three_d_output]
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
# Events for APDL Script
|
| 119 |
def handle_download(apdl_script, apdl_file_path):
|
| 120 |
if apdl_file_path and os.path.exists(apdl_file_path):
|
| 121 |
return apdl_file_path
|
| 122 |
return None
|
| 123 |
|
| 124 |
+
generate_apdl_btn.click(
|
| 125 |
fn=generate_apdl_script,
|
| 126 |
inputs=[step_input, force_input, material_input],
|
| 127 |
+
outputs=[script_output, download_button]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
app.launch()
|