diff --git a/docs/skill/references/domains/part-modeling.md b/docs/skill/references/domains/part-modeling.md index 72540b4e..4d0bf443 100644 --- a/docs/skill/references/domains/part-modeling.md +++ b/docs/skill/references/domains/part-modeling.md @@ -68,6 +68,9 @@ consult their own documentation before the first call. `make_ruled_surface_rface`, `make_gordon_surface_rface`, `make_cylindrical_surface_rface`. - Editing an existing curve or surface (poles, weights, knots, degree, refit): `domains/curve-surface-editing.md`. +- An `Edge` is taken wherever a `Wire` is (a sweep path, a loft section, a face boundary): it becomes the wire of that + one edge (recorded as `make_wire_from_edges_rwire`). Several edges make one wire only through an explicit + `make_wire_from_edges_rwire([a, b, ...])`. `loft_rshell` of two open curves is the surface between them (no end wires). - Features: `extrude_rsolid`, `revolve_rsolid`, `sweep_rsolid`, `twisted_sweep_rsolid` (supports inner-wire profiles), `helical_sweep_rsolid`, `loft_rsolid`, `loft_rshell`, `shell_rsolid`. diff --git a/src/cpp/bindings/python/bind_ops_features.cpp b/src/cpp/bindings/python/bind_ops_features.cpp index ffc3d44e..9f10e1a7 100644 --- a/src/cpp/bindings/python/bind_ops_features.cpp +++ b/src/cpp/bindings/python/bind_ops_features.cpp @@ -408,7 +408,7 @@ void bind_sweeps(py::module_& m) { const py::object& binormal = binormal_arg; const std::vector sections = shape_list(profiles, "sections must be Face or Wire"); std::optional aux; - if (py::isinstance(binormal)) + if (py::isinstance(binormal) || py::isinstance(binormal)) // an edge: the wire of that one edge aux = Binormal(binormal.cast()); else if (!binormal.is_none()) aux = Binormal(vector_arg(binormal, "binormal")); diff --git a/src/cpp/bindings/python/bind_surface.cpp b/src/cpp/bindings/python/bind_surface.cpp index 1175a910..7970991f 100644 --- a/src/cpp/bindings/python/bind_surface.cpp +++ b/src/cpp/bindings/python/bind_surface.cpp @@ -5,6 +5,7 @@ // the Python package. This is also where the units hook of the surface area is wired to the // reactive area's dimension inference. #include "bindings.hpp" +#include "scad/ops/geometry/wires.hpp" #include #include @@ -284,6 +285,8 @@ void bind_surface(py::module_& m) { const py::object& item = section.value; if (py::isinstance(item)) sections.emplace_back(item.cast()); + else if (py::isinstance(item)) // the wire of that one edge + sections.emplace_back(as_wire(item.cast(), "")); else if (py::isinstance(item)) sections.emplace_back(item.cast()); else diff --git a/src/cpp/bindings/python/bind_topo.cpp b/src/cpp/bindings/python/bind_topo.cpp index 2884effe..6dced796 100644 --- a/src/cpp/bindings/python/bind_topo.cpp +++ b/src/cpp/bindings/python/bind_topo.cpp @@ -16,6 +16,7 @@ #include "scad/reactive/graph.hpp" #include "scad/reactive/lineage.hpp" #include "scad/topo/recorder.hpp" +#include "scad/ops/geometry/wires.hpp" #include "scad/topo/shape.hpp" namespace occt = scad::pybind::occt; @@ -161,6 +162,10 @@ void bind_topo(py::module_& m) { .def("is_closed", &Wire::is_closed) .def("edges", &Wire::edges); bind_common(wire); + // An edge is taken wherever a wire is: the wire of that one edge (nothing is lost), made by the recorded + // make_wire_from_edges_rwire so a graph sees where it came from. + wire.def(py::init([](const Edge& edge) { return make_wire_from_edges_rwire({edge}); }), py::arg("edge")); + py::implicitly_convertible(); auto face = py::class_(m, "Face"); face.def("get_area", &Face::area) diff --git a/src/cpp/include/scad/ops/geometry/wires.hpp b/src/cpp/include/scad/ops/geometry/wires.hpp index 3d6cc405..a2be553d 100644 --- a/src/cpp/include/scad/ops/geometry/wires.hpp +++ b/src/cpp/include/scad/ops/geometry/wires.hpp @@ -8,9 +8,18 @@ namespace scad { -// A wire through `edges`, which must chain end to start. Identity tags of the edges +// A wire through `edges`, given in any order: they must connect into one chain (the kernel orders them; an edge that +// touches none of the others is an error, never left out). Identity tags of the edges // ("circle", "line1", ...) carry over to the identical edges of the wire. // Error "make_wire_from_edges_rwire": empty list, edges that do not connect. Wire make_wire_from_edges_rwire(const std::vector& edges, const OptionalString& tag_prefix = std::nullopt); +// What an operation that takes a wire is given: the Wire itself, or for an Edge the wire of that one edge (nothing is +// lost: the same edge, its tags and lineage), made by make_wire_from_edges_rwire so a recording sees where it came +// from. ValueError `message` for any other shape. +Wire as_wire(const Shape& shape, const char* message); + +// `shapes` with every Edge made the wire of that one edge (as_wire); the other shapes as they are. +std::vector edges_as_wires(const std::vector& shapes); + } // namespace scad diff --git a/src/cpp/src/kernel/curves.cpp b/src/cpp/src/kernel/curves.cpp index 5a567010..f4ee00d7 100644 --- a/src/cpp/src/kernel/curves.cpp +++ b/src/cpp/src/kernel/curves.cpp @@ -2,6 +2,12 @@ #include #include +#include +#include +#include +#include +#include +#include #include #include #include @@ -133,11 +139,85 @@ TopoDS_Edge make_interpolated_bspline_edge(const std::vector& points, bool }); } +namespace { + +// `edges` in the order of one chain, each touching the one before it (an end of one at an end of the next, within +// their tolerances), starting from a free end when the chain is open. Empty when they do not make one chain. +std::vector chained(const std::vector& edges) { + struct Ends { + gp_Pnt a, b; + double tolerance; + }; + std::vector ends; + for (const TopoDS_Edge& edge : edges) { + TopoDS_Vertex first, last; + TopExp::Vertices(edge, first, last); + if (first.IsNull() || last.IsNull()) + return {}; + ends.push_back({BRep_Tool::Pnt(first), BRep_Tool::Pnt(last), + std::max(BRep_Tool::Tolerance(first), BRep_Tool::Tolerance(last))}); + } + const auto meet = [&](const gp_Pnt& p, const gp_Pnt& q, double tolerance) { return p.Distance(q) <= 2 * tolerance; }; + const auto touches = [&](std::size_t i, const gp_Pnt& p) { + return meet(ends[i].a, p, ends[i].tolerance) || meet(ends[i].b, p, ends[i].tolerance); + }; + // an edge with an end no other edge touches starts an open chain + std::size_t start = 0; + for (std::size_t i = 0; i < edges.size(); ++i) { + bool free_end = false; + for (const gp_Pnt& p : {ends[i].a, ends[i].b}) { + bool touched = false; + for (std::size_t j = 0; j < edges.size() && !touched; ++j) + touched = j != i && touches(j, p); + free_end = free_end || !touched; + } + if (free_end) { + start = i; + break; + } + } + std::vector out{edges[start]}; + std::vector used(edges.size(), false); + used[start] = true; + // the chain's open end: the end of the first edge no other edge touches (either, for a closed chain) + gp_Pnt tip = ends[start].b; + for (std::size_t j = 0; j < edges.size(); ++j) + if (j != start && touches(j, ends[start].b)) { + tip = ends[start].b; + break; + } else if (j != start && touches(j, ends[start].a)) { + tip = ends[start].a; + } + while (out.size() < edges.size()) { + std::size_t next = edges.size(); + for (std::size_t j = 0; j < edges.size() && next == edges.size(); ++j) + if (!used[j] && touches(j, tip)) + next = j; + if (next == edges.size()) + return {}; + used[next] = true; + out.push_back(edges[next]); + tip = meet(ends[next].a, tip, ends[next].tolerance) ? ends[next].b : ends[next].a; + } + return out; +} + +} // namespace + TopoDS_Wire make_wire_from_edges(const std::vector& edges) { return occt_guard([&] { + if (edges.empty()) + throw KernelError("StdFail_NotDone", "a wire needs at least one edge"); + // Ordered into one chain first, then added one by one: an edge that does not touch the wire built so far would + // otherwise be left out without a word, and an edge added in turn keeps its identity (its tags). + const std::vector chain = chained(edges); + if (chain.empty()) + throw KernelError("BRepBuilderAPI_DisconnectedWire", "the edges do not connect into one wire"); BRepBuilderAPI_MakeWire builder; - for (const TopoDS_Edge& edge : edges) + for (const TopoDS_Edge& edge : chain) builder.Add(edge); + if (!builder.IsDone()) + throw KernelError("BRepBuilderAPI_DisconnectedWire", "the edges do not connect into one wire"); return builder.Wire(); }); } diff --git a/src/cpp/src/ops/features/multisection.cpp b/src/cpp/src/ops/features/multisection.cpp index e21b0dec..8d4a863f 100644 --- a/src/cpp/src/ops/features/multisection.cpp +++ b/src/cpp/src/ops/features/multisection.cpp @@ -7,11 +7,14 @@ #include "scad/core/tolerance.hpp" #include "scad/kernel/convert.hpp" #include "scad/ops/features/sweeps.hpp" +#include "scad/ops/geometry/wires.hpp" namespace scad { -Solid sweep_multisection_rsolid(const std::vector& profiles, const Wire& path, bool is_frenet, +Solid sweep_multisection_rsolid(const std::vector& given, const Wire& path, bool is_frenet, const std::optional& binormal) { + // an edge section is the wire of that one edge + const std::vector profiles = edges_as_wires(given); if (profiles.size() < 2) throw ValueError("multi-section sweep requires at least two sections"); diff --git a/src/cpp/src/ops/features/sweeps.cpp b/src/cpp/src/ops/features/sweeps.cpp index 524e8d94..0b5c0c8b 100644 --- a/src/cpp/src/ops/features/sweeps.cpp +++ b/src/cpp/src/ops/features/sweeps.cpp @@ -7,6 +7,7 @@ #include "internal.hpp" #include "scad/ops/features/diagnostics.hpp" +#include "scad/ops/geometry/wires.hpp" #include "scad/semantic/tracking.hpp" namespace scad { @@ -38,7 +39,9 @@ void validate_loft_sections(const std::vector& sections, std::string_view } // namespace -Solid loft_rsolid(const std::vector& profiles, bool ruled, const LoftOptions& options) { +Solid loft_rsolid(const std::vector& given, bool ruled, const LoftOptions& options) { + // an edge section is the wire of that one edge + const std::vector profiles = edges_as_wires(given); static const ErrorSpec spec{ "loft_rsolid", "Failed to loft the input profiles into a solid.", diff --git a/src/cpp/src/ops/geometry/faces.cpp b/src/cpp/src/ops/geometry/faces.cpp index 88f67ee2..2989a042 100644 --- a/src/cpp/src/ops/geometry/faces.cpp +++ b/src/cpp/src/ops/geometry/faces.cpp @@ -6,6 +6,7 @@ #include "internal.hpp" #include "scad/ops/geometry/diagnostics.hpp" +#include "scad/ops/geometry/wires.hpp" namespace scad { @@ -22,10 +23,9 @@ void warn_if_reversed(const Vec3& requested, const Face& face) { dot); } +// A wire, or an edge as the wire of that one edge. Wire require_wire(const Shape& shape, const char* message) { - if (shape.kind() != Kind::Wire) - throw ValueError(message); - return Wire(shape); + return as_wire(shape, message); } } // namespace diff --git a/src/cpp/src/ops/geometry/wires.cpp b/src/cpp/src/ops/geometry/wires.cpp index c33d89ef..d706ec03 100644 --- a/src/cpp/src/ops/geometry/wires.cpp +++ b/src/cpp/src/ops/geometry/wires.cpp @@ -38,4 +38,20 @@ Wire make_wire_from_edges_rwire(const std::vector& edges, const OptionalSt }); } +Wire as_wire(const Shape& shape, const char* message) { + if (shape.kind() == Kind::Wire) + return Wire(shape); + if (shape.kind() == Kind::Edge) + return make_wire_from_edges_rwire({Edge(shape)}); + throw ValueError(message); +} + +std::vector edges_as_wires(const std::vector& shapes) { + std::vector out; + out.reserve(shapes.size()); + for (const Shape& shape : shapes) + out.push_back(shape.kind() == Kind::Edge ? Shape(as_wire(shape, "")) : shape); + return out; +} + } // namespace scad diff --git a/src/cpp/src/surface/loft.cpp b/src/cpp/src/surface/loft.cpp index 37f15eff..3cafa1cf 100644 --- a/src/cpp/src/surface/loft.cpp +++ b/src/cpp/src/surface/loft.cpp @@ -124,12 +124,17 @@ Shell loft_rshell(const std::vector& sections, bool ruled, const Lo try { return occt_guard([&] { validate_sections(sections); - const bool start_is_wire = std::holds_alternative(sections.front()); - const bool end_is_wire = std::holds_alternative(sections.back()); + // A closed end section is a boundary loop of its own; an open one (a surface between two curves) is a stretch + // of the shell's one boundary, so it has no end wire to name. + const auto closed = [](const LoftSection& section) { + return std::holds_alternative(section) && std::get(section).is_closed(); + }; + const bool start_is_wire = closed(sections.front()); + const bool end_is_wire = closed(sections.back()); if (tags.start_wire_tag && !start_is_wire) - throw ValueError("start_wire_tag requires a Wire start section"); + throw ValueError("start_wire_tag requires a closed Wire start section"); if (tags.end_wire_tag && !end_is_wire) - throw ValueError("end_wire_tag requires a Wire end section"); + throw ValueError("end_wire_tag requires a closed Wire end section"); // Bad tag names fail before any geometry is built. (void)plan_output_tags(kOp, {.tag_prefix = tags.tag_prefix, .result_tag = tags.result_tag, diff --git a/src/cpp/tests/cpp/ops_geometry/test_profiles.cpp b/src/cpp/tests/cpp/ops_geometry/test_profiles.cpp index 7642056f..1ec5ca8e 100644 --- a/src/cpp/tests/cpp/ops_geometry/test_profiles.cpp +++ b/src/cpp/tests/cpp/ops_geometry/test_profiles.cpp @@ -78,8 +78,11 @@ TEST_CASE("make_face_from_wire_rface keeps the wire's names") { CHECK(has_tag(face, "plate.face")); CHECK(count_with_tag(face.edges(), "frame.edge.line1") == 1); + // an edge is the wire of that one edge (here open, so it makes no face); a face is not a wire at all const Edge edge = make_line_redge(scalar3(0, 0, 0), scalar3(1, 0, 0)); - const auto not_wire = failure_of([&] { make_face_from_wire_rface(edge); }); + CHECK(failure_of([&] { make_face_from_wire_rface(edge); })->guidance().possible_causes.front() != + "The input is not a Wire instance."); + const auto not_wire = failure_of([&] { make_face_from_wire_rface(face); }); REQUIRE(not_wire); CHECK(not_wire->guidance().possible_causes.front() == "The input is not a Wire instance."); } @@ -119,6 +122,8 @@ TEST_CASE("make_face_from_wires_rface punches holes") { const Edge edge = make_line_redge(scalar3(0, 0, 0), scalar3(1, 0, 0)); CHECK(failure_of([&] { make_face_from_wires_rface(edge, {inner}); })->operation() == "make_face_from_wires_rface"); CHECK(failure_of([&] { make_face_from_wires_rface(outer, {edge}); })->guidance().technical_details == + "ValueError: inner wires must be closed"); // an edge is the wire of that one edge + CHECK(failure_of([&] { make_face_from_wires_rface(outer, {plate}); })->guidance().technical_details == "ValueError: inner_wires must contain only Wire objects"); CHECK( failure_of([&] { make_face_from_wires_rface(outer, {inner}, scalar3(0, 0, 0)); })->guidance().technical_details == diff --git a/test/test_edge_as_wire.py b/test/test_edge_as_wire.py new file mode 100644 index 00000000..3b94ab50 --- /dev/null +++ b/test/test_edge_as_wire.py @@ -0,0 +1,48 @@ +"""An edge is taken wherever a wire is: the wire of that one edge, made by the recorded make_wire_from_edges_rwire (a +curve drawn in the editor is one edge; a sweep path, a loft section or a face boundary asks for a wire).""" + +import pytest + +import simplecadapi as s +from simplecadapi.recording.graph import GraphSession + + +def _ring(): + return s.make_face_from_wire_rface(s.make_circle_rwire(center=(0, 0, 0), radius=2, normal=(1, 0, 0)), normal=(1, 0, 0)) + + +def test_an_edge_is_a_sweep_path_a_loft_section_and_a_face_boundary(): + path = s.make_interpolated_spline_redge(points=[(0, 0, 0), (30, 10, 10), (60, 0, 25)]) + swept = s.sweep_rsolid(_ring(), path, False) + assert swept.get_volume() == pytest.approx(s.sweep_rsolid(_ring(), s.Wire(path), False).get_volume()) + loop = s.make_interpolated_spline_redge(points=[(0, 0, 0), (10, 0, 0), (10, 10, 0), (0, 10, 0), (0, 0, 0)]) + top = s.make_interpolated_spline_redge(points=[(0, 0, 10), (10, 0, 10), (10, 10, 10), (0, 10, 10), (0, 0, 10)]) + assert s.loft_rsolid([loop, top]).get_volume() > 1000 + assert s.make_face_from_wire_rface(loop, normal=(0, 0, 1)).get_area() > 100 + with pytest.raises(s.SimpleCADError, match="must be a Wire"): + s.make_face_from_wire_rface(s.make_box_rsolid(1, 1, 1), normal=(0, 0, 1)) + + +def test_a_surface_between_two_open_curves_and_the_wrap_is_recorded(): + with GraphSession() as session: + a = s.make_segment_redge((0, 0, 0), (10, 0, 0)) + b = s.make_interpolated_spline_redge(points=[(0, 0, 10), (5, 4, 12), (10, 0, 15)]) + skin = s.loft_rshell([a, b]) + session.capture_result(value=skin) + assert len(s.ql.faces().resolve(skin)) == 1 + ops = [node.op for node in session.graph.nodes] + assert ops.count("make_wire_from_edges_rwire") == 2 and ops[-1] == "make_loft_rshell" + replayed = s.replay_model_json(s.export_model_json(session))[0] + assert replayed.get_area() == pytest.approx(skin.get_area()) + with pytest.raises(s.SimpleCADError, match="closed Wire start section"): + s.loft_rshell([a, b], start_wire_tag="lip") + + +def test_edges_given_in_any_order_make_one_wire_and_a_stray_one_is_an_error(): + a = s.make_segment_redge((0, 0, 0), (10, 0, 0)) + b = s.make_segment_redge((10, 0, 0), (10, 10, 0)) + c = s.make_segment_redge((10, 10, 0), (0, 10, 5)) + for order in ([a, b, c], [c, a, b], [a, c, b]): + assert len(s.ql.edges().resolve(s.make_wire_from_edges_rwire(order))) == 3 + with pytest.raises(s.SimpleCADError, match="do not connect into one wire"): + s.make_wire_from_edges_rwire([a, b, s.make_segment_redge((50, 50, 0), (60, 50, 0))])