Skip to content

Commit

Permalink
Merge pull request #905 from snoyer/axis-props
Browse files Browse the repository at this point in the history
make `Axis.position` and `Axis.direction` properties
  • Loading branch information
gumyr authored Feb 18, 2025
2 parents 39b4fc2 + e0e5d0d commit ffc3eba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/build123d/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,16 +661,21 @@ def __init__(self, *args, **kwargs):
gp_Dir(*tuple(direction_vector.normalized())),
)

self.position = Vector(
self.wrapped.Location().X(),
self.wrapped.Location().Y(),
self.wrapped.Location().Z(),
) #: Axis origin
self.direction = Vector(
self.wrapped.Direction().X(),
self.wrapped.Direction().Y(),
self.wrapped.Direction().Z(),
) #: Axis direction
@property
def position(self):
return Vector(self.wrapped.Location())

@position.setter
def position(self, position: VectorLike):
self.wrapped.SetLocation(Vector(position).to_pnt())

@property
def direction(self):
return Vector(self.wrapped.Direction())

@direction.setter
def direction(self, direction: VectorLike):
self.wrapped.SetDirection(Vector(direction).to_dir())

@property
def location(self) -> Location:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_direct_api/test_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ def test_axis_not_equal(self):
random_obj = object()
self.assertNotEqual(Axis.X, random_obj)

def test_position_property(self):
axis = Axis.X
axis.position = 1, 2, 3
self.assertAlmostEqual(axis.position, (1, 2, 3))

axis.position += 1, 2, 3
self.assertAlmostEqual(axis.position, (2, 4, 6))

self.assertAlmostEqual(Axis(axis.wrapped).position, (2, 4, 6))

def test_direction_property(self):
axis = Axis.X
axis.direction = 1, 2, 3
self.assertAlmostEqual(axis.direction, Vector(1, 2, 3).normalized())

axis.direction += 5, 3, 1
expected = (Vector(1, 2, 3).normalized() + Vector(5, 3, 1)).normalized()
self.assertAlmostEqual(axis.direction, expected)

self.assertAlmostEqual(Axis(axis.wrapped).direction, expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit ffc3eba

Please sign in to comment.