01 pythonocc基础:基础三维实体
一、基础三维实体
1、长方体
my_box = BRepPrimAPI_MakeBox (10,20,30).Shape()
2、球体
center=gp_Pnt(5,5,10)
radius=19
my_sphere =BRepPrimAPI_MakeSphere(center ,radius).Shape()
3、圆柱、圆锥
#圆锥
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
my_cone=BRepPrimAPI_MakeCone (gp_Ax2 (gp_Pnt(0,0,0),gp_Dir (0,0,-1)),1,2,4).Shape( )
#圆柱
my_cyliner = BRepPrimAPI_MakeCylinder (gp_Ax2 (gp_Pnt (-3,-3,-4),gp_Dir (0,0,-1)),7,10).Shape()
4、环
my_Torus = BRepPrimAPI_MakeTorus(gp_Ax2 (gp_Pnt (0,0,2),gp_Dir (0,0,1)),0.55,0.1).Shape()
5、棱柱/棱锥/棱台
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()
my_wedge2 = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(4, 0,4), gp_Dir(1,1,0)),4,4,4,2,2,2,2).Shape()
6、综合示例
#!/usr/bin/env python
# encoding: utf-8
from OCC.Core.BRepPrimAPI import (BRepPrimAPI_MakeBox,BRepPrimAPI_MakeSphere,BRepPrimAPI_MakeCone,
BRepPrimAPI_MakeCylinder,BRepPrimAPI_MakeTorus,BRepPrimAPI_MakeWedge)
from OCC.Core.gp import gp_Pnt,gp_Ax2,gp_Dir
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
def displaybox():
my_box = BRepPrimAPI_MakeBox (10,20,30).Shape()
display.DisplayShape(my_box,update=True)
start_display()
def displayball():
center=gp_Pnt(5,5,10)
radius=19
my_sphere =BRepPrimAPI_MakeSphere(center ,radius).Shape()
display.DisplayShape(my_sphere,update=True)
start_display()
def displaycone():
#圆锥
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
display.DisplayShape(my_cone,update=True)
start_display()
def displayfrustum():
my_cone=BRepPrimAPI_MakeCone (gp_Ax2 (gp_Pnt(0,0,0),gp_Dir (0,0,-1)),1,2,4).Shape( )
display.DisplayShape(my_cone,update=True)
start_display()
def displaycylinder():
#圆柱
my_cylinder = BRepPrimAPI_MakeCylinder (gp_Ax2 (gp_Pnt (-3,-3,-4),gp_Dir (0,0,-1)),7,10).Shape()
display.DisplayShape(my_cylinder,update=True)
start_display()
def displaytorus():
my_Torus = BRepPrimAPI_MakeTorus(gp_Ax2(gp_Pnt(0, 0, 2), gp_Dir(0, 0, 1)), 0.55, 0.1).Shape()
display.DisplayShape(my_Torus,update=True)
start_display()
def displaywedge():
my_wedge = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(-4, -8, -4), gp_Dir(0, 0, 1)), 4, 4, 8, 1, 1, 3, 7).Shape()
display.DisplayShape(my_wedge,update=True)
start_display()
def displaywedge1():
my_wedge1 = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(2, -6, -4), gp_Dir(0, 0, 1)), 2, 3, 4, 1).Shape()
display.DisplayShape(my_wedge1,update=True)
start_display()
def displaywedge2():
my_wedge2 = BRepPrimAPI_MakeWedge(gp_Ax2(gp_Pnt(4, 0, 4), gp_Dir(1, 1, 0)), 4, 4, 4, 2, 2, 2, 2).Shape()
# display.display_trihedron()
display.initialized()
display.DisplayShape(my_wedge2,update=True)
start_display()
if __name__ == '__main__':
add_menu('display')
add_function_to_menu('display', displaybox)
add_function_to_menu('display', displayball)
add_function_to_menu('display', displaycone)
add_function_to_menu('display', displayfrustum)
add_function_to_menu('display', displaycylinder)
add_function_to_menu('display', displaytorus)
add_function_to_menu('display', displaywedge)
add_function_to_menu('display', displaywedge1)
add_function_to_menu('display', displaywedge2)
start_display()
实际效果