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

如何在CadQuery中检测倒圆角或倒角边

检测CadQuery中STEP模型的倒圆角/倒角边

CadQuery本身依赖OpenCASCADE(OCC),可以通过访问对象的wrapped属性获取底层OCC实例,以此实现倒圆角和倒角的检测,无需单独引入PythonOCC包。

步骤与代码示例

  1. 读取STEP文件并获取目标边
import cadquery as cq

# 读取STEP模型
part = cq.importers.importStep("your_part.step")

# 获取目标边(可通过选择器或索引定位,此处以第一条边为例)
target_edge = part.edges().first()
  1. 提取底层OCC曲线对象
    利用OCC的工具函数从边中提取曲线:
from OCC.Core.BRep_Tool import BRep_Tool

# 获取CadQuery边对应的底层OCC边对象
occ_edge = target_edge.wrapped
# 提取曲线及参数范围
curve, _, _ = BRep_Tool.Curve(occ_edge)
  1. 判断边类型
  • 倒圆角的边曲线类型为Geom_Circle
  • 倒角的边通常为Geom_Line,且连接的两个相邻面存在特定夹角(如常见的90°倒角)
from OCC.Core.Geom import Geom_Circle, Geom_Line
from OCC.Core.BRepGProp import BRepGProp_SurfaceProperties
from math import pi

# 检测倒圆角
if curve.IsInstance(Geom_Circle.GetObjectType()):
    print("该边为倒圆角")
# 检测倒角
elif curve.IsInstance(Geom_Line.GetObjectType()):
    # 获取边的相邻面
    adjacent_faces = target_edge.faces()
    if len(adjacent_faces) == 2:
        # 计算两个相邻面的法向量夹角
        face1_occ = adjacent_faces[0].wrapped
        face2_occ = adjacent_faces[1].wrapped
        
        prop1 = BRepGProp_SurfaceProperties(face1_occ)
        norm1 = prop1.Normal()
        prop2 = BRepGProp_SurfaceProperties(face2_occ)
        norm2 = prop2.Normal()
        
        angle_rad = norm1.Angle(norm2)
        # 以90°倒角为例,允许微小误差
        if abs(angle_rad - pi/2) < 0.01:
            print("该边为倒角")
else:
    print("该边既非倒圆角也非倒角")

批量检测示例

如果需要批量检测所有边:

for edge in part.edges():
    occ_edge = edge.wrapped
    curve, _, _ = BRep_Tool.Curve(occ_edge)
    
    if curve.IsInstance(Geom_Circle.GetObjectType()):
        print(f"边{edge}是倒圆角")
    elif curve.IsInstance(Geom_Line.GetObjectType()):
        adjacent_faces = edge.faces()
        if len(adjacent_faces) == 2:
            face1_occ = adjacent_faces[0].wrapped
            face2_occ = adjacent_faces[1].wrapped
            prop1 = BRepGProp_SurfaceProperties(face1_occ)
            norm1 = prop1.Normal()
            prop2 = BRepGProp_SurfaceProperties(face2_occ)
            norm2 = prop2.Normal()
            angle_rad = norm1.Angle(norm2)
            if abs(angle_rad - pi/2) < 0.01:
                print(f"边{edge}是倒角")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 23:12:32