先验知识
对以下三个TopoDS_Edge
,TopoDS_Wire
,Geom_Curve
三种类型,前两者为拓扑类型后一种为几何类型,三种类型互转:
- BRepBuilderAPI_MakeWire(edge):实现从edge到wire的转变。一般一个wire有多条edge
- BRep_Tool.Curve(edge):实现从edge到curve的改变,curve一般用于后续的几何运算
- TopExp_Explorer(Wire, TopAbs_EDGE),使wire转edge,一般来说会得到多个edge
from OCC.Core.BRep import BRep_Tool
from OCC.Core.GeomAPI import GeomAPI_ExtremaCurveCurve
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_DX, gp_Circ, gp_Elips
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_MakeOffset
import OCC.Core.Geom2dAPI as Geom2dAPI
from OCC.Display.SimpleGui import init_display
from OCC.Display.OCCViewer import rgb_color
import OCC.Core.TopoDS as TopoDS
import OCC.Core.BRepBuilderAPI as BRepBuilderAPI
import OCC.Core.GeomConvert as GeomConvert
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_FACE, TopAbs_WIRE,TopAbs_EDGE
from OCC.Core.TopoDS import topods
# 在yz平面上通过一顶点及长宽绘制矩形
def create_rounded_rectangle(coor0, Length, Width, radius):
yPlus = [0, Length, 0]
zPlus = [0, 0, Width]
coor1 = [coor0[i] + yPlus[i] for i in range(3)]
coor2 = [coor0[i] + zPlus[i] + yPlus[i] for i in range(3)]
coor3 = [coor0[i] + zPlus[i] for i in range(3)]
P0 = gp_Pnt(coor0[0], coor0[1], coor0[2])
P1 = gp_Pnt(coor1[0], coor1[1], coor1[2])
P2 = gp_Pnt(coor2[0], coor2[1], coor2[2])
P3 = gp_Pnt(coor3[0], coor3[1], coor3[2])
aSegment1 = GC_MakeSegment(P0, P1)
anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1.Value())
aWire1 = BRepBuilderAPI_MakeWire(anEdge1.Edge())
aSegment2 = GC_MakeSegment(P1, P2)
anEdge2 = BRepBuilderAPI_MakeEdge(aSegment2.Value())
aWire2 = BRepBuilderAPI_MakeWire(anEdge2.Edge())
aSegment3 = GC_MakeSegment(P2, P3)
anEdge3 = BRepBuilderAPI_MakeEdge(aSegment3.Value())
aWire3 = BRepBuilderAPI_MakeWire(anEdge3.Edge())
aSegment4 = GC_MakeSegment(P3, P0)
anEdge4 = BRepBuilderAPI_MakeEdge(aSegment4.Value())
aWire4 = BRepBuilderAPI_MakeWire(anEdge4.Edge())
aRectangle = BRepBuilderAPI_MakeWire(aWire1.Edge(), aWire2.Edge(), aWire3.Edge(), aWire4.Edge())
if radius > 0:
fillet = BRepOffsetAPI_MakeOffset(aRectangle.Wire())
fillet.Perform(radius)
aRectangle = fillet.Shape()
return aRectangle
coor0 = [0, 3, 3]
Length = 20
Width = 10
radius = 2
aRectangle = create_rounded_rectangle(coor0, Length, Width, radius)
#增加offset,>0为向外 offest,<0为向内offeset
offset_distance1 = 1
offset_tool1 = BRepOffsetAPI_MakeOffset()
offset_tool1.AddWire(aRectangle)
offset_tool1.Perform(offset_distance1)
offset_shape1 = offset_tool1.Shape()
if __name__ == "__main__":
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(aRectangle, update=True)
display.DisplayShape(offset_shape1, update=True, color=rgb_color(0, 0, 1)) # 偏移后的草图蓝色
# 逐一获取edge比对
edge_explorer1 = TopExp_Explorer(aRectangle, TopAbs_EDGE)
curve1 =[]
while edge_explorer1.More():
edge = topods.Edge(edge_explorer1.Current())
curve = BRep_Tool.Curve(edge)[0]
curve1.append(curve)
edge_explorer1.Next()
edge_explorer2 = TopExp_Explorer(offset_shape1, TopAbs_EDGE)
curve2 = []
while edge_explorer2.More():
edge = topods.Edge(edge_explorer2.Current())
curve = BRep_Tool.Curve(edge)[0]
curve2.append(curve)
edge_explorer2.Next()
# 识别aRectangle和offset_shape1之间的距离
# 使用 Geom2dAPI_ExtremaCurveCurve 计算曲线之间的最近距离
extrema_calculator = GeomAPI_ExtremaCurveCurve(curve1[0],curve2[0])
min_distance=extrema_calculator.LowerDistance()
# 输出结果
print("最小距离:",min_distance)
start_display()