直线圆角
类似于Abaqus草图中的Create Fillet操作。
ChFi2d_AnaFilletAlgo
:给两条直线做 2D 圆角,使用步骤:
- 初始化,两条相交的边+所在的平面
f = ChFi2d_AnaFilletAlgo()
f.Init(edge1, edge2, plane)
- 执行圆角
f.Perform(radius) # 指定圆弧半径
arc = f.Result(edge1, edge2)
from OCC.Core.gp import gp_Pnt, gp_Pln, gp_Dir
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
from OCC.Core.ChFi2d import ChFi2d_AnaFilletAlgo
from OCC.Display.SimpleGui import init_display
display, start_display, *_ = init_display()
# 工作平面(XY 平面)
pln = gp_Pln(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))
# 定义三点 & 边(两条线在 p2 相交)
p1 = gp_Pnt(0, 0, 0)
p2 = gp_Pnt(5, 5, 0)
p3 = gp_Pnt(-5, 5, 0)
ed1 = BRepBuilderAPI_MakeEdge(p3, p2).Edge()
ed2 = BRepBuilderAPI_MakeEdge(p2, p1).Edge()
# 用 ChFi2d_AnaFilletAlgo 计算圆角
fillet = ChFi2d_AnaFilletAlgo()
fillet.Init(ed1, ed2, pln)
fillet.Perform(1.0) # 半径=1
arc = fillet.Result(ed1, ed2)
display.DisplayShape(ed1, color="BLUE", update=False) # 原线1
display.DisplayShape(ed2, color="GREEN", update=False) # 原线2
display.DisplayShape(arc, color="RED", update=True) # 圆角弧
display.FitAll()
start_display()
三维实体圆角
BRepFilletAPI_MakeFillet
:在三维形体 (TopoDS_Shape)的边 (edges)上生成光顺的圆角倒圆处理。
构造方法:
from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet
mk = BRepFilletAPI_MakeFillet(shape, FShape=ChFi3d_FilletShape.ChFi3d_Rational)
- 参数
shape
输入的三维形体 (TopoDS_Shape
),必须是一个Solid/Shell,通常是BRepPrimAPI
构造的立方体、圆柱等。 - 参数
FShape
(可选,默认 Rational)
决定圆角表面的数学表示方式:ChFi3d_Rational
:用 Nurbs 表示圆角(默认)ChFi3d_QuasiAngular
:圆弧更接近真实圆,但仍然是 nurbsChFi3d_Polynomial
:多项式表示
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet
from OCC.Extend.TopologyUtils import TopologyExplorer
import OCC.Core.Quantity as Q
from OCC.Display.SimpleGui import init_display
display, start_display, *_ = init_display()
# 1) 做一个简单长方体(80 x 50 x 30)
box = BRepPrimAPI_MakeBox(80, 50, 30).Shape()
# 2) 创建 3D 圆角生成器,并把所有边都加到圆角集合
mk = BRepFilletAPI_MakeFillet(box)
for e in TopologyExplorer(box).edges(): # 遍历box的所有边
mk.Add(4.0, e) # 为每条边添加半径=4.0的圆角
# 3) 生成结果形体
filleted_box = mk.Shape()
# 4) 显示:原始盒子(半透明灰) + 圆角后的盒子(蓝色)
display.DisplayShape(box, color=Q.Quantity_NOC_GRAY31, transparency=0.7, update=False)
display.DisplayShape(filleted_box, color=Q.Quantity_NOC_BLUE1, transparency=0.1, update=False)
display.FitAll()
start_display()
上面的例子给整个box的所有边都进行了圆角处理,当然也可以指定边进行圆角。
指定边圆角
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.BRepFilletAPI import BRepFilletAPI_MakeFillet
from OCC.Extend.TopologyUtils import TopologyExplorer
from OCC.Display.SimpleGui import init_display
display, start_display, *_ = init_display()
box = BRepPrimAPI_MakeBox(80, 50, 30).Shape()
mk = BRepFilletAPI_MakeFillet(box)
# 把所有边取出来
edges = list(TopologyExplorer(box).edges())
# 只选第1和第5条边
mk.Add(5.0, edges[0])
mk.Add(5.0, edges[4])
filleted = mk.Shape()
display.DisplayShape(filleted, update=True)
display.FitAll()
start_display()
根据几何特征筛选
只想给 Z 方向的竖直边 加圆角,就可以判断边的方向:
from OCC.Core.BRep import BRep_Tool
from OCC.Core.gp import gp_Dir
edges = list(TopologyExplorer(box).edges())
for e in edges:
curve, first, last = BRep_Tool.Curve(e)
p1 = curve.Value(first)
p2 = curve.Value(last)
# 边的方向
dir_vec = gp_Dir(p2.X() - p1.X(), p2.Y() - p1.Y(), p2.Z() - p1.Z())
# 如果接近Z轴方向
if abs(dir_vec.Dot(gp_Dir(0,0,1))) > 0.9:
mk.Add(5.0, e)