Skip to content

block_data

DmBlockData

二次封装 dmBlockDataNew 类,提供 Python 化的块段模型操作接口 职责:加载块段模型文件、获取模型属性、遍历块段数据

Source code in dimine_python_sdk\lib\prospecting\block_data.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class DmBlockData:
    """
    二次封装 dmBlockDataNew 类,提供 Python 化的块段模型操作接口
    职责:加载块段模型文件、获取模型属性、遍历块段数据
    """
    def __init__(self):
        # 初始化底层块段模型对象
        self._block_data = Dm.dmBlockDataNew()
        # 标记模型是否已成功加载
        self._is_open = False

    def open(self, file_path: str) -> bool:
        """
        加载块段模型文件
        :param file_path: 块段模型文件路径(自动处理编码转换)
        :raises RuntimeError: 加载失败时抛出异常

        :result: 是否成功加载模型

        example:
        ```python
        block_data = DmBlockData()
        success = block_data.open("model.dmf")
        if success:
            print("模型加载成功")
        else:
            print("模型加载失败")
        ```
        """
        result = self._block_data.Open(file_path)
        if not result:
            raise RuntimeError(f"加载块段模型失败: {file_path}")
        self._is_open = True
        return result

    def _check_open(self) -> None:
        """内部校验:确保模型已成功加载"""
        if not self._is_open:
            raise RuntimeError("请先调用 open() 加载块段模型")

    def get_origin(self) -> DmDPoint:
        """
        获取模型原点坐标 (x, y, z)
        :return: 模型原点坐标点对象
        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        origin = block_data.get_origin()
        print(f"模型原点坐标: {origin}")
        ```
        """
        self._check_open()
        cpp_result = self._block_data.GetOrigin()
        return DmDPoint._from_obj(cpp_result)

    def get_xyz_length(self) -> List[float]:
        """
        获取模型在 X/Y/Z 三个方向的总长度
        :return: 包含 X/Y/Z 方向总长度的列表 [x_len, y_len, z_len]

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        xyz_len = block_data.get_xyz_length()
        print(f"模型在 X/Y/Z 方向的总长度: {xyz_len}")
        ```
        """
        self._check_open()
        return self._block_data.GetXYZLen()

    def get_max_level(self) -> int:
        """
        获取模型的最大精度层级
        :return: 模型的最大精度层级整数

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        max_level = block_data.get_max_level()
        print(f"模型最大精度层级: {max_level}")
        ```
        """
        self._check_open()
        return self._block_data.GetMaxLevel()

    def get_min_size(self) -> List[float]:
        """
        获取模型最小块段的尺寸 (x, y, z)
        :return: 包含最小块段尺寸的列表 [x_size, y_size, z_size]

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        min_size = block_data.get_min_size()
        print(f"模型最小块段尺寸: {min_size}")
        ```
        """
        self._check_open()
        return self._block_data.GetMinSize()

    def get_rotation(self) -> List[float]:
        """
        获取模型的旋转角度 (x, y, z)
        :return: 包含旋转角度的列表 [x_angle, y_angle, z_angle]

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        rotation = block_data.get_rotation()
        print(f"模型旋转角度: {rotation}")
        ```
        """
        self._check_open()
        return self._block_data.GetRotation()

    def get_field_definitions(self) -> List[Dict[str, Any]]:
        """
        获取模型的字段定义列表,每个字段包含 name/type/size 信息
        :return: 包含字段定义的列表,每个元素为字典 {"name": str, "type": str, "size": int}

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        field_defs = block_data.get_field_definitions()
        print(f"模型字段定义: {field_defs}")
        ```
        """
        self._check_open()
        return self._block_data.GetFieldDefine()

    def get_total_block_count(self) -> int:
        """
        获取模型中块段的总数量
        :return: 模型中块段的总数量整数

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        total_count = block_data.get_total_block_count()
        print(f"模型中块段总数量: {total_count}")
        ```
        """
        self._check_open()
        return self._block_data.GetTotalBlockCount()

    def init_cursor(self) -> int:
        """
        初始化块段遍历游标,用于遍历所有块段
        :return: 初始化结果整数(0 表示成功)

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        cursor = block_data.init_cursor()
        print(f"初始化游标结果: {cursor}")
        ```
        """
        self._check_open()
        return self._block_data.InitCursor()

    def advance_cursor(self) -> bool:
        """将游标移动到下一个块段
        :return: 是否成功移动(False 表示已遍历结束)

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        cursor = block_data.init_cursor()
        while block_data.advance_cursor():
            # 处理当前块段数据
            pass
        ```
        """
        self._check_open()
        return self._block_data.AdvCursor()

    def get_cursor_data(self, field_name: str) -> Tuple[bool, str]:
        """获取当前游标指向块段的指定字段值
        :param field_name: 字段名称
        :return: (是否成功, 字段值JSON字符串)

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        cursor = block_data.init_cursor()
        while block_data.advance_cursor():
            success, data = block_data.get_cursor_data("field_name")
            if success:
                print(f"当前块段字段值: {data}")
        ```
        """
        self._check_open()
        return self._block_data.GetCursorData(field_name)

    def is_beyond_boundary(self) -> bool:
        """
        检查当前游标是否超出模型边界
        :return: 是否超出边界(True/False)

        example:
        ```python
        block_data = DmBlockData()
        block_data.open("model.dmf")
        cursor = block_data.init_cursor()
        while block_data.advance_cursor():
            if block_data.is_beyond_boundary():
                print("游标超出模型边界")
                break
        ```
        """
        self._check_open()
        return self._block_data.IsBeyondBoundaryNode()

    # 上下文管理器,支持 with 语句
    # def __enter__(self) -> "BlockModelCore":
    #     return self
    #
    # def __exit__(self, exc_type, exc_val, exc_tb) -> None:
    #     """资源释放逻辑"""
    #     self._is_open = False
    @staticmethod
    def create_block_model(params: ModelTransformParams) -> dict:
        """
        创建空块段模型
        :param params: Python字典,包含样长组合所需的参数
        :return: 处理结果,成功返回{"state": "ok", "message": "..."},失败抛出异常

        example:
        ```python
        params = ModelTransformParams(
        file_name: "123",
        # 原点
        origin_x = 3.0,origin_y = 3.0,origin_z = 3.0,
        # 旋转角度
        rotate_x = 0.0,rotate_y = 0.0,rotate_z = 0.0,
        # 单元块尺寸
        size_x = 1.0,size_y = 1.0,size_z = 1.0,
        # 延伸长度
        length_x = 10.0,length_y = 10.0,length_z = 10.0,
        )
        response = DmBlockData.create_block_model(params)
        print(f"创建空块段模型结果: {response}")
        ```
        """
        json_param = json.dumps(params.to_dict(), ensure_ascii=False)
        raw_response = Dm.CreateBlockModel(json_param)
        response = json.loads(raw_response)
        if response["state"] == "failed":
            raise RuntimeError(f"样长组合失败: {response['message']}")
        return response

    @staticmethod
    def block_constrain_to_dmc_file(block_model_file: str,constrain_json_param:BlockModelConstraint,constrain_result_file:str) -> bool:
        """
        块约束保存到约束结果dmc文件
        :param block_model_file:
        :param constrain_json_param:
        :param constrain_result_file:
        :return: 处理结果,返回True/False

        example:
        ```python
        params = BlockModelConstraint(
        type = 0,file = "model.dmf",range = 0,inside_level = 0,boundary_level = 0,bool_operate = "and",
        )
        response = DmBlockData.block_constrain_to_dmc_file("model.dmf",params,"constrain.dmc")
        print(f"块约束保存到约束结果dmc文件结果: {response}")
        ```
        """
        json_param = json.dumps(constrain_json_param.to_dict(), ensure_ascii=False)
        raw_response = Dm.BlockConstrainToDmcFile(block_model_file,json_param,constrain_result_file)
        return raw_response

