gumyr/build123d

build123d imports SVG incorrectly (but ocpsvg does not show this issue)

Closed this issue · 1 comments

I've got the latest version (0.6.0) of build123d installed. I am trying to render an SVG file that contains 2 L shapes:

ll

This renders in most SVG viewers (browsers, Inkscape, etc) looking like LL.

When I use import_svg to import it to build123d:

from build123d import *

with BuildSketch() as svg:
    svg_obj = import_svg("ll.svg")
    add(svg_obj)
display(svg.sketch)

... it renders with the first L flipped along the X axis, and the second L is in the correct orientation.

If I write similar code to render this in ocpsvg directly:

from ocpsvg import ColorAndLabel, import_svg_document

svg_path = "ll.svg"

imported = list(import_svg_document(svg_path, metadata=ColorAndLabel))

for face_or_wire, color_and_label in imported:
    show_object(
        face_or_wire,
        f"{color_and_label.label} #{id(face_or_wire):x}",
        dict(color=color_and_label.stroke_color),
    )

... the output is correct - LL.

Is there something wrong with my build123d code, or is this a bug in build123d?

Thanks for raising the issue; it has nothing to do with SVG. The problem occurs because one of the "L"s is up-side-down:

show(import_svg("ll.svg"))

Screenshot from 2024-07-20 09-51-52
The same issue can be seen by just flipping a Face over:

f1 = make_face(Polyline((4, 0), (7, 0), (7, 1), (5, 1), (5, 6), (4, 6), (4, 0))).face()
f2 = -make_face(Polyline((0, 0), (3, 0), (3, 1), (1, 1), (1, 6), (0, 6), (0, 0))).face()
with BuildSketch() as s:
    add(f1)
    add(f2)
show_all()

image

The problem was in Face.is_coplanar which required that the Face normal and the Plane z_dir be equal which resulted in flipped Faces being declared non-coplanar which wasn't the desired behavior. I've relaxed this requirement to allow normal and flipped Faces to be coplanar which fixes the problem and causes your code to generate:
image