Skip to content

数据修改

主要用于修改数采软件场景内的模型对象、属性、图层、文件等数据, 支持修改范围包括:创建、更新、删除模型对象、属性、图层、文件等

1. 数采文件关闭

将数采软件场景下的某个打开的文件关闭

import asyncio
from dimine_python_sdk.conn import open_client
async def main():
    async with open_client() as client:
        # 获取文件列表
        files = await client.get_files()
        # 关闭文件
        await files[0].close()

if __name__ == '__main__':
    asyncio.run(main())

2. 数采文件打开

将控制数采软件打开某个文件到当前场景下

import asyncio
from dimine_python_sdk.conn import open_client
async def main():
    async with open_client() as client:
        file_path = "D:/test.dmf"   # 支持打开多种类型文件:dmf、dmm、cad、dmb、dmd、dmo
        file = await client.open_file(file_path)
        print(file.name)
        print(file.id)

if __name__ == '__main__':
    asyncio.run(main())

3. 创建或更新模型

通过update_or_create_geometry方法批量创建或更新模型,如果模型已存在则更新,不存在则创建。

import asyncio
import numpy as np
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point, Line, Shell, TINGeometry

async def main():
    async with open_client() as client:
        # 获取当前文件的图层和要素
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[len(files)-1].id)
        features = await client.get_features(files[len(files)-1].id)

        # 准备模型数据 - 包含点、线、实体
        geometries = [
            # 创建点
            Point(
                file=files[0].id,
                layer=layers[0].id,
                feature=features[0].name,
                geometry=np.array([11, 32, 44]),
                color=[255, 0, 0]  # 红色
            ),
            # 创建线(多段线)
            Line(
                file=files[0].id,
                layer=layers[0].id,
                feature=features[0].name,
                geometry=np.array([[11, 12, 13], [14, 25, 26]]),
                color=[0, 255, 0]  # 绿色
            ),
            # 创建实体(三角网)
            Shell(
                file=files[0].id,
                layer=layers[0].id,
                feature=features[0].name,
                geometry=TINGeometry(
                    points=np.array([[11, 12, 13], [14, 25, 26], [27, 28, 29]]),
                    faces=np.array([[0, 1, 2]])  # 三个点构成一个三角形面
                ),
                color=[0, 0, 255]  # 蓝色
            )
        ]

        # 批量创建或更新模型
        await client.update_or_create_geometry(geometries)

if __name__ == '__main__':
    asyncio.run(main())

4. 创建模型(单独)

如果只需要创建新模型,使用create_geometry方法:

import asyncio
import numpy as np
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point, Line, Shell, TINGeometry

async def main():
    async with open_client() as client:
        # 获取当前文件、图层、要素信息
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[0].id)
        features = await client.get_features(files[0].id)

        # 创建点模型
        point = Point(
            file=files[0].id,
            layer=layers[0].id,
            feature=features[0].name,
            geometry=np.array([0, 0, 0])
        )

        await client.create_geometry([point])

if __name__ == '__main__':
    asyncio.run(main())

5. 更新模型(单独)

如果只需要更新已存在的模型,使用update_geometry方法:

import asyncio
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point

async def main():
    async with open_client() as client:
        # 首先获取模型列表找到要更新的模型
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[0].id)
        geometries = await client.get_geometry(file_id=files[0].id, layer_id=layers[0].id)

        # 找到第一个点模型
        point_to_update = None
        for geom in geometries:
            if geom.type == "point":
                point_to_update = geom
                break

        if point_to_update:
            # 更新点坐标和颜色
            point_to_update.geometry = np.array([100, 200, 300])
            point_to_update.color = [255, 255, 0]  # 黄色

            await client.update_geometry([point_to_update])

if __name__ == '__main__':
    asyncio.run(main())

6. 创建图层

创建新的图层:

import asyncio
from dimine_python_sdk.conn import open_client

async def main():
    async with open_client() as client:
        files = await client.get_files()
        # 创建新图层
        layer = await client.create_layer(
            file_id=files[len(files)-1].id,
            name="test_layer"
        )
        print(f"创建图层: {layer.name}, ID: {layer.id}")

if __name__ == '__main__':
    asyncio.run(main())

7. 删除图层

删除指定图层:

import asyncio
from dimine_python_sdk.conn import open_client

async def main():
    async with open_client() as client:
        files = await client.get_files()
        # 创建测试图层
        new_layer = await client.create_layer(
            file_id=files[len(files)-1].id,
            name="test_layer_to_delete"
        )
        # 删除图层
        await client.delete_layer(
            file_id=files[len(files)-1].id,
            layer_name=new_layer.name
        )
        print("图层删除成功")

if __name__ == '__main__':
    asyncio.run(main())

8. 更新模型

更新已存在的模型属性、颜色或几何信息:

