654a6d3e33
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>
240 lines
10 KiB
C++
240 lines
10 KiB
C++
// Materials, placements, parts, and the assembly structure operators.
|
|
#include "scad/core/coordinate_system.hpp"
|
|
#include "scad/core/error.hpp"
|
|
#include "scad/ops/geometry/params.hpp"
|
|
#include "scad/product/guard.hpp"
|
|
#include "scad/product/operators.hpp"
|
|
#include "scad/reactive/lineage.hpp"
|
|
#include "record.hpp"
|
|
|
|
namespace scad::product {
|
|
|
|
using namespace detail;
|
|
|
|
namespace {
|
|
|
|
// The recorded reference of a Part's body, as `_serialize_shape_refs` writes it.
|
|
Value input_refs(const Shape& body) {
|
|
std::optional<TopoRef> ref = reactive::topo_ref_of(body);
|
|
if (!ref) {
|
|
if (const auto lineage = reactive::lineage_of(body)) {
|
|
ref = TopoRef{lineage->graph_id, lineage->node_id, lineage->output_slot, body.kind(), body.topo_id()};
|
|
}
|
|
}
|
|
Value refs = Value::array();
|
|
if (!ref)
|
|
return refs;
|
|
const kernel::BoundingBox box = body.bounding_box();
|
|
Value hint = Value::object();
|
|
hint.set("kind", std::string(kind_name(body.kind())));
|
|
Value tags = Value::array();
|
|
for (const std::string& tag : body.list_tags())
|
|
tags.push_back(tag);
|
|
hint.set("tags", std::move(tags));
|
|
hint.set("volume", Solid(body).volume());
|
|
Value bounds = Value::object();
|
|
bounds.set("min", to_array({box.xmin, box.ymin, box.zmin}));
|
|
bounds.set("max", to_array({box.xmax, box.ymax, box.zmax}));
|
|
hint.set("bbox", std::move(bounds));
|
|
Value data = to_value(*ref);
|
|
data.set("selector_hint", std::move(hint));
|
|
refs.push_back(std::move(data));
|
|
return refs;
|
|
}
|
|
|
|
// A coordinate triple as np.asarray(dtype=float) reads it: a NaN literal passes, and is rejected by
|
|
// the coordinate system's own structured error, as in Python.
|
|
Vec3 raw_point(const Scalar3& point) {
|
|
const auto number = [](const Scalar& s) { return s.symbolic() ? s.value() : s.literal(); };
|
|
return {number(point[0]), number(point[1]), number(point[2])};
|
|
}
|
|
|
|
Value optional_name(const OptionalString& name) {
|
|
return name ? Value(*name) : Value();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Material make_material_rmaterial(const std::string& material_id, const OptionalString& name,
|
|
const std::optional<Scalar>& density, const OptionalString& density_unit,
|
|
const std::optional<Vec3>& color) {
|
|
return guard_operation("make_material_rmaterial", [&] {
|
|
OptionalDouble density_value;
|
|
if (density)
|
|
density_value = scalar_float(*density);
|
|
Material material(material_id, name, density_value, density_unit, color);
|
|
reserve_semantic_id("material", material.material_id());
|
|
record_value(
|
|
[&] {
|
|
OpRecord record = op_record("make_material_rmaterial", material.to_dict());
|
|
record.semantic_delta = semantic_created("Material", material.material_id(), material.to_dict());
|
|
return record;
|
|
},
|
|
material);
|
|
return material;
|
|
});
|
|
}
|
|
|
|
Placement make_placement_rplacement(const Scalar3& origin, const Scalar3& x_axis, const Scalar3& y_axis) {
|
|
return guard_operation("make_placement_rplacement", [&] {
|
|
const CoordinateSystem& cs = current_cs();
|
|
Placement placement(cs.transform_point(raw_point(origin)), cs.transform_vector(raw_point(x_axis)),
|
|
cs.transform_vector(raw_point(y_axis)));
|
|
record_value(
|
|
[&] {
|
|
return OpParams("make_placement_rplacement")
|
|
.point("origin", origin)
|
|
.point("x_axis", x_axis)
|
|
.point("y_axis", y_axis)
|
|
.take();
|
|
},
|
|
placement);
|
|
return placement;
|
|
});
|
|
}
|
|
|
|
Placement identity_placement_rplacement() {
|
|
return guard_operation("identity_placement_rplacement", [&] {
|
|
const CoordinateSystem& cs = current_cs();
|
|
Placement placement(cs.origin(), cs.x_axis(), cs.y_axis());
|
|
record_value([&] { return op_record("make_identity_placement_rplacement", Value::object()); }, placement);
|
|
return placement;
|
|
});
|
|
}
|
|
|
|
PartPtr make_part_rpart(const std::string& part_id, const Solid& body, const OptionalString& name) {
|
|
return guard_operation("make_part_rpart", [&] {
|
|
auto part = std::make_shared<const Part>(part_id, body, name);
|
|
reserve_semantic_id("part", part->part_id());
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("part_id", part->part_id());
|
|
params.set("name", optional_name(part->name()));
|
|
params.set("input_refs", input_refs(body));
|
|
OpRecord record = op_record("make_part_rpart", std::move(params));
|
|
record.inputs.push_back(OpInput{body, "body"});
|
|
record.semantic_delta = semantic_created("Part", part->part_id(), part->to_dict());
|
|
return record;
|
|
},
|
|
part);
|
|
return part;
|
|
});
|
|
}
|
|
|
|
PartPtr assign_material_rpart(const Part& part, const Material& material) {
|
|
return guard_operation("assign_material_rpart", [&] {
|
|
auto result = std::make_shared<const Part>(part.with_material(material));
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("part_id", part.part_id());
|
|
params.set("material_id", material.material_id());
|
|
params.set("material", material.to_dict());
|
|
OpRecord record = op_record("make_assign_material_rpart", std::move(params));
|
|
Value metadata = Value::object();
|
|
metadata.set("material_id", material.material_id());
|
|
record.semantic_delta = semantic_modified("Part", part.part_id(), std::move(metadata));
|
|
return record;
|
|
},
|
|
result, part, material);
|
|
return result;
|
|
});
|
|
}
|
|
|
|
PartPtr set_body_appearance_rpart(const Part& part, const std::string& body_id, const std::optional<Vec3>& color,
|
|
const OptionalDouble& opacity, const OptionalString& name, bool reset) {
|
|
return guard_operation("set_body_appearance_rpart", [&] {
|
|
const Body& body = part.get_body(body_id);
|
|
const Body updated = body.with_appearance(name ? name : (reset ? OptionalString{} : body.name()),
|
|
color ? color : (reset ? std::optional<Vec3>{} : body.color()),
|
|
opacity ? opacity : (reset ? OptionalDouble{} : body.opacity()));
|
|
auto result = std::make_shared<const Part>(part.with_body(updated));
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("part_id", part.part_id());
|
|
params.set("body_id", updated.body_id());
|
|
params.set("color", color ? to_array(*updated.color()) : Value());
|
|
params.set("opacity", opacity ? Value(*updated.opacity()) : Value());
|
|
params.set("name", name ? Value(*updated.name()) : Value());
|
|
params.set("reset", reset);
|
|
OpRecord record = op_record("make_set_body_appearance_rpart", std::move(params));
|
|
record.semantic_delta = semantic_modified("Part", part.part_id(), updated.to_dict());
|
|
return record;
|
|
},
|
|
result, part);
|
|
return result;
|
|
});
|
|
}
|
|
|
|
AssemblyPtr make_assembly_rassembly(const std::string& assembly_id, const OptionalString& name) {
|
|
return guard_operation("make_assembly_rassembly", [&] {
|
|
auto assembly = std::make_shared<const Assembly>(assembly_id, name);
|
|
reserve_semantic_id("assembly", assembly->assembly_id());
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("assembly_id", assembly->assembly_id());
|
|
params.set("name", optional_name(assembly->name()));
|
|
OpRecord record = op_record("make_assembly_rassembly", std::move(params));
|
|
record.semantic_delta = semantic_created("Assembly", assembly->assembly_id(), assembly->to_dict());
|
|
return record;
|
|
},
|
|
assembly);
|
|
return assembly;
|
|
});
|
|
}
|
|
|
|
AssemblyPtr add_component_rassembly(const Assembly& assembly, const Item& item, const std::string& component_id,
|
|
const Placement& placement, const OptionalString& name) {
|
|
return guard_operation("add_component_rassembly", [&] {
|
|
const Component component(component_id, item, placement, name);
|
|
auto result = std::make_shared<const Assembly>(assembly.with_component(component));
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("assembly_id", assembly.assembly_id());
|
|
params.set("component_id", component.component_id);
|
|
params.set("name", optional_name(component.name));
|
|
params.set("item_kind", std::string(item_kind(item)));
|
|
params.set("item_id", item_id(item));
|
|
params.set("placement", placement.to_dict());
|
|
OpRecord record = op_record("make_add_component_rassembly", std::move(params));
|
|
SemanticDelta delta;
|
|
delta.created.push_back(
|
|
{"pending", "pending", "Component", assembly.assembly_id() + ":" + component.component_id});
|
|
delta.modified.push_back({"pending", "pending", "Assembly", assembly.assembly_id()});
|
|
delta.metadata.set("component", component.to_dict());
|
|
record.semantic_delta = std::move(delta);
|
|
return record;
|
|
},
|
|
result, assembly, item, placement);
|
|
return result;
|
|
});
|
|
}
|
|
|
|
AssemblyPtr place_component_rassembly(const Assembly& assembly, const std::string& component_id,
|
|
const Placement& placement) {
|
|
return guard_operation("place_component_rassembly", [&] {
|
|
auto result = std::make_shared<const Assembly>(assembly.with_component_placement(component_id, placement));
|
|
record_value(
|
|
[&] {
|
|
Value params = Value::object();
|
|
params.set("assembly_id", assembly.assembly_id());
|
|
params.set("component_id", component_id);
|
|
params.set("placement", placement.to_dict());
|
|
OpRecord record = op_record("make_place_component_rassembly", std::move(params));
|
|
Value metadata = Value::object();
|
|
metadata.set("placement", placement.to_dict());
|
|
record.semantic_delta =
|
|
semantic_modified("Component", assembly.assembly_id() + ":" + component_id, std::move(metadata));
|
|
return record;
|
|
},
|
|
result, assembly, placement);
|
|
return result;
|
|
});
|
|
}
|
|
|
|
} // namespace scad::product
|