交的概念是两个东西的公共部分,而并的概念是两个东西加起来,减的概念是从A中减去B,用集合的语言就是,交就是,并就是,减就是A-B。在Pythonocc里,交,并,减相对应的是common,fuse和cut,下面附上代码。
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Fuse, BRepAlgoAPI_Common
from OCC.Display.SimpleGui import init_display
def GetRectShape(x0, x1, z0, z1, aPlane):
# ZOX plane
# width is from x0 to x1, and height is from z0 to z1.
aP1 = gp_Pnt(x0, 0.0, z0)
aP2 = gp_Pnt(x1, 0.0, z0)
aP3 = gp_Pnt(x1, 0.0, z1)
aP4 = gp_Pnt(x0, 0.0, z1)
aPolygon = BRepBuilderAPI_MakePolygon(aP1, aP2, aP3, aP4, True)
return BRepBuilderAPI_MakeFace(aPlane, aPolygon.Wire()).Shape()
def GetTrapezoidShape(x0, x1, x2, x3, z0, z1, aPlane):
# ZOX plane
# x0 is bottom left, x1 is bottom is right, x2 is top right, x3 is top left
# z0 is bottom, z1 is top
aP1 = gp_Pnt(x0, 0.0, z0)
aP2 = gp_Pnt(x1, 0.0, z0)
aP3 = gp_Pnt(x2, 0.0, z1)
aP4 = gp_Pnt(x3, 0.0, z1)
aPolygon = BRepBuilderAPI_MakePolygon(aP1, aP2, aP3, aP4, True)
return BRepBuilderAPI_MakeFace(aPlane, aPolygon.Wire()).Shape()
# ZOX平面
p0 = gp_Pnt()
vnorm = gp_Dir(0, 1, 0)
aPlane = gp_Pln(p0, vnorm)
x0 = -0.5 * 100
x1 = 0.5 * 100
z0 = 0
z1 = 10
aRect = GetRectShape(x0, x1, z0, z1, aPlane)
x2 = 0.5 * 50
x3 = -0.5 * 50
z1 = 5
aPolygon = GetTrapezoidShape(x0, x1, x2, x3, z0, z1, aPlane)
Jiao = BRepAlgoAPI_Common(aRect, aPolygon).Shape() #
Bing = BRepAlgoAPI_Fuse(aRect, aPolygon).Shape() #
Jian = BRepAlgoAPI_Cut(aRect, aPolygon).Shape() # aRect- aPolygon
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(Jiao , update=True)
display.FitAll()
start_display()