Skip to content

text

文字标注相关 CLI 命令。

cmd_text_add(fmt, file, layer, text, position, color, size, feature, normal, rotate, geometries)

添加文字标注(需 Dimine 运行)。

Source code in dimine_python_sdk\cli\commands\text.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def cmd_text_add(
    fmt: str,
    file: str | None,
    layer: str | None,
    text: str,
    position: str,
    color: str | None,
    size: float,
    feature: str | None,
    normal: str | None,
    rotate: float,
    geometries: str | None,
) -> None:
    """添加文字标注(需 Dimine 运行)。"""
    err = require_remote_sdk()
    if err:
        output_error(err, fmt)

    try:
        text_infos = []
        if geometries is not None:
            # 批量模式:从 JSON 解析完整的 TextInfo 列表
            items = parse_json_arg(geometries, "geometries")
            if not isinstance(items, list):
                raise ValueError("--geometries 必须是 JSON 数组格式")
            for item in items:
                text_infos.append(TextInfo(**item))
        else:
            # 单条模式:通过命令行参数构建
            if not text:
                output_error("--text 文字内容不能为空", fmt)
            try:
                pos = parse_json_arg(position, "position")
            except ValueError:
                pos = parse_point3d(position)
            clr = parse_color(color) if color else [255, 255, 255]
            nml = parse_json_arg(normal, "normal") if normal else [0, 0, 1]
            text_infos.append(TextInfo(
                file=file,
                layer=layer,
                text=text,
                position=pos,
                color=clr,
                size=size,
                feature=feature,
                normal=nml,
                rotate=rotate,
            ))
    except (ValueError, TypeError) as e:
        output_error(str(e), fmt)

    async def _run():
        client = await get_client()
        await client.add_text(text_infos)
        return {
            "success": True,
            "count": len(text_infos),
            "message": f"已添加 {len(text_infos)} 个文字标注",
        }

    try:
        result = asyncio.run(_run())
        output_result(result, fmt)
    except Exception as e:
        handle_sdk_error(e, fmt, "添加文字标注失败")

register_commands(subparsers, format_parent)

向 subparsers 注册文字标注相关子命令。

Source code in dimine_python_sdk\cli\commands\text.py
85
86
87
88
89
90
91
92
93
94
95
96
97
def register_commands(subparsers, format_parent) -> None:
    """向 subparsers 注册文字标注相关子命令。"""
    p = subparsers.add_parser("text-add", parents=[format_parent], help="添加文字标注")
    p.add_argument("--file", "--file-id", dest="file", default=None, help="文件ID(不提供则使用当前打开的第一个文件)")
    p.add_argument("--layer", "--layer-id", dest="layer", default=None, help="图层名称(不提供则使用第一个图层)")
    p.add_argument("--text", default=None, help="文字内容(单条模式必填)")
    p.add_argument("--position", default=None, help="文字位置,JSON 数组如 '[x, y, z]' 或 x,y,z 逗号分隔")
    p.add_argument("--color", default=None, help="颜色 r,g,b(默认 255,255,255)")
    p.add_argument("--size", type=float, default=20, help="文字大小(默认 20)")
    p.add_argument("--feature", default="0", help="所属要素名")
    p.add_argument("--normal", default=None, help="法向量 JSON 数组 [x, y, z](默认 [0, 0, 1])")
    p.add_argument("--rotate", type=float, default=0, help="旋转角度(默认 0)")
    p.add_argument("--geometries", default=None, help="完整 JSON 数组(批量模式),支持 JSON 字符串、@文件路径 或 - 从 stdin 读取")