Added Edge as valid parameter for Axis Issue #256

This commit is contained in:
gumyr 2023-08-21 15:11:38 -04:00
parent e756f8a003
commit 494841acb9
2 changed files with 63 additions and 1 deletions

View file

@ -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())
)

View file

@ -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)