411 lines
16 KiB
C++
411 lines
16 KiB
C++
#include <pybind11/pybind11.h>
|
|
#include <pybind11/eigen.h>
|
|
#include <pybind11/stl.h>
|
|
|
|
#include <functional>
|
|
|
|
#include "vde/core/transform.h"
|
|
#include "vde/brep/assembly.h"
|
|
#include "vde/brep/interference_check.h"
|
|
#include "vde/brep/explode_view.h"
|
|
|
|
namespace py = pybind11;
|
|
using namespace vde;
|
|
using namespace vde::brep;
|
|
|
|
void bind_assembly(py::module& m) {
|
|
auto assy = m.def_submodule("assembly",
|
|
R"pbdoc(
|
|
Assembly modeling — hierarchical part and subassembly management.
|
|
|
|
Represents a tree structure where each node can be either:
|
|
- A part: contains a BrepModel with a local transform
|
|
- A subassembly: contains child nodes with a local transform
|
|
|
|
Classes:
|
|
Assembly: the root of an assembly tree
|
|
AssemblyNode: a node in the assembly tree (part or subassembly)
|
|
|
|
Operations:
|
|
interference_check: detect collisions between parts
|
|
explode_view: generate exploded assembly view
|
|
)pbdoc");
|
|
|
|
// AssemblyNode is intentionally not exposed: its unique_ptr children
|
|
// cannot be safely copied/held by pybind11. Use the Assembly-level API.
|
|
|
|
// AssemblyNode is intentionally not exposed: its unique_ptr children
|
|
// cannot be safely copied/held by pybind11. Use the Assembly-level API.
|
|
|
|
py::class_<Assembly>(assy, "Assembly",
|
|
R"pbdoc(
|
|
Assembly model — hierarchical tree of parts and subassemblies.
|
|
|
|
The root node is accessed via `assembly.root`. Parts and subassemblies
|
|
are added through the root node's add_part() and add_subassembly().
|
|
|
|
Attributes:
|
|
name: assembly name
|
|
version: version string (default "1.0")
|
|
root: root AssemblyNode
|
|
|
|
Methods:
|
|
part_count(): total number of leaf parts
|
|
node_count(): total number of tree nodes
|
|
|
|
Example:
|
|
>>> assy = assembly.Assembly("engine")
|
|
>>> assy.version = "2.0"
|
|
>>> block = assy.root.add_part("engine_block", brep.make_box(20, 15, 10))
|
|
>>> head = assy.root.add_subassembly("cylinder_head",
|
|
... core.translate(0, 7.5, 0))
|
|
>>> head.add_part("head_gasket", brep.make_box(20, 1, 10))
|
|
>>> print(assy.part_count()) # 2
|
|
)pbdoc")
|
|
.def(py::init<const std::string&>(),
|
|
py::arg("name"),
|
|
"Create an assembly with given name")
|
|
.def_readwrite("name", &Assembly::name, "Assembly name")
|
|
.def_readwrite("version", &Assembly::version, "Version string")
|
|
.def("add_part",
|
|
[](Assembly& a, const std::string& name, BrepModel model) {
|
|
return a.root.add_part(name, std::move(model),
|
|
core::Transform3D::Identity()) != nullptr;
|
|
},
|
|
py::arg("name"), py::arg("model"),
|
|
"Add a leaf part at identity transform")
|
|
.def("add_subassembly",
|
|
[](Assembly& a, const std::string& name) {
|
|
return a.root.add_subassembly(name,
|
|
core::Transform3D::Identity()) != nullptr;
|
|
},
|
|
py::arg("name"),
|
|
"Add an empty subassembly node")
|
|
.def("add_part_to",
|
|
[](Assembly& a, const std::string& parent, const std::string& name,
|
|
BrepModel model) {
|
|
AssemblyNode* node = a.root.find_child(parent);
|
|
if (!node) return false;
|
|
return node->add_part(name, std::move(model),
|
|
core::Transform3D::Identity()) != nullptr;
|
|
},
|
|
py::arg("parent"), py::arg("name"), py::arg("model"),
|
|
"Add a part under an existing named node")
|
|
.def("add_subassembly_to",
|
|
[](Assembly& a, const std::string& parent, const std::string& name) {
|
|
AssemblyNode* node = a.root.find_child(parent);
|
|
if (!node) return false;
|
|
return node->add_subassembly(
|
|
name, core::Transform3D::Identity()) != nullptr;
|
|
},
|
|
py::arg("parent"), py::arg("name"),
|
|
"Add a subassembly under an existing named node")
|
|
.def("remove",
|
|
[](Assembly& a, const std::string& name) {
|
|
std::function<bool(AssemblyNode&)> erase =
|
|
[&](AssemblyNode& node) {
|
|
for (auto it = node.children.begin();
|
|
it != node.children.end(); ++it) {
|
|
if ((*it)->name == name) {
|
|
node.children.erase(it);
|
|
return true;
|
|
}
|
|
if (erase(**it)) return true;
|
|
}
|
|
return false;
|
|
};
|
|
return erase(a.root);
|
|
},
|
|
py::arg("name"),
|
|
"Remove a child node by name (depth-first)")
|
|
.def("clear", [](Assembly& a) {
|
|
a.root.children.clear();
|
|
a.root.model.reset();
|
|
}, "Remove all child nodes and the root model")
|
|
.def("part_count", &Assembly::part_count, "Total number of leaf parts")
|
|
.def("node_count", &Assembly::node_count, "Total number of tree nodes (including subassemblies)")
|
|
.def("visit_parts", [](const Assembly& a) {
|
|
// Collect all parts as tuples (name, model, world_transform)
|
|
py::list result;
|
|
a.visit_parts([&result](const std::string& name,
|
|
const BrepModel& model,
|
|
const core::Transform3D& xform) {
|
|
result.append(py::make_tuple(name, model, xform));
|
|
});
|
|
return result;
|
|
}, R"pbdoc(
|
|
Visit all leaf parts in the assembly.
|
|
|
|
Returns:
|
|
list of (name, BrepModel, world_transform) tuples
|
|
)pbdoc")
|
|
.def("tree", [](Assembly& a) {
|
|
a.assign_part_indices();
|
|
std::function<py::dict(const AssemblyNode&)> walk =
|
|
[&](const AssemblyNode& node) {
|
|
py::dict item;
|
|
item["name"] = node.name;
|
|
item["is_part"] = node.is_part();
|
|
item["part_index"] = node.part_index;
|
|
py::list children;
|
|
for (const auto& child : node.children) {
|
|
children.append(walk(*child));
|
|
}
|
|
item["children"] = children;
|
|
return item;
|
|
};
|
|
return walk(a.root);
|
|
}, R"pbdoc(
|
|
Return a JSON-like read-only assembly tree.
|
|
|
|
Each node is a dict with name, is_part, part_index and children.
|
|
AssemblyNode objects themselves are not exposed.
|
|
)pbdoc")
|
|
.def("find", [](Assembly& a, const std::string& name) -> py::object {
|
|
a.assign_part_indices();
|
|
const AssemblyNode* target =
|
|
a.root.name == name ? &a.root : a.root.find_child(name);
|
|
if (!target) return py::none();
|
|
std::function<py::dict(const AssemblyNode&)> walk =
|
|
[&](const AssemblyNode& node) {
|
|
py::dict item;
|
|
item["name"] = node.name;
|
|
item["is_part"] = node.is_part();
|
|
item["part_index"] = node.part_index;
|
|
py::list children;
|
|
for (const auto& child : node.children) {
|
|
children.append(walk(*child));
|
|
}
|
|
item["children"] = children;
|
|
return item;
|
|
};
|
|
return walk(*target);
|
|
}, py::arg("name"), R"pbdoc(
|
|
Find a node by name and return its read-only dict subtree (or None).
|
|
)pbdoc")
|
|
.def("__repr__", [](const Assembly& a) {
|
|
return "Assembly(name=\"" + a.name
|
|
+ "\", parts=" + std::to_string(a.part_count()) + ")";
|
|
});
|
|
|
|
// ── Interference check ─────────────────────────────
|
|
py::class_<InterferencePair>(assy, "InterferencePair",
|
|
R"pbdoc(
|
|
Interference result for a single part pair.
|
|
|
|
Attributes:
|
|
part_a, part_b: part names
|
|
interfering: True if parts collide
|
|
penetration: estimated penetration depth (mm)
|
|
overlap_volume: approximate overlap volume (mm³)
|
|
)pbdoc")
|
|
.def_readonly("part_a", &InterferencePair::part_a)
|
|
.def_readonly("part_b", &InterferencePair::part_b)
|
|
.def_readonly("interfering", &InterferencePair::interfering)
|
|
.def_readonly("penetration", &InterferencePair::penetration)
|
|
.def_readonly("overlap_volume", &InterferencePair::overlap_volume)
|
|
.def("__repr__", [](const InterferencePair& ip) {
|
|
return "InterferencePair(" + ip.part_a + " ↔ " + ip.part_b
|
|
+ ", interfering=" + std::string(ip.interfering ? "True" : "False")
|
|
+ ", pen=" + std::to_string(ip.penetration) + ")";
|
|
});
|
|
|
|
py::class_<InterferenceResult>(assy, "InterferenceResult",
|
|
R"pbdoc(
|
|
Complete interference check result.
|
|
|
|
Attributes:
|
|
has_interference: True if any pair interferes
|
|
total_pairs_checked: number of part pairs checked
|
|
interfering_pairs: number of interfering pairs
|
|
pairs: all checked pair details
|
|
conflicts: only interfering pair details (convenience)
|
|
)pbdoc")
|
|
.def_readonly("has_interference", &InterferenceResult::has_interference)
|
|
.def_readonly("total_pairs_checked", &InterferenceResult::total_pairs_checked)
|
|
.def_readonly("interfering_pairs", &InterferenceResult::interfering_pairs)
|
|
.def_readonly("pairs", &InterferenceResult::pairs)
|
|
.def_readonly("conflicts", &InterferenceResult::conflicts)
|
|
.def("summary", &InterferenceResult::summary,
|
|
"Get a human-readable summary of the interference check")
|
|
.def("__repr__", [](const InterferenceResult& r) {
|
|
return "InterferenceResult(interference="
|
|
+ std::string(r.has_interference ? "True" : "False")
|
|
+ ", conflicts=" + std::to_string(r.interfering_pairs) + ")";
|
|
});
|
|
|
|
// VDE-1230: interference_check.h gained InterferenceOptions overloads, so a
|
|
// bare &check_interference / &check_pair is ambiguous; bind the legacy
|
|
// signatures explicitly (python bindings stay source-compatible).
|
|
InterferenceResult (*check_interference_legacy)(const Assembly&, bool) =
|
|
&check_interference;
|
|
assy.def("check_interference", check_interference_legacy,
|
|
py::arg("assembly"), py::arg("coarse_only") = false,
|
|
R"pbdoc(
|
|
Check for interference/collisions in the entire assembly.
|
|
|
|
Uses AABB pre-filtering followed by GJK/EPA precise detection.
|
|
|
|
Args:
|
|
assembly: Assembly to check
|
|
coarse_only: if True, only do AABB coarse check (faster but may have false positives)
|
|
|
|
Returns:
|
|
InterferenceResult with detailed collision info
|
|
|
|
Example:
|
|
>>> result = assembly.check_interference(my_assy)
|
|
>>> if result.has_interference:
|
|
... for c in result.conflicts:
|
|
... print(f"{c.part_a} collides with {c.part_b}, pen={c.penetration:.3f}")
|
|
)pbdoc");
|
|
|
|
InterferencePair (*check_pair_legacy)(const BrepModel&, const BrepModel&,
|
|
const vde::core::Transform3D&,
|
|
const vde::core::Transform3D&) = &check_pair;
|
|
assy.def("check_pair", check_pair_legacy,
|
|
py::arg("model_a"), py::arg("model_b"),
|
|
py::arg("transform_a"), py::arg("transform_b"),
|
|
R"pbdoc(
|
|
Check interference between two B-Rep models with world transforms.
|
|
|
|
Args:
|
|
model_a, model_b: BrepModel for each part
|
|
transform_a, transform_b: world-space transforms
|
|
|
|
Returns:
|
|
InterferencePair
|
|
)pbdoc");
|
|
|
|
// VDE-1230: penetration_depth gained a trailing mesh_deflection default arg,
|
|
// so bind a 4-arg lambda to keep the Python signature unchanged.
|
|
auto penetration_depth_4arg =
|
|
[](const BrepModel& a, const BrepModel& b,
|
|
const vde::core::Transform3D& ta, const vde::core::Transform3D& tb) {
|
|
return penetration_depth(a, b, ta, tb);
|
|
};
|
|
assy.def("penetration_depth", penetration_depth_4arg,
|
|
py::arg("model_a"), py::arg("model_b"),
|
|
py::arg("transform_a"), py::arg("transform_b"),
|
|
R"pbdoc(
|
|
Compute penetration depth between two interfering models (EPA).
|
|
|
|
Only call when models are confirmed interfering.
|
|
|
|
Args:
|
|
model_a, model_b: BrepModel
|
|
transform_a, transform_b: world transforms
|
|
|
|
Returns:
|
|
float: penetration depth (>= 0)
|
|
)pbdoc");
|
|
|
|
// ── Explode view ───────────────────────────────────
|
|
py::class_<ExplodeConfig>(assy, "ExplodeConfig",
|
|
R"pbdoc(
|
|
Explode view configuration.
|
|
|
|
Attributes:
|
|
factor: explosion distance multiplier (default 1.0, larger = more spread)
|
|
direction: global explosion direction (zero vector = auto-compute)
|
|
use_constraint_dirs: whether to explode along constraint directions
|
|
preserve_subassemblies: whether to keep internal subassembly structure
|
|
)pbdoc")
|
|
.def(py::init<>())
|
|
.def_readwrite("factor", &ExplodeConfig::factor,
|
|
"Explosion distance coefficient")
|
|
.def_readwrite("direction", &ExplodeConfig::direction,
|
|
"Global explosion direction (Vector3D)")
|
|
.def_readwrite("use_constraint_dirs", &ExplodeConfig::use_constraint_dirs,
|
|
"Explode along constraint directions")
|
|
.def_readwrite("preserve_subassemblies", &ExplodeConfig::preserve_subassemblies,
|
|
"Preserve subassembly internal structure")
|
|
.def("__repr__", [](const ExplodeConfig& c) {
|
|
return "ExplodeConfig(factor=" + std::to_string(c.factor) + ")";
|
|
});
|
|
|
|
py::class_<ExplodeStep>(assy, "ExplodeStep",
|
|
R"pbdoc(
|
|
Single explode step (for animation).
|
|
|
|
Attributes:
|
|
part_name: part name
|
|
original_transform: transform before explosion
|
|
exploded_transform: transform after explosion
|
|
displacement: displacement vector
|
|
)pbdoc")
|
|
.def_readonly("part_name", &ExplodeStep::part_name)
|
|
.def_readonly("original_transform", &ExplodeStep::original_transform)
|
|
.def_readonly("exploded_transform", &ExplodeStep::exploded_transform)
|
|
.def_readonly("displacement", &ExplodeStep::displacement)
|
|
.def("__repr__", [](const ExplodeStep& s) {
|
|
return "ExplodeStep(\"" + s.part_name
|
|
+ "\", disp=" + std::to_string(s.displacement.norm()) + ")";
|
|
});
|
|
|
|
py::class_<ExplodeResult>(assy, "ExplodeResult",
|
|
R"pbdoc(
|
|
Explode view result.
|
|
|
|
Attributes:
|
|
steps: list of ExplodeStep for each part (for animation)
|
|
original_bounds: AABB3D before explosion
|
|
exploded_bounds: AABB3D after explosion
|
|
)pbdoc")
|
|
.def_readonly("steps", &ExplodeResult::steps)
|
|
.def_readonly("original_bounds", &ExplodeResult::original_bounds)
|
|
.def_readonly("exploded_bounds", &ExplodeResult::exploded_bounds)
|
|
.def("max_displacement", &ExplodeResult::max_displacement,
|
|
"Get maximum displacement distance")
|
|
.def("__repr__", [](const ExplodeResult& r) {
|
|
return "ExplodeResult(steps=" + std::to_string(r.steps.size()) + ")";
|
|
});
|
|
|
|
assy.def("explode_view", &explode_view,
|
|
py::arg("assembly"), py::arg("config") = ExplodeConfig{},
|
|
R"pbdoc(
|
|
Generate an exploded view of the assembly.
|
|
|
|
Spreads parts outward along the assembly hierarchy direction.
|
|
The assembly's part transforms are modified in place.
|
|
|
|
Args:
|
|
assembly: Assembly (modified in place)
|
|
config: ExplodeConfig (optional)
|
|
|
|
Returns:
|
|
ExplodeResult with displacement information
|
|
|
|
Example:
|
|
>>> config = assembly.ExplodeConfig()
|
|
>>> config.factor = 2.0
|
|
>>> result = assembly.explode_view(my_assy, config)
|
|
>>> for step in result.steps:
|
|
... print(f"{step.part_name}: displacement {step.displacement.norm():.3f}")
|
|
)pbdoc");
|
|
|
|
assy.def("un_explode", &un_explode,
|
|
py::arg("assembly"), py::arg("result"),
|
|
R"pbdoc(
|
|
Undo an explode view, restoring original transforms.
|
|
|
|
Args:
|
|
assembly: Assembly (modified in place)
|
|
result: ExplodeResult from previous explode_view() call
|
|
)pbdoc");
|
|
|
|
assy.def("compute_explode_displacement", &compute_explode_displacement,
|
|
py::arg("node"), py::arg("parent_tf"), py::arg("factor"),
|
|
R"pbdoc(
|
|
Compute explosion displacement vector for a single node.
|
|
|
|
Args:
|
|
node: AssemblyNode
|
|
parent_tf: parent world transform (Transform3D)
|
|
factor: explosion coefficient
|
|
|
|
Returns:
|
|
Vector3D: displacement vector
|
|
)pbdoc");
|
|
}
|