sdk: workplanes are recorded values (make_workplane_*_rworkplane); sketch plane=wp; replay follows the geometry
A SimpleWorkplane is now a value: copyable, entered again (`with wp:` in later
cells), picklable for the cell cache (exact frame + metadata/runtime, so it
keeps its graph lineage), with origin/normal/x_dir/y_dir/plane.
Six recorded constructors (C++ ops/geometry/workplanes.cpp, typed bindings):
make_workplane_rworkplane (origin/normal/x_dir in the current system),
_from_face_ (centre or projected origin, outward normal, x along an edge / a
direction / the face's own x, offset, flip), _from_points_ (Vertex or point),
_from_axis_ (straight edge or circle axis, turned by an angle, optional start
face), _midplane_ (two parallel faces), _normal_to_curve_ (edge or wire, at a
fraction of its length). Inputs are recorded as ordered input_refs; replay
(serializer) rebuilds the frame from the replayed geometry.
make_sketch_rsketch(plane=wp) uses the workplane's world frame, records
plane={workplane: true, frame} and the workplane node as an input; replay
takes the frame from the replayed workplane. Inside `with wp:` every recorded
node's context names the workplane node; replay orders it after that node and
uses the replayed frame. types.Plane includes SimpleWorkplane; catalog kind
"workplane".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -80,14 +80,37 @@ void bind_core(py::module_& m) {
|
||||
// The world frame as a module constant, like simplecadapi.WORLD_CS.
|
||||
m.attr("WORLD_CS") = py::cast(scad::world_cs());
|
||||
|
||||
py::class_<Workplane>(m, "SimpleWorkplane")
|
||||
const auto tuple3 = [](const scad::Vec3& v) { return py::make_tuple(v.x, v.y, v.z); };
|
||||
py::class_<Workplane>(m, "SimpleWorkplane",
|
||||
"A workplane: a frame in world coordinates, and the context manager that makes it the current "
|
||||
"coordinate system (`with wp:`). A value: it can be entered again, pickled, and given to "
|
||||
"make_sketch_rsketch(plane=wp).")
|
||||
.def(py::init([](const py::object& origin, const py::object& normal, const py::object& x_dir) {
|
||||
return std::make_unique<Workplane>(vec_from_python(origin), vec_from_python(normal),
|
||||
vec_from_python(x_dir));
|
||||
}),
|
||||
py::arg("origin") = py::make_tuple(0, 0, 0), py::arg("normal") = py::make_tuple(0, 0, 1),
|
||||
py::arg("x_dir") = py::make_tuple(1, 0, 0))
|
||||
.def_static(
|
||||
"_from_frame",
|
||||
[](const py::object& origin, const py::object& x_axis, const py::object& y_axis) {
|
||||
return Workplane::from_frame(
|
||||
CoordinateSystem(vec_from_python(origin), vec_from_python(x_axis), vec_from_python(y_axis)));
|
||||
},
|
||||
py::arg("origin"), py::arg("x_axis"), py::arg("y_axis"))
|
||||
.def_property_readonly("cs", &Workplane::cs, py::return_value_policy::reference_internal)
|
||||
.def_property_readonly(
|
||||
"origin", [tuple3](const Workplane& w) { return tuple3(w.origin()); }, "The origin in world coordinates.")
|
||||
.def_property_readonly(
|
||||
"normal", [tuple3](const Workplane& w) { return tuple3(w.normal()); },
|
||||
"The unit normal (the local z axis) in world coordinates.")
|
||||
.def_property_readonly(
|
||||
"x_dir", [tuple3](const Workplane& w) { return tuple3(w.x_dir()); }, "The unit x axis in world coordinates.")
|
||||
.def_property_readonly(
|
||||
"y_dir", [tuple3](const Workplane& w) { return tuple3(w.y_dir()); }, "The unit y axis in world coordinates.")
|
||||
.def_property_readonly(
|
||||
"plane", [](const Workplane& w) { return scad::pybind::value_to_python(w.plane_value()); },
|
||||
"The frame as a sketch plane mapping {'origin', 'x_axis', 'y_axis'} (world coordinates).")
|
||||
.def(
|
||||
"__enter__",
|
||||
[](Workplane& w) -> Workplane& {
|
||||
@@ -96,12 +119,63 @@ void bind_core(py::module_& m) {
|
||||
},
|
||||
py::return_value_policy::reference)
|
||||
.def("__exit__", [](Workplane& w, const py::object&, const py::object&, const py::object&) { w.exit(); })
|
||||
.def("set_metadata",
|
||||
[](const Workplane& w, const std::string& key, const scad::Value& value) { w.set_metadata(key, value); })
|
||||
.def(
|
||||
"get_metadata",
|
||||
[](const Workplane& w, const std::string& key, const scad::Value& fallback) {
|
||||
return w.get_metadata(key, fallback);
|
||||
},
|
||||
py::arg("key"), py::arg("default") = scad::Value())
|
||||
.def("_set_runtime",
|
||||
[](const Workplane& w, const std::string& key, const scad::Value& value) { w.set_runtime(key, value); })
|
||||
.def(
|
||||
"_get_runtime",
|
||||
[](const Workplane& w, const std::string& key, const scad::Value& fallback) {
|
||||
return w.get_runtime(key, fallback);
|
||||
},
|
||||
py::arg("key"), py::arg("default") = scad::Value())
|
||||
.def("_metadata_dict", [](const Workplane& w) { return scad::pybind::value_to_python(w.metadata()); })
|
||||
.def("_runtime_dict", [](const Workplane& w) { return scad::pybind::value_to_python(w.runtime()); })
|
||||
.def("__copy__", [](const Workplane& w) { return Workplane(w); })
|
||||
.def(
|
||||
"__deepcopy__", [](const Workplane& w, const py::object&) { return Workplane(w); }, py::arg("memo"))
|
||||
// The cell cache pickles cell outputs: the exact frame, then the dictionaries (the graph lineage is in
|
||||
// the runtime one, so a restored workplane is still the output of the node that made it).
|
||||
.def(py::pickle(
|
||||
[tuple3](const Workplane& w) {
|
||||
return py::make_tuple(tuple3(w.origin()), tuple3(w.x_dir()), tuple3(w.y_dir()),
|
||||
scad::pybind::value_to_python(w.metadata()),
|
||||
scad::pybind::value_to_python(w.runtime()));
|
||||
},
|
||||
[](const py::tuple& state) {
|
||||
if (state.size() != 5)
|
||||
throw std::runtime_error("Invalid SimpleWorkplane state");
|
||||
Workplane w = Workplane::from_frame(
|
||||
CoordinateSystem(vec_from_python(state[0]), vec_from_python(state[1]), vec_from_python(state[2])));
|
||||
for (const auto& [key, value] : state[3].cast<py::dict>())
|
||||
w.set_metadata(key.cast<std::string>(), scad::pybind::value_from_python(value));
|
||||
for (const auto& [key, value] : state[4].cast<py::dict>())
|
||||
w.set_runtime(key.cast<std::string>(), scad::pybind::value_from_python(value));
|
||||
return w;
|
||||
}))
|
||||
.def("_format_string", &Workplane::to_string, py::arg("indent") = 0, py::arg("show_coordinate_system") = true)
|
||||
.def("__str__", [](const Workplane& w) { return w.to_string(0, true); })
|
||||
.def("__repr__", &Workplane::repr);
|
||||
m.attr("Workplane") = m.attr("SimpleWorkplane");
|
||||
|
||||
m.def("get_current_cs", [] { return scad::current_cs(); });
|
||||
// The recorded workplane the innermost frame comes from: {"graph_id", "node_id", "output_slot"} or None.
|
||||
m.def("get_current_frame_source", []() -> py::object {
|
||||
const scad::FrameSource* source = scad::current_frame_source();
|
||||
if (source == nullptr)
|
||||
return py::none();
|
||||
py::dict out;
|
||||
out["graph_id"] = source->graph_id;
|
||||
out["node_id"] = source->node_id;
|
||||
out["output_slot"] = source->output_slot;
|
||||
return std::move(out);
|
||||
});
|
||||
|
||||
// use_coordinate_system(cs): context manager over the same stack.
|
||||
struct CsScope {
|
||||
|
||||
Reference in New Issue
Block a user