点线面体pythonocc

越麒
2023-12-01
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire, BRepBuilderAPI_MakeFace
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_MakePipe
from OCC.Core.AIS import AIS_Shape
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_ThruSections
from OCC.Core.gp import gp_Pnt
from OCC.Display.SimpleGui import init_display
from OCC.Core.GCE2d import GCE2d_MakeParabola
from OCC.Core.gp import gp_Pnt, gp_Ax2d, gp_Pnt2d, gp_Dir2d
from OCC.Core.gp import (gp_Pnt, gp_OX, gp_Vec, gp_Trsf, gp_DZ, gp_Ax2, gp_Ax3,
                         gp_Pnt2d, gp_Dir2d, gp_Ax2d, gp_Pln)

from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut, BRepAlgoAPI_Fuse,BRepAlgoAPI_Common
from OCC.Core.BRepPrimAPI import (BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeCylinder,
                             BRepPrimAPI_MakeTorus, BRepPrimAPI_MakeRevol)

from OCC.Core.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire,
                                     BRepBuilderAPI_MakeFace, BRepBuilderAPI_Transform)
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_MakeThickSolid, BRepOffsetAPI_ThruSections

from OCC.Core.GeomAbs import GeomAbs_Plane
from OCC.Core.BRepAdaptor import BRepAdaptor_Surface
from OCC.Core.TopoDS import topods, TopoDS_Compound, TopoDS_Face
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.TopAbs import TopAbs_EDGE, TopAbs_FACE
from OCC.Core.TopTools import TopTools_ListOfShape
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.GCE2d import GCE2d_MakeHyperbola
from OCC.Core.gp import gp_Pnt, gp_Ax2d, gp_Pnt2d, gp_Dir2d
from OCC.Display.OCCViewer import rgb_color
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakePrism, BRepPrimAPI_MakeRevol
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Ax1, gp_Dir
from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, 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

def wire_banyuan():
    # 函数作用:通过圆心和半径和角度生成圆弧
    # 输入:圆心坐标,半径值,角度值
    # 输出:暂定只输出圆弧
    Location = gp_Pnt(0, 0, 0)
    Axis = gp_Dir(0, 0, -1)
    CircleAxis = gp_Ax2(Location, Axis)
    Circle = gp_Circ(CircleAxis, 5)
    ArcofCircle0 = GC_MakeArcOfCircle(Circle, 0 / 180 * 3.14, 180 / 180 * 3.14, True)
    ArcofCircle1 = BRepBuilderAPI_MakeEdge(ArcofCircle0.Value())
    ArcofCircle = BRepBuilderAPI_MakeWire(ArcofCircle1.Edge())
    display.DisplayShape(ArcofCircle.Edge(),update=True)

def wire_tuoyuan():
    # 函数作用:椭圆--通过半长轴和半短轴及原点绘制椭圆
    # 输入:原点坐标,半长轴值,半短轴值
    # 输出:暂定只输出椭圆
    Location = gp_Pnt(0, 0, 0)
    Axis = gp_Dir(0, 0, 1)
    ElipsAxis = gp_Ax2(Location, Axis)
    ElipsAxis0 = gp_Elips(ElipsAxis, 9, 3)
    ElipsAxis1 = BRepBuilderAPI_MakeEdge(ElipsAxis0)
    ElipsAxis = BRepBuilderAPI_MakeWire(ElipsAxis1.Edge())

    display.DisplayShape(ElipsAxis.Edge(), update=True)

def wire_shuangquxian():
    '''
    双曲线
    :return:
    '''
    MajorRadius = 5
    MinorRadius = 1
    hyperbola = GCE2d_MakeHyperbola(gp_Ax2d(gp_Pnt2d(0, 0.2), gp_Dir2d(0, 1)), MajorRadius, MinorRadius, False)
    display.DisplayShape(hyperbola.Value(), update=True)

def wire_paowuxian():
    '''
       抛物线
       :return:
       '''
    focal = 5
    parabola = GCE2d_MakeParabola(gp_Ax2d(gp_Pnt2d(0, 0), gp_Dir2d(0, 1)), focal, False)
    display.DisplayShape(parabola.Value(), update=True)

def wire__xianduan():
    # 函数作用:通过两点生成线段
    # 输入:两点
    # 输出:线段
    aSegment = GC_MakeSegment(gp_Pnt(1, 1, 1), gp_Pnt(1, 10, 1))
    anEdge = BRepBuilderAPI_MakeEdge(aSegment.Value())
    aWire = BRepBuilderAPI_MakeWire(anEdge.Edge())
def wire_juixng():
    '''
    函数:矩形
    :return:
    '''

    # 函数作用:在yz平面上通过一顶点及长宽绘制矩形
    # 输入:一顶点坐标,长值,宽值
    # 输出:暂定只输出矩形
    coor0 = [0, 3, 3]  # 矩阵某一行
    Length = 3
    Width = 1
    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())
    display.DisplayShape(aRectangle.Shape(), update=True)

