Skip to content

Commit

Permalink
Fixed objects_part.py typing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Jan 12, 2025
1 parent 18957ef commit 475bf42
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/build123d/objects_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@

from math import radians, tan

from typing import Union
from build123d.build_common import LocationList, validate_inputs
from build123d.build_enums import Align, Mode
from build123d.build_part import BuildPart
from build123d.geometry import Location, Plane, Rotation, RotationLike, Vector
from build123d.topology import Compound, Part, Solid, tuplify
from build123d.geometry import Location, Plane, Rotation, RotationLike
from build123d.topology import Compound, Part, ShapeList, Solid, tuplify


class BasePartObject(Part):
Expand All @@ -46,7 +45,7 @@ class BasePartObject(Part):
Args:
solid (Solid): object to create
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to None.
mode (Mode, optional): combination mode. Defaults to Mode.ADD.
"""
Expand All @@ -57,7 +56,7 @@ def __init__(
self,
part: Part | Solid,
rotation: RotationLike = (0, 0, 0),
align: Align | tuple[Align, Align, Align] = None,
align: Align | tuple[Align, Align, Align] | None = None,
mode: Mode = Mode.ADD,
):
if align is not None:
Expand All @@ -66,7 +65,7 @@ def __init__(
offset = bbox.to_align_offset(align)
part.move(Location(offset))

context: BuildPart = BuildPart._get_context(self, log=False)
context: BuildPart | None = BuildPart._get_context(self, log=False)
rotate = Rotation(*rotation) if isinstance(rotation, tuple) else rotation
self.rotation = rotate
if context is None:
Expand Down Expand Up @@ -111,7 +110,7 @@ class Box(BasePartObject):
width (float): box size
height (float): box size
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -131,7 +130,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.length = length
Expand All @@ -156,7 +155,7 @@ class Cone(BasePartObject):
height (float): cone size
arc_size (float, optional): angular size of cone. Defaults to 360.
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -177,7 +176,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.bottom_radius = bottom_radius
Expand Down Expand Up @@ -218,10 +217,10 @@ def __init__(
radius: float,
counter_bore_radius: float,
counter_bore_depth: float,
depth: float = None,
depth: float | None = None,
mode: Mode = Mode.SUBTRACT,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.radius = radius
Expand All @@ -235,7 +234,7 @@ def __init__(
raise ValueError("No depth provided")
self.mode = mode

solid = Solid.make_cylinder(
fused = Solid.make_cylinder(
radius, self.hole_depth, Plane(origin=(0, 0, 0), z_dir=(0, 0, -1))
).fuse(
Solid.make_cylinder(
Expand All @@ -244,6 +243,10 @@ def __init__(
Plane((0, 0, -counter_bore_depth)),
)
)
if isinstance(fused, ShapeList):
solid = Part(fused)
else:
solid = fused
super().__init__(part=solid, rotation=(0, 0, 0), mode=mode)


Expand All @@ -266,11 +269,11 @@ def __init__(
self,
radius: float,
counter_sink_radius: float,
depth: float = None,
depth: float | None = None,
counter_sink_angle: float = 82, # Common tip angle
mode: Mode = Mode.SUBTRACT,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.radius = radius
Expand All @@ -285,7 +288,7 @@ def __init__(
self.mode = mode
cone_height = counter_sink_radius / tan(radians(counter_sink_angle / 2.0))

solid = Solid.make_cylinder(
fused = Solid.make_cylinder(
radius, self.hole_depth, Plane(origin=(0, 0, 0), z_dir=(0, 0, -1))
).fuse(
Solid.make_cone(
Expand All @@ -296,6 +299,11 @@ def __init__(
),
Solid.make_cylinder(counter_sink_radius, self.hole_depth),
)
if isinstance(fused, ShapeList):
solid = Part(fused)
else:
solid = fused

super().__init__(part=solid, rotation=(0, 0, 0), mode=mode)


Expand All @@ -309,7 +317,7 @@ class Cylinder(BasePartObject):
height (float): cylinder size
arc_size (float, optional): angular size of cone. Defaults to 360.
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -329,7 +337,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.radius = radius
Expand Down Expand Up @@ -363,10 +371,10 @@ class Hole(BasePartObject):
def __init__(
self,
radius: float,
depth: float = None,
depth: float | None = None,
mode: Mode = Mode.SUBTRACT,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.radius = radius
Expand Down Expand Up @@ -405,7 +413,7 @@ class Sphere(BasePartObject):
arc_size2 (float, optional): angular size of sphere. Defaults to 90.
arc_size3 (float, optional): angular size of sphere. Defaults to 360.
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -426,7 +434,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.radius = radius
Expand Down Expand Up @@ -458,7 +466,7 @@ class Torus(BasePartObject):
major_arc_size (float, optional): angular size of torus. Defaults to 0.
minor_arc_size (float, optional): angular size or torus. Defaults to 360.
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -480,7 +488,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

self.major_radius = major_radius
Expand Down Expand Up @@ -516,7 +524,7 @@ class Wedge(BasePartObject):
xmax (float): maximum X location
zmax (float): maximum Z location
rotation (RotationLike, optional): angles to rotate about axes. Defaults to (0, 0, 0).
align (Union[Align, tuple[Align, Align, Align]], optional): align min, center,
align (Align | tuple[Align, Align, Align] | None, optional): align min, center,
or max of object. Defaults to (Align.CENTER, Align.CENTER, Align.CENTER).
mode (Mode, optional): combine mode. Defaults to Mode.ADD.
"""
Expand All @@ -540,7 +548,7 @@ def __init__(
),
mode: Mode = Mode.ADD,
):
context: BuildPart = BuildPart._get_context(self)
context: BuildPart | None = BuildPart._get_context(self)
validate_inputs(context, self)

if any([value <= 0 for value in [xsize, ysize, zsize]]):
Expand Down

0 comments on commit 475bf42

Please sign in to comment.