Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,57 +5,63 @@ import subprocess
|
|
| 5 |
# Default script template for FreeCAD
|
| 6 |
freecad_script = """import FreeCAD
|
| 7 |
import Part
|
| 8 |
-
|
| 9 |
# Open STEP file
|
| 10 |
doc = FreeCAD.newDocument("MyDocument")
|
| 11 |
shape = Part.Shape()
|
| 12 |
shape.read("{step_file}")
|
| 13 |
part = doc.addObject("Part::Feature", "ImportedPart")
|
| 14 |
part.Shape = shape
|
| 15 |
-
|
| 16 |
# Save as a new STEP file
|
| 17 |
doc.recompute()
|
| 18 |
doc.saveAs("{output_file}")
|
| 19 |
print(f"STEP file processed and saved to {output_file}")
|
| 20 |
"""
|
| 21 |
|
|
|
|
| 22 |
def upload_step_file(file):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
def modify_script(script_text):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
def execute_freecad_script(step_file):
|
| 39 |
-
"""Execute the FreeCAD script."""
|
| 40 |
global freecad_script
|
| 41 |
script_path = "temp_script.py"
|
| 42 |
output_file = "output_processed.step"
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
]
|
| 56 |
|
| 57 |
-
|
| 58 |
-
subprocess.run(
|
|
|
|
|
|
|
| 59 |
if os.path.exists(output_file):
|
| 60 |
return f"Script executed successfully. Processed STEP file saved at {output_file}.", output_file
|
| 61 |
else:
|
|
@@ -63,18 +69,24 @@ def execute_freecad_script(step_file):
|
|
| 63 |
except subprocess.CalledProcessError as e:
|
| 64 |
return f"Error executing FreeCAD script: {e}", None
|
| 65 |
except FileNotFoundError:
|
| 66 |
-
return "FreeCAD executable not found. Verify the path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Gradio interface
|
| 69 |
with gr.Blocks() as app:
|
| 70 |
gr.Markdown("# FreeCAD Script Processor")
|
| 71 |
|
|
|
|
| 72 |
with gr.Row():
|
| 73 |
step_file = gr.File(label="Upload STEP File")
|
| 74 |
upload_status = gr.Text(label="Upload Status")
|
| 75 |
step_path = gr.Text(label="Uploaded File Path (Internal Use)", visible=False)
|
| 76 |
step_file.upload(upload_step_file, inputs=step_file, outputs=[upload_status, step_path])
|
| 77 |
|
|
|
|
| 78 |
with gr.Row():
|
| 79 |
script_view = gr.Textbox(freecad_script, lines=20, label="FreeCAD Script")
|
| 80 |
save_button = gr.Button("Save Script")
|
|
@@ -82,6 +94,7 @@ with gr.Blocks() as app:
|
|
| 82 |
modified_script = gr.File(label="Download Modified Script")
|
| 83 |
save_button.click(modify_script, inputs=script_view, outputs=[script_status, modified_script])
|
| 84 |
|
|
|
|
| 85 |
with gr.Row():
|
| 86 |
execute_button = gr.Button("Run FreeCAD Script")
|
| 87 |
execution_status = gr.Text(label="Execution Status")
|
|
@@ -89,4 +102,3 @@ with gr.Blocks() as app:
|
|
| 89 |
execute_button.click(execute_freecad_script, inputs=step_path, outputs=[execution_status, output_file])
|
| 90 |
|
| 91 |
app.launch()
|
| 92 |
-
|
|
|
|
| 5 |
# Default script template for FreeCAD
|
| 6 |
freecad_script = """import FreeCAD
|
| 7 |
import Part
|
|
|
|
| 8 |
# Open STEP file
|
| 9 |
doc = FreeCAD.newDocument("MyDocument")
|
| 10 |
shape = Part.Shape()
|
| 11 |
shape.read("{step_file}")
|
| 12 |
part = doc.addObject("Part::Feature", "ImportedPart")
|
| 13 |
part.Shape = shape
|
|
|
|
| 14 |
# Save as a new STEP file
|
| 15 |
doc.recompute()
|
| 16 |
doc.saveAs("{output_file}")
|
| 17 |
print(f"STEP file processed and saved to {output_file}")
|
| 18 |
"""
|
| 19 |
|
| 20 |
+
# Function to handle STEP file upload
|
| 21 |
def upload_step_file(file):
|
| 22 |
+
try:
|
| 23 |
+
file_path = os.path.join(os.getcwd(), file.name)
|
| 24 |
+
with open(file_path, "wb") as f:
|
| 25 |
+
f.write(file.read())
|
| 26 |
+
return f"STEP file '{file.name}' uploaded successfully.", file_path
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error uploading STEP file: {e}", None
|
| 29 |
|
| 30 |
+
# Function to modify and save FreeCAD script
|
| 31 |
def modify_script(script_text):
|
| 32 |
+
try:
|
| 33 |
+
global freecad_script
|
| 34 |
+
freecad_script = script_text
|
| 35 |
+
script_file = "modified_script.py"
|
| 36 |
+
with open(script_file, "w") as f:
|
| 37 |
+
f.write(freecad_script)
|
| 38 |
+
return "FreeCAD script updated successfully.", script_file
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error modifying script: {e}", None
|
| 41 |
|
| 42 |
+
# Function to execute the FreeCAD script
|
| 43 |
def execute_freecad_script(step_file):
|
|
|
|
| 44 |
global freecad_script
|
| 45 |
script_path = "temp_script.py"
|
| 46 |
output_file = "output_processed.step"
|
| 47 |
|
| 48 |
+
try:
|
| 49 |
+
# Fill in file paths in the script
|
| 50 |
+
script_with_paths = freecad_script.format(
|
| 51 |
+
step_file=step_file, output_file=output_file
|
| 52 |
+
)
|
| 53 |
+
with open(script_path, "w") as f:
|
| 54 |
+
f.write(script_with_paths)
|
| 55 |
|
| 56 |
+
# Find FreeCAD executable path
|
| 57 |
+
freecad_path = os.environ.get(
|
| 58 |
+
"FREECAD_EXEC", "C:\\Program Files\\FreeCAD 1.0\\bin\\FreeCADCmd.exe"
|
| 59 |
+
)
|
|
|
|
| 60 |
|
| 61 |
+
# Run FreeCAD
|
| 62 |
+
subprocess.run([freecad_path, script_path], check=True)
|
| 63 |
+
|
| 64 |
+
# Check if output file was created
|
| 65 |
if os.path.exists(output_file):
|
| 66 |
return f"Script executed successfully. Processed STEP file saved at {output_file}.", output_file
|
| 67 |
else:
|
|
|
|
| 69 |
except subprocess.CalledProcessError as e:
|
| 70 |
return f"Error executing FreeCAD script: {e}", None
|
| 71 |
except FileNotFoundError:
|
| 72 |
+
return "FreeCAD executable not found. Verify the installation path.", None
|
| 73 |
+
finally:
|
| 74 |
+
# Clean up temporary script
|
| 75 |
+
if os.path.exists(script_path):
|
| 76 |
+
os.remove(script_path)
|
| 77 |
|
| 78 |
# Gradio interface
|
| 79 |
with gr.Blocks() as app:
|
| 80 |
gr.Markdown("# FreeCAD Script Processor")
|
| 81 |
|
| 82 |
+
# File upload
|
| 83 |
with gr.Row():
|
| 84 |
step_file = gr.File(label="Upload STEP File")
|
| 85 |
upload_status = gr.Text(label="Upload Status")
|
| 86 |
step_path = gr.Text(label="Uploaded File Path (Internal Use)", visible=False)
|
| 87 |
step_file.upload(upload_step_file, inputs=step_file, outputs=[upload_status, step_path])
|
| 88 |
|
| 89 |
+
# Script modification
|
| 90 |
with gr.Row():
|
| 91 |
script_view = gr.Textbox(freecad_script, lines=20, label="FreeCAD Script")
|
| 92 |
save_button = gr.Button("Save Script")
|
|
|
|
| 94 |
modified_script = gr.File(label="Download Modified Script")
|
| 95 |
save_button.click(modify_script, inputs=script_view, outputs=[script_status, modified_script])
|
| 96 |
|
| 97 |
+
# Script execution
|
| 98 |
with gr.Row():
|
| 99 |
execute_button = gr.Button("Run FreeCAD Script")
|
| 100 |
execution_status = gr.Text(label="Execution Status")
|
|
|
|
| 102 |
execute_button.click(execute_freecad_script, inputs=step_path, outputs=[execution_status, output_file])
|
| 103 |
|
| 104 |
app.launch()
|
|
|