# save_fcstd.py
# A Python script for use with FreeCAD's command-line interface.
# Imports an OpenSCAD file and saves it as a FreeCAD project.

import sys
import FreeCAD
import Part

# --- Argument Validation ---
if len(sys.argv) < 3:
    print("Converter script usage: <input_file.scad> <output_file.FCStd>")
    sys.exit(1)

input_file_path = sys.argv[-2]
output_file_path = sys.argv[-1]

print(f"Input file: {input_file_path}")
print(f"Output file: {output_file_path}")

# --- Conversion Logic ---
try:
    # 1. Create a new, empty FreeCAD document
    doc = FreeCAD.newDocument("ImportedSCAD")

    # 2. Use the Part module to import the .scad file into the document.
    # This requires the OpenSCAD workbench to be installed in FreeCAD.
    # FreeCAD manages the call to the 'openscad' executable itself.
    Part.insert(input_file_path, doc.Name)

    # 3. It's good practice to recompute the model after an import.
    doc.recompute()

    # 4. Save the document to the specified output file path.
    doc.saveAs(output_file_path)

    print("FreeCAD project file saved successfully.")
    sys.exit(0)

except Exception as e:
    print(f"An error occurred during FreeCAD project creation: {e}")
    sys.exit(1) 