part: a Part holds exactly one solid (the constructor refuses several; make_part_rpart and Part lose bodies=)

DECISION: the product item `Part` is one solid again. The multi-body Part added on 2026-11-04
(b607c852, design-docs/multi-solid-design.md) is withdrawn. This commit is the C++ half; the Python
consumers, the editor and the docs follow in the later commits of this series, one decision each.

WHY - THE DISCUSSION THIS DECISION COMES FROM (owner and assistant, 2026-11-10)
1. Why a Part was ever allowed several solids (the owner's account). The editor must support several
   solids existing in one notebook with no assembly constraint between them: to pattern a tool and then
   cut, there is necessarily a moment where more than one tool and the body exist together. That is easy
   to understand and it is a real need. At the time the Part was the only product value a notebook could
   end in, so the Part was made to hold several named bodies.
2. Why it is the wrong place (the owner's judgement). It makes assembly hard. The whole design was made
   for a human-friendly editor and it led to a wrong decision: a multi-solid Part. The question put was:
   should we go back to "a Part has exactly one solid" and support the editor some other, better way?
3. The assistant's analysis, which the owner accepted. The solids side by side in the pattern-then-cut
   example are INTERMEDIATE VALUES OF A NOTEBOOK, not product items. A Part is a line of the bill of
   materials and the unit an Assembly places, mates and gives connectors to; "the working set of solids
   while modelling" and "the product" were two levels that got mixed. The editor's need is a PREVIEW /
   notebook-value need (show every open solid), and a deliverable that really is several solids is an
   Assembly of Parts. So: Part = one solid; the editor shows open solids as loose values (later commit).
4. Urgency. The owner asked for the audit and the change at once because the multi-body Part might be a
   malignant bug in the released 2.2.8; its GitHub release was deleted on 2026-11-10 while this was done.

WHAT THE AUDIT FOUND (checked on 2026-11-10 against the code and by running it)
- Connectors on a multi-body Part were refused outright (Part constructor, part.cpp), so no mate, no
  public connector and no ConnectorRef could ever reach one: it could join an assembly only as a rigid
  blob placed by its origin.
- explode_rassembly raised TypeError on any assembly that held one (ops_explode.cpp collect_box called
  Part::body(), which throws for a multi-body Part).
- The scene compiler scene/compiler.py read part.body in four places (compile_scene fails on a
  multi-body Part). Every consumer needed a second code path, and some were missed. Not yet checked:
  joint / constraint solving, interference and MJCF paths (they depend on connectors anyway).

ALTERNATIVE CONSIDERED AND REJECTED: keep multi-body Parts and finish stage 3 of the design (a
connector anchored by body_id + entity). It changes the part-definition contract, the interface and
binding hashes and the entity bindings, and it still leaves a concept that overlaps with Assembly (a
multi-body part is "an assembly without constraints").

WHAT THIS COMMIT DOES
- The invariant is enforced in the lowest layer so that every consumer can rely on it instead of each
  branching on `multi_body`: Part(std::vector<Body>) throws ValueError unless there is exactly one body,
  and the message says what to do (one Part per solid in an Assembly, or union / cut to one solid).
  Part::multi_body() is gone.
- Binding: make_part_rpart(part_id, body, name) and Part(part_id, body, ...) take one Solid, one Body, or
  a list of exactly one piece. `bodies=` is removed (TypeError: unexpected keyword); more than one piece
  is a ValueError with the same guidance. Part(body=Body) is how a body that carries a name / colour /
  opacity survives pickling and copying (it replaces Part(bodies=(Body,))).
- The make_part_rpart(vector<Body>) overload and the mapping / sequence parsing helpers are removed.

KEPT ON PURPOSE: Body (it carries the body's display name, colour and opacity), Part::bodies() (always
one element), body_ids(), get_body(), set_body_appearance_rpart: one-body Parts already use them and
removing them would only churn the one-body path, which did not break.

Verified with the native module directly: a Solid, a one-element list and a Body are accepted; `bodies=`,
two solids and Part(body=[a, b]) are refused with the guidance.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ni Jingzhe
2026-11-10 03:48:34 +08:00
parent 292045d3fa
commit 654a6d3e33
8 changed files with 48 additions and 275 deletions
+7 -24
View File
@@ -38,12 +38,9 @@ SCAD_SIGNATURE_NAME(DirectionOrNone, scad::pybind::DirectionTag::name + kOrNone)
SCAD_SIGNATURE_NAME(Directions, const_name("dict[str, ") + scad::pybind::DirectionTag::name + const_name("] | None"));
SCAD_SIGNATURE_NAME(Weights, const_name("dict[str, float | Expr] | None"));
SCAD_SIGNATURE_NAME(Solid, type_name<scad::Solid>());
// make_part_rpart: one Solid, or the pieces of a split cut (each a body), or several named bodies.
// make_part_rpart: one Solid (or the single piece of a split cut).
SCAD_SIGNATURE_NAME(SolidOrPieces, type_name<scad::Solid>() + kOr + const_name("collections.abc.Sequence[") +
type_name<scad::Solid>() + const_name("] | None"));
SCAD_SIGNATURE_NAME(Bodies, const_name("dict[str, ") + type_name<scad::Solid>() + kOr +
const_name("collections.abc.Sequence[") + type_name<scad::Solid>() +
const_name("]] | None"));
type_name<scad::Solid>() + const_name("]"));
SCAD_SIGNATURE_NAME(Face, type_name<scad::Face>());
SCAD_SIGNATURE_NAME(Edge, type_name<scad::Edge>());
SCAD_SIGNATURE_NAME(Vertex, type_name<scad::Vertex>());
@@ -76,7 +73,6 @@ using DirectionsArg = AnyOf<names::Directions>;
using WeightsArg = AnyOf<names::Weights>;
using SolidArg = AnyOf<names::Solid>;
using SolidOrPiecesArg = AnyOf<names::SolidOrPieces>;
using BodiesArg = AnyOf<names::Bodies>;
using FaceArg = AnyOf<names::Face>;
using EdgeArg = AnyOf<names::Edge>;
using VertexArg = AnyOf<names::Vertex>;
@@ -153,27 +149,14 @@ void bind_structure(py::module_& m) {
m.def(
"make_part_rpart",
[](const TextArg& part_id, const SolidOrPiecesArg& body, const NameArg& name, const BodiesArg& bodies) {
[](const TextArg& part_id, const SolidOrPiecesArg& body, const NameArg& name) {
return guard_operation("make_part_rpart", [&] {
if (body.is_none() == bodies.is_none())
throw scad::TypeError("give exactly one of body (a Solid) or bodies (several named solids)");
if (!bodies.is_none())
return sp::make_part_rpart(text(part_id, "part_id"), body_list(bodies), optional_name(name));
if (is_pieces(body) && py::len(body) > 1) { // a split cut: its pieces are bodies body_1 .. body_n
std::vector<sp::Body> pieces;
add_bodies(pieces, sp::kDefaultBodyId, body);
return sp::make_part_rpart(text(part_id, "part_id"), pieces, optional_name(name));
}
if (is_pieces(body))
return sp::make_part_rpart(
text(part_id, "part_id"),
typed<Solid>(py::reinterpret_borrow<py::object>(body.value[py::int_(0)]), "body"), optional_name(name));
return sp::make_part_rpart(text(part_id, "part_id"), typed<Solid>(body, "body must be a Solid"),
optional_name(name));
const std::string id = text(part_id, "part_id");
return sp::make_part_rpart(id, single_solid(body, id), optional_name(name));
});
},
py::arg("part_id"), py::arg("body") = py::none(), py::arg("name") = py::none(), py::arg("bodies") = py::none(),
"Wrap one Solid (body) or several named solids (bodies) as a semantic Part.");
py::arg("part_id"), py::arg("body"), py::arg("name") = py::none(),
"Wrap one Solid (body) as a semantic Part. A Part holds exactly one solid.");
m.def(
"assign_material_rpart",
[](const PartArg& part, const MaterialArg& material) {