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

Fix offset of faces with holes #491

Merged
merged 4 commits into from
Jan 23, 2024
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
13 changes: 8 additions & 5 deletions src/build123d/operations_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,14 @@ def offset(
outer_wire = outer_wire.fix_degenerate_edges(min_edge_length)
inner_wires = []
for inner_wire in face.inner_wires():
offset_wire = inner_wire.offset_2d(-amount, kind=kind)
if min_edge_length is not None:
inner_wires.append(offset_wire.fix_degenerate_edges(min_edge_length))
else:
inner_wires.append(offset_wire)
try:
offset_wire = inner_wire.offset_2d(-amount, kind=kind)
if min_edge_length is not None:
inner_wires.append(offset_wire.fix_degenerate_edges(min_edge_length))
else:
inner_wires.append(offset_wire)
except:
pass
new_faces.append(Face.make_from_wires(outer_wire, inner_wires))
if edges:
if len(edges) == 1 and edges[0].geom_type() == "LINE":
Expand Down
21 changes: 21 additions & 0 deletions tests/test_build_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,27 @@ def test_offset_algebra_wire(self):
o_line = offset(line, amount=3.177)
self.assertEqual(len(o_line.edges()), 19)

def test_offset_face_with_inner_wire(self):
# offset amount causes the inner wire to have zero length
b = Rectangle(1, 1)
b -= RegularPolygon(.25, 3)
b = offset(b, amount=0.125)
self.assertAlmostEqual(b.area, 1 ** 2 + 2 * 0.125 * 2 + pi * 0.125 ** 2, 5)
self.assertEqual(len(b.face().inner_wires()), 0)

def test_offset_face_with_min_length(self):
c = Circle(.5)
c = offset(c, amount=0.125, min_edge_length=0.1)
self.assertAlmostEqual(c.area, pi * (0.5 + 0.125) ** 2, 5)

def test_offset_face_with_min_length_and_inner(self):
# offset amount causes the inner wire to have zero length
c = Circle(.5)
c -= RegularPolygon(.25, 3)
c = offset(c, amount=0.125, min_edge_length=0.1)
self.assertAlmostEqual(c.area, pi * (0.5 + 0.125) ** 2, 5)
self.assertEqual(len(c.face().inner_wires()), 0)

def test_offset_bad_type(self):
with self.assertRaises(TypeError):
offset(Vertex(), amount=1)
Expand Down
Loading