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
+12 -19
View File
@@ -424,7 +424,7 @@ void bind_constraints(py::module_& m) {
void bind_part(py::module_& m) {
using namespace sp;
py::class_<Body, py::smart_holder>(m, "Body", "One named solid of a Part; a multi-body Part keeps several apart.")
py::class_<Body, py::smart_holder>(m, "Body", "The solid of a Part, with its display name, colour and opacity.")
.def(py::init([](const py::object& body_id, const py::object& solid, const py::object& name,
const py::object& color, const py::object& opacity) {
std::optional<double> opacity_value;
@@ -454,10 +454,9 @@ void bind_part(py::module_& m) {
return out + ")";
});
py::class_<Part, py::smart_holder> cls(
m, "Part", "Product item holding one Solid (`body`) or several named bodies (`bodies`), never placed apart.");
py::class_<Part, py::smart_holder> cls(m, "Part", "Product item holding exactly one Solid (`body`).");
cls.def(py::init([](const py::object& part_id, const py::object& body, const py::object& name,
const py::object& material, const py::object& connectors, const py::object& bodies) {
const py::object& material, const py::object& connectors) {
std::optional<Material> material_value;
if (!material.is_none())
material_value = typed<Material>(material, "material must be a Material");
@@ -468,30 +467,24 @@ void bind_part(py::module_& m) {
"connectors must contain Connector values"));
}
}
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() || is_pieces(body)) { // several bodies, or the pieces of a split cut
std::vector<Body> values;
if (bodies.is_none())
add_bodies(values, kDefaultBodyId, body);
else
values = body_list(bodies);
return std::make_shared<Part>(text(part_id, "part_id"), std::move(values), optional_name(name),
if (py::isinstance<Body>(body)) { // a Body keeps its id, display name and appearance (pickle, copy)
std::vector<Body> only{typed<Body>(body, "body must be a Solid or a Body")};
return std::make_shared<Part>(text(part_id, "part_id"), std::move(only), optional_name(name),
material_value, std::move(connector_values));
}
return std::make_shared<Part>(text(part_id, "part_id"), typed<Solid>(body, "body must be a Solid"),
return std::make_shared<Part>(text(part_id, "part_id"), single_solid(body, text(part_id, "part_id")),
optional_name(name), material_value, std::move(connector_values));
}),
py::arg("part_id"), py::arg("body") = py::none(), py::arg("name") = py::none(),
py::arg("material") = py::none(), py::arg("connectors") = py::tuple(), py::arg("bodies") = py::none())
.def_property_readonly("bodies", [](const Part& self) { return to_tuple(self.bodies()); })
.def_property_readonly("multi_body", &Part::multi_body)
py::arg("part_id"), py::arg("body"), py::arg("name") = py::none(), py::arg("material") = py::none(),
py::arg("connectors") = py::tuple())
.def_property_readonly("bodies", [](const Part& self) { return to_tuple(self.bodies()); },
"The Part's body as a one-element tuple (the Body carries the display appearance).")
.def("body_ids", [](const Part& self) { return to_tuple(self.body_ids()); })
.def(
"get_body", [](const Part& self, const py::object& id) { return self.get_body(text(id, "body_id")); },
py::arg("body_id"))
.def_property_readonly("part_id", &Part::part_id)
.def_property_readonly("body", &Part::body, "The Solid of a one-body Part; TypeError for a multi-body Part.")
.def_property_readonly("body", &Part::body, "The Solid of the Part.")
.def_property_readonly("name", [](const Part& self) { return optional_object(self.name()); })
.def_property_readonly(
"material",