Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cadquery as cq | |
| from cadquery import exporters | |
| import json | |
| import os | |
| # Generate APDL Script | |
| def generate_apdl_script(file, press_force, material_json): | |
| try: | |
| # Parse material properties from JSON | |
| material = json.loads(material_json) | |
| elastic_modulus = material.get("elastic_modulus", 2e11) | |
| poisson = material.get("poisson", 0.3) | |
| # Get the uploaded file path | |
| step_file_path = file.name | |
| # Load STEP file with CadQuery | |
| model = cq.importers.importStep(step_file_path) | |
| # Export as STL for meshing | |
| stl_file = step_file_path.replace(".stp", ".stl") | |
| exporters.export(model, stl_file) | |
| # Generate APDL script | |
| apdl_script = f"""\ | |
| /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 | |
| """ | |
| # Save APDL script to file | |
| apdl_file_path = step_file_path.replace(".stp", ".inp") | |
| with open(apdl_file_path, "w") as apdl_file: | |
| apdl_file.write(apdl_script) | |
| # Return script content and file path for download | |
| 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("# APDL Script Generator for Press Tool Design") | |
| # Input Row | |
| with gr.Row(): | |
| step_input = gr.File(label="Upload STEP File") | |
| 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}' | |
| ) | |
| # Output Row | |
| with gr.Row(): | |
| script_output = gr.Textbox(label="Generated APDL Script", interactive=False) | |
| download_button = gr.File(label="Download Script") | |
| # Button Row | |
| with gr.Row(): | |
| generate_button = gr.Button("Generate APDL Script") | |
| submit_button = gr.Button("Submit") | |
| # Events | |
| 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_button.click( | |
| fn=generate_apdl_script, | |
| inputs=[step_input, force_input, material_input], | |
| outputs=[script_output, download_button], | |
| ) | |
| submit_button.click( | |
| fn=lambda: "Submission successful!", | |
| inputs=None, | |
| outputs=gr.Textbox(label="Submission Status") | |
| ) | |
| app.launch() | |
| if __name__ == "__main__": | |
| main() | |