Skip to content

Commit

Permalink
Fixed objects_curve.py typing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Jan 11, 2025
1 parent 152aedf commit 61ddeed
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 67 deletions.
12 changes: 10 additions & 2 deletions src/build123d/build_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from abc import ABC, abstractmethod
from itertools import product
from math import sqrt, cos, pi
from typing import Any, Type, TypeVar
from typing import Any, overload, Type, TypeVar

from collections.abc import Callable, Iterable
from typing_extensions import Self
Expand Down Expand Up @@ -1295,8 +1295,16 @@ def _get_context(cls):
"""Return the instance of the current ContextList"""
return cls._current.get(None)

@overload
@classmethod
def localize(cls, *points: VectorLike) -> list[Vector] | Vector:
def localize(cls, points: VectorLike) -> Vector: ...

@overload
@classmethod
def localize(cls, *points: VectorLike) -> list[Vector]: ...

@classmethod
def localize(cls, *points: VectorLike):
"""Localize a sequence of points to the active workplane
(only used by BuildLine where there is only one active workplane)
Expand Down
124 changes: 59 additions & 65 deletions src/build123d/objects_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _add_curve_to_context(curve, mode: Mode):
curve (Union[Wire, Edge]): curve to add to the context (either a Wire or an Edge).
mode (Mode): combination mode.
"""
context: BuildLine = BuildLine._get_context(log=False)
context: BuildLine | None = BuildLine._get_context(log=False)