#圆形
def make_pipe():
    #边1(从第1---2点) #边2(从第2---3点)
    Edge21=BRepBuilderAPI_MakeEdge(gp_Pnt(40,0,10),gp_Pnt(82.5,25,10)).Edge()
    Edge22=BRepBuilderAPI_MakeEdge(gp_Pnt(82.5,25,10),gp_Pnt(42.5,93,10)).Edge()
    #组成1条线(1---2--3折线)
    pipe_outline_Wire=BRepBuilderAPI_MakeWire(Edge21,Edge22)
    # pipe_outline_Wire = BRepBuilderAPI_MakeWire(Edge21)

    #形成点圆的坐标点---#把点--边----- #边变成线 ---根据圆形的先变成面
    circe_gp_dian=gp_Circ(gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 1, 0)), 10)
    circe_Edge = BRepBuilderAPI_MakeEdge(circe_gp_dian).Edge()
    circe_Wire= BRepBuilderAPI_MakeWire(circe_Edge).Wire()
    circe_Face = BRepBuilderAPI_MakeFace(circe_Wire, True)

    #z组合成管子
    pipe_shape = BRepOffsetAPI_MakePipe(pipe_outline_Wire.Wire(), circe_Face.Shape()).Shape()# 第一个参数 轨迹线,第二个参数是轮廓线
    display.DisplayShape(pipe_shape, update=True)
    return  pipe_shape

def make_Revol():
    # 函数作用:旋转拉伸
    # 输入:面、围绕轴转
    # 输出:一个壶
    E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(40., 0., 0.), gp_Pnt(82.5, 25., 0.)).Edge()
    E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(82.5, 25., 0.), gp_Pnt(42.5, 93., 0.)).Edge()
    E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(42.5, 93., 0.), gp_Pnt(0., 68., 0.)).Edge()
    E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(0., 68., 0.), gp_Pnt(40., 0., 0.)).Edge()
    #点----线---面--体
    shape_wire = BRepBuilderAPI_MakeWire(E11, E12, E13, E14)
    shape_face = BRepBuilderAPI_MakeFace(shape_wire.Wire()).Face()
    #旋转拉伸
    shape =BRepPrimAPI_MakeRevol(shape_face, gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(1, 1, 1)))
    #拉升立方体--封闭曲线拉伸
    # shape =BRepPrimAPI_MakePrism(shape_face, gp_Vec(0., 0, 50))



    # display.DisplayShape(shape_wire.Shape(), update=True)
    display.DisplayShape(shape.Shape(), update=True)

def make_Cone():
    # 函数作用:长方形、棱锥、棱台、棱柱、圆锥、圆台、圆柱、圆环
    # 输入:两点
    # 输出:线段
    ######################################################
    # shape = BRepPrimAPI_MakeBox(10, 10, 10).Shape()#长方形
    #my_wedge = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(4, 0,4), gp_Dir(1,1,0)),4,4,4,2,2,2,2).Shape()#棱锥
    # my_wedge = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(-4, -8, -4), gp_Dir(0, 0, 1)), 4, 4, 8, 1, 1, 3, 7).Shape()#棱台
    # my_wedge1 = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(2, -6, -4), gp_Dir(0, 0, 1)), 2, 3, 4, 1).Shape()棱柱
    ######################################################
    # 圆形
    #Circle = GC_MakeCircle(gp_Ax2(gp_Pnt(0, 0, 5), gp_Dir(0, 0, -1)), 8).Value()
    #################################

    ###############################

    #shape = BRepPrimAPI_MakeCone(1,0,4).Shape() # 圆锥
    # shape=BRepPrimAPI_MakeCone(gp_Ax2 (gp_Pnt(0,0,0),gp_Dir (0,0,-1)),1,2,4).Shape( )# 圆台
    # shape=BRepPrimAPI_MakeCylinder(10,50).Shape()#圆柱
    shape = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt(0, 0, 2), gp_Dir(0, 0, 1)), 0.55, 0.1).Shape()#环形
    ##########################################
    # 球形
    # center = gp_Pnt(5, 5, 10)
    # radius = 19
    # shape = BRepPrimAPI_MakeSphere(center, radius).Shape()#球体
    ###########################################
    display.DisplayShape(shape, update=True, color=rgb_color(0,0, 1 ))
    # display.DisplayShape(my_cone, update=True, color=rgb_color(0, 1, 1 ))
    return

def make_box():
    Solid_guanzi = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 3, 3,3).Shape()  # 每个填充快
    display.DisplayShape(Solid_guanzi, update=True)
    return  Solid_guanzi


def face_is_plane(face: TopoDS_Face) -> bool:
    """
    Returns True if the TopoDS_Face is a plane, False otherwise
    """
    surf = BRepAdaptor_Surface(face, True)
    surf_type = surf.GetType()
    return surf_type == GeomAbs_Plane


