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
from OCC.Display.SimpleGui import init_display
from OCC.Display.OCCViewer import rgb_color
# 在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
if __name__ == "__main__":
display, start_display, add_menu, add_function_to_menu = init_display()
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()
offset_distance2 = 2
offset_tool2 = BRepOffsetAPI_MakeOffset()
offset_tool2.AddWire(aRectangle)
offset_tool2.Perform(offset_distance2)
offset_shape2 = offset_tool2.Shape()
offset_distance3 = -1
offset_tool3 = BRepOffsetAPI_MakeOffset()
offset_tool3.AddWire(aRectangle)
offset_tool3.Perform(offset_distance3)
offset_shape3 = offset_tool3.Shape()
display.DisplayShape(aRectangle, update=True)
display.DisplayShape(offset_shape1, update=True, color=rgb_color(0, 0, 1)) # 偏移后的草图蓝色
display.DisplayShape(offset_shape2, update=True, color=rgb_color(0, 0, 1)) # 偏移后的草图蓝色
display.DisplayShape(offset_shape3, update=True, color=rgb_color(0, 0, 1)) # 偏移后的草图蓝色
start_display()
关联:判定wire 是offset关系