#! python 3
# 创建线字：将文字对象转换为曲线轮廓
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc


def text_to_curves():
    # 1. 只允许选择注释类对象（文字属于 annotation）
    text_id = rs.GetObject("选择要转换为曲线的文字对象", rs.filter.annotation)
    if not text_id:
        return

    # 2. 确认选中的确实是文字对象
    if not rs.IsText(text_id):
        rs.MessageBox("请选择一个文字对象")
        return

    # 3. 从原文字对象读取所有属性
    content = rs.TextObjectText(text_id)
    font    = rs.TextObjectFont(text_id) or "Arial"
    height  = rs.TextObjectHeight(text_id)
    style   = rs.TextObjectStyle(text_id) or 0
    plane   = rs.TextObjectPlane(text_id)

    # 4. 生成文字轮廓（返回 Curve[] 数组）
    outlines = Rhino.Geometry.Curve.CreateTextOutlines(
        content,
        font,
        height,
        style,
        False,                          # closeArcs
        plane,                          # 使用原文字所在平面
        1.0,                            # smallCapsScale
        sc.doc.ModelAbsoluteTolerance   # 使用文档公差
    )

    if not outlines or len(outlines) == 0:
        print("未能创建文字轮廓，请检查字体是否有效。")
        return

    # 5. 逐条添加曲线
    new_ids = []
    for crv in outlines:
        new_ids.append(sc.doc.Objects.AddCurve(crv))

    sc.doc.Views.Redraw()
    rs.SelectObjects(new_ids)
    return new_ids


if __name__ == "__main__":
    text_to_curves()
    