|
from src.llm_client import prompt_llm |
|
from pathlib import Path |
|
import subprocess |
|
|
|
|
|
prompt_base = Path("prompts/base_instruction.txt") |
|
prompt_examples = Path("prompts/example_code.txt") |
|
GEN_SCRIPT = Path("generated/result_script.py") |
|
RUN_SCRIPT = Path("src/run_freecad.py") |
|
|
|
|
|
GUI_SNIPPET = """ |
|
import FreeCADGui |
|
FreeCADGui.activeDocument().activeView().viewAxometric() |
|
FreeCADGui.SendMsgToActiveView("ViewFit") |
|
""" |
|
|
|
def main(): |
|
|
|
user_input = input("Describe your FreeCAD part: ") |
|
|
|
|
|
base_prompt = prompt_base.read_text().strip() |
|
example_prompt = prompt_examples.read_text().strip() |
|
full_prompt = f"{base_prompt}\n\nExamples:\n{example_prompt}\n\nUser instruction: {user_input.strip()}" |
|
|
|
|
|
|
|
generated_code = prompt_llm(full_prompt) |
|
|
|
|
|
if generated_code.startswith("```"): |
|
generated_code = generated_code.strip("`\n ") |
|
if generated_code.lower().startswith("python"): |
|
generated_code = generated_code[len("python"):].lstrip() |
|
|
|
|
|
generated_code += "\n\n" + GUI_SNIPPET |
|
|
|
|
|
GEN_SCRIPT.write_text(generated_code) |
|
print(f"\n Code generated and written to {GEN_SCRIPT}") |
|
|
|
|
|
print("Running FreeCAD with the generated script...") |
|
try: |
|
subprocess.run(["python", str(RUN_SCRIPT)], check=True) |
|
except subprocess.CalledProcessError as e: |
|
print(f"❌ FreeCAD script execution failed with error code: {e.returncode}") |
|
except Exception as e: |
|
print(f"❌ Error running run_freecad.py: {e}") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|