// Argument conversion for the product bindings. // // The Python operators check their argument types inside the wrapped body, so a wrong type // is the operation's SimpleCADError (technical details "TypeError: part must be a Part"), not // a pybind11 overload error. The bindings therefore take py::object for every argument whose // type is checked, and convert it here, inside scad::product::guard_operation. #pragma once #include #include #include "bindings.hpp" #include "scad/core/error.hpp" #include "scad/product/product.hpp" namespace scad::product_binding { namespace sp = scad::product; // `value` as a T, or a TypeError(message). template const T& typed(const py::object& value, const char* message) { if (!py::isinstance(value)) throw scad::TypeError(message); return value.cast(); } template T converted(const py::object& value, const char* message) { try { return value.cast(); } catch (const py::cast_error&) { throw scad::TypeError(message); } } inline bool is_none(const py::object& value) { return value.is_none(); } // str arguments: Python raises TypeError(" must be a string") for anything else. inline std::string text(const py::object& value, const char* field) { if (!py::isinstance(value)) throw scad::TypeError(std::string(field) + " must be a string"); return value.cast(); } // A display name: None, or anything str() accepts. inline sp::OptionalString optional_name(const py::object& value) { if (value.is_none()) return std::nullopt; return py::str(value).cast(); } // True for a list or tuple of Solids: the pieces of a cut that split a body. inline bool is_pieces(const py::object& value) { return (py::isinstance(value) || py::isinstance(value)) && py::len(value) > 0 && 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; } 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")); } } // 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; } inline scad::Scalar scalar(const py::object& value, const char* field) { try { return value.cast(); } catch (const py::cast_error&) { throw scad::TypeError(std::string(field) + " must be numeric"); } } inline std::optional optional_scalar(const py::object& value, const char* field) { if (value.is_none()) return std::nullopt; return scalar(value, field); } inline double number(const py::object& value, const char* field) { return scalar(value, field).value(); } // Three numbers from a list or tuple. inline Vec3 vec3(const py::object& value, const char* field) { if (!py::isinstance(value) && !py::isinstance(value)) { throw scad::ValueError(std::string(field) + " must be a 3-element tuple or list"); } const py::sequence seq = py::reinterpret_borrow(value); if (seq.size() != 3) throw scad::ValueError(std::string(field) + " must be a 3-element tuple or list"); double v[3]; for (std::size_t i = 0; i < 3; ++i) { try { v[i] = seq[i].cast(); } catch (const py::cast_error&) { throw scad::TypeError(std::string(field) + "[" + std::to_string(i) + "] must be numeric"); } } return {v[0], v[1], v[2]}; } inline std::optional optional_vec3(const py::object& value, const char* field) { if (value.is_none()) return std::nullopt; return vec3(value, field); } inline bool truthy(const py::object& value) { return PyObject_IsTrue(value.ptr()) == 1; } // `item` of add_component_rassembly. inline sp::Item item_of(const py::object& value) { if (py::isinstance(value)) return sp::Item{value.cast()}; if (py::isinstance(value)) return sp::Item{value.cast()}; throw scad::TypeError("item must be a Part or Assembly"); } inline py::object py_item(const sp::Item& item) { if (const auto* part = std::get_if(&item)) return py::cast(*part); return py::cast(std::get(item)); } template py::tuple to_tuple(const std::vector& items) { py::tuple out(items.size()); for (std::size_t i = 0; i < items.size(); ++i) out[i] = py::cast(items[i]); return out; } } // namespace scad::product_binding void bind_product_values(py::module_& m);