#!/bin/bash

# --- export_dxf.sh ---
# Exports a 2D projection of an OpenSCAD file to a DXF file.
#
# Usage: ./export_dxf.sh <source.scad> [output.dxf]
#
# Arguments:
#   $1: source_file  - Input .scad file (Required)
#   $2: output_file  - Output .dxf file (Optional)

# --- Input Validation ---
if [ -z "$1" ]; then
    echo "Usage: $0 <source.scad> [output.dxf]"
    echo "Error: Source file not specified."
    exit 1
fi

# --- Argument Parsing ---
SOURCE_FILE="$1"
OUTPUT_FILE="$2"

# --- Set Default Output Filename ---
if [ -z "$OUTPUT_FILE" ]; then
    OUTPUT_FILE="${SOURCE_FILE%.scad}_unfolded.dxf"
fi

# --- OpenSCAD DXF Export Command ---
echo "Exporting 2D projection from '$SOURCE_FILE' to '$OUTPUT_FILE'..."
openscad \
    -o "$OUTPUT_FILE" \
    -D "view_mode=\"dxf\"" \
    "$SOURCE_FILE"

# --- Completion Message ---
if [ $? -eq 0 ]; then
    echo "Export complete: '$OUTPUT_FILE' created successfully."
else
    echo "Error: OpenSCAD DXF export failed."
    exit 1
fi 