Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Axis.position and Axis.direction properties #905

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()