Skip to content

要素管理

要素(Feature)是几何模型的属性集合定义。例如,巷道要素可能包含名称、长度、宽度、高度等属性。通过要素管理系统,可以创建、更新和删除要素定义。

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 client.create_feature(
            file_id=files[len(files)-1].id,
            feature_name="巷道要素",
            feature_define=[
                {"name": "名称", "type": "string"},
                {"name": "长度", "type": "double"},
                {"name": "宽度", "type": "double"},
                {"name": "高度", "type": "double"},
                {"name": "材质", "type": "string"},
                {"name": "掘进日期", "type": "string"}
            ]
        )
        print("要素创建成功")

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

2. 获取要素信息

获取文件中所有要素的定义信息:

import asyncio
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import FeatureInfo, PropertyInfo

async def main():
    async with open_client() as client:
        # 获取文件列表
        files = await client.get_files()
        features = await client.get_features(files[0].id)

        print(f"文件包含 {len(features)} 个要素:")
        for feature in features:
            print(f"\n要素名称: {feature.name}")
            print("属性定义:")
            for prop in feature.properties:
                print(f"  - {prop.name}: {prop.type}")

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

3. 更新要素

更新已存在的要素定义,可以添加、修改或删除属性:

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 client.update_feature(
            file_id=files[len(files)-1].id,
            feature_name="巷道要素",
            feature_define=[
                {"name": "名称", "type": "string"},
                {"name": "长度", "type": "double"},
                {"name": "宽度", "type": "double"},
                {"name": "高度", "type": "double"},
                {"name": "材质", "type": "string"},
                {"name": "掘进日期", "type": "string"},
                {"name": "支护类型", "type": "string"}  # 新增属性
            ]
        )
        print("要素更新成功")

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

4. 删除要素

删除文件中的指定要素:

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 client.delete_feature(
            file=files[len(files)-1].id,
            feature_name="巷道要素"
        )
        print("要素删除成功")

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

5. 要素与几何模型的关系

要素定义了几何模型可以拥有的属性。创建几何模型时,需要指定所属的要素:

import asyncio
import numpy as np
from dimine_python_sdk.conn import open_client
from dimine_python_sdk.models.types import Point, 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([100, 200, 300]),
            properties=[
                PropertyValue(name="名称", value="钻孔ZK-001", type="string"),
                PropertyValue(name="高程", value="1250.5", type="float"),
                PropertyValue(name="孔深", value="150.0", type="float")
            ]
        )

        await client.create_geometry([point])
        print("带属性的点模型创建成功")

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

6. 属性类型说明

要素属性支持以下数据类型:

  • string: 字符串类型
  • int: 整数类型
  • long: 长整数类型
  • short: 短整数类型
  • double: 浮点数类型

属性使用注意事项

  1. 数据类型匹配: 设置属性值时需要与定义的类型匹配
  2. 属性名称唯一: 在同一个要素中,属性名称不能重复
  3. 字段命名: 属性名称建议使用中文,便于在Dimine软件中显示

7. 完整示例:创建和管理要素

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

async def main():
    async with open_client() as client:
        # 1. 获取文件信息
        files = await client.get_files()
        file_id = files[0].id
        layers = await client.get_layers(file_id=file_id)
        layer_id = layers[0].id

        # 2. 创建多个要素
        await client.create_feature(
            file_id=file_id,
            feature_name="勘探孔",
            feature_define=[
                {"name": "孔号", "type": "string"},
                {"name": "孔深", "type": "double"},
                {"name": "倾角", "type": "double"},
                {"name": "方位角", "type": "double"},
                {"name": "岩性", "type": "string"}
            ]
        )

        await client.create_feature(
            file_id=file_id,
            feature_name="测点",
            feature_define=[
                {"name": "点号", "type": "string"},
                {"name": "高程", "type": "double"},
                {"name": "X坐标", "type": "double"},
                {"name": "Y坐标", "type": "double"},
                {"name": "Z坐标", "type": "double"}
            ]
        )

        # 3. 获取要素信息
        features = await client.get_features(file_id)
        print(f"创建了 {len(features)} 个要素")

        # 4. 创建与要素关联的几何模型
       勘探孔要素 = None
        测点要素 = None

        for feature in features:
            if feature.name == "勘探孔":
                勘探孔要素 = feature
            elif feature.name == "测点":
                测点要素 = feature

        # 创建勘探孔模型
        if 勘探孔要素:
            drill_hole = Line(
                file=file_id,
                layer=layer_id,
                feature=勘探孔要素.name,
                geometry=np.array([[0, 0, 0], [100, 50, -150]]),
                color=[255, 0, 0],
                properties=[
                    PropertyValue(name="孔号", value="ZK-001", type="string"),
                    PropertyValue(name="孔深", value="150.0", type="float"),
                    PropertyValue(name="倾角", value="56.3", type="float")
                ]
            )
            await client.create_geometry([drill_hole])

        # 创建测点模型
        if 测点要素:
            survey_point = Point(
                file=file_id,
                layer=layer_id,
                feature=测点要素.name,
                geometry=np.array([50, 25, -75]),
                color=[0, 255, 0],
                properties=[
                    PropertyValue(name="点号", value="D-001", type="string"),
                    PropertyValue(name="高程", value="1225.0", type="float")
                ]
            )
            await client.create_geometry([survey_point])

        print("要素和模型创建完成")

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

8. 最佳实践

  1. 要素规划: 在创建模型前,先规划好需要的要素类型和属性
  2. 命名规范: 使用清晰的要素名称,如"巷道"、"钻孔"、"测点"等
  3. 属性精简: 只包含必要的属性,避免过度设计
  4. 类型选择: 根据数据特性选择合适的属性类型
  5. 版本管理: 如果需要修改要素定义,建议先删除后重建,避免数据不一致