use crate::atoms::Node;
use crate::atoms::NodeId;
use crate::coords::Coords;
use crate::py::mesh::PyMesh;
use crate::store::Handle;
use pyo3::prelude::*;
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pyclass)]
#[pyclass(name = "Node")]
pub struct PyNode {
node: Node,
}
impl PyNode {
pub(crate) fn from_raw(handle: Handle<Coords>, id: NodeId) -> Self {
Self {
node: Node::from_parts(handle, id),
}
}
pub(crate) fn from_node(node: Node) -> Self {
Self { node }
}
pub(crate) fn as_node(&self) -> &Node {
&self.node
}
}
#[cfg_attr(feature = "stub-gen", pyo3_stub_gen::derive::gen_stub_pymethods)]
#[pymethods]
impl PyNode {
#[getter]
fn id(&self) -> u32 {
self.node.id().0
}
fn coord(&self) -> PyResult<Vec<f64>> {
Ok(self.node.coord()?)
}
fn set_coord(&self, coords: Vec<f64>) -> PyResult<()> {
self.node.set_coord(&coords)?;
Ok(())
}
fn __repr__(&self) -> PyResult<String> {
Ok(format!("{:?}", self.node))
}
fn __str__(&self) -> PyResult<String> {
Ok(format!("{}", self.node))
}
}
#[pymethods]
impl PyNode {
fn __or__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(o) = other.extract::<PyRef<'_, PyNode>>() {
let mesh = self.as_node().union(o.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
fn __ror__(&self, py: Python<'_>, other: &Bound<'_, PyAny>) -> PyResult<Py<PyAny>> {
if let Ok(m) = other.extract::<PyRef<'_, PyMesh>>() {
let mesh = m.inner.union_node(self.as_node())?;
return Ok(Py::new(py, PyMesh { inner: mesh })?.into_any());
}
Ok(py.NotImplemented())
}
}
#[cfg(feature = "stub-gen")]
pyo3_stub_gen::inventory::submit! {
pyo3_stub_gen::derive::gen_methods_from_python! { r#"
class PyNode:
def __or__(self, other: pyo3_stub_gen.RustType["PyNode"]) -> pyo3_stub_gen.RustType["PyMesh"]:
"""`node | node` → a unitary POI1 `Mesh` over both nodes — the usual way
to build the support of a point load or a boundary condition."""
def __ror__(self, other: pyo3_stub_gen.RustType["PyMesh"]) -> pyo3_stub_gen.RustType["PyMesh"]:
"""`mesh | node` → a fresh POI1 `Mesh` with this node appended. The
left-hand `Mesh` must be unitary POI1."""
"# }
}
crate::impl_dump_pymethod!(value PyNode, node);