天天看點

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)