APDLscript / app.py
jithenderchoudary's picture
Update app.py
89c646b verified
raw
history blame
3.72 kB
import gradio as gr
import os
import subprocess
# Default script template for FreeCAD
freecad_script = """import FreeCAD
import Part
# Open STEP file
doc = FreeCAD.newDocument("MyDocument")
shape = Part.Shape()
shape.read("{step_file}")
part = doc.addObject("Part::Feature", "ImportedPart")
part.Shape = shape
# Save as a new STEP file
doc.recompute()
doc.saveAs("{output_file}")
print(f"STEP file processed and saved to {output_file}")
"""
# Function to handle STEP file upload
def upload_step_file(file):
try:
file_path = os.path.join(os.getcwd(), file.name)
with open(file_path, "wb") as f:
f.write(file.read())
return f"STEP file '{file.name}' uploaded successfully.", file_path
except Exception as e:
return f"Error uploading STEP file: {e}", None
# Function to modify and save FreeCAD script
def modify_script(script_text):
try:
global freecad_script
freecad_script = script_text
script_file = "modified_script.py"
with open(script_file, "w") as f:
f.write(freecad_script)
return "FreeCAD script updated successfully.", script_file
except Exception as e:
return f"Error modifying script: {e}", None
# Function to execute the FreeCAD script
def execute_freecad_script(step_file):
global freecad_script
script_path = "temp_script.py"
output_file = "output_processed.step"
try:
# Fill in file paths in the script
script_with_paths = freecad_script.format(
step_file=step_file, output_file=output_file
)
with open(script_path, "w") as f:
f.write(script_with_paths)
# Find FreeCAD executable path
freecad_path = os.environ.get(
"FREECAD_EXEC", "C:\\Program Files\\FreeCAD 1.0\\bin\\FreeCADCmd.exe"
)
# Run FreeCAD
subprocess.run([freecad_path, script_path], check=True)
# Check if output file was created
if os.path.exists(output_file):
return f"Script executed successfully. Processed STEP file saved at {output_file}.", output_file
else:
return "Script executed, but no output file found.", None
except subprocess.CalledProcessError as e:
return f"Error executing FreeCAD script: {e}", None
except FileNotFoundError:
return "FreeCAD executable not found. Verify the installation path.", None
finally:
# Clean up temporary script
if os.path.exists(script_path):
os.remove(script_path)
# Gradio interface
with gr.Blocks() as app:
gr.Markdown("# FreeCAD Script Processor")
# File upload
with gr.Row():
step_file = gr.File(label="Upload STEP File")
upload_status = gr.Text(label="Upload Status")
step_path = gr.Text(label="Uploaded File Path (Internal Use)", visible=False)
step_file.upload(upload_step_file, inputs=step_file, outputs=[upload_status, step_path])
# Script modification
with gr.Row():
script_view = gr.Textbox(freecad_script, lines=20, label="FreeCAD Script")
save_button = gr.Button("Save Script")
script_status = gr.Text(label="Script Status")
modified_script = gr.File(label="Download Modified Script")
save_button.click(modify_script, inputs=script_view, outputs=[script_status, modified_script])
# Script execution
with gr.Row():
execute_button = gr.Button("Run FreeCAD Script")
execution_status = gr.Text(label="Execution Status")
output_file = gr.File(label="Download Processed STEP File")
execute_button.click(execute_freecad_script, inputs=step_path, outputs=[execution_status, output_file])
app.launch()