advance_cursor()

将游标移动到下一个块段 :return: 是否成功移动(False 表示已遍历结束)

example:

block_data = DmBlockData()
block_data.open("model.dmf")
cursor = block_data.init_cursor()
while block_data.advance_cursor():
    # 处理当前块段数据
    pass
Source code in dimine_python_sdk\lib\prospecting\block_data.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def advance_cursor(self) -> bool:
    """将游标移动到下一个块段
    :return: 是否成功移动(False 表示已遍历结束)

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    cursor = block_data.init_cursor()
    while block_data.advance_cursor():
        # 处理当前块段数据
        pass
    ```
    """
    self._check_open()
    return self._block_data.AdvCursor()

block_constrain_to_dmc_file(block_model_file, constrain_json_param, constrain_result_file) staticmethod

块约束保存到约束结果dmc文件 :param block_model_file: :param constrain_json_param: :param constrain_result_file: :return: 处理结果,返回True/False

example:

params = BlockModelConstraint(
type = 0,file = "model.dmf",range = 0,inside_level = 0,boundary_level = 0,bool_operate = "and",
)
response = DmBlockData.block_constrain_to_dmc_file("model.dmf",params,"constrain.dmc")
print(f"块约束保存到约束结果dmc文件结果: {response}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@staticmethod
def block_constrain_to_dmc_file(block_model_file: str,constrain_json_param:BlockModelConstraint,constrain_result_file:str) -> bool:
    """
    块约束保存到约束结果dmc文件
    :param block_model_file:
    :param constrain_json_param:
    :param constrain_result_file:
    :return: 处理结果,返回True/False

    example:
    ```python
    params = BlockModelConstraint(
    type = 0,file = "model.dmf",range = 0,inside_level = 0,boundary_level = 0,bool_operate = "and",
    )
    response = DmBlockData.block_constrain_to_dmc_file("model.dmf",params,"constrain.dmc")
    print(f"块约束保存到约束结果dmc文件结果: {response}")
    ```
    """
    json_param = json.dumps(constrain_json_param.to_dict(), ensure_ascii=False)
    raw_response = Dm.BlockConstrainToDmcFile(block_model_file,json_param,constrain_result_file)
    return raw_response

create_block_model(params) staticmethod

创建空块段模型 :param params: Python字典,包含样长组合所需的参数 :return: 处理结果,成功返回{"state": "ok", "message": "..."},失败抛出异常

example:

params = ModelTransformParams(
file_name: "123",
# 原点
origin_x = 3.0,origin_y = 3.0,origin_z = 3.0,
# 旋转角度
rotate_x = 0.0,rotate_y = 0.0,rotate_z = 0.0,
# 单元块尺寸
size_x = 1.0,size_y = 1.0,size_z = 1.0,
# 延伸长度
length_x = 10.0,length_y = 10.0,length_z = 10.0,
)
response = DmBlockData.create_block_model(params)
print(f"创建空块段模型结果: {response}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
@staticmethod
def create_block_model(params: ModelTransformParams) -> dict:
    """
    创建空块段模型
    :param params: Python字典,包含样长组合所需的参数
    :return: 处理结果,成功返回{"state": "ok", "message": "..."},失败抛出异常

    example:
    ```python
    params = ModelTransformParams(
    file_name: "123",
    # 原点
    origin_x = 3.0,origin_y = 3.0,origin_z = 3.0,
    # 旋转角度
    rotate_x = 0.0,rotate_y = 0.0,rotate_z = 0.0,
    # 单元块尺寸
    size_x = 1.0,size_y = 1.0,size_z = 1.0,
    # 延伸长度
    length_x = 10.0,length_y = 10.0,length_z = 10.0,
    )
    response = DmBlockData.create_block_model(params)
    print(f"创建空块段模型结果: {response}")
    ```
    """
    json_param = json.dumps(params.to_dict(), ensure_ascii=False)
    raw_response = Dm.CreateBlockModel(json_param)
    response = json.loads(raw_response)
    if response["state"] == "failed":
        raise RuntimeError(f"样长组合失败: {response['message']}")
    return response

get_cursor_data(field_name)

获取当前游标指向块段的指定字段值 :param field_name: 字段名称 :return: (是否成功, 字段值JSON字符串)

example:

block_data = DmBlockData()
block_data.open("model.dmf")
cursor = block_data.init_cursor()
while block_data.advance_cursor():
    success, data = block_data.get_cursor_data("field_name")
    if success:
        print(f"当前块段字段值: {data}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def get_cursor_data(self, field_name: str) -> Tuple[bool, str]:
    """获取当前游标指向块段的指定字段值
    :param field_name: 字段名称
    :return: (是否成功, 字段值JSON字符串)

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    cursor = block_data.init_cursor()
    while block_data.advance_cursor():
        success, data = block_data.get_cursor_data("field_name")
        if success:
            print(f"当前块段字段值: {data}")
    ```
    """
    self._check_open()
    return self._block_data.GetCursorData(field_name)

get_field_definitions()

获取模型的字段定义列表,每个字段包含 name/type/size 信息 :return: 包含字段定义的列表,每个元素为字典 {"name": str, "type": str, "size": int}

example:

block_data = DmBlockData()
block_data.open("model.dmf")
field_defs = block_data.get_field_definitions()
print(f"模型字段定义: {field_defs}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def get_field_definitions(self) -> List[Dict[str, Any]]:
    """
    获取模型的字段定义列表,每个字段包含 name/type/size 信息
    :return: 包含字段定义的列表,每个元素为字典 {"name": str, "type": str, "size": int}

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    field_defs = block_data.get_field_definitions()
    print(f"模型字段定义: {field_defs}")
    ```
    """
    self._check_open()
    return self._block_data.GetFieldDefine()

get_max_level()

获取模型的最大精度层级 :return: 模型的最大精度层级整数

example:

block_data = DmBlockData()
block_data.open("model.dmf")
max_level = block_data.get_max_level()
print(f"模型最大精度层级: {max_level}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def get_max_level(self) -> int:
    """
    获取模型的最大精度层级
    :return: 模型的最大精度层级整数

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    max_level = block_data.get_max_level()
    print(f"模型最大精度层级: {max_level}")
    ```
    """
    self._check_open()
    return self._block_data.GetMaxLevel()

get_min_size()

获取模型最小块段的尺寸 (x, y, z) :return: 包含最小块段尺寸的列表 [x_size, y_size, z_size]

example:

block_data = DmBlockData()
block_data.open("model.dmf")
min_size = block_data.get_min_size()
print(f"模型最小块段尺寸: {min_size}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_min_size(self) -> List[float]:
    """
    获取模型最小块段的尺寸 (x, y, z)
    :return: 包含最小块段尺寸的列表 [x_size, y_size, z_size]

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    min_size = block_data.get_min_size()
    print(f"模型最小块段尺寸: {min_size}")
    ```
    """
    self._check_open()
    return self._block_data.GetMinSize()

get_origin()

获取模型原点坐标 (x, y, z) :return: 模型原点坐标点对象 example:

block_data = DmBlockData()
block_data.open("model.dmf")
origin = block_data.get_origin()
print(f"模型原点坐标: {origin}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def get_origin(self) -> DmDPoint:
    """
    获取模型原点坐标 (x, y, z)
    :return: 模型原点坐标点对象
    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    origin = block_data.get_origin()
    print(f"模型原点坐标: {origin}")
    ```
    """
    self._check_open()
    cpp_result = self._block_data.GetOrigin()
    return DmDPoint._from_obj(cpp_result)

get_rotation()

获取模型的旋转角度 (x, y, z) :return: 包含旋转角度的列表 [x_angle, y_angle, z_angle]

example:

block_data = DmBlockData()
block_data.open("model.dmf")
rotation = block_data.get_rotation()
print(f"模型旋转角度: {rotation}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def get_rotation(self) -> List[float]:
    """
    获取模型的旋转角度 (x, y, z)
    :return: 包含旋转角度的列表 [x_angle, y_angle, z_angle]

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    rotation = block_data.get_rotation()
    print(f"模型旋转角度: {rotation}")
    ```
    """
    self._check_open()
    return self._block_data.GetRotation()

get_total_block_count()

获取模型中块段的总数量 :return: 模型中块段的总数量整数

example:

block_data = DmBlockData()
block_data.open("model.dmf")
total_count = block_data.get_total_block_count()
print(f"模型中块段总数量: {total_count}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def get_total_block_count(self) -> int:
    """
    获取模型中块段的总数量
    :return: 模型中块段的总数量整数

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    total_count = block_data.get_total_block_count()
    print(f"模型中块段总数量: {total_count}")
    ```
    """
    self._check_open()
    return self._block_data.GetTotalBlockCount()

get_xyz_length()

获取模型在 X/Y/Z 三个方向的总长度 :return: 包含 X/Y/Z 方向总长度的列表 [x_len, y_len, z_len]

example:

block_data = DmBlockData()
block_data.open("model.dmf")
xyz_len = block_data.get_xyz_length()
print(f"模型在 X/Y/Z 方向的总长度: {xyz_len}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def get_xyz_length(self) -> List[float]:
    """
    获取模型在 X/Y/Z 三个方向的总长度
    :return: 包含 X/Y/Z 方向总长度的列表 [x_len, y_len, z_len]

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    xyz_len = block_data.get_xyz_length()
    print(f"模型在 X/Y/Z 方向的总长度: {xyz_len}")
    ```
    """
    self._check_open()
    return self._block_data.GetXYZLen()

init_cursor()

初始化块段遍历游标,用于遍历所有块段 :return: 初始化结果整数(0 表示成功)

example:

block_data = DmBlockData()
block_data.open("model.dmf")
cursor = block_data.init_cursor()
print(f"初始化游标结果: {cursor}")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def init_cursor(self) -> int:
    """
    初始化块段遍历游标,用于遍历所有块段
    :return: 初始化结果整数(0 表示成功)

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    cursor = block_data.init_cursor()
    print(f"初始化游标结果: {cursor}")
    ```
    """
    self._check_open()
    return self._block_data.InitCursor()

is_beyond_boundary()

检查当前游标是否超出模型边界 :return: 是否超出边界(True/False)

example:

block_data = DmBlockData()
block_data.open("model.dmf")
cursor = block_data.init_cursor()
while block_data.advance_cursor():
    if block_data.is_beyond_boundary():
        print("游标超出模型边界")
        break
Source code in dimine_python_sdk\lib\prospecting\block_data.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def is_beyond_boundary(self) -> bool:
    """
    检查当前游标是否超出模型边界
    :return: 是否超出边界(True/False)

    example:
    ```python
    block_data = DmBlockData()
    block_data.open("model.dmf")
    cursor = block_data.init_cursor()
    while block_data.advance_cursor():
        if block_data.is_beyond_boundary():
            print("游标超出模型边界")
            break
    ```
    """
    self._check_open()
    return self._block_data.IsBeyondBoundaryNode()

open(file_path)

加载块段模型文件 :param file_path: 块段模型文件路径(自动处理编码转换) :raises RuntimeError: 加载失败时抛出异常

:result: 是否成功加载模型

example:

block_data = DmBlockData()
success = block_data.open("model.dmf")
if success:
    print("模型加载成功")
else:
    print("模型加载失败")
Source code in dimine_python_sdk\lib\prospecting\block_data.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def open(self, file_path: str) -> bool:
    """
    加载块段模型文件
    :param file_path: 块段模型文件路径(自动处理编码转换)
    :raises RuntimeError: 加载失败时抛出异常

    :result: 是否成功加载模型

    example:
    ```python
    block_data = DmBlockData()
    success = block_data.open("model.dmf")
    if success:
        print("模型加载成功")
    else:
        print("模型加载失败")
    ```
    """
    result = self._block_data.Open(file_path)
    if not result:
        raise RuntimeError(f"加载块段模型失败: {file_path}")
    self._is_open = True
    return result