diff --git a/docs/skill/references/domains/part-modeling.md b/docs/skill/references/domains/part-modeling.md index 4d0bf443..b08a10fe 100644 --- a/docs/skill/references/domains/part-modeling.md +++ b/docs/skill/references/domains/part-modeling.md @@ -70,7 +70,8 @@ consult their own documentation before the first call. - Editing an existing curve or surface (poles, weights, knots, degree, refit): `domains/curve-surface-editing.md`. - An `Edge` is taken wherever a `Wire` is (a sweep path, a loft section, a face boundary): it becomes the wire of that one edge (recorded as `make_wire_from_edges_rwire`). Several edges make one wire only through an explicit - `make_wire_from_edges_rwire([a, b, ...])`. `loft_rshell` of two open curves is the surface between them (no end wires). + `make_wire_from_edges_rwire([a, b, ...])` (in any order: they are chained by their ends). `loft_rshell` of two open + curves is the surface between them (no end wires); `sew_faces_rshell` takes faces and shells (a shell as its faces). - Features: `extrude_rsolid`, `revolve_rsolid`, `sweep_rsolid`, `twisted_sweep_rsolid` (supports inner-wire profiles), `helical_sweep_rsolid`, `loft_rsolid`, `loft_rshell`, `shell_rsolid`. diff --git a/src/cpp/bindings/python/bind_surface.cpp b/src/cpp/bindings/python/bind_surface.cpp index 7970991f..f2fd824d 100644 --- a/src/cpp/bindings/python/bind_surface.cpp +++ b/src/cpp/bindings/python/bind_surface.cpp @@ -305,11 +305,11 @@ void bind_surface(py::module_& m) { m.def( "sew_faces_rshell", - [](const std::vector& faces, double tolerance, const TagPrefix& tag_prefix) { - return sew_faces_rshell(faces, tolerance, tag_prefix); + [](const std::vector& pieces, double tolerance, const TagPrefix& tag_prefix) { + return sew_faces_rshell(pieces, tolerance, tag_prefix); }, py::arg("faces"), py::kw_only(), py::arg("tolerance") = tol::kSewing, py::arg("tag_prefix") = py::none(), - "Sew faces into exactly one connected Shell."); + "Sew faces, and the faces of shells, into exactly one connected Shell."); m.def( "make_solid_from_shell_rsolid", diff --git a/src/cpp/include/scad/surface/operators.hpp b/src/cpp/include/scad/surface/operators.hpp index 66670a94..71b36e0c 100644 --- a/src/cpp/include/scad/surface/operators.hpp +++ b/src/cpp/include/scad/surface/operators.hpp @@ -6,6 +6,7 @@ // failures as scad::Error with the Python operation name and guidance text. #pragma once +#include #include #include #include @@ -91,9 +92,17 @@ struct LoftShellTags { // endpoint wires and side faces. Shell loft_rshell(const std::vector& sections, bool ruled = false, const LoftShellTags& tags = {}); -// Sew faces into exactly one connected Shell. -Shell sew_faces_rshell(const std::vector& faces, double tolerance = tol::kSewing, +// Sew faces, and the faces of shells (a loft's surface, a shell sewn before), into exactly one connected Shell. +// TypeError for any other shape. +Shell sew_faces_rshell(const std::vector& pieces, double tolerance = tol::kSewing, const TagPrefix& tag_prefix = std::nullopt); +// A list of faces as it is (a template, so a braced list still means the pieces above). +template + requires std::same_as +Shell sew_faces_rshell(const std::vector& faces, double tolerance = tol::kSewing, + const TagPrefix& tag_prefix = std::nullopt) { + return sew_faces_rshell(std::vector(faces.begin(), faces.end()), tolerance, tag_prefix); +} // One oriented Solid from one valid closed Shell. Solid make_solid_from_shell_rsolid(const Shell& shell, const TagPrefix& tag_prefix = std::nullopt); diff --git a/src/cpp/src/surface/sewing.cpp b/src/cpp/src/surface/sewing.cpp index 081c9554..c3c78901 100644 --- a/src/cpp/src/surface/sewing.cpp +++ b/src/cpp/src/surface/sewing.cpp @@ -98,7 +98,7 @@ std::vector free_boundaries(const TopoDS_Shape& shell, double toler using namespace surface::detail; -Shell sew_faces_rshell(const std::vector& faces, double tolerance, const TagPrefix& tag_prefix) { +Shell sew_faces_rshell(const std::vector& pieces, double tolerance, const TagPrefix& tag_prefix) { static const ErrorSpec kSpec{ "sew_faces_rshell", "Failed to sew faces into one shell.", @@ -110,24 +110,35 @@ Shell sew_faces_rshell(const std::vector& faces, double tolerance, const T "When freeform patches must share exact boundary edges, build them with make_gordon_surface_rface on a shared " "edge network before sewing."}}; return operator_scope(kSpec, [&] { - if (faces.empty()) + if (pieces.empty()) throw ValueError("sew_faces_rshell requires a non-empty Face sequence"); require_positive_finite(tolerance, "tolerance"); + // a shell is sewn as its faces + std::vector faces; + for (const Shape& piece : pieces) { + if (piece.kind() == Kind::Face) + faces.push_back(Face(piece)); + else if (piece.kind() == Kind::Shell) + for (const Face& face : Shell(piece).faces()) + faces.push_back(face); + else + throw TypeError("sew_faces_rshell takes Face and Shell objects"); + } std::vector raw; for (const Face& face : faces) raw.push_back(TopoDS::Face(face.wrapped())); SewnShell sewn = sew_faces_with_history(raw, tolerance); Value params = Value::object(); - params.set("face_count", static_cast(faces.size())); + params.set("face_count", static_cast(pieces.size())); // the pieces given: faces and shells params.set("tolerance", tolerance); params.set("tag_prefix", to_value(tag_prefix)); SewnOutput output{.record = {.op = "sew_faces_rshell", .params = std::move(params)}, .tag_prefix = tag_prefix, .sources = faces, .result_face_indices = std::move(sewn.result_face_indices)}; - for (const Face& face : faces) - output.record.inputs.push_back({face, "face"}); + for (const Shape& piece : pieces) + output.record.inputs.push_back({piece, piece.kind() == Kind::Shell ? "shell" : "face"}); return finalize_surface_output(Shell(sewn.shell), std::move(output)); }); } diff --git a/src/simplecadapi/recording/serializer.py b/src/simplecadapi/recording/serializer.py index f78cd7c2..b29e6adb 100644 --- a/src/simplecadapi/recording/serializer.py +++ b/src/simplecadapi/recording/serializer.py @@ -4219,14 +4219,15 @@ def _execute_graph( ordered = _ordered_input_shapes( ctx, graph, outputs, node, params ) + # the pieces given: faces, and shells sewn as their faces if len(ordered) != int(params["face_count"]) or not all( - isinstance(item, Face) for item in ordered + isinstance(item, (Face, Shell)) for item in ordered ): ctx.fail( f"Graph node '{node.node_id}' ({op_name}) face inputs do not match face_count" ) result = ops.sew_faces_rshell( - cast(Sequence[Face], ordered), + cast(Sequence[Face | Shell], ordered), tolerance=float(params["tolerance"]), tag_prefix=cast(Optional[str], params.get("tag_prefix")), ) diff --git a/test/test_surfaces_from_curves.py b/test/test_surfaces_from_curves.py new file mode 100644 index 00000000..5ce5327b --- /dev/null +++ b/test/test_surfaces_from_curves.py @@ -0,0 +1,47 @@ +"""Surfaces from curves (edges) and a closed body from surfaces: the chain the editor offers on picked curves and +surfaces (ruled, loft, Gordon, patch; sew faces and shells; a solid from the closed shell).""" + +import pytest + +import simplecadapi as s +from simplecadapi.recording.graph import GraphSession + + +def test_a_closed_body_from_curves_through_surfaces_and_sewing(): + with GraphSession() as session: + # the four lower and four upper edges of a 10 x 10 x 10 box, as curves + p = [(0, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0)] + lower = [s.make_segment_redge(p[i], p[(i + 1) % 4]) for i in range(4)] + upper = [s.make_segment_redge((*p[i][:2], 10), (*p[(i + 1) % 4][:2], 10)) for i in range(4)] + walls = s.loft_rshell([s.make_wire_from_edges_rwire(lower), s.make_wire_from_edges_rwire(upper)], ruled=True) + floor = s.make_gordon_surface_rface([lower[0], lower[2]], [lower[1], lower[3]]) + roof = s.make_surface_patch_rface([s.SurfaceBoundary(edge) for edge in upper]) + skin = s.sew_faces_rshell([walls, floor, roof]) # a shell and two faces + assert skin.is_closed() + body = s.make_solid_from_shell_rsolid(skin) + session.capture_result(value=body) + assert body.get_volume() == pytest.approx(1000.0, rel=1e-3) + replayed = s.replay_model_json(s.export_model_json(session))[0] + assert replayed.get_volume() == pytest.approx(body.get_volume()) + + +def test_sewing_takes_faces_and_shells_and_nothing_else(): + a = s.make_segment_redge((0, 0, 0), (10, 0, 0)) + ruled = s.make_ruled_surface_rface(a, s.make_segment_redge((0, 10, 2), (10, 10, 0))) + assert len(s.ql.faces().resolve(s.sew_faces_rshell([s.sew_faces_rshell([ruled])]))) == 1 + with pytest.raises(s.SimpleCADError, match="takes Face and Shell"): + s.sew_faces_rshell([a]) + + +def test_the_preview_shows_the_surfaces_no_later_cell_builds_on(): + from simplecadapi.runtime.live import open_surfaces + from simplecadapi.scene.preview import SURFACE_NODE_PREFIX, PreviewScene, add_surfaces, decode_preview_header, encode_preview + + a = s.make_segment_redge((0, 0, 0), (10, 0, 0)) + b = s.make_interpolated_spline_redge(points=[(0, 0, 10), (5, 4, 12), (10, 0, 15)]) + values = {"a": a, "skin": s.loft_rshell([a, b]), "ruled": s.make_ruled_surface_rface(a, b), "_private": s.make_ruled_surface_rface(a, b)} + surfaces = open_surfaces(values, read={"ruled"}) # a later cell builds on `ruled` + assert list(surfaces) == ["skin"] + scene = add_surfaces(PreviewScene(parts={}, nodes=[], solids={}, definitions={}), surfaces) + header = decode_preview_header(encode_preview(scene)) + assert [node["node_id"] for node in header["nodes"]] == [SURFACE_NODE_PREFIX + "skin"]