Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor naming and typing fixes #32

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/svg_tiger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def timer(title: str):
for k, v in Counter(map(type, (o for o, _ in imported))).items()
)
)

show_object = globals().get("show_object")
if callable(show_object):
try:
for face_or_wire, color_and_label in imported:
show_object(
show_object( # type: ignore
face_or_wire,
f"{color_and_label.label} #{id(face_or_wire):x}",
dict(color=color_and_label.color),
dict(color=color_and_label.color_for(face_or_wire)),
)
except NameError: # no `show_object`
pass
6 changes: 3 additions & 3 deletions ocpsvg/ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class InvalidWiresForFace(ValueError):
pass


def faces_from_wire_soup(wires: Iterable[TopoDS_Wire]) -> Iterable[TopoDS_Face]:
def faces_from_wire_soup(wires: Iterable[TopoDS_Wire]) -> Iterator[TopoDS_Face]:
"""Make faces from unorganized, possibly nested (but non-intersecting) wires."""

wires = list(wires)
Expand Down Expand Up @@ -300,7 +300,7 @@ def circle_curve(
start_angle: float = 360,
end_angle: float = 360,
*,
clockwise: bool = False,
reversed: bool = False,
center: gp_Pnt = gp_Pnt(0, 0, 0),
normal: gp_Dir = gp_Dir(0, 0, 1),
) -> Union[Geom_Circle, Geom_TrimmedCurve]:
Expand All @@ -310,7 +310,7 @@ def circle_curve(
circle = GC_MakeCircle(circle_gp).Value()
else:
circle = GC_MakeArcOfCircle(
circle_gp, radians(start_angle), radians(end_angle), clockwise
circle_gp, radians(start_angle), radians(end_angle), reversed
).Value()

trsf = gp_Trsf()
Expand Down
24 changes: 12 additions & 12 deletions ocpsvg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def f(element: ShapeElement, parents: Sequence[ParentElement]):
####


def faces_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Face]:
def faces_from_svg_path(path: SvgPathLike) -> Iterator[TopoDS_Face]:
"""Create faces from an SVG path.

:param SvgPathLike path: input SVG path
Expand All @@ -280,7 +280,7 @@ def faces_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Face]:
return faces_from_wire_soup(wires_from_svg_path(path))


def wires_from_svg_element(element: ShapeElement) -> Iterable[TopoDS_Wire]:
def wires_from_svg_element(element: ShapeElement) -> Iterator[TopoDS_Wire]:
def check_unskewed_transform() -> Union[tuple[float, float, float], None]:
o = svgelements.Point(0, 0) * element.transform
x = svgelements.Point(1, 0) * element.transform
Expand Down Expand Up @@ -318,7 +318,7 @@ def svg_element_to_path(element: ShapeElement):
return path


def wires_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Wire]:
def wires_from_svg_path(path: SvgPathLike) -> Iterator[TopoDS_Wire]:
"""Create wires from an SVG path.

:param SvgPathLike path: input SVG path
Expand All @@ -331,7 +331,7 @@ def wires_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Wire]:
yield wire_from_continuous_edges(edges, closed=closed)


