9778a8e0a9
- A cut that splits the body is no longer refused: cut_rsolid(on_split="pieces")
(the default) returns every piece as list[Solid] ordered by centroid, recorded as
make_cut_rsolid_list with piece_order=centroid and replayed in that order;
on_split="error" keeps the strict contract. One-piece cuts are unchanged.
- Pieces become bodies: make_part_rpart(body=pieces) -> body_1..n,
bodies={"bar": pieces} -> bar_1..n.
- Overlapping bodies are allowed and warned: inspect_body_overlaps_rlist and a
BodyOverlapWarning when the definition is built.
- Multi-body definitions use profile simplecad-part-definition-3 (new schema);
profile 2 is back to its dev bytes, so profile-2 readers refuse them.
- part_from_bodies: the implicit-Part rule (all open bodies, named by variable).
- Skill pages, API docs, stubs, design doc section 9, SDK defect 24.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
169 lines
6.0 KiB
C++
169 lines
6.0 KiB
C++
// 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 <optional>
|
|
#include <string>
|
|
|
|
#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 <class T>
|
|
const T& typed(const py::object& value, const char* message) {
|
|
if (!py::isinstance<T>(value))
|
|
throw scad::TypeError(message);
|
|
return value.cast<const T&>();
|
|
}
|
|
|
|
template <class T>
|
|
T converted(const py::object& value, const char* message) {
|
|
try {
|
|
return value.cast<T>();
|
|
} 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("<field> must be a string") for anything else.
|
|
inline std::string text(const py::object& value, const char* field) {
|
|
if (!py::isinstance<py::str>(value))
|
|
throw scad::TypeError(std::string(field) + " must be a string");
|
|
return value.cast<std::string>();
|
|
}
|
|
|
|
// 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<std::string>();
|
|
}
|
|
|
|
// 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<py::list>(value) || py::isinstance<py::tuple>(value)) && py::len(value) > 0 &&
|
|
py::isinstance<Solid>(py::reinterpret_borrow<py::object>(value[py::int_(0)]));
|
|
}
|
|
|
|
// One mapping entry: a Solid is one body; the pieces of a split cut are bodies <id>_1 .. <id>_n (one piece keeps <id>).
|
|
inline void add_bodies(std::vector<sp::Body>& out, const std::string& body_id, const py::object& value) {
|
|
if (!is_pieces(value)) {
|
|
out.emplace_back(body_id, typed<Solid>(value, "bodies values must be Solids or lists of Solids"));
|
|
return;
|
|
}
|
|
const py::sequence pieces = py::reinterpret_borrow<py::sequence>(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<Solid>(py::reinterpret_borrow<py::object>(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<sp::Body> body_list(const py::object& value) {
|
|
std::vector<sp::Body> out;
|
|
if (py::isinstance<py::dict>(value) || py::hasattr(value, "items")) {
|
|
for (const py::handle item : value.attr("items")()) {
|
|
const py::tuple pair = py::reinterpret_borrow<py::tuple>(item);
|
|
add_bodies(out, text(py::reinterpret_borrow<py::object>(pair[0]), "body_id"),
|
|
py::reinterpret_borrow<py::object>(pair[1]));
|
|
}
|
|
return out;
|
|
}
|
|
if (py::isinstance<py::str>(value) || !py::isinstance<py::sequence>(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<sp::Body>(py::reinterpret_borrow<py::object>(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<scad::Scalar>();
|
|
} catch (const py::cast_error&) {
|
|
throw scad::TypeError(std::string(field) + " must be numeric");
|
|
}
|
|
}
|
|
|
|
inline std::optional<scad::Scalar> 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<py::list>(value) && !py::isinstance<py::tuple>(value)) {
|
|
throw scad::ValueError(std::string(field) + " must be a 3-element tuple or list");
|
|
}
|
|
const py::sequence seq = py::reinterpret_borrow<py::sequence>(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<double>();
|
|
} 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<Vec3> 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<sp::Part>(value))
|
|
return sp::Item{value.cast<sp::PartPtr>()};
|
|
if (py::isinstance<sp::Assembly>(value))
|
|
return sp::Item{value.cast<sp::AssemblyPtr>()};
|
|
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<sp::PartPtr>(&item))
|
|
return py::cast(*part);
|
|
return py::cast(std::get<sp::AssemblyPtr>(item));
|
|
}
|
|
|
|
template <class T>
|
|
py::tuple to_tuple(const std::vector<T>& 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);
|