From 494841acb988eb09a12c439d2aaed8716a6ca025 Mon Sep 17 00:00:00 2001 From: gumyr Date: Mon, 21 Aug 2023 15:11:38 -0400 Subject: [PATCH] Added Edge as valid parameter for Axis Issue #256 --- src/build123d/geometry.py | 40 ++++++++++++++++++++++++++++++++++++++- tests/test_direct_api.py | 24 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/build123d/geometry.py b/src/build123d/geometry.py index caad08e..31c5261 100644 --- a/src/build123d/geometry.py +++ b/src/build123d/geometry.py @@ -425,6 +425,10 @@ class Axis: Args: origin (VectorLike): start point direction (VectorLike): direction + + or + + edge (Edge): origin & direction defined by start of edge """ @classmethod @@ -450,7 +454,41 @@ class Axis: """Return self as Location""" return Location(Plane(origin=self.position, z_dir=self.direction)) - def __init__(self, origin: VectorLike, direction: VectorLike): + @overload + def __init__(self, origin: VectorLike, direction: VectorLike): # pragma: no cover + """Axis: point and direction""" + + @overload + def __init__(self, edge: "Edge"): # pragma: no cover + """Axis: start of Edge""" + + def __init__(self, *args, **kwargs): + origin = None + direction = None + if len(args) == 1: + if type(args[0]).__name__ == "Edge": + origin = args[0].position_at(0) + direction = args[0].tangent_at(0) + else: + origin = args[0] + if len(args) == 2: + origin = args[0] + direction = args[1] + + if "origin" in kwargs: + origin = kwargs["origin"] + if "direction" in kwargs: + direction = kwargs["direction"] + if "edge" in kwargs and type(kwargs["edge"]).__name__ == "Edge": + origin = kwargs["edge"].position_at(0) + direction = kwargs["edge"].tangent_at(0) + + try: + origin = Vector(origin) + direction = Vector(direction) + except TypeError as exc: + raise ValueError("Invalid Axis parameters") from exc + self.wrapped = gp_Ax1( Vector(origin).to_pnt(), gp_Dir(*Vector(direction).normalized().to_tuple()) ) diff --git a/tests/test_direct_api.py b/tests/test_direct_api.py index 57a55b5..40867df 100644 --- a/tests/test_direct_api.py +++ b/tests/test_direct_api.py @@ -202,6 +202,30 @@ class TestAssembly(unittest.TestCase): class TestAxis(DirectApiTestCase): """Test the Axis class""" + def test_axis_init(self): + test_axis = Axis((1, 2, 3), (0, 0, 1)) + self.assertVectorAlmostEquals(test_axis.position, (1, 2, 3), 5) + self.assertVectorAlmostEquals(test_axis.direction, (0, 0, 1), 5) + + test_axis = Axis((1, 2, 3), direction=(0, 0, 1)) + self.assertVectorAlmostEquals(test_axis.position, (1, 2, 3), 5) + self.assertVectorAlmostEquals(test_axis.direction, (0, 0, 1), 5) + + test_axis = Axis(origin=(1, 2, 3), direction=(0, 0, 1)) + self.assertVectorAlmostEquals(test_axis.position, (1, 2, 3), 5) + self.assertVectorAlmostEquals(test_axis.direction, (0, 0, 1), 5) + + test_axis = Axis(Edge.make_line((1, 2, 3), (1, 2, 4))) + self.assertVectorAlmostEquals(test_axis.position, (1, 2, 3), 5) + self.assertVectorAlmostEquals(test_axis.direction, (0, 0, 1), 5) + + test_axis = Axis(edge=Edge.make_line((1, 2, 3), (1, 2, 4))) + self.assertVectorAlmostEquals(test_axis.position, (1, 2, 3), 5) + self.assertVectorAlmostEquals(test_axis.direction, (0, 0, 1), 5) + + with self.assertRaises(ValueError): + Axis("one", "up") + def test_axis_from_occt(self): occt_axis = gp_Ax1(gp_Pnt(1, 1, 1), gp_Dir(0, 1, 0)) test_axis = Axis.from_occt(occt_axis)