|
| 1 | +""" Cylinder Shaft """ |
| 2 | +from OCC.Display.SimpleGui import init_display |
| 3 | +from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeCylinder |
| 4 | +from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Ax2 |
| 5 | + |
| 6 | + |
| 7 | +class CylinderShaft: |
| 8 | + """ |
| 9 | + Cylinder shaft construction. |
| 10 | +
|
| 11 | + :param float radius: radius of the cylinder shaft. Defaults to 1.0. |
| 12 | + :param float height: height of the cylinder shaft. Defaults to 1.0. |
| 13 | + :param list orientation: orientation vector of the cylinder shaft. Defaults |
| 14 | + to [1.0, 0.0, 0.0], so along X axis. |
| 15 | + :param list origin: origin point of the cylinder shaft. Defaults to |
| 16 | + [0.0, 0.0, 0.0]. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, radius=1.0, height=1.0, orientation=None, origin=None): |
| 20 | + self.radius = radius |
| 21 | + self.height = height |
| 22 | + |
| 23 | + if orientation is None: |
| 24 | + self.orientation = [1.0, 0.0, 0.0] # default orientation along X |
| 25 | + |
| 26 | + if origin is None: |
| 27 | + self.origin = [0.0, 0.0, 0.0] # default origin at (0,0,0) |
| 28 | + |
| 29 | + def generate_solid(self): |
| 30 | + """ |
| 31 | + Generate a cylindrical shaft using the BRepBuilderAPI_MakeCylinder |
| 32 | + algorithm. This method requires PythonOCC to be installed. |
| 33 | +
|
| 34 | + :return: solid shaft |
| 35 | + :rtype: OCC.Core.TopoDS.TopoDS_Solid |
| 36 | + """ |
| 37 | + |
| 38 | + origin = gp_Pnt(*self.origin) |
| 39 | + orientation = gp_Dir(*self.orientation) |
| 40 | + ax2 = gp_Ax2(origin, orientation) |
| 41 | + |
| 42 | + shape = BRepPrimAPI_MakeCylinder(ax2, self.radius, self.height).Shape() |
| 43 | + |
| 44 | + return shape |
| 45 | + |
| 46 | + def display(self): |
| 47 | + """ |
| 48 | + Display the shaft. |
| 49 | + """ |
| 50 | + shaft_solid = self.generate_solid() |
| 51 | + display, start_display = init_display()[:2] |
| 52 | + display.DisplayShape(shaft_solid, update=True) |
| 53 | + start_display() |
0 commit comments