#!/usr/bin/env python3
"""
LibreCAD ASCII Points Exporter
Exports selected lines' corner/end points to ASCII format for GeoMax Zoom3D

Usage:
1. Export your LibreCAD drawing as DXF
2. Run: python3 export_ascii_points.py drawing.dxf points.txt
"""

import ezdxf
import sys

def export_lines_to_ascii(dxf_path, output_path, start_num=101):
    """Extract LINE endpoints from DXF and write ASCII point format"""
    doc = ezdxf.readfile(dxf_path)
    msp = doc.modelspace()
    
    with open(output_path, 'w') as f:
        point_num = start_num
        for entity in msp:
            if entity.dxftype() == 'LINE':
                # Write start point
                f.write(f"{point_num} {entity.dxf.start.x:.3f} {entity.dxf.start.y:.3f} 0.000\n")
                point_num += 1
                
                # Write end point
                f.write(f"{point_num} {entity.dxf.end.x:.3f} {entity.dxf.end.y:.3f} 0.000\n")
                point_num += 1
    
    return point_num - start_num

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python3 export_ascii_points.py <drawing.dxf> <points.txt>")
        sys.exit(1)
    
    dxf_file = sys.argv[^1]
    output_file = sys.argv[^2]
    
    count = export_lines_to_ascii(dxf_file, output_file)
    print(f"Exported {count} points to {output_file}")