def geom_plane_from_face(aFace: TopoDS_Face) -> gp_Pln:
    """
    Returns the geometric plane entity from a planar surface
    """
    return BRepAdaptor_Surface(aFace, True).Plane()
def zhongkong():
    # Our goal is to find the highest Z face and remove it
    zMax = -1.
    thickness=5
    shape = BRepPrimAPI_MakeBox(gp_Pnt(0, 0, 0), 3, 3, 3)
    # We have to work our way through all the faces to find the highest Z face so we can remove it for the shell
    aFaceExplorer = TopExp_Explorer(shape.Shape(), TopAbs_FACE)
    while aFaceExplorer.More():
        aFace = topods.Face(aFaceExplorer.Current())
        if face_is_plane(aFace):
            aPlane = geom_plane_from_face(aFace)

            # We want the highest Z face, so compare this to the previous faces
            aPntLoc = aPlane.Location()
            aZ = aPntLoc.Z()
            if aZ > zMax:
                zMax = aZ
        aFaceExplorer.Next()

    facesToRemove = TopTools_ListOfShape()
    facesToRemove.Append(aFace)

    shape = BRepOffsetAPI_MakeThickSolid(shape.Shape(), facesToRemove, -thickness / 50.0, 0.001)
    display.DisplayShape(shape.Shape(), update=True)

def buer():
    '''
    布尔剪(Boolean    cut)
    布尔并(Boolean    fuse)

    布尔交(Boolean    common)
    :return:
   '''
    my_cylinder = BRepPrimAPI_MakeCylinder(gp_Ax2(gp_Pnt(-3, 5, 2), gp_Dir(1, 0, 1)), 1, 20).Shape()#圆筒
    # cylinder_radius = 0.25
    # cylinder_height = 2.0
    # cylinder_origin = gp_Ax2(gp_Pnt(0.0, 0.0, - cylinder_height / 2.0), gp_Dir(0.0, 0.0, 1.0))
    my_box = BRepPrimAPI_MakeBox(10, 10, 10).Shape()
    shape = BRepAlgoAPI_Cut(my_box, my_cylinder).Shape()#(1-2)
    # shape = BRepAlgoAPI_Common(my_box, my_cylinder).Shape()  # (布尔交)
    # shape = BRepAlgoAPI_Fuse(my_box, my_cylinder).Shape()  # (在1+2)

    # shape = BRepPrimAPI_MakeCylinder(cylinder_origin, cylinder_radius, cylinder_height).Shape()
    display.DisplayShape(shape, update=True)

def fangyang():
    '''
    放样
    :return:
    '''

    # 做第一条封闭曲线(四边形)
    E11 = BRepBuilderAPI_MakeEdge(gp_Pnt(40, 0, 0), gp_Pnt(82.5, 25, 0)).Edge()
    E12 = BRepBuilderAPI_MakeEdge(gp_Pnt(82.5, 25, 0), gp_Pnt(42.5, 93, 0)).Edge()
    E13 = BRepBuilderAPI_MakeEdge(gp_Pnt(42.5, 93, 0), gp_Pnt(0, 68, 0)).Edge()
    E14 = BRepBuilderAPI_MakeEdge(gp_Pnt(0, 68, 0), gp_Pnt(40, 0, 0)).Edge()
    W1 = BRepBuilderAPI_MakeWire(E11, E12, E13, E14)

    # 做第2条封闭曲线(三角形)
    E21 = BRepBuilderAPI_MakeEdge(gp_Pnt(40, 0, 10), gp_Pnt(82.5, 25, 10)).Edge()
    E22 = BRepBuilderAPI_MakeEdge(gp_Pnt(82.5, 25, 10), gp_Pnt(42.5, 93, 10)).Edge()
    E23 = BRepBuilderAPI_MakeEdge(gp_Pnt(42.5, 93, 10), gp_Pnt(40, 0, 10)).Edge()
    W2 = BRepBuilderAPI_MakeWire(E21, E22, E23)

    generator = BRepOffsetAPI_ThruSections(True)
    generator.AddWire(W1.Wire())
    generator.AddWire(W2.Wire())
    ais = AIS_Shape(generator.Shape())
    generator.Build()

    # display.DisplayShape(W1.Shape(), update=True)
    # display.DisplayShape(W2.Shape(), update=True)
    display.DisplayShape(generator.Shape(),update=True)
    # display.DisplayShape(ais.Shape(), update=True)


if __name__ == "__main__":
    from OCC.Display.SimpleGui import init_display
    display, start_display, add_menu, add_function_to_menu = init_display()
    # make_Cone()
    # make_xian()
    # make_MakeRevol()
    # pipe_shape=make_pipe()

    # Solid_guanzi=make_box()
    # zhongkong(Solid_guanzi)
    # scoop(Solid_guanzi,3)
    # buer()
    # fangyang()
    zhongkong()

    start_display()

 类似资料: