APDL_STPL / app.py
jithenderchoudary's picture
Update app.py
b5f490a verified
raw
history blame
4.42 kB
import os
import gradio as gr
import cadquery as cq
from cadquery import exporters
import trimesh
import pyvista as pv
from io import BytesIO
import json
import atexit
import glob
import os
# Cleanup function to remove temporary files
def cleanup_temp_files():
temp_files = glob.glob("*.stl") + glob.glob("*.inp")
for temp_file in temp_files:
try:
os.remove(temp_file)
except OSError:
pass
atexit.register(cleanup_temp_files)
# Define 2D and 3D view generation functions with error handling
def generate_2d_view(step_file):
try:
model = cq.importers.importStep(step_file.name)
two_d_image = model.toSvg()
return two_d_image
except Exception as e:
return f"Error generating 2D view: {str(e)}"
def generate_3d_view(step_file):
try:
model = cq.importers.importStep(step_file.name)
mesh = trimesh.Trimesh(vertices=model.val().vertices(), faces=model.val().faces())
three_d_image = mesh.show()
return three_d_image
except Exception as e:
return f"Error generating 3D view: {str(e)}"
# Wrapper function to generate views
def generate_views(step_file):
try:
two_d_view = generate_2d_view(step_file)
three_d_view = generate_3d_view(step_file)
return two_d_view, three_d_view
except Exception as e:
return f"Error: {str(e)}", None
# APDL script generation
def generate_apdl_script(file, press_force, material_json):
try:
material = json.loads(material_json)
elastic_modulus = material.get("elastic_modulus", 2e11)
poisson = material.get("poisson", 0.3)
# Use temporary file path for processing
step_file_path = file.name
model = cq.importers.importStep(step_file_path)
stl_file = step_file_path.replace(".stp", ".stl")
exporters.export(model, stl_file)
apdl_script = f"""\n
/PREP7
! Importing Geometry
/import, '{step_file_path}', geom, STEP
! Material Properties
MP, EX, 1, {elastic_modulus}
MP, PRXY, 1, {poisson}
! Load and Boundary Conditions
F, NODE, ALL, FX, {press_force}
! Solve
/SOLU
SOLVE
/POST1
*GET, stress, NODE, 0, S, MAX
/EXIT, SAVE
"""
apdl_file_path = step_file_path.replace(".stp", ".inp")
with open(apdl_file_path, "w") as apdl_file:
apdl_file.write(apdl_script)
return apdl_script, apdl_file_path
except Exception as e:
return f"Error: {str(e)}", None
# Gradio App
def main():
with gr.Blocks() as app:
gr.Markdown("# Press Tool Design and APDL Script Generator")
with gr.Row():
step_input = gr.File(label="Upload STEP File")
with gr.Row():
two_d_output = gr.Textbox(label="2D View/Error Message", interactive=False)
three_d_output = gr.Textbox(label="3D View/Error Message", interactive=False)
generate_views_btn = gr.Button("Generate 2D & 3D Views")
with gr.Row():
force_input = gr.Number(label="Press Force (N)", value=1000)
material_input = gr.Textbox(
label="Material Properties (JSON)",
value='{"elastic_modulus": 2e11, "poisson": 0.3}'
)
with gr.Row():
script_output = gr.Textbox(label="Generated APDL Script", interactive=False)
download_button = gr.File(label="Download Script")
generate_apdl_btn = gr.Button("Generate APDL Script")
# Events for Visualization
generate_views_btn.click(
fn=generate_views,
inputs=step_input,
outputs=[two_d_output, three_d_output]
)
# Events for APDL Script
def handle_download(apdl_script, apdl_file_path):
if apdl_file_path and os.path.exists(apdl_file_path):
return apdl_file_path
return None
generate_apdl_btn.click(
fn=generate_apdl_script,
inputs=[step_input, force_input, material_input],
outputs=[script_output, download_button]
)
return app
if __name__ == "__main__":
app = main()
try:
# Automatically find an available port in the range 7860-7870
port = find_free_port(7860, 7870)
print(f"Launching on port: {port}")
app.launch(server_port=port, debug=True)
except Exception as e:
print(f"Failed to launch: {str(e)}")