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

Added Shell.loft #767

Merged
merged 3 commits into from
Nov 8, 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
104 changes: 86 additions & 18 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -6977,6 +6977,29 @@
builder.Build()
return Shape.cast(builder.Shape())

@classmethod
def make_loft(
cls, objs: Iterable[Union[Vertex, Wire]], ruled: bool = False
) -> Shell:
"""make loft

Makes a loft from a list of wires and vertices.
Vertices can appear only at the beginning or end of the list, but cannot appear consecutively within the list
nor between wires.
Wires may be closed or opened.

Args:
objs (list[Vertex, Wire]): wire perimeters or vertices
ruled (bool, optional): stepped or smooth. Defaults to False (smooth).

Raises:
ValueError: Too few wires

Returns:
Shell: Lofted object
"""
return cls(_make_loft(objs, False, ruled))


class Solid(Mixin3D, Shape):
"""A Solid in build123d represents a three-dimensional solid geometry
Expand Down Expand Up @@ -7240,8 +7263,9 @@
) -> Solid:
"""make loft

Makes a loft from a list of wires and vertices, where vertices can be the first,
last, or first and last elements.
Makes a loft from a list of wires and vertices.
Vertices can appear only at the beginning or end of the list, but cannot appear consecutively within the list
nor between wires.

Args:
objs (list[Vertex, Wire]): wire perimeters or vertices
Expand All @@ -7253,22 +7277,7 @@
Returns:
Solid: Lofted object
"""

if len(objs) < 2:
raise ValueError("More than one wire, or a wire and a vertex is required")

# the True flag requests building a solid instead of a shell.
loft_builder = BRepOffsetAPI_ThruSections(True, ruled)

for obj in objs:
if isinstance(obj, Vertex):
loft_builder.AddVertex(obj.wrapped)
elif isinstance(obj, Wire):
loft_builder.AddWire(obj.wrapped)

loft_builder.Build()

return cls(loft_builder.Shape())
return cls(_make_loft(objs, True, ruled))

@classmethod
def make_wedge(
Expand Down Expand Up @@ -8852,6 +8861,65 @@
raise NotImplementedError


def _make_loft(
objs: Iterable[Union[Vertex, Wire]],
filled: bool,
ruled: bool = False,
) -> TopoDS_Shape:
"""make loft

Makes a loft from a list of wires and vertices.
Vertices can appear only at the beginning or end of the list, but cannot appear consecutively within the list
nor between wires.

Args:
wires (list[Wire]): section perimeters
ruled (bool, optional): stepped or smooth. Defaults to False (smooth).

Raises:
ValueError: Too few wires

Returns:
TopoDS_Shape: Lofted object
"""
if len(objs) < 2:
raise ValueError("More than one wire is required")
vertices = [obj for obj in objs if isinstance(obj, Vertex)]
vertex_count = len(vertices)

if vertex_count > 2:
raise ValueError("Only two vertices are allowed")

if vertex_count == 1 and not (
isinstance(objs[0], Vertex) or isinstance(objs[-1], Vertex)
):
raise ValueError(
"The vertex must be either at the beginning or end of the list"
)

if vertex_count == 2:
if len(objs) == 2:
raise ValueError(
"You can't have only 2 vertices to loft; try adding some wires"
)
if not (isinstance(objs[0], Vertex) and isinstance(objs[-1], Vertex)):
raise ValueError(
"The vertices must be at the beginning and end of the list"
)

loft_builder = BRepOffsetAPI_ThruSections(filled, ruled)

for obj in objs:

Check warning on line 8912 in src/build123d/topology.py

View check run for this annotation

Codecov / codecov/patch

src/build123d/topology.py#L8912

Added line #L8912 was not covered by tests
if isinstance(obj, Vertex):
loft_builder.AddVertex(obj.wrapped)
elif isinstance(obj, Wire):
loft_builder.AddWire(obj.wrapped)

loft_builder.Build()

return loft_builder.Shape()


def downcast(obj: TopoDS_Shape) -> TopoDS_Shape:
"""Downcasts a TopoDS object to suitable specialized type

Expand Down
26 changes: 26 additions & 0 deletions tests/test_direct_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3687,6 +3687,15 @@ def test_sweep(self):
self.assertEqual(len(sweep_w_w.faces()), 4)
self.assertEqual(len(sweep_c2_c2.faces()), 4)

def test_make_loft(self):
r = 3
h = 2
loft = Shell.make_loft(
[Wire.make_circle(r, Plane((0, 0, h))), Wire.make_circle(r)]
)
self.assertEqual(loft.volume, 0, "A shell has no volume")
cylinder_area = 2 * math.pi * r * h
self.assertAlmostEqual(loft.area, cylinder_area)

class TestSolid(DirectApiTestCase):
def test_make_solid(self):
Expand Down Expand Up @@ -3813,6 +3822,23 @@ def test_make_loft(self):
with self.assertRaises(ValueError):
Solid.make_loft([Wire.make_rect(1, 1)])

def test_make_loft_with_vertices(self):
loft = Solid.make_loft(
[Vertex(0, 0, -1), Wire.make_rect(1, 1.5), Vertex(0, 0, 1)], True
)
self.assertAlmostEqual(loft.volume, 1, 5)

with self.assertRaises(ValueError):
Solid.make_loft(
[Wire.make_rect(1, 1), Vertex(0, 0, 1), Wire.make_rect(1, 1)]
)

with self.assertRaises(ValueError):
Solid.make_loft([Vertex(0, 0, 1), Vertex(0, 0, 2)])

with self.assertRaises(ValueError):
Solid.make_loft([Vertex(0, 0, 1),Wire.make_rect(1, 1), Vertex(0, 0, 2), Vertex(0, 0, 3)])

def test_extrude_until(self):
square = Face.make_rect(1, 1)
box = Solid.make_box(4, 4, 1, Plane((-2, -2, 3)))
Expand Down