def edges_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Edge]:
def edges_from_svg_path(path: SvgPathLike) -> Iterator[TopoDS_Edge]:
"""Create edges from an SVG path.

:param SvgPathLike path: input SVG path
Expand All @@ -345,7 +345,7 @@ def edges_from_svg_path(path: SvgPathLike) -> Iterable[TopoDS_Edge]:

def continuous_edges_from_svg_path(
path: SvgPathLike,
) -> Iterable[tuple[Iterable[TopoDS_Edge], bool]]:
) -> Iterator[tuple[Iterable[TopoDS_Edge], bool]]:
def p(c: Union[svgelements.Point, None]):
if c is None: # pragma: nocover
logger.warning("found None point in %s", path)
Expand Down Expand Up @@ -431,7 +431,7 @@ def face_to_svg_path(
use_quadratics: bool = True,
use_arcs: bool = True,
split_full_arcs: bool = True,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
for wire in topoDS_iterator(face):
cmd = None
for cmd in wire_to_svg_path(
Expand All @@ -457,7 +457,7 @@ def wire_to_svg_path(
use_arcs: bool = True,
split_full_arcs: bool = True,
with_first_move: bool = True,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
edges = topoDS_iterator(wire)
if is_reversed(wire):
edges = reversed(list(edges))
Expand Down Expand Up @@ -485,7 +485,7 @@ def edge_to_svg_path(
use_arcs: bool = True,
split_full_arcs: bool = True,
with_first_move: bool = True,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
return curve_to_svg_path(
edge_to_curve(edge),
tolerance=tolerance,
Expand All @@ -508,7 +508,7 @@ def curve_to_svg_path(
split_full_arcs: bool = True,
with_first_move: bool = True,
reverse: bool = False,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
_curve, adaptor = curve_and_adaptor(curve_or_adaptor)
curve_type = adaptor.GetType()

Expand Down Expand Up @@ -586,7 +586,7 @@ def ellipse_to_svg_path(
with_first_move: bool = True,
split_full_arcs: bool = True,
reverse: bool = False,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
t0 = adaptor_curve.FirstParameter()
t1 = adaptor_curve.LastParameter()
if reverse:
Expand Down Expand Up @@ -622,7 +622,7 @@ def bezier_to_svg_path(
use_quadratics: bool = True,
with_first_move: bool = True,
reverse: bool = False,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
degree = bezier.Degree()

p0 = bezier.Pole(1)
Expand Down Expand Up @@ -674,7 +674,7 @@ def polyline_to_svg_path(
with_first_move: bool = True,
closed: bool = False,
reverse: bool = False,
) -> Iterable[SvgPathCommand]:
) -> Iterator[SvgPathCommand]:
points = list(points)
if points:
first, *others = reversed(points) if reverse else points
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ def test_bezier_curve_invalid(controls: Iterable[gp_Pnt]):
curve_to_bspline(
bezier_curve(Pnt(0, 1), Pnt(23, 45), Pnt(66, 89), Pnt(101, 213), Pnt(141, 516))
),
circle_curve(12, clockwise=True),
circle_curve(12, clockwise=False),
circle_curve(12, 30, 210, clockwise=False),
circle_curve(12, 30, 210, clockwise=True),
circle_curve(12, reversed=True),
circle_curve(12, reversed=False),
circle_curve(12, 30, 210, reversed=False),
circle_curve(12, 30, 210, reversed=True),
ellipse_curve(12, 34, clockwise=True),
ellipse_curve(12, 34, clockwise=False),
ellipse_curve(12, 34, 30, 210, clockwise=False),
Expand Down
26 changes: 23 additions & 3 deletions tests/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,25 @@ def test_path_regression3():
assert len(face_inner_wires(faces[0])) == 3


def test_path_issue20():
"""This path used to not close properly, likely due to accumulated error with the relative commands."""
d = """
M 104.11876,56.934021
C 63.736597,56.737648 25.41552,85.182615 13.677255,126.80417
c -16.0876352,57.04356 23.474457,116.30107 80.550639,120.65207
5.762765,0.4393 20.717906,0.12691 23.944796,-0.50023
29.53134,-5.73943 50.78323,-20.45287 66.04713,-45.72692
4.31274,-7.14105 7.72319,-15.32478 10.39678,-24.94783
13.17482,-47.42016 -14.87779,-98.88564 -62.53934,-114.734601
-3.61363,-1.201644 -4.26535,-1.369129 -10.59728,-2.729549
-5.78154,-1.242168 -11.59234,-1.855036 -17.36122,-1.883089
z """
wires = list(wires_from_svg_path(d))
assert len(wires) == 1
assert is_valid(wires[0])
assert len(list(topoDS_iterator(wires[0]))) == 8


def test_arcs_path_to_wire():
"""this continuous path introduces small discontinuities when making the edges"""
res = list(
Expand Down Expand Up @@ -655,7 +674,7 @@ def test_svg_doc_metadata_legacy():
</svg>
"""
buf = StringIO(svg_src)
imported = list(import_svg_document(buf, metadata=ColorAndLabel.Label_by("class")))
imported = import_svg_document(buf, metadata=ColorAndLabel.Label_by("class"))
assert [
(metadata.label, metadata.color_for(shape)) for shape, metadata in imported
] == [
Expand All @@ -674,7 +693,7 @@ def test_svg_doc_metadata():
</svg>
"""
buf = StringIO(svg_src)
imported = list(import_svg_document(buf, metadata=ColorAndLabel.Label_by("class")))
imported = import_svg_document(buf, metadata=ColorAndLabel.Label_by("class"))
assert [
(metadata.label, metadata.color_for(shape)) for shape, metadata in imported
] == [
Expand Down Expand Up @@ -702,7 +721,8 @@ def test_svg_nested_use_metadata():
</g>
</svg>"""
buf = StringIO(svg_src)
imported = list(import_svg_document(buf, metadata=ColorAndLabel.Label_by("id")))
imported = import_svg_document(buf, metadata=ColorAndLabel.Label_by("id"))
imported = list(imported)

expected = (
(("svg1", "main", "white_stroke", "two-circles", "blue_fill"), "circle"),
Expand Down
Loading