Skip to content

Commit 00fbff3

Browse files
committed
cylinder shaft, minimal test
1 parent 0a4379e commit 00fbff3

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

bladex/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
from .params import ParamFile
1717
from .ndinterpolator import RBF, reconstruct_f, scipy_bspline
1818
from .reversepropeller import ReversePropeller
19+
from .cylinder_shaft import CylinderShaft

bladex/cylinder_shaft.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import OCC.Core.TopoDS
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(object):
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()

tests/test_cylinder_shaft.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from unittest import TestCase
2+
from bladex import CylinderShaft
3+
from OCC.Core.TopoDS import TopoDS_Solid
4+
5+
6+
def test_generate_solid_01():
7+
sh = CylinderShaft()
8+
shaft_solid = sh.generate_solid()
9+
assert isinstance(shaft_solid, TopoDS_Solid)

0 commit comments

Comments
 (0)