Skip to content

Commit

Permalink
Adding Face radius and rotational_axis properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Feb 13, 2025
1 parent f6f9167 commit fd44037
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/build123d/topology/two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ def length(self) -> None | float:
result = face_vertices[-1].X - face_vertices[0].X
return result

@property
def radius(self) -> None | float:
"""Return the radius of a cylinder or sphere, otherwise None"""
if self.geom_type in [GeomType.CYLINDER, GeomType.SPHERE]:
return self.geom_adaptor().Radius()
else:
return None

@property
def rotational_axis(self) -> None | Axis:
"""Get the rotational axis of a cylinder"""
if self.geom_type == GeomType.CYLINDER:
return Axis(self.geom_adaptor().Cylinder().Axis())
else:
return None

@property
def volume(self) -> float:
"""volume - the volume of this Face, which is always zero"""
Expand Down
21 changes: 20 additions & 1 deletion tests/test_direct_api/test_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from build123d.geometry import Axis, Location, Plane, Pos, Vector
from build123d.importers import import_stl
from build123d.objects_curve import Line, Polyline, Spline, ThreePointArc
from build123d.objects_part import Box, Cylinder
from build123d.objects_part import Box, Cylinder, Sphere
from build123d.objects_sketch import (
Circle,
Ellipse,
Expand Down Expand Up @@ -733,6 +733,25 @@ def test_axes_of_symmetry(self):
make_face()
self.assertEqual(len(skt.face().axes_of_symmetry), 0)

def test_radius_property(self):
c = Cylinder(1.5, 2).faces().filter_by(GeomType.CYLINDER)[0]
s = Sphere(3).faces().filter_by(GeomType.SPHERE)[0]
b = Box(1, 1, 1).faces()[0]
self.assertAlmostEqual(c.radius, 1.5, 5)
self.assertAlmostEqual(s.radius, 3, 5)
self.assertIsNone(b.radius)

def test_rotational_axis_property(self):
c = (
Cylinder(1.5, 2, rotation=(90, 0, 0))
.faces()
.filter_by(GeomType.CYLINDER)[0]
)
s = Sphere(3).faces().filter_by(GeomType.SPHERE)[0]
self.assertAlmostEqual(c.rotational_axis.direction, (0, -1, 0), 5)
self.assertAlmostEqual(c.rotational_axis.position, (0, 1, 0), 5)
self.assertIsNone(s.rotational_axis)


class TestAxesOfSymmetrySplitNone(unittest.TestCase):
def test_split_returns_none(self):
Expand Down

0 comments on commit fd44037

Please sign in to comment.