karthikmn's picture
Update app.py
e0f6981 verified
import os
import gradio as gr
import FreeCAD
import Part
def generate_apdl_from_freecad(freecad_file_path):
"""
Generate an APDL script from a FreeCAD file.
Parameters:
freecad_file_path: Path to the FreeCAD file (.FCStd).
Returns:
Path to the generated APDL script.
"""
# Open the FreeCAD file
doc = FreeCAD.openDocument(freecad_file_path)
obj = doc.Objects[0] # Assuming a single object in the file
# Extract bounding box dimensions
bbox = obj.Shape.BoundBox
length = bbox.XLength
width = bbox.YLength
height = bbox.ZLength
# Generate APDL script
apdl_script = f"""
/prep7
block, 0, {length:.4f}, 0, {width:.4f}, 0, {height:.4f}
/mesh
esize, 10
vmesh, all
allsel, all
/solu
antype, static
d, 1, ux, 0
d, 1, uy, 0
d, 1, uz, 0
f, 2, fy, -1000
solve
/exit
"""
apdl_path = "/tmp/generated_apdl.txt"
with open(apdl_path, "w") as f:
f.write(apdl_script)
# Close the FreeCAD document
FreeCAD.closeDocument(doc.Name)
return apdl_path
def process_freecad_file(freecad_file):
"""
Process the FreeCAD file and generate an APDL script.
Parameters:
freecad_file: Uploaded FreeCAD file (.FCStd).
Returns:
Path to the generated APDL script.
"""
apdl_script_path = generate_apdl_from_freecad(freecad_file.name)
return apdl_script_path
# Gradio Interface
app = gr.Interface(
fn=process_freecad_file,
inputs=gr.File(label="Upload FreeCAD File (.FCStd)"),
outputs=gr.File(label="Download Generated APDL Script"),
live=True,
)
if __name__ == "__main__":
app.launch()