diff --git a/src/cpp/bindings/python/bind_product.cpp b/src/cpp/bindings/python/bind_product.cpp index 8de0ab1f..4ec66d89 100644 --- a/src/cpp/bindings/python/bind_product.cpp +++ b/src/cpp/bindings/python/bind_product.cpp @@ -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()); -// 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() + kOr + const_name("collections.abc.Sequence[") + - type_name() + const_name("] | None")); -SCAD_SIGNATURE_NAME(Bodies, const_name("dict[str, ") + type_name() + kOr + - const_name("collections.abc.Sequence[") + type_name() + - const_name("]] | None")); + type_name() + const_name("]")); SCAD_SIGNATURE_NAME(Face, type_name()); SCAD_SIGNATURE_NAME(Edge, type_name()); SCAD_SIGNATURE_NAME(Vertex, type_name()); @@ -76,7 +73,6 @@ using DirectionsArg = AnyOf; using WeightsArg = AnyOf; using SolidArg = AnyOf; using SolidOrPiecesArg = AnyOf; -using BodiesArg = AnyOf; using FaceArg = AnyOf; using EdgeArg = AnyOf; using VertexArg = AnyOf; @@ -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 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(py::reinterpret_borrow(body.value[py::int_(0)]), "body"), optional_name(name)); - return sp::make_part_rpart(text(part_id, "part_id"), typed(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) { diff --git a/src/cpp/bindings/python/bind_product_args.hpp b/src/cpp/bindings/python/bind_product_args.hpp index cbe8ddf4..eda787d0 100644 --- a/src/cpp/bindings/python/bind_product_args.hpp +++ b/src/cpp/bindings/python/bind_product_args.hpp @@ -58,39 +58,18 @@ inline bool is_pieces(const py::object& value) { py::isinstance(py::reinterpret_borrow(value[py::int_(0)])); } -// One mapping entry: a Solid is one body; the pieces of a split cut are bodies _1 .. _n (one piece keeps ). -inline void add_bodies(std::vector& out, const std::string& body_id, const py::object& value) { - if (!is_pieces(value)) { - out.emplace_back(body_id, typed(value, "bodies values must be Solids or lists of Solids")); - return; - } +// The one Solid of a Part: a Solid, or the pieces of a split cut when there is only one piece. Several pieces are +// several solids, which a Part cannot hold: ValueError says what to do instead. +inline Solid single_solid(const py::object& value, const std::string& part_id) { + if (!is_pieces(value)) + return typed(value, "body must be a Solid"); const py::sequence pieces = py::reinterpret_borrow(value); - const std::size_t count = pieces.size(); - for (std::size_t index = 0; index < count; ++index) { - out.emplace_back(count == 1 ? body_id : body_id + "_" + std::to_string(index + 1), - typed(py::reinterpret_borrow(pieces[index]), - "bodies values must be Solids or lists of Solids")); + if (pieces.size() != 1) { + throw scad::ValueError("part '" + part_id + "' was given " + std::to_string(pieces.size()) + + " solids (the pieces of a split cut); a Part holds exactly one solid. Make one Part per " + "piece and put them in an Assembly, or union the pieces if they are one body"); } -} - -// The bodies of a multi-body Part: a mapping body_id -> Solid or pieces (in its order), or a sequence of Body values. -inline std::vector body_list(const py::object& value) { - std::vector out; - if (py::isinstance(value) || py::hasattr(value, "items")) { - for (const py::handle item : value.attr("items")()) { - const py::tuple pair = py::reinterpret_borrow(item); - add_bodies(out, text(py::reinterpret_borrow(pair[0]), "body_id"), - py::reinterpret_borrow(pair[1])); - } - return out; - } - if (py::isinstance(value) || !py::isinstance(value)) - throw scad::TypeError("bodies must be a mapping body_id -> Solid or a sequence of Body values"); - for (const py::handle item : value) { - out.push_back(typed(py::reinterpret_borrow(item), - "bodies must be a mapping body_id -> Solid or a sequence of Body values")); - } - return out; + return typed(py::reinterpret_borrow(pieces[0]), "body must be a Solid"); } inline scad::Scalar scalar(const py::object& value, const char* field) { diff --git a/src/cpp/bindings/python/bind_product_values.cpp b/src/cpp/bindings/python/bind_product_values.cpp index cd8d2f3f..2f001669 100644 --- a/src/cpp/bindings/python/bind_product_values.cpp +++ b/src/cpp/bindings/python/bind_product_values.cpp @@ -424,7 +424,7 @@ void bind_constraints(py::module_& m) { void bind_part(py::module_& m) { using namespace sp; - py::class_(m, "Body", "One named solid of a Part; a multi-body Part keeps several apart.") + py::class_(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 opacity_value; @@ -454,10 +454,9 @@ void bind_part(py::module_& m) { return out + ")"; }); - py::class_ cls( - m, "Part", "Product item holding one Solid (`body`) or several named bodies (`bodies`), never placed apart."); + py::class_ 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_value; if (!material.is_none()) material_value = typed(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 values; - if (bodies.is_none()) - add_bodies(values, kDefaultBodyId, body); - else - values = body_list(bodies); - return std::make_shared(text(part_id, "part_id"), std::move(values), optional_name(name), + if (py::isinstance(body)) { // a Body keeps its id, display name and appearance (pickle, copy) + std::vector only{typed(body, "body must be a Solid or a Body")}; + return std::make_shared(text(part_id, "part_id"), std::move(only), optional_name(name), material_value, std::move(connector_values)); } - return std::make_shared(text(part_id, "part_id"), typed(body, "body must be a Solid"), + return std::make_shared(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", diff --git a/src/cpp/include/scad/product/operators.hpp b/src/cpp/include/scad/product/operators.hpp index e56d8c81..b8344f9e 100644 --- a/src/cpp/include/scad/product/operators.hpp +++ b/src/cpp/include/scad/product/operators.hpp @@ -39,8 +39,6 @@ Placement make_placement_rplacement(const Scalar3& origin, const Scalar3& x_axis Placement identity_placement_rplacement(); PartPtr make_part_rpart(const std::string& part_id, const Solid& body, const OptionalString& name = {}); -// A Part of several named bodies, kept apart (no boolean between them), in this order. -PartPtr make_part_rpart(const std::string& part_id, const std::vector& bodies, const OptionalString& name = {}); PartPtr assign_material_rpart(const Part& part, const Material& material); // The Part with the display colour, opacity and display name of one body set. An argument left empty keeps what // the body has; `reset` first clears all three (the body then shows the material colour, opaque, under its id). diff --git a/src/cpp/include/scad/product/part.hpp b/src/cpp/include/scad/product/part.hpp index b3b5faa6..28025414 100644 --- a/src/cpp/include/scad/product/part.hpp +++ b/src/cpp/include/scad/product/part.hpp @@ -1,9 +1,10 @@ -// A Part: one or more named bodies (Solids), with an optional material and its connector datums -// (product/part.py). Values are immutable: every `with_*` returns a new Part. +// A Part: one Solid (its body), with an optional material and its connector datums (product/part.py). Values are +// immutable: every `with_*` returns a new Part. // -// A one-body Part is what every Part was before multi-body parts: `body()` is its Solid. A multi-body -// Part keeps several solids apart inside one product item (bodies of one part are not components: they -// have no placements, joints or constraints between them). See design-docs/multi-solid-design.md. +// A Part holds exactly one solid: it is the unit an assembly places, mates and gives connectors to. Several solids that +// exist side by side while a model is built (a tool and its target) are loose values of the notebook, and a deliverable +// of several solids is an Assembly of Parts. The body is kept as a `Body` only because it carries the display name, +// colour and opacity (`set_body_appearance_rpart`). #pragma once #include @@ -61,16 +62,15 @@ class Part : public SemanticValue { // ValueError for a malformed id, an empty name, or duplicate connector ids. Part(std::string part_id, Solid body, OptionalString name = {}, std::optional material = {}, std::vector connectors = {}); - // A Part of several bodies, in this order. ValueError for no bodies, duplicate body ids, or connectors on - // more than one body (connectors of a multi-body part are not supported yet). + // A Part of the one body in `bodies` (a vector so that the body keeps its id and appearance). ValueError for any + // other number of bodies. Part(std::string part_id, std::vector bodies, OptionalString name = {}, std::optional material = {}, std::vector connectors = {}); const std::string& part_id() const noexcept { return part_id_; } - // The Solid of a one-body Part. TypeError for a multi-body Part: read bodies() instead. const Solid& body() const; + // Always one element. const std::vector& bodies() const noexcept { return bodies_; } - bool multi_body() const noexcept { return bodies_.size() > 1; } std::vector body_ids() const; // KeyError ("part has no body_id 'x'") for an unknown id. const Body& get_body(std::string_view body_id) const; diff --git a/src/cpp/src/product/ops_structure.cpp b/src/cpp/src/product/ops_structure.cpp index 9d4f72ff..8674bf54 100644 --- a/src/cpp/src/product/ops_structure.cpp +++ b/src/cpp/src/product/ops_structure.cpp @@ -122,36 +122,6 @@ PartPtr make_part_rpart(const std::string& part_id, const Solid& body, const Opt }); } -PartPtr make_part_rpart(const std::string& part_id, const std::vector& bodies, const OptionalString& name) { - return guard_operation("make_part_rpart", [&] { - auto part = std::make_shared(part_id, bodies, 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())); - Value rows = Value::array(); - Value refs = Value::array(); - for (const Body& body : part->bodies()) { - rows.push_back(body.to_dict()); - const Value body_refs = input_refs(body.solid()); - for (const Value& ref : body_refs.as_array()) - refs.push_back(ref); - } - params.set("bodies", std::move(rows)); - params.set("input_refs", std::move(refs)); - OpRecord record = op_record("make_part_rpart", std::move(params)); - for (const Body& body : part->bodies()) - record.inputs.push_back(OpInput{body.solid(), "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(part.with_material(material)); diff --git a/src/cpp/src/product/part.cpp b/src/cpp/src/product/part.cpp index fa8cbdba..1f570fea 100644 --- a/src/cpp/src/product/part.cpp +++ b/src/cpp/src/product/part.cpp @@ -109,15 +109,17 @@ Part::Part(std::string part_id, std::vector bodies, OptionalString name, s name_(clean_name(name)), material_(std::move(material)), connectors_(unique_connectors(std::move(connectors))) { - if (bodies_.size() > 1 && !connectors_.empty()) - throw ValueError("connectors on a multi-body part are not supported yet (part '" + part_id_ + "')"); + // A Part is one product item: one solid, so that it can be placed, mated and given connectors as a whole. Several + // solids that exist side by side while a model is built (a tool and its target) are loose values, not a Part. + if (bodies_.size() != 1) { + throw ValueError("part '" + part_id_ + "' would hold " + std::to_string(bodies_.size()) + " solids (" + + joined_ids(bodies_) + + "); a Part holds exactly one solid. Make one Part per solid and put them in an Assembly, or " + "union / cut them into one solid first"); + } } const Solid& Part::body() const { - if (bodies_.size() != 1) { - throw TypeError("part '" + part_id_ + "' has " + std::to_string(bodies_.size()) + " bodies (" + - joined_ids(bodies_) + "); read Part.bodies or Part.get_body(body_id)"); - } return bodies_.front().solid(); } @@ -198,8 +200,8 @@ Value Part::to_dict() const { for (const Connector& connector : connectors_) rows.push_back(connector.to_dict()); out.set("connectors", std::move(rows)); - // a one-body Part keeps the dict it always had, unless its body has a name or an appearance - if (bodies_.size() > 1 || !plain_default_body(bodies_.front())) { + // a Part keeps the dict it always had, unless its body has a name or an appearance + if (!plain_default_body(bodies_.front())) { Value bodies = Value::array(); for (const Body& body : bodies_) bodies.push_back(body.to_dict()); diff --git a/src/simplecadapi/contracts/part-definition-3.schema.json b/src/simplecadapi/contracts/part-definition-3.schema.json deleted file mode 100644 index 956d72dc..00000000 --- a/src/simplecadapi/contracts/part-definition-3.schema.json +++ /dev/null @@ -1,152 +0,0 @@ -{ - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://simplecad.dev/schemas/part-definition-3.schema.json", - "title": "SimpleCAD PartDefinition 2.0, profile 3 (multi-body parts)", - "type": "object", - "additionalProperties": false, - "required": ["schema_version", "artifact_kind", "definition_kind", "definition_id", "revision", "content_hash", "units", "tolerance_profile", "profile", "generator", "definition", "solid_cache", "topology_snapshot_ref", "connectors", "material_ref", "file_inputs", "interface_hashes", "metadata"], - "properties": { - "schema_version": {"const": "2.0"}, - "artifact_kind": {"const": "part_definition"}, - "definition_kind": {"const": "single_solid"}, - "definition_id": {"$ref": "#/$defs/id"}, - "revision": {"$ref": "#/$defs/revision"}, - "content_hash": {"$ref": "#/$defs/hash"}, - "units": {"const": "mm"}, - "tolerance_profile": {"type": "string", "minLength": 1, "maxLength": 128}, - "profile": {"const": "simplecad-part-definition-3"}, - "generator": {"$ref": "#/$defs/generator"}, - "definition": { - "type": "object", - "additionalProperties": false, - "required": ["kind", "feature_graph_ref"], - "properties": { - "kind": {"const": "feature_graph"}, - "feature_graph_ref": {"$ref": "#/$defs/blobRef"} - } - }, - "solid_cache": { - "type": "object", - "additionalProperties": false, - "required": ["evaluator_profile", "body_ref", "bodies"], - "properties": { - "evaluator_profile": {"const": "ocp-evaluated-solid-1"}, - "body_ref": {"$ref": "#/$defs/blobRef"}, - "bodies": { - "type": "array", - "minItems": 2, - "maxItems": 10000, - "items": { - "type": "object", - "additionalProperties": false, - "required": ["body_id", "name", "body_ref", "topology_snapshot_ref"], - "properties": { - "body_id": {"type": "string", "minLength": 1, "maxLength": 128}, - "name": {"type": ["string", "null"]}, - "body_ref": {"$ref": "#/$defs/blobRef"}, - "topology_snapshot_ref": {"$ref": "#/$defs/blobRef"} - } - } - } - } - }, - "topology_snapshot_ref": {"$ref": "#/$defs/blobRef"}, - "connectors": {"type": "array", "maxItems": 100000, "items": {"$ref": "#/$defs/connector"}}, - "material_ref": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/materialRef"}]}, - "file_inputs": {"type": "array", "maxItems": 1024, "items": {"$ref": "#/$defs/fileInput"}}, - "interface_hashes": {"$ref": "#/$defs/interfaceHashes"}, - "metadata": {"type": "object"}, - "appearance": { - "type": "object", - "additionalProperties": false, - "required": ["bodies"], - "properties": { - "bodies": { - "type": "array", - "minItems": 1, - "maxItems": 10000, - "items": { - "type": "object", - "additionalProperties": false, - "required": ["body_id", "color", "opacity"], - "properties": { - "body_id": {"type": "string", "minLength": 1, "maxLength": 128}, - "color": {"oneOf": [{"type": "null"}, {"type": "array", "minItems": 3, "maxItems": 3, "items": {"type": "number", "minimum": 0, "maximum": 1}}]}, - "opacity": {"oneOf": [{"type": "null"}, {"type": "number", "minimum": 0, "maximum": 1}]}, - "name": {"type": "string", "minLength": 1} - } - } - } - } - } - }, - "$defs": { - "id": {"type": "string", "minLength": 1, "maxLength": 256, "pattern": "^[A-Za-z][A-Za-z0-9_.:-]*$"}, - "revision": {"type": "string", "minLength": 1, "maxLength": 128, "pattern": "^[A-Za-z0-9][A-Za-z0-9._+-]*$"}, - "hash": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}, - "path": {"type": "string", "minLength": 1, "maxLength": 4096, "pattern": "^[A-Za-z0-9][A-Za-z0-9._/-]*$"}, - "vec3": {"type": "array", "minItems": 3, "maxItems": 3, "items": {"type": "number"}}, - "frame": { - "type": "object", - "additionalProperties": false, - "required": ["origin", "x_axis", "y_axis", "z_axis"], - "properties": {"origin": {"$ref": "#/$defs/vec3"}, "x_axis": {"$ref": "#/$defs/vec3"}, "y_axis": {"$ref": "#/$defs/vec3"}, "z_axis": {"$ref": "#/$defs/vec3"}} - }, - "generator": { - "type": "object", - "additionalProperties": false, - "required": ["simplecadapi_version", "ocp_version", "python_abi", "platform_tag", "semantic_registry_version"], - "properties": { - "simplecadapi_version": {"$ref": "#/$defs/revision"}, - "ocp_version": {"$ref": "#/$defs/revision"}, - "python_abi": {"$ref": "#/$defs/revision"}, - "platform_tag": {"$ref": "#/$defs/revision"}, - "semantic_registry_version": {"$ref": "#/$defs/revision"} - } - }, - "blobRef": { - "type": "object", - "additionalProperties": false, - "required": ["path", "sha256", "byte_length", "media_type"], - "properties": { - "path": {"$ref": "#/$defs/path"}, - "sha256": {"$ref": "#/$defs/hash"}, - "byte_length": {"type": "integer", "minimum": 0, "maximum": 9007199254740991}, - "media_type": {"type": "string", "minLength": 1, "maxLength": 128} - } - }, - "fileInput": { - "type": "object", - "additionalProperties": false, - "required": ["path", "byte_length", "sha256"], - "properties": {"path": {"$ref": "#/$defs/path"}, "byte_length": {"type": "integer", "minimum": 0, "maximum": 9007199254740991}, "sha256": {"$ref": "#/$defs/hash"}} - }, - "materialRef": { - "type": "object", - "additionalProperties": false, - "required": ["material_id", "path", "revision", "sha256", "byte_length"], - "properties": {"material_id": {"$ref": "#/$defs/id"}, "path": {"$ref": "#/$defs/path"}, "revision": {"$ref": "#/$defs/revision"}, "sha256": {"$ref": "#/$defs/hash"}, "byte_length": {"type": "integer", "minimum": 0, "maximum": 9007199254740991}} - }, - "connector": { - "type": "object", - "additionalProperties": false, - "required": ["connector_id", "name", "anchor_kind", "local_frame", "binding", "interface_hash", "binding_hash"], - "properties": { - "connector_id": {"$ref": "#/$defs/id"}, - "name": {"type": ["string", "null"], "minLength": 1}, - "anchor_kind": {"enum": ["geometry", "placement"]}, - "local_frame": {"$ref": "#/$defs/frame"}, - "binding": {"type": ["object", "null"]}, - "interface_hash": {"$ref": "#/$defs/hash"}, - "binding_hash": {"$ref": "#/$defs/hash"} - } - }, - "hashMap": {"type": "object", "propertyNames": {"$ref": "#/$defs/id"}, "additionalProperties": {"$ref": "#/$defs/hash"}}, - "interfaceHashes": { - "type": "object", - "additionalProperties": false, - "required": ["geometry", "connectors", "bindings", "material"], - "properties": {"geometry": {"$ref": "#/$defs/hash"}, "connectors": {"$ref": "#/$defs/hashMap"}, "bindings": {"$ref": "#/$defs/hashMap"}, "material": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/hash"}]}} - } - } -}