File size: 3,721 Bytes
4c12729
 
b697dc3
4c12729
b697dc3
 
 
 
 
 
 
 
 
 
 
 
 
609c6e8
 
89c646b
5293b90
89c646b
 
 
 
 
 
 
b697dc3
89c646b
b697dc3
89c646b
 
 
 
 
 
 
 
 
b697dc3
89c646b
b697dc3
 
 
 
 
89c646b
 
 
 
 
 
 
b697dc3
89c646b
 
 
 
4c12729
89c646b
 
 
 
b697dc3
 
5293b90
b697dc3
5293b90
b697dc3
5293b90
89c646b
 
 
 
 
5293b90
 
4c12729
b697dc3
5293b90
89c646b
4c12729
 
b697dc3
 
 
4c12729
89c646b
609c6e8
b697dc3
 
 
 
 
4c12729
89c646b
609c6e8
b697dc3
 
 
 
c65ef55
609c6e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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()