From 4b8a4e92c11964aeacec7f55d0bcab98084de92a Mon Sep 17 00:00:00 2001 From: Jonathan Wagenet Date: Tue, 16 Sep 2025 12:42:47 -0400 Subject: [PATCH] Tidy geometry and zero_d intersection typing and docstrings. --- src/build123d/geometry.py | 44 +++++++++++++------- src/build123d/topology/shape_core.py | 2 +- src/build123d/topology/zero_d.py | 62 ++++++++++++++-------------- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/build123d/geometry.py b/src/build123d/geometry.py index 69fae4f..e4c0eeb 100644 --- a/src/build123d/geometry.py +++ b/src/build123d/geometry.py @@ -527,18 +527,22 @@ class Vector: @overload def intersect(self, location: Location) -> Vector | None: - """Find intersection of location and vector""" + """Find intersection of vector and location""" @overload def intersect(self, axis: Axis) -> Vector | None: - """Find intersection of axis and vector""" + """Find intersection of vector and axis""" @overload def intersect(self, plane: Plane) -> Vector | None: - """Find intersection of plane and vector""" + """Find intersection of vector and plane""" + + @overload + def intersect(self, shape: Shape) -> Shape | None: + """Find intersection of vector and shape""" def intersect(self, *args, **kwargs): - """Find intersection of geometric objects and vector""" + """Find intersection of vector and geometric object or shape""" axis, plane, vector, location, shape = _parse_intersect_args(*args, **kwargs) if axis is not None: @@ -906,11 +910,11 @@ class Axis(metaclass=AxisMeta): @overload def intersect(self, vector: VectorLike) -> Vector | None: - """Find intersection of vector and axis""" + """Find intersection of axis and vector""" @overload def intersect(self, location: Location) -> Vector | Location | None: - """Find intersection of location and axis""" + """Find intersection of axis and location""" @overload def intersect(self, axis: Axis) -> Vector | Axis | None: @@ -918,10 +922,14 @@ class Axis(metaclass=AxisMeta): @overload def intersect(self, plane: Plane) -> Vector | Axis | None: - """Find intersection of plane and axis""" + """Find intersection of axis and plane""" + + @overload + def intersect(self, shape: Shape) -> Shape | None: + """Find intersection of axis and shape""" def intersect(self, *args, **kwargs): - """Find intersection of geometric object and axis""" + """Find intersection of axis and geometric object or shape""" axis, plane, vector, location, shape = _parse_intersect_args(*args, **kwargs) if axis is not None: @@ -1929,7 +1937,7 @@ class Location: @overload def intersect(self, vector: VectorLike) -> Vector | None: - """Find intersection of vector and location""" + """Find intersection of location and vector""" @overload def intersect(self, location: Location) -> Vector | Location | None: @@ -1937,14 +1945,18 @@ class Location: @overload def intersect(self, axis: Axis) -> Vector | Location | None: - """Find intersection of axis and location""" + """Find intersection of location and axis""" @overload def intersect(self, plane: Plane) -> Vector | Location | None: - """Find intersection of plane and location""" + """Find intersection of location and plane""" + + @overload + def intersect(self, shape: Shape) -> Shape | None: + """Find intersection of location and shape""" def intersect(self, *args, **kwargs): - """Find intersection of geometric object and location""" + """Find intersection of location and geometric object or shape""" axis, plane, vector, location, shape = _parse_intersect_args(*args, **kwargs) if axis is not None: @@ -3131,15 +3143,15 @@ class Plane(metaclass=PlaneMeta): @overload def intersect(self, vector: VectorLike) -> Vector | None: - """Find intersection of vector and plane""" + """Find intersection of plane and vector""" @overload def intersect(self, location: Location) -> Vector | Location | None: - """Find intersection of location and plane""" + """Find intersection of plane and location""" @overload def intersect(self, axis: Axis) -> Vector | Axis | None: - """Find intersection of axis and plane""" + """Find intersection of plane and axis""" @overload def intersect(self, plane: Plane) -> Axis | Plane | None: @@ -3150,7 +3162,7 @@ class Plane(metaclass=PlaneMeta): """Find intersection of plane and shape""" def intersect(self, *args, **kwargs): - """Find intersection of geometric object and shape""" + """Find intersection of plane and geometric object or shape""" axis, plane, vector, location, shape = _parse_intersect_args(*args, **kwargs) diff --git a/src/build123d/topology/shape_core.py b/src/build123d/topology/shape_core.py index ec9e2ae..4d06397 100644 --- a/src/build123d/topology/shape_core.py +++ b/src/build123d/topology/shape_core.py @@ -1326,7 +1326,7 @@ class Shape(NodeMixin, Generic[TOPODS]): ) def intersect( - self, *to_intersect: Shape | Axis | Plane + self, *to_intersect: Shape | Vector | Location | Axis | Plane ) -> None | Self | ShapeList[Self]: """Intersection of the arguments and this shape diff --git a/src/build123d/topology/zero_d.py b/src/build123d/topology/zero_d.py index 7d52245..bb36513 100644 --- a/src/build123d/topology/zero_d.py +++ b/src/build123d/topology/zero_d.py @@ -169,41 +169,41 @@ class Vertex(Shape[TopoDS_Vertex]): raise NotImplementedError("Vertices can't be created by extrusion") def intersect( - self, *to_intersect: Shape | Vector | Location | Axis | Plane - ) -> None | ShapeList[Vertex]: - """Intersection of the arguments and this shape + self, *to_intersect: Shape | Vector | Location | Axis | Plane + ) -> ShapeList[Vertex] | None: + """Intersection of vertex and geometric objects or shapes. - Args: - to_intersect (sequence of Union[Shape, Axis, Plane]): Shape(s) to - intersect with + Args: + to_intersect (sequence of [Shape | Vector | Location | Axis | Plane]): + Objects(s) to intersect with - Returns: - ShapeList[Shape]: Resulting object may be of a ShapeList of multiple - non-Compound object created - """ - points_sets: list[set] = [] - for obj in to_intersect: - # Treat as Vector, otherwise call intersection from Shape - match obj: - case Vertex(): - result = Vector(self).intersect(Vector(obj)) - case Vector() | Location() | Axis() | Plane(): - result = obj.intersect(Vector(self)) - case _ if issubclass(type(obj), Shape): - result = obj.intersect(self) - case _: - raise ValueError(f"Unknown object type: {type(obj)}") + Returns: + ShapeList[Vertex] | None: Vertex intersection in a ShapeList or None + """ + points_sets: list[set] = [] + result: Shape | ShapeList[Shape] | Vector | None + for obj in to_intersect: + # Treat as Vector, otherwise call intersection from Shape + match obj: + case Vertex(): + result = Vector(self).intersect(Vector(obj)) + case Vector() | Location() | Axis() | Plane(): + result = obj.intersect(Vector(self)) + case _ if issubclass(type(obj), Shape): + result = obj.intersect(self) + case _: + raise ValueError(f"Unknown object type: {type(obj)}") - if isinstance(result, Vector): - points_sets.append(set([result])) - else: - points_sets.append(set()) - - common_points = set.intersection(*points_sets) - if common_points: - return ShapeList([Vertex(p) for p in common_points]) + if isinstance(result, Vector): + points_sets.append(set([result])) else: - return None + points_sets.append(set()) + + common_points = set.intersection(*points_sets) + if common_points: + return ShapeList([Vertex(p) for p in common_points]) + + return None # ---- Instance Methods ----