PythonOCC基础使用:建模——矩阵变换(平移/旋转/缩放/镜像)
此处特别感谢小昌做出的贡献!
PythonOCC基础使用:建模——矩阵变换(平移/旋转/缩放/镜像)-卡核 (caxkernel.com)
1.平移
效果图:
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCone
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.gp import gp_Pnt, gp_Trsf, gp_Vec, gp_Ax1, gp_Dir
from OCC.Display.OCCViewer import rgb_color
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
cone=TopoDS_Shape(my_cone)
T=gp_Trsf()
T.SetTranslation(gp_Vec(0,5,0))
loc=TopLoc_Location(T)
cone.Location(loc)
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(my_cone, update=True,color=rgb_color(1,0,0))
display.DisplayShape(cone, update=True,color=rgb_color(0,0,1))
start_display()
2.旋转
效果图:
from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCone
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax1, gp_Dir, gp_Circ, gp_Elips,gp_Trsf,gp_Vec
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse,GC_MakeRotation
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Display.OCCViewer import rgb_color
#旋转
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
cone=TopoDS_Shape(my_cone)
T1=gp_Trsf()
T=gp_Ax1(gp_Pnt(0, 1, 0), gp_Dir(0, 6, 4))
T1.SetRotation(T,1)
coneLoc=TopLoc_Location(T1)
my_cone.Move(coneLoc)
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(cone,update=True,color=rgb_color(0,0,0))
display.DisplayShape(my_cone, update=True,color=rgb_color(1,0,0))
start_display()
3.缩放
效果图:
from OCC.Core.GeomAPI import GeomAPI_Interpolate
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCone
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.gp import gp_Pnt, gp_Ax1, gp_Dir, gp_Circ, gp_Elips,gp_Trsf,gp_Vec
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse,GC_MakeRotation
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Display.OCCViewer import rgb_color
#缩放
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
cone=TopoDS_Shape(my_cone)
T1=gp_Trsf()
T=gp_Pnt(0, 20, 0)
T1.SetScale(T,5)
coneLoc=TopLoc_Location(T1)
my_cone.Move(coneLoc)
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(cone,update=True,color=rgb_color(0,0,0))
display.DisplayShape(my_cone, update=True,color=rgb_color(1,0,0))
start_display()
4.镜像
效果图:
from OCC.Core.TColgp import TColgp_HArray1OfPnt
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCone
from OCC.Core.gp import gp_Pnt, gp_Ax1, gp_Dir, gp_Circ, gp_Elips,gp_Trsf,gp_Vec
from OCC.Core.TopLoc import TopLoc_Location
from OCC.Core.TopoDS import TopoDS_Shape
from OCC.Core.GC import GC_MakeSegment, GC_MakeCircle, GC_MakeArcOfCircle, GC_MakeEllipse,GC_MakeRotation
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeWire
from OCC.Display.OCCViewer import rgb_color
#镜像
my_cone = BRepPrimAPI_MakeCone(1,0,4).Shape()
cone=TopoDS_Shape(my_cone)
T1 = gp_Trsf()
T=gp_Ax1(gp_Pnt(0,6,0),gp_Dir(0,4,1))
T1.SetMirror(T)
coneLoc=TopLoc_Location(T1)
my_cone.Move(coneLoc)
if __name__ == "__main__":
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape( my_cone, update=True, color=rgb_color( 1, 0, 0 ) )
display.DisplayShape(cone,update=True,color=rgb_color(0,0,0))
start_display()