天天看点

pythonOCC各种实用的方法

  1. 混合多个模型为一个模型
def fix_shape(*args):
    if len(args) == 0:
        return None
    fix_shape = None
    for shape in args:
        if fix_shape == None:
            fix_shape = shape
            continue
        fix_shape = BRepAlgoAPI_Fuse(fix_shape.Shape(), shape.Shape())
    return fix_shape
           
  1. 判断是否为平面
def face_is_plane(face):
    # 如果是平面,则返回True
    hs = BRep_Tool_Surface(face)
    downcast_result = Geom_Plane.DownCast(hs)
    if downcast_result is None:
        return False
    else:
        return True
           
  1. 挖空三维模型
# 挖空三维体,针对圆锥以及面在底部的图形
def scoop(shape, thickness):
    faceToRemove = None
    zMax = 1
    aFaceExplorer = TopExp_Explorer(shape, TopAbs_FACE)
    while aFaceExplorer.More():
        aFace = topods.Face(aFaceExplorer.Current())
        if face_is_plane(aFace):
            aPlane = surface_plane_from_face(aFace)
            aPnt = aPlane.Location()
            aZ = aPnt.Z()
            if aZ < zMax:
                zMax = aZ
                faceToRemove = aFace
        aFaceExplorer.Next()
    facesToRemove = TopTools_ListOfShape()
    facesToRemove.Append(faceToRemove)
    # 第一个参数是shape,第二个是要删除的面的集合,第三个是变化后的厚度,第四个是偏差
    scoopShape = BRepOffsetAPI_MakeThickSolid(shape, facesToRemove, thickness, 0.001)
    return scoopShape
           
  1. 倒圆角(所有边)
def set_fillet(shape, thickness):
    mkFillet = BRepFilletAPI_MakeFillet(shape)
    anEdgeExploer = TopExp_Explorer(shape, TopAbs_EDGE)
    while anEdgeExploer.More():
        anEdge = topods.Edge(anEdgeExploer.Current())
        mkFillet.Add(thickness, anEdge)
        anEdgeExploer.Next()
    return mkFillet
           
  1. 二维曲线生成
def create_curve(pnt_list, close=False):
    points = TColgp_HArray1OfPnt(1, len(pnt_list))
    for i in range(1, len(pnt_list) + 1):
        points.SetValue(i, pnt_list[i - 1])
    # 第二个参数为是否闭曲线
    interp = GeomAPI_Interpolate(points, close, 0.0001)
    interp.Perform()
    return interp
           
  1. 二维直线生成
# 二维直线生成
def create_segment(pnt_list, close=False):
    if len(pnt_list) == 1:
        return None

    wire = BRepBuilderAPI_MakeWire()
    for i in range(0, len(pnt_list)):
        if i == len(pnt_list) - 1 and close:
            segment = BRepBuilderAPI_MakeEdge(pnt_list[0], pnt_list[len(pnt_list) - 1])
        elif i != len(pnt_list) - 1:
            segment = BRepBuilderAPI_MakeEdge(pnt_list[i], pnt_list[i + 1])
        else:
            continue
        wire.Add(segment.Edge())
    return wire
           
  1. 拼接线
# 拼接线(直线、曲线)
def joint_wire(*args):
    try:
        wire = BRepBuilderAPI_MakeWire(args[0].Wire())
    except AttributeError:
        wire = BRepBuilderAPI_MakeWire(BRepBuilderAPI_MakeEdge(args[0]).Edge())
    for i in range(1, len(args)):
        wire.Add(args[i].Wire())
    return wire
           
  1. 生成平面
# 曲线生成面
def curve_to_face(wire):
    return BRepBuilderAPI_MakeFace(wire)
           
  1. 平面绕轴旋转生成模型
# 二维绕Z轴或指定射线旋转生成形状(实体)
def revol_shape(shape, **kwargs):
    if len(kwargs) == 0:
        return BRepPrimAPI_MakeRevol(shape.Shape(), gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)))
    return BRepPrimAPI_MakeRevol(shape.Shape(), kwargs["ax1"])
           
  1. 平面向量拉伸生成模型
# 向量拉伸
def stretch_plane(shape, **kwargs):
    if len(kwargs) == 0:
        return BRepPrimAPI_MakePrism(shape.Shape(), gp_Vec(0, 0, 2))
    return BRepPrimAPI_MakePrism(shape.Shape(), kwargs["vec"])
           
  1. 各种三维模型构建
# 球体
def create_sphere(radius, ax2=gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))):
    return BRepPrimAPI_MakeSphere(ax2, radius)


# 方体
def create_box(l, w, h, ax2=gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))):
    return BRepPrimAPI_MakeBox(ax2, l, w, h)


# 圆柱体
def create_cylinder(r, h, ax2=gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))):
    return BRepPrimAPI_MakeCylinder(ax2, r, h)


# 圆锥体
def create_cone(r1, r2, h, ax2=gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))):
    return BRepPrimAPI_MakeCone(ax2, r1, r2, h)


# 圆环
def create_torus(r1, r2, ax2=gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1))):
    return BRepPrimAPI_MakeTorus(ax2, r1, r2)