sdk: sew_faces_rshell takes faces and shells (a shell is sewn as its faces; recorded with its pieces, replayed the same), a TypeError for anything else

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-11-06 19:04:16 +08:00
parent 6f1c2485b7
commit 6813528ab2
6 changed files with 82 additions and 13 deletions
@@ -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`.
+3 -3
View File
@@ -305,11 +305,11 @@ void bind_surface(py::module_& m) {
m.def(
"sew_faces_rshell",
[](const std::vector<Face>& faces, double tolerance, const TagPrefix& tag_prefix) {
return sew_faces_rshell(faces, tolerance, tag_prefix);
[](const std::vector<Shape>& 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",
+11 -2
View File
@@ -6,6 +6,7 @@
// failures as scad::Error with the Python operation name and guidance text.
#pragma once
#include <concepts>
#include <optional>
#include <string>
#include <vector>
@@ -91,9 +92,17 @@ struct LoftShellTags {
// endpoint wires and side faces.
Shell loft_rshell(const std::vector<LoftSection>& sections, bool ruled = false, const LoftShellTags& tags = {});
// Sew faces into exactly one connected Shell.
Shell sew_faces_rshell(const std::vector<Face>& 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<Shape>& 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 <class F>
requires std::same_as<F, Face>
Shell sew_faces_rshell(const std::vector<F>& faces, double tolerance = tol::kSewing,
const TagPrefix& tag_prefix = std::nullopt) {
return sew_faces_rshell(std::vector<Shape>(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);
+16 -5
View File
@@ -98,7 +98,7 @@ std::vector<TopoDS_Wire> free_boundaries(const TopoDS_Shape& shell, double toler
using namespace surface::detail;
Shell sew_faces_rshell(const std::vector<Face>& faces, double tolerance, const TagPrefix& tag_prefix) {
Shell sew_faces_rshell(const std::vector<Shape>& 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<Face>& 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<Face> 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<TopoDS_Face> 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<std::int64_t>(faces.size()));
params.set("face_count", static_cast<std::int64_t>(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));
});
}
+3 -2
View File
@@ -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")),
)
+47
View File
@@ -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"]