Skip to content

Commit

Permalink
Merge branch 'dev' into ocp781
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegenstein authored Jan 13, 2025
2 parents 453ae63 + 629572d commit b665cb5
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 264 deletions.
11 changes: 10 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ignore_missing_imports = True
[mypy-build123d.topology.jupyter_tools.*]
ignore_missing_imports = True

[mypy-IPython.lib.pretty.*]
[mypy-IPython.*]
ignore_missing_imports = True

[mypy-numpy.*]
Expand All @@ -28,3 +28,12 @@ ignore_missing_imports = True

[mypy-vtkmodules.*]
ignore_missing_imports = True

[mypy-ezdxf.*]
ignore_missing_imports = True

[mypy-setuptools_scm.*]
ignore_missing_imports = True

[mypy-py_lib3mf.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion src/build123d/build_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def __init__(
self.builder_parent = None
self.lasts: dict = {Vertex: [], Edge: [], Face: [], Solid: []}
self.workplanes_context = None
self.exit_workplanes = None
self.exit_workplanes: list[Plane] = []
self.obj_before: Shape | None = None
self.to_combine: list[Shape] = []

Expand Down
77 changes: 44 additions & 33 deletions src/build123d/drafting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from dataclasses import dataclass
from datetime import date
from math import copysign, floor, gcd, log2, pi
from typing import ClassVar, Optional, Union
from typing import cast, ClassVar, TypeAlias

from collections.abc import Iterable

Expand Down Expand Up @@ -102,7 +102,7 @@ class Arrow(BaseSketchObject):
Args:
arrow_size (float): arrow head tip to tail length
shaft_path (Union[Edge, Wire]): line describing the shaft shape
shaft_path (Edge | Wire): line describing the shaft shape
shaft_width (float): line width of shaft
head_at_start (bool, optional): Defaults to True.
head_type (HeadType, optional): arrow head shape. Defaults to HeadType.CURVED.
Expand Down Expand Up @@ -141,17 +141,15 @@ def __init__(
shaft_pen = shaft_path.perpendicular_line(shaft_width, 0)
shaft = sweep(shaft_pen, shaft_path, mode=Mode.PRIVATE)

arrow = arrow_head.fuse(shaft).clean()
arrow = cast(Compound, arrow_head.fuse(shaft)).clean()

super().__init__(arrow, rotation=0, align=None, mode=mode)


PathDescriptor = Union[
Wire,
Edge,
list[Union[Vector, Vertex, tuple[float, float, float]]],
]
PointLike = Union[Vector, Vertex, tuple[float, float, float]]
PointLike: TypeAlias = Vector | Vertex | tuple[float, float, float]
"""General type for points in 3D space"""
PathDescriptor: TypeAlias = Wire | Edge | list[PointLike]
"""General type for a path in 3D space"""


@dataclass
Expand Down Expand Up @@ -223,7 +221,7 @@ def _round_to_str(self, number: float) -> str:
def _number_with_units(
self,
number: float,
tolerance: float | tuple[float, float] = None,
tolerance: float | tuple[float, float] | None = None,
display_units: bool | None = None,
) -> str:
"""Convert a raw number to a unit of measurement string based on the class settings"""
Expand Down Expand Up @@ -295,7 +293,7 @@ def _process_path(path: PathDescriptor) -> Edge | Wire:

def _label_to_str(
self,
label: str,
label: str | None,
line_wire: Wire,
label_angle: bool,
tolerance: float | tuple[float, float] | None,
Expand Down Expand Up @@ -351,7 +349,7 @@ class DimensionLine(BaseSketchObject):
argument is desired not an actual measurement. Defaults to None.
arrows (tuple[bool, bool], optional): a pair of boolean values controlling the placement
of the start and end arrows. Defaults to (True, True).
tolerance (Union[float, tuple[float, float]], optional): an optional tolerance
tolerance (float | tuple[float, float], optional): an optional tolerance
value to add to the extracted length value. If a single tolerance value is provided
it is shown as ± the provided value while a pair of values are shown as
separate + and - values. Defaults to None.
Expand All @@ -368,14 +366,14 @@ class DimensionLine(BaseSketchObject):
def __init__(
self,
path: PathDescriptor,
draft: Draft = None,
sketch: Sketch = None,
label: str = None,
draft: Draft,
sketch: Sketch | None = None,
label: str | None = None,
arrows: tuple[bool, bool] = (True, True),
tolerance: float | tuple[float, float] = None,
tolerance: float | tuple[float, float] | None = None,
label_angle: bool = False,
mode: Mode = Mode.ADD,
) -> Sketch:
):
# pylint: disable=too-many-locals

context = BuildSketch._get_context(self)
Expand Down Expand Up @@ -452,22 +450,35 @@ def __init__(
flip_label = path_obj.tangent_at(u_value).get_angle(Vector(1, 0, 0)) >= 180
loc = Draft._sketch_location(path_obj, u_value, flip_label)
placed_label = label_shape.located(loc)
self_intersection = Sketch.intersect(d_line, placed_label).area
self_intersection = cast(
Sketch | None, Sketch.intersect(d_line, placed_label)
)
if self_intersection is None:
self_intersection_area = 0.0
else:
self_intersection_area = self_intersection.area
d_line += placed_label
bbox_size = d_line.bounding_box().size

# Minimize size while avoiding intersections
common_area = (
0.0 if sketch is None else Sketch.intersect(d_line, sketch).area
)
common_area += self_intersection
if sketch is None:
common_area = 0.0
else:
line_intersection = cast(
Sketch | None, Sketch.intersect(d_line, sketch)
)
if line_intersection is None:
common_area = 0.0
else:
common_area = line_intersection.area
common_area += self_intersection_area
score = (d_line.area - 10 * common_area) / bbox_size.X
d_lines[d_line] = score

# Sort by score to find the best option
d_lines = sorted(d_lines.items(), key=lambda x: x[1])
sorted_d_lines = sorted(d_lines.items(), key=lambda x: x[1])

super().__init__(obj=d_lines[-1][0], rotation=0, align=None, mode=mode)
super().__init__(obj=sorted_d_lines[-1][0], rotation=0, align=None, mode=mode)


class ExtensionLine(BaseSketchObject):
Expand All @@ -489,7 +500,7 @@ class ExtensionLine(BaseSketchObject):
is desired not an actual measurement. Defaults to None.
arrows (tuple[bool, bool], optional): a pair of boolean values controlling the placement
of the start and end arrows. Defaults to (True, True).
tolerance (Union[float, tuple[float, float]], optional): an optional tolerance
tolerance (float | tuple[float, float], optional): an optional tolerance
value to add to the extracted length value. If a single tolerance value is provided
it is shown as ± the provided value while a pair of values are shown as
separate + and - values. Defaults to None.
Expand All @@ -507,12 +518,12 @@ def __init__(
border: PathDescriptor,
offset: float,
draft: Draft,
sketch: Sketch = None,
label: str = None,
sketch: Sketch | None = None,
label: str | None = None,
arrows: tuple[bool, bool] = (True, True),
tolerance: float | tuple[float, float] = None,
tolerance: float | tuple[float, float] | None = None,
label_angle: bool = False,
project_line: VectorLike = None,
project_line: VectorLike | None = None,
mode: Mode = Mode.ADD,
):
# pylint: disable=too-many-locals
Expand All @@ -531,7 +542,7 @@ def __init__(
if offset == 0:
raise ValueError("A dimension line should be used if offset is 0")
dimension_path = object_to_measure.offset_2d(
distance=offset, side=side_lut[copysign(1, offset)], closed=False
distance=offset, side=side_lut[int(copysign(1, offset))], closed=False
)
dimension_label_str = (
label
Expand Down Expand Up @@ -629,7 +640,7 @@ def __init__(
title: str = "Title",
sub_title: str = "Sub Title",
drawing_number: str = "B3D-1",
sheet_number: int = None,
sheet_number: int | None = None,
drawing_scale: float = 1.0,
nominal_text_size: float = 10.0,
line_width: float = 0.5,
Expand Down Expand Up @@ -691,12 +702,12 @@ def __init__(
4: 3 / 12,
5: 5 / 12,
}
for i, label in enumerate(["F", "E", "D", "C", "B", "A"]):
for i, grid_label in enumerate(["F", "E", "D", "C", "B", "A"]):
for y_index in [-0.5, 0.5]:
grid_labels += Pos(
x_centers[i] * frame_width,
y_index * (frame_height + 1.5 * nominal_text_size),
) * Sketch(Compound.make_text(label, nominal_text_size).wrapped)
) * Sketch(Compound.make_text(grid_label, nominal_text_size).wrapped)

# Text Box Frame
bf_pnt1 = frame_wire.edges().sort_by(Axis.Y)[0] @ 0.5
Expand Down
Loading

0 comments on commit b665cb5

Please sign in to comment.