diff --git a/src/build123d/build_common.py b/src/build123d/build_common.py index 697ec30..37d0184 100644 --- a/src/build123d/build_common.py +++ b/src/build123d/build_common.py @@ -464,6 +464,7 @@ class HexLocations(LocationList): # Determine the minimum point and size of the array sorted_points = [points.sort_by(Axis.X), points.sort_by(Axis.Y)] + # pylint doesn't recognize that a ShapeList of Vector is valid # pylint: disable=no-member size = [ sorted_points[0][-1].X - sorted_points[0][0].X, diff --git a/src/build123d/direct_api.py b/src/build123d/direct_api.py index a014686..1db233f 100644 --- a/src/build123d/direct_api.py +++ b/src/build123d/direct_api.py @@ -520,14 +520,6 @@ class Vector: """Vector length""" return self.wrapped.Magnitude() - def to_vertex(self) -> Vertex: - """Convert to Vector to Vertex - - Returns: - Vertex equivalent of Vector - """ - return Vertex(*self.to_tuple()) - def cross(self, vec: Vector) -> Vector: """Mathematical cross function""" return Vector(self.wrapped.Crossed(vec.wrapped)) @@ -3173,7 +3165,7 @@ class Shape(NodeMixin): self, other: Union[Shape, VectorLike] ) -> tuple[float, Vector, Vector]: """Minimal distance between two shapes and the points on each shape""" - other = other if isinstance(other, Shape) else Vector(other).to_vertex() + other = other if isinstance(other, Shape) else Vertex(other) dist_calc = BRepExtrema_DistShapeShape() dist_calc.LoadS1(self.wrapped) dist_calc.LoadS2(other.wrapped) @@ -3921,7 +3913,7 @@ class ShapeList(list[T]): Returns: ShapeList: Sorted shapes """ - other = other if isinstance(other, Shape) else Vector(other).to_vertex() + other = other if isinstance(other, Shape) else Vertex(other) distances = {other.distance_to(obj): obj for obj in self} return ShapeList( distances[key] for key in sorted(distances.keys(), reverse=reverse) @@ -6787,15 +6779,23 @@ class Vertex(Shape): @overload def __init__(self): # pragma: no cover - ... + """Default Vertext at the origin""" @overload def __init__(self, obj: TopoDS_Vertex): # pragma: no cover - ... + """Vertex from OCCT TopoDS_Vertex object""" @overload def __init__(self, X: float, Y: float, Z: float): # pragma: no cover - ... + """Vertex from three float values""" + + @overload + def __init__(self, values: Iterable[float]): + """Vertex from Vector or other iterators""" + + @overload + def __init__(self, values: tuple[float]): + """Vertex from tuple of floats""" def __init__(self, *args): if len(args) == 0: @@ -6804,6 +6804,11 @@ class Vertex(Shape): ) elif len(args) == 1 and isinstance(args[0], TopoDS_Vertex): self.wrapped = args[0] + elif len(args) == 1 and isinstance(args[0], (Iterable, tuple)): + values = [float(value) for value in args[0]] + if len(values) < 3: + values += [0.0] * (3 - len(values)) + self.wrapped = downcast(BRepBuilderAPI_MakeVertex(gp_Pnt(*values)).Vertex()) elif len(args) == 3 and all(isinstance(v, (int, float)) for v in args): self.wrapped = downcast( BRepBuilderAPI_MakeVertex(gp_Pnt(args[0], args[1], args[2])).Vertex() diff --git a/tests/test_direct_api.py b/tests/test_direct_api.py index 297966e..f07bb3b 100644 --- a/tests/test_direct_api.py +++ b/tests/test_direct_api.py @@ -2487,12 +2487,6 @@ class TestVector(unittest.TestCase): v = Vector(1, 1, 1) self.assertAlmostEqual(v, v.center()) - def test_to_vertex(self): - """Verify conversion of Vector to Vertex""" - v = Vector(1, 2, 3).to_vertex() - self.assertTrue(isinstance(v, Vertex)) - self.assertTupleAlmostEquals(v.to_tuple(), (1, 2, 3), 5) - def test_dot(self): v1 = Vector(2, 2, 2) v2 = Vector(1, -1, 1) @@ -2599,8 +2593,10 @@ class VertexTests(unittest.TestCase): self.assertEqual(1, v.X) self.assertEqual(Vector, type(v.center())) - with self.assertRaises(ValueError): - Vertex(Vector()) + self.assertTupleAlmostEquals(Vertex(Vector(1, 2, 3)).to_tuple(), (1, 2, 3), 7) + self.assertTupleAlmostEquals(Vertex((4, 5, 6)).to_tuple(), (4, 5, 6), 7) + self.assertTupleAlmostEquals(Vertex((7,)).to_tuple(), (7, 0, 0), 7) + self.assertTupleAlmostEquals(Vertex((8, 9)).to_tuple(), (8, 9, 0), 7) def test_vertex_add(self): test_vertex = Vertex(0, 0, 0)