if context is not None and isinstance(context, BuildLine):
if isinstance(curve, Wire):
Expand Down Expand Up @@ -108,14 +108,14 @@ class Bezier(BaseEdgeObject):
def __init__(
self,
*cntl_pnts: VectorLike,
weights: list[float] = None,
weights: list[float] | None = None,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

cntl_pnts = flatten_sequence(*cntl_pnts)
polls = WorkplaneList.localize(*cntl_pnts)
cntl_pnt_list = flatten_sequence(*cntl_pnts)
polls = WorkplaneList.localize(*cntl_pnt_list)
curve = Edge.make_bezier(*polls, weights=weights)

super().__init__(curve, mode=mode)
Expand Down Expand Up @@ -144,7 +144,7 @@ def __init__(
arc_size: float,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

center_point = WorkplaneList.localize(center)
Expand Down Expand Up @@ -203,7 +203,7 @@ def __init__(
keep: Keep = Keep.TOP,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

arc_pt = WorkplaneList.localize(pnt)
Expand Down Expand Up @@ -304,11 +304,11 @@ def __init__(
sweep_flag: bool = True,
plane: Plane = Plane.XY,
mode: Mode = Mode.ADD,
) -> Edge:
):
# Debugging incomplete
raise RuntimeError("Implementation incomplete")

# context: BuildLine = BuildLine._get_context(self)
# context: BuildLine | None = BuildLine._get_context(self)
# context.validate_inputs(self)

# # Calculate the ellipse parameters based on the SVG implementation here:
Expand Down Expand Up @@ -374,7 +374,7 @@ def __init__(
# context._add_to_context(curve, mode=mode)
# super().__init__(curve.wrapped)

# context: BuildLine = BuildLine._get_context(self)
# context: BuildLine | None = BuildLine._get_context(self)


class EllipticalCenterArc(BaseEdgeObject):
Expand Down Expand Up @@ -408,7 +408,7 @@ def __init__(
angular_direction: AngularDirection = AngularDirection.COUNTER_CLOCKWISE,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

center_pnt = WorkplaneList.localize(center)
Expand Down Expand Up @@ -460,7 +460,7 @@ def __init__(
lefthand: bool = False,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

center_pnt = WorkplaneList.localize(center)
Expand Down Expand Up @@ -496,17 +496,17 @@ def __init__(
close: bool = False,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

pts = flatten_sequence(*pts)
points = flatten_sequence(*pts)

if len(pts) < 2:
if len(points) < 2:
raise ValueError("FilletPolyline requires two or more pts")
if radius <= 0:
raise ValueError("radius must be positive")

lines_pts = WorkplaneList.localize(*pts)
lines_pts = WorkplaneList.localize(*points)

# Create the polyline
new_edges = [
Expand Down Expand Up @@ -539,9 +539,7 @@ def __init__(
for vertex, edges in vertex_to_edges.items():
if len(edges) != 2:
continue
other_vertices = {
ve for e in edges for ve in e.vertices() if ve != vertex
}
other_vertices = {ve for e in edges for ve in e.vertices() if ve != vertex}
third_edge = Edge.make_line(*[v.to_tuple() for v in other_vertices])
fillet_face = Face(Wire(edges + [third_edge])).fillet_2d(radius, [vertex])
fillets.append(fillet_face.edges().filter_by(GeomType.CIRCLE)[0])
Expand Down Expand Up @@ -595,7 +593,7 @@ def __init__(
arc_size: float,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

start = WorkplaneList.localize(start)
Expand Down Expand Up @@ -642,19 +640,17 @@ class Line(BaseEdgeObject):

_applies_to = [BuildLine._tag]

def __init__(
self, *pts: VectorLike | Iterable[VectorLike], mode: Mode = Mode.ADD
):
pts = flatten_sequence(*pts)
if len(pts) != 2:
def __init__(self, *pts: VectorLike | Iterable[VectorLike], mode: Mode = Mode.ADD):
points = flatten_sequence(*pts)
if len(points) != 2:
raise ValueError("Line requires two pts")

context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

pts = WorkplaneList.localize(*pts)
points_localized = WorkplaneList.localize(*points)

lines_pts = [Vector(p) for p in pts]
lines_pts = [Vector(p) for p in points_localized]

new_edge = Edge.make_line(lines_pts[0], lines_pts[1])
super().__init__(new_edge, mode=mode)
Expand Down Expand Up @@ -682,7 +678,7 @@ def __init__(
other: Curve | Edge | Wire,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

start = WorkplaneList.localize(start)
Expand Down Expand Up @@ -724,12 +720,12 @@ def __init__(
self,
start: VectorLike,
length: float,
angle: float = None,
direction: VectorLike = None,
angle: float | None = None,
direction: VectorLike | None = None,
length_mode: LengthMode = LengthMode.DIAGONAL,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

start = WorkplaneList.localize(start)
Expand All @@ -738,23 +734,23 @@ def __init__(
else:
polar_workplane = copy.copy(WorkplaneList._get_context().workplanes[0])

if direction:
direction = WorkplaneList.localize(direction)
angle = Vector(1, 0, 0).get_angle(direction)
if direction is not None:
direction_localized = WorkplaneList.localize(direction)
angle = Vector(1, 0, 0).get_angle(direction_localized)
elif angle is not None:
direction = polar_workplane.x_dir.rotate(
direction_localized = polar_workplane.x_dir.rotate(
Axis((0, 0, 0), polar_workplane.z_dir),
angle,
)
else:
raise ValueError("Either angle or direction must be provided")

if length_mode == LengthMode.DIAGONAL:
length_vector = direction * length
length_vector = direction_localized * length
elif length_mode == LengthMode.HORIZONTAL:
length_vector = direction * (length / cos(radians(angle)))
length_vector = direction_localized * (length / cos(radians(angle)))
elif length_mode == LengthMode.VERTICAL:
length_vector = direction * (length / sin(radians(angle)))
length_vector = direction_localized * (length / sin(radians(angle)))

new_edge = Edge.make_line(start, start + length_vector)

Expand Down Expand Up @@ -783,14 +779,14 @@ def __init__(
close: bool = False,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

pts = flatten_sequence(*pts)
if len(pts) < 2:
points = flatten_sequence(*pts)
if len(points) < 2:
raise ValueError("Polyline requires two or more pts")

lines_pts = WorkplaneList.localize(*pts)
lines_pts = WorkplaneList.localize(*points)

new_edges = [
Edge.make_line(lines_pts[i], lines_pts[i + 1])
Expand Down Expand Up @@ -829,7 +825,7 @@ def __init__(
short_sagitta: bool = True,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

start, end = WorkplaneList.localize(start_point, end_point)
Expand Down Expand Up @@ -875,7 +871,7 @@ def __init__(
sagitta: float,
mode: Mode = Mode.ADD,
):
context: BuildLine = BuildLine._get_context(self)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

start, end = WorkplaneList.localize(start_point, end_point)
Expand Down Expand Up @@ -915,16 +911,16 @@ class Spline(BaseEdgeObject):
def __init__(
self,
*pts: VectorLike | Iterable[VectorLike],
tangents: Iterable[VectorLike] = None,
tangent_scalars: Iterable[float] = None,
tangents: Iterable[VectorLike] | None = None,
tangent_scalars: Iterable[float] | None = None,
periodic: bool = False,
mode: Mode = Mode.ADD,
):
pts = flatten_sequence(*pts)
context: BuildLine = BuildLine._get_context(self)
points = flatten_sequence(*pts)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

spline_pts = WorkplaneList.localize(*pts)
spline_pts = WorkplaneList.localize(*points)

if tangents:
spline_tangents = [
Expand All @@ -933,10 +929,10 @@ def __init__(
else:
spline_tangents = None

if tangents and not tangent_scalars:
scalars = [1.0] * len(tangents)
if tangents is not None and tangent_scalars is None:
scalars = [1.0] * len(list(tangents))
else:
scalars = tangent_scalars
scalars = list(tangent_scalars) if tangent_scalars is not None else []

spline = Edge.make_spline(
[p if isinstance(p, Vector) else Vector(*p) for p in spline_pts],
Expand Down Expand Up @@ -979,13 +975,13 @@ def __init__(
tangent_from_first: bool = True,
mode: Mode = Mode.ADD,
):
pts = flatten_sequence(*pts)
context: BuildLine = BuildLine._get_context(self)
points = flatten_sequence(*pts)
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

if len(pts) != 2:
if len(points) != 2:
raise ValueError("tangent_arc requires two points")
arc_pts = WorkplaneList.localize(*pts)
arc_pts = WorkplaneList.localize(*points)
arc_tangent = WorkplaneList.localize(tangent).normalized()

point_indices = (0, -1) if tangent_from_first else (-1, 0)
Expand All @@ -1011,16 +1007,14 @@ class ThreePointArc(BaseEdgeObject):

_applies_to = [BuildLine._tag]

def __init__(
self, *pts: VectorLike | Iterable[VectorLike], mode: Mode = Mode.ADD
):
context: BuildLine = BuildLine._get_context(self)
def __init__(self, *pts: VectorLike | Iterable[VectorLike], mode: Mode = Mode.ADD):
context: BuildLine | None = BuildLine._get_context(self)
validate_inputs(context, self)

pts = flatten_sequence(*pts)
if len(pts) != 3:
points = flatten_sequence(*pts)
if len(points) != 3:
raise ValueError("ThreePointArc requires three points")
points = WorkplaneList.localize(*pts)
arc = Edge.make_three_point_arc(*points)
points_localized = WorkplaneList.localize(*points)
arc = Edge.make_three_point_arc(*points_localized)

super().__init__(arc, mode=mode)

0 comments on commit 61ddeed

Please sign in to comment.