############################ EXPORT CRVs ############################
import rhinoscriptsyntax as rs
import sys
import csv
import time
sys.path.append("P:\\WWW\\mraontu\\dm2")
import DM_lib as dm
reload(dm)
############################
# Rhino Setup
rs.UnitSystem(4)                                                                # km = 5, meters = 4, cm = 3 etc
rs.ShowGrid(None, 0)
rs.ShowGridAxes(None, 0)
rs.ViewDisplayMode(rs.CurrentView(), "shaded")
rs.DeleteObjects(rs.AllObjects())
rs.EnableRedraw(False)
############################
# Datei importieren
file_path = "C:\\Users\\rothl\\dm2\\3DM\\crv_kontur_01.3dm"                  # set path 3dm file 
rs.Command("-_Import \"" + file_path + "\" _Enter", echo=False)
############################
rs.Sleep(1000)  
rs.Redraw()

def export_closed_curves(file_path="C:\\Users\\rothl\\dm2\\export_test\\closed_curves.csv", layer_name="Curves"):
    curves = rs.ObjectsByLayer(layer_name)  # Kurven von Layer
    closed_curves = []
    
    for crv in curves:
        if rs.IsCurveClosed(crv):
            points = rs.CurveEditPoints(crv)  # Kontrollpunkte
            closed_curves.append([list(pt) for pt in points])  

    # CSV-Datei
    with open(file_path, "w") as f:
        writer = csv.writer(f)
        for curve in closed_curves:
            writer.writerow([str(pt) for pt in curve])  # Punkte als Strings speichern

    print("finished export")

export_closed_curves("P:\\WWW\\mraontu\\dm2\\09\\Lists\\test_kontur_curves.csv", "_bldg3D_contour")  # Datei speichern, Path List, Layer to export
