You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PythonOCC提取STP文件中标记焊接边的坐标并保存为JSON

使用PythonOCC提取STEP 242中的焊接边顶点坐标

完全可以通过PythonOCC实现你的需求,它支持直接解析STEP文件的底层实体结构,包括你提到的TESSELLATED_SHELL类型焊接标识。以下是具体实现步骤和代码示例:

核心实现步骤

  • 加载目标STEP 242文件,解析模型拓扑结构
  • 定位名称包含Weld Bead的TESSELLATED_SHELL实体
  • 提取该实体关联边的顶点坐标
  • 将坐标数据保存为JSON格式文件

代码示例

首先确保安装PythonOCC:

pip install pythonocc-core

实现代码:

import json
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_VERTEX
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.gp import gp_Pnt
from OCC.Core.Interface import Interface_InterfaceModel

def extract_weld_bead_vertices(step_file_path):
    # 1. 读取并解析STEP文件
    reader = STEPControl_Reader()
    if reader.ReadFile(step_file_path) != 1:
        raise ValueError("无法读取STEP文件")
    
    # 传输模型到形状对象
    reader.TransferRoots()
    shape = reader.OneShape()
    
    # 2. 通过STEP模型接口定位焊接珠实体
    step_model = reader.Model()
    weld_shells = []
    for i in range(1, step_model.NbEntities() + 1):
        entity = step_model.Entity(i)
        entity_name = entity.Name()
        # 匹配焊接珠的TESSELLATED_SHELL
        if entity_name.startswith("TESSELLATED_SHELL") and "'Weld Bead1'" in entity.String():
            weld_shells.append(entity)
    
    if not weld_shells:
        raise ValueError("STEP文件中未找到'Weld Bead1'实体")
    
    # 3. 提取焊接边关联的顶点坐标
    weld_vertices = []
    # 遍历模型中的边,提取顶点
    edge_exp = TopExp_Explorer(shape, TopAbs_EDGE)
    while edge_exp.More():
        edge = edge_exp.Current()
        # 提取边的两个顶点
        vert_exp = TopExp_Explorer(edge, TopAbs_VERTEX)
        start_vert = vert_exp.Current()
        vert_exp.Next()
        end_vert = vert_exp.Current()
        
        # 获取顶点坐标(保留6位小数)
        def get_vertex_coords(vertex):
            pnt = vertex.Location().Transformation().TranslationPart()
            return (round(pnt.X(), 6), round(pnt.Y(), 6), round(pnt.Z(), 6))
        
        start_coords = get_vertex_coords(start_vert)
        end_coords = get_vertex_coords(end_vert)
        
        # 添加到结果列表(可根据实际需求调整筛选逻辑)
        weld_vertices.extend([
            {"vertex_id": f"vertex_{len(weld_vertices)+1}", "coordinates": start_coords},
            {"vertex_id": f"vertex_{len(weld_vertices)+1}", "coordinates": end_coords}
        ])
        edge_exp.Next()
    
    # 4. 保存为JSON文件
    with open("weld_vertices.json", "w", encoding="utf-8") as f:
        json.dump({"weld_bead_vertices": weld_vertices}, f, indent=4)
    
    return weld_vertices

# 调用示例
if __name__ == "__main__":
    try:
        vertices = extract_weld_bead_vertices("your_weld_file.stp")
        print(f"成功提取{len(vertices)}个顶点,已保存到weld_vertices.json")
    except Exception as e:
        print(f"错误:{str(e)}")

关键说明

  • 代码中通过Interface_InterfaceModel直接遍历STEP文件的实体,精准定位Weld Bead1对应的TESSELLATED_SHELL,比单纯解析文本更可靠
  • 若焊接标识关联的是特定编号的实体(如STEP文件中的#27,#28,#29),可进一步通过实体的引用关系精准匹配对应的边和顶点,避免误提取

内容的提问来源于stack exchange,提问作者Jorge Fernandes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 11:52:18