Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,17 @@ import gradio as gr
|
|
| 2 |
import cadquery as cq
|
| 3 |
from cadquery import exporters
|
| 4 |
import tempfile
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
# Generate APDL Script
|
| 8 |
-
def generate_apdl_script(file, press_force,
|
| 9 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Save uploaded STEP file temporarily
|
| 11 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".step") as temp_file:
|
| 12 |
temp_file.write(file.read())
|
|
@@ -16,25 +22,17 @@ def generate_apdl_script(file, press_force, material):
|
|
| 16 |
model = cq.importers.importStep(step_file_path)
|
| 17 |
|
| 18 |
# Export as STL for meshing
|
| 19 |
-
stl_file = "
|
| 20 |
exporters.export(model, stl_file)
|
| 21 |
|
| 22 |
-
# Load STL with trimesh
|
| 23 |
-
mesh = trimesh.load(stl_file)
|
| 24 |
-
volume = mesh.volume
|
| 25 |
-
surface_area = mesh.area
|
| 26 |
-
|
| 27 |
-
# Placeholder stress calculation
|
| 28 |
-
stress = press_force / surface_area if surface_area > 0 else "Undefined (Area is zero)"
|
| 29 |
-
|
| 30 |
# Generate APDL script
|
| 31 |
apdl_script = f"""\
|
| 32 |
/PREP7
|
| 33 |
! Importing Geometry
|
| 34 |
/import, '{step_file_path}', geom, STEP
|
| 35 |
! Material Properties
|
| 36 |
-
MP, EX, 1, {
|
| 37 |
-
MP, PRXY, 1, {
|
| 38 |
! Load and Boundary Conditions
|
| 39 |
F, NODE, ALL, FX, {press_force}
|
| 40 |
! Solve
|
|
@@ -45,10 +43,11 @@ SOLVE
|
|
| 45 |
/EXIT, SAVE
|
| 46 |
"""
|
| 47 |
# Save APDL script to file
|
| 48 |
-
apdl_file_path = "
|
| 49 |
with open(apdl_file_path, "w") as apdl_file:
|
| 50 |
apdl_file.write(apdl_script)
|
| 51 |
|
|
|
|
| 52 |
return apdl_script, apdl_file_path
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error: {str(e)}", None
|
|
@@ -58,6 +57,7 @@ def main():
|
|
| 58 |
with gr.Blocks() as app:
|
| 59 |
gr.Markdown("# APDL Script Generator for Press Tool Design")
|
| 60 |
|
|
|
|
| 61 |
with gr.Row():
|
| 62 |
step_input = gr.File(label="Upload STEP File")
|
| 63 |
force_input = gr.Number(label="Press Force (N)", value=1000)
|
|
@@ -65,9 +65,16 @@ def main():
|
|
| 65 |
label="Material Properties (JSON)",
|
| 66 |
value='{"elastic_modulus": 2e11, "poisson": 0.3}'
|
| 67 |
)
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
download_button = gr.File(label="Download Script")
|
|
|
|
|
|
|
|
|
|
| 70 |
generate_button = gr.Button("Generate APDL Script")
|
|
|
|
| 71 |
|
| 72 |
# Events
|
| 73 |
generate_button.click(
|
|
@@ -75,6 +82,12 @@ def main():
|
|
| 75 |
inputs=[step_input, force_input, material_input],
|
| 76 |
outputs=[script_output, download_button],
|
| 77 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
app.launch()
|
| 80 |
|
|
|
|
| 2 |
import cadquery as cq
|
| 3 |
from cadquery import exporters
|
| 4 |
import tempfile
|
| 5 |
+
import json
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Generate APDL Script
|
| 9 |
+
def generate_apdl_script(file, press_force, material_json):
|
| 10 |
try:
|
| 11 |
+
# Parse material properties from JSON
|
| 12 |
+
material = json.loads(material_json)
|
| 13 |
+
elastic_modulus = material.get("elastic_modulus", 2e11)
|
| 14 |
+
poisson = material.get("poisson", 0.3)
|
| 15 |
+
|
| 16 |
# Save uploaded STEP file temporarily
|
| 17 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".step") as temp_file:
|
| 18 |
temp_file.write(file.read())
|
|
|
|
| 22 |
model = cq.importers.importStep(step_file_path)
|
| 23 |
|
| 24 |
# Export as STL for meshing
|
| 25 |
+
stl_file = step_file_path.replace(".step", ".stl")
|
| 26 |
exporters.export(model, stl_file)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Generate APDL script
|
| 29 |
apdl_script = f"""\
|
| 30 |
/PREP7
|
| 31 |
! Importing Geometry
|
| 32 |
/import, '{step_file_path}', geom, STEP
|
| 33 |
! Material Properties
|
| 34 |
+
MP, EX, 1, {elastic_modulus}
|
| 35 |
+
MP, PRXY, 1, {poisson}
|
| 36 |
! Load and Boundary Conditions
|
| 37 |
F, NODE, ALL, FX, {press_force}
|
| 38 |
! Solve
|
|
|
|
| 43 |
/EXIT, SAVE
|
| 44 |
"""
|
| 45 |
# Save APDL script to file
|
| 46 |
+
apdl_file_path = step_file_path.replace(".step", ".inp")
|
| 47 |
with open(apdl_file_path, "w") as apdl_file:
|
| 48 |
apdl_file.write(apdl_script)
|
| 49 |
|
| 50 |
+
# Return script content and file path for download
|
| 51 |
return apdl_script, apdl_file_path
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}", None
|
|
|
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
gr.Markdown("# APDL Script Generator for Press Tool Design")
|
| 59 |
|
| 60 |
+
# Input Row
|
| 61 |
with gr.Row():
|
| 62 |
step_input = gr.File(label="Upload STEP File")
|
| 63 |
force_input = gr.Number(label="Press Force (N)", value=1000)
|
|
|
|
| 65 |
label="Material Properties (JSON)",
|
| 66 |
value='{"elastic_modulus": 2e11, "poisson": 0.3}'
|
| 67 |
)
|
| 68 |
+
|
| 69 |
+
# Output Row
|
| 70 |
+
with gr.Row():
|
| 71 |
+
script_output = gr.Textbox(label="Generated APDL Script", interactive=False)
|
| 72 |
download_button = gr.File(label="Download Script")
|
| 73 |
+
|
| 74 |
+
# Button Row
|
| 75 |
+
with gr.Row():
|
| 76 |
generate_button = gr.Button("Generate APDL Script")
|
| 77 |
+
submit_button = gr.Button("Submit")
|
| 78 |
|
| 79 |
# Events
|
| 80 |
generate_button.click(
|
|
|
|
| 82 |
inputs=[step_input, force_input, material_input],
|
| 83 |
outputs=[script_output, download_button],
|
| 84 |
)
|
| 85 |
+
|
| 86 |
+
submit_button.click(
|
| 87 |
+
fn=lambda: "Submission successful!",
|
| 88 |
+
inputs=None,
|
| 89 |
+
outputs=gr.Textbox(label="Submission Status")
|
| 90 |
+
)
|
| 91 |
|
| 92 |
app.launch()
|
| 93 |
|