Skip to content

轮廓线建模与可视化

通过两条闭合轮廓线(上下两层)调用 ContourModeling.two_contour_modeling() 生成三角网模型,再使用 render_tin() 多角度离屏渲染保存为图片文件。整个流程均基于本地 C++ 扩展(DmPyBindInterface)和 PyVista 渲染引擎,无需连接数采软件。

前置条件

pip install dimine-python-sdk[vis]

[vis] 额外依赖包含 PyVista 渲染引擎。如果已安装基础包,也可单独安装:

pip install pyvista

完整示例

import numpy as np
from pathlib import Path
from dimine_python_sdk.lib.exploitation import ContourModeling, ContourAlgorithm
from dimine_python_sdk.lib.visualization import (
    render_tin,
    CameraAngle,
    RenderOptions,
    TinRenderConfig,
)


def main():
    # ------------------------------------------------------------------
    # 1. 定义两条闭合轮廓线(上下两层矩形,首尾闭合)
    # ------------------------------------------------------------------
    # 下层轮廓线(Z=0,100×80 的矩形)
    contour_bottom = [
        [0.0, 0.0, 0.0],
        [100.0, 0.0, 0.0],
        [100.0, 80.0, 0.0],
        [0.0, 80.0, 0.0],
        [0.0, 0.0, 0.0],  # 闭合回起点
    ]

    # 上层轮廓线(Z=50,100×80 的矩形)
    contour_top = [
        [0.0, 0.0, 50.0],
        [100.0, 0.0, 50.0],
        [100.0, 80.0, 50.0],
        [0.0, 80.0, 50.0],
        [0.0, 0.0, 50.0],  # 闭合回起点
    ]

    # ------------------------------------------------------------------
    # 2. 两轮廓线建模:生成三角网模型
    # ------------------------------------------------------------------
    models = ContourModeling.two_contour_modeling(
        ContourAlgorithm.MIN_PERIMETER,
        contour_bottom,
        contour_top,
    )

    print(f"生成模型数量: {len(models)}")
    for i, tin in enumerate(models):
        print(f"  模型[{i}]: {tin.points.shape[0]} 个顶点, {tin.faces.shape[0]} 个三角面片")

    # ------------------------------------------------------------------
    # 3. 构造多角度渲染配置
    # ------------------------------------------------------------------
    angles = [
        CameraAngle(name="top", preset="top"),           # 俯视图
        CameraAngle(name="north", preset="north"),       # 北视图
        CameraAngle(name="east", preset="east"),         # 东视图
        CameraAngle(name="isometric", preset="isometric"),  # 等轴测视图
    ]

    render_options = RenderOptions(
        width=1024,
        height=768,
        background_color="white",
        show_edges=True,
        edge_color="#333333",
        edge_width=1.0,
        mesh_opacity=1.0,
        show_axes=True,
    )

    config = TinRenderConfig(
        angles=angles,
        mode="separate",  # 每个角度输出一张独立图片
        render_options=render_options,
        labels=["轮廓线模型"],
    )

    # ------------------------------------------------------------------
    # 4. 离屏渲染并保存图片
    # ------------------------------------------------------------------
    output_dir = "./output"
    result = render_tin(
        geometries=models,
        config=config,
        output_path=output_dir,
        output_prefix="contour_model",
        image_format="png",
    )

    # ------------------------------------------------------------------
    # 5. 输出结果信息
    # ------------------------------------------------------------------
    print(f"\n渲染完成!输出目录: {Path(output_dir).resolve()}")
    print(f"渲染模式: {result['mode']}")
    print(f"渲染角度: {result['angles']}")
    if result["output_files"]:
        print(f"输出文件:")
        for f in result["output_files"]:
            print(f"  {f}")


if __name__ == "__main__":
    main()

运行上述脚本后,在 ./output/ 目录下生成以下图片文件:

output/
  contour_model_top.png        # 俯视图(从 +Z 向下看)
  contour_model_north.png      # 北视图(从 +Y 向 -Y 看)
  contour_model_east.png       # 东视图(从 +X 向 -X 看)
  contour_model_isometric.png  # 等轴测视图

输出说明

渲染模式

模式 mode 说明
分离模式 "separate" 每个角度输出一张独立图片,通过 output_prefix 控制文件名前缀
组合模式 "combined" 所有角度拼接在一张图上,通过 TinSubplotLayout 控制子图布局

渲染配置

  • CameraAngle:支持 7 种预设角度(top/bottom/north/south/east/west/isometric)和自定义相机位置
  • RenderOptions:控制图像尺寸、背景色、网格边线、透明度、光照等显示效果
  • TinRenderConfig:总配置,包含角度列表、渲染模式、渲染选项,以及可选的 colors(颜色列表)和 labels(标签列表)

返回值

render_tin() 返回一个 dict

{
    "mode": "separate",           # 渲染模式
    "angles": ["top", "north", ...],  # 渲染的角度名列表
    "output_files": ["path/to/..."],  # 输出文件路径列表(离屏模式)
    "config": TinRenderConfig,        # 回显的渲染配置
}
  • 交互模式(output_path=None)下,output_filesNoneplotters 返回 PyVista Plotter 对象列表,可用于交互式显示
  • 离屏模式(output_path 非空)下,plottersNoneoutput_files 包含保存的图片路径