Files
ocp/src/modules/mod_TopoDS.cpp
stroblme 6139852768 Phase 10A/10B Inc 0: build system, handle model, first module surface
Builds n3xd-ocp end to end and publishes 7.9.3.1.dev1 to the Gitea registry,
where it installs anonymously and passes its suite.

- occt/Dockerfile: OCCT 7.9.3 compiled once into a manylinux_2_28 builder
  image (base digest + tarball sha256 pinned), Draw/VTK/Tk/Xlib/OpenGL off,
  FreeType on, -O2 without fast-math or march=native. A final layer asserts
  TKService/TKV3d exist with no libGL/libX11 DT_NEEDED, which is what lets the
  app image drop libgl1/libx11-6. Mounted into, never built FROM.
- scikit-build-core + nanobind STABLE_ABI -> one cp312-abi3 extension that
  registers every OCP.* submodule via PyImport_AddModule, so `import
  OCP.TopoDS` needs no shim and cls.__module__ is right. Version <occt>.N is
  asserted against the OCCT found, keeping occt_version() truthful.
- occt_handle.h: type caster for opencascade::handle<T> over OCCT's intrusive
  refcount. Wrappers are non-owning instances holding exactly one handle in
  their keep-alive list, reusing an existing wrapper so identity survives a
  round trip. Transient constructors go through ocp_new (never nb::init<>,
  which would let OCCT delete nanobind's storage); the caster refuses a
  refcount-0 object rather than corrupt the heap. Verified under ASAN with no
  memory-safety errors, plus an RSS bound over 50k create/destroy cycles.
- Sub-shapes are returned by value everywhere, making the TShape lifetime class
  that segfaulted a process-global face memo unrepresentable.
- Standard_Failure derives RuntimeError, with ~20 concrete types dispatched on
  the dynamic OCCT type (cad_pool marshals failures home by type name).
- Inc 0 surface: gp subset, TopAbs, TopoDS (+ downcasts), TopExp, TopLoc,
  TopTools, BRep, BinTools, Poly, Standard. 34 of the app's 139 symbols.
- n3xd_ocp: additive APIs kept out of the OCP namespace so parity testing stays
  meaningful. bintools (shape <-> bytes, GIL-free, byte-identical) and _debug.

Two findings worth the record, both verified against the stock wheel rather
than assumed: upstream binds __hash__ but leaves __eq__ at identity, which is
exactly what geom_memo.py's hash-bucket + IsSame scan is built around, so we
match it instead of "fixing" it; and BinTools can release the GIL after all, by
slurping the file-like object instead of bridging a streambuf that would call
back into Python.

Gate: BREP round-trips are byte-identical to cadquery-ocp-novtk across six
fixtures (the generator asserts stock idempotency first). That matters beyond
IPC — derive.py content-addresses BREP payloads by sha256 and stores the ref.
2026-08-10 16:10:28 +02:00

115 lines
4.7 KiB
C++

/*
OCP.TopoDS — shapes are value types, and that is the point.
A TopoDS_Shape is a small value (a handle to its TShape, a location, an
orientation), so every shape crossing into Python is an owned copy. A
wrapper can therefore never alias storage owned by an explorer, a map or a
BOP history list — which is the lifetime class that segfaulted a
process-global face memo under upstream OCP.
Fidelity note on __hash__ / __eq__, verified against the stock wheel:
upstream binds __hash__ (TShape + Location) and leaves __eq__ at Python's
default identity comparison. That pairing looks odd — two re-extracted
copies of one face hash equal but compare unequal — and it is exactly what
cad/topology/geom_memo.py is built around: it buckets on hash(face) and
disambiguates with IsSame, because == cannot be trusted. Binding __eq__ to
IsEqual here would silently change that memo's behaviour, so we match
upstream rather than improve on it.
*/
#include "../common/occt_module.h"
#include "../common/occt_policies.h"
#include <TopoDS.hxx>
#include <TopoDS_CompSolid.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <functional>
namespace {
/// Stands in for the TopoDS namespace so its statics can hang off a Python
/// class of that name — see the comment at the downcast block below.
struct TopoDSStatics {};
} // namespace
void register_TopoDS(nb::module_ &root) {
nb::module_ m = ocp_submodule(root, "TopoDS");
nb::class_<TopoDS_Shape>(m, "TopoDS_Shape")
.def(nb::init<>())
.def("IsNull", &TopoDS_Shape::IsNull)
.def("Nullify", &TopoDS_Shape::Nullify)
.def("ShapeType", &TopoDS_Shape::ShapeType)
.def("Orientation",
nb::overload_cast<>(&TopoDS_Shape::Orientation, nb::const_))
.def("Location",
nb::overload_cast<>(&TopoDS_Shape::Location, nb::const_),
OCP_RETURN_COPY)
.def("Closed", nb::overload_cast<>(&TopoDS_Shape::Closed, nb::const_))
.def("Reverse", &TopoDS_Shape::Reverse)
.def("Reversed", &TopoDS_Shape::Reversed, OCP_RETURN_COPY)
.def("Moved", &TopoDS_Shape::Moved, "position"_a,
"raiseExc"_a = Standard_False, OCP_RETURN_COPY)
.def("Located", &TopoDS_Shape::Located, "loc"_a,
"raiseExc"_a = Standard_False, OCP_RETURN_COPY)
.def("IsSame", &TopoDS_Shape::IsSame, "other"_a)
.def("IsEqual", &TopoDS_Shape::IsEqual, "other"_a)
.def("IsPartner", &TopoDS_Shape::IsPartner, "other"_a)
.def("__hash__", [](const TopoDS_Shape &s) {
return std::hash<TopoDS_Shape>{}(s);
});
#define OCP_SHAPE_SUBCLASS(Type) \
nb::class_<Type, TopoDS_Shape>(m, #Type).def(nb::init<>())
OCP_SHAPE_SUBCLASS(TopoDS_Vertex);
OCP_SHAPE_SUBCLASS(TopoDS_Edge);
OCP_SHAPE_SUBCLASS(TopoDS_Wire);
OCP_SHAPE_SUBCLASS(TopoDS_Face);
OCP_SHAPE_SUBCLASS(TopoDS_Shell);
OCP_SHAPE_SUBCLASS(TopoDS_Solid);
OCP_SHAPE_SUBCLASS(TopoDS_CompSolid);
OCP_SHAPE_SUBCLASS(TopoDS_Compound);
#undef OCP_SHAPE_SUBCLASS
nb::class_<TopoDS_Iterator>(m, "TopoDS_Iterator")
.def(nb::init<>())
.def(nb::init<const TopoDS_Shape &, Standard_Boolean,
Standard_Boolean>(),
"S"_a, "cumOri"_a = Standard_True, "cumLoc"_a = Standard_True)
.def("More", &TopoDS_Iterator::More)
.def("Next", &TopoDS_Iterator::Next)
.def("Value", &TopoDS_Iterator::Value, OCP_RETURN_COPY);
// Checked downcasts. These raise Standard_TypeMismatch on a kind
// mismatch, which the translator turns into a RuntimeError subclass.
//
// OCCT 7.9 turned TopoDS from a class into a namespace, but upstream OCP
// still presents it as a class carrying the _s statics, and the app calls
// TopoDS.Face_s(...). An empty carrier type reproduces that surface.
nb::class_<TopoDSStatics> cls(m, "TopoDS");
#define OCP_DOWNCAST(Name) \
OCP_DEF_S( \
cls, #Name, \
[](const TopoDS_Shape &s) { return TopoDS::Name(s); }, "S"_a, \
OCP_RETURN_COPY)
OCP_DOWNCAST(Vertex);
OCP_DOWNCAST(Edge);
OCP_DOWNCAST(Wire);
OCP_DOWNCAST(Face);
OCP_DOWNCAST(Shell);
OCP_DOWNCAST(Solid);
OCP_DOWNCAST(CompSolid);
OCP_DOWNCAST(Compound);
#undef OCP_DOWNCAST
}