import asyncio
from dimine_python_sdk.conn import open_client

async def main():
    async with open_client() as client:
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[len(files)-1].id)
        features = await client.get_features(files[len(files)-1].id)
        geometries = await client.get_geometry(
            file_id=files[len(files)-1].id,
            layer_id=layers[0].id
        )

        if len(geometries) > 0:
            # 更新几何颜色
            geometries[0].color = [255, 0, 0]  # 红色
            # 更新几何坐标(如果需要)
            # geometries[0].geometry = np.array([new_x, new_y, new_z])

            await client.update_geometry([geometries[0]])
            print(f"几何更新成功: {geometries[0].id}")

if __name__ == '__main__':
    asyncio.run(main())

9. 删除模型

删除指定的几何对象:

import asyncio
import numpy as np
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point

async def main():
    async with open_client() as client:
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[len(files)-1].id)
        features = await client.get_features(files[len(files)-1].id)

        # 先创建几何
        new_point = Point(
            file=files[0].id,
            layer=layers[0].id,
            feature=features[0].name,
            geometry=np.array([100, 200, 300])
        )
        await client.create_geometry([new_point])
        print(f"创建点几何: {new_point.id}")

        # 删除几何
        delete_items = [{"id": new_point.id, "file": files[0].id}]
        await client.delete_geometry(delete_items)
        print("几何删除成功")

if __name__ == '__main__':
    asyncio.run(main())

10. 模型属性管理

可以为模型添加自定义属性:

import asyncio
import numpy as np
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point
from dimine_python_sdk.models.types import PropertyValue

async def main():
    async with open_client() as client:
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[0].id)
        features = await client.get_features(files[0].id)

        # 创建带属性的点
        point = Point(
            file=files[0].id,
            layer=layers[0].id,
            feature=features[0].name,
            geometry=np.array([50, 50, 50]),
            properties=[
                PropertyValue(name="类型", value="勘探点", type="string"),
                PropertyValue(name="高程", value="1250.5", type="float"),
                PropertyValue(name="标识号", value="KP-001", type="string")
            ]
        )

        await client.create_geometry([point])

if __name__ == '__main__':
    asyncio.run(main())

11. 模型选中

控制数采软件场景中模型的选中状态,支持替换、追加、移除三种模式。

import asyncio
from dimine_python_sdk.conn import open_client

async def main():
    async with open_client() as client:
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[0].id)
        geometries = await client.get_geometry(
            file_id=files[0].id,
            layer_id=layers[0].id
        )

        if len(geometries) > 0:
            # 构造模型引用列表(仅需 id 和 file)
            geometry_refs = [
                {"id": g.id, "file": g.file}
                for g in geometries[:2]
            ]

            # replace: 替换当前所有选中状态
            selected = await client.select_geometry(geometry_refs, mode="replace")
            print(f"当前选中 {len(selected)} 个模型")

            # add: 追加到当前选中
            # selected = await client.select_geometry(geometry_refs, mode="add")

            # remove: 从当前选中中移除
            # selected = await client.select_geometry(geometry_refs, mode="remove")

if __name__ == '__main__':
    asyncio.run(main())

参数说明: - geometrys: 要操作的模型列表,每个元素包含 id(模型ID)和 file(所属文件ID) - mode: 选择模式 - "replace"(默认): 替换当前所有选中状态 - "add": 追加到当前选择 - "remove": 从当前选择中移除

返回值: - 执行后当前选中的模型列表,每个元素包含 idfile

12. 模型显示/隐藏

控制数采软件场景中模型的显示或隐藏状态,支持显示、隐藏、排他性显示三种操作。

import asyncio
from dimine_python_sdk.conn import open_client

async def main():
    async with open_client() as client:
        files = await client.get_files()
        layers = await client.get_layers(file_id=files[0].id)
        geometries = await client.get_geometry(
            file_id=files[0].id,
            layer_id=layers[0].id
        )

        if len(geometries) > 0:
            geometry_refs = [
                {"id": g.id, "file": g.file}
                for g in geometries[:2]
            ]

            # hide: 隐藏指定模型
            await client.set_geometry_visibility(geometry_refs, action="hide")
            print("模型已隐藏")

            # show: 显示指定模型
            # await client.set_geometry_visibility(geometry_refs, action="show")

            # exclusive_show: 仅显示指定模型,隐藏其他所有模型
            # await client.set_geometry_visibility(geometry_refs, action="exclusive_show")

if __name__ == '__main__':
    asyncio.run(main())

参数说明: - geometrys: 要操作的模型列表,每个元素包含 id(模型ID)和 file(所属文件ID) - action: 操作类型 - "show"(默认): 显示指定模型 - "hide": 隐藏指定模型 - "exclusive_show": 排他性显示,仅显示指定模型并隐藏其他所有模型