from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pln
from OCC.Core.GC import GC_MakeSegment
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge,BRepBuilderAPI_MakePolygon, BRepBuilderAPI_MakeFace
from OCC.Core.BRepGProp import brepgprop_LinearProperties, brepgprop_SurfaceProperties, brepgprop_VolumeProperties
from OCC.Core.GProp import GProp_GProps
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
def GetTrapezoid2dShape(x0, x1, x2, x3, z0, z1, aPlane):
# ZOX plane
# x0 is bottom left, x1 is bottom 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()
def GetBoxShape(box_corner, width = 1.0, height = 2.0, depth = 3.0):
# width: 宽,dx height:长,dy depth:高,dz box_corner:box的左下角的点
box = BRepPrimAPI_MakeBox(box_corner, width, height, depth).Shape()
return box
if __name__ == '__main__':
aP1 = gp_Pnt(0, 0.0, 1)
aP2 = gp_Pnt(13, 0.0, 1)
aSegment12 = GC_MakeSegment(aP1, aP2)
aEdge12 = BRepBuilderAPI_MakeEdge(aSegment12.Value()).Edge()
system = GProp_GProps()
brepgprop_LinearProperties(aEdge12, system)
print("aEdge12.Mass(): ", system.Mass()) # 周长
p0 = gp_Pnt()
vnorm = gp_Dir(0, 1, 0)
aPlane = gp_Pln(p0, vnorm)
aTrapezoidShape = shape2d.GetTrapezoid2dShape(-15, 15, 5, -5, 0, 10, aPlane)
brepgprop_SurfaceProperties(aTrapezoidShape, system)
print("aTrapezoidShape.Mass(): ", system.Mass()) # 面积
aFilmShape = shape3d.GetBoxShape(gp_Pnt(-50, -50, 0), 100, 100, 1)
brepgprop_VolumeProperties(aFilmShape, system)
print("aTrapezoidShape.Mass(): ", system.Mass()) # 体积