sdk: an edge is taken wherever a wire is (a sweep path, a loft section, a face boundary, a binormal guide): the wire of that one edge, made by the recorded make_wire_from_edges_rwire so a graph sees where it came from (as_wire, edges_as_wires; Wire(edge) and an implicit Edge -> Wire conversion in Python). make_wire_from_edges_rwire takes its edges in any order (they are chained by their ends first, then added in turn so each keeps its identity) and an edge that touches none of the others is an error instead of being left out. loft_rshell of open sections (the surface between two curves) no longer fails looking for end wires: only a closed end section is a boundary loop of its own

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NiJingzhe
2026-11-06 06:12:46 +08:00
parent 53de920e81
commit f8a13de6e4
13 changed files with 193 additions and 13 deletions
@@ -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`.
@@ -408,7 +408,7 @@ void bind_sweeps(py::module_& m) {
const py::object& binormal = binormal_arg;
const std::vector<Shape> sections = shape_list(profiles, "sections must be Face or Wire");
std::optional<Binormal> aux;
if (py::isinstance<Wire>(binormal))
if (py::isinstance<Wire>(binormal) || py::isinstance<Edge>(binormal)) // an edge: the wire of that one edge
aux = Binormal(binormal.cast<Wire>());
else if (!binormal.is_none())
aux = Binormal(vector_arg(binormal, "binormal"));
+3
View File
@@ -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 <array>
#include <optional>
@@ -284,6 +285,8 @@ void bind_surface(py::module_& m) {
const py::object& item = section.value;
if (py::isinstance<Wire>(item))
sections.emplace_back(item.cast<Wire>());
else if (py::isinstance<Edge>(item)) // the wire of that one edge
sections.emplace_back(as_wire(item.cast<Edge>(), ""));
else if (py::isinstance<Vertex>(item))
sections.emplace_back(item.cast<Vertex>());
else
+5
View File
@@ -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>(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<Edge, Wire>();
auto face = py::class_<Face, Shape>(m, "Face");
face.def("get_area", &Face::area)
+10 -1
View File
@@ -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<Edge>& 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<Shape> edges_as_wires(const std::vector<Shape>& shapes);
} // namespace scad
+81 -1
View File
@@ -2,6 +2,12 @@
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <algorithm>
#include <gp_Pnt.hxx>
#include <TopoDS_Vertex.hxx>
#include <BRep_Tool.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <BRepLib.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <GC_MakeArcOfCircle.hxx>
@@ -133,11 +139,85 @@ TopoDS_Edge make_interpolated_bspline_edge(const std::vector<Vec3>& 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<TopoDS_Edge> chained(const std::vector<TopoDS_Edge>& edges) {
struct Ends {
gp_Pnt a, b;
double tolerance;
};
std::vector<Ends> 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<TopoDS_Edge> out{edges[start]};
std::vector<bool> 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<TopoDS_Edge>& 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<TopoDS_Edge> 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();
});
}
+4 -1
View File
@@ -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<Shape>& profiles, const Wire& path, bool is_frenet,
Solid sweep_multisection_rsolid(const std::vector<Shape>& given, const Wire& path, bool is_frenet,
const std::optional<Binormal>& binormal) {
// an edge section is the wire of that one edge
const std::vector<Shape> profiles = edges_as_wires(given);
if (profiles.size() < 2)
throw ValueError("multi-section sweep requires at least two sections");
+4 -1
View File
@@ -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<Shape>& sections, std::string_view
} // namespace
Solid loft_rsolid(const std::vector<Shape>& profiles, bool ruled, const LoftOptions& options) {
Solid loft_rsolid(const std::vector<Shape>& given, bool ruled, const LoftOptions& options) {
// an edge section is the wire of that one edge
const std::vector<Shape> profiles = edges_as_wires(given);
static const ErrorSpec spec{
"loft_rsolid",
"Failed to loft the input profiles into a solid.",
+3 -3
View File
@@ -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
+16
View File
@@ -38,4 +38,20 @@ Wire make_wire_from_edges_rwire(const std::vector<Edge>& 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<Shape> edges_as_wires(const std::vector<Shape>& shapes) {
std::vector<Shape> 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
+9 -4
View File
@@ -124,12 +124,17 @@ Shell loft_rshell(const std::vector<LoftSection>& sections, bool ruled, const Lo
try {
return occt_guard([&] {
validate_sections(sections);
const bool start_is_wire = std::holds_alternative<Wire>(sections.front());
const bool end_is_wire = std::holds_alternative<Wire>(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<Wire>(section) && std::get<Wire>(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,
@@ -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 ==
+48
View File
@@ -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))])