Skip to content

Associate a block with geometry and snap ​

Use this when block nodes must lie on a CAD face, edge or corner (or on a whole body): you record which geometry entity each block face, edge or vertex belongs to, then snap projects the nodes onto it.

The script needs the cad extra and runs in about 3 s. Associations are a Python-API and GUI feature; the CLI has no associate command.

Find the geometry entities ​

python
# a 2 x 1 x 1 box with a hole; its top face is z = 0.5
bd.export_step(bd.Box(2.0, 1.0, 1.0) - bd.Cylinder(0.3, 2.0), "part.step")

p = Project()
p.load_geometry("part.step", name="part")
p.build_primitive("box", vmin=(-0.8, -0.4, 0.55), vmax=(0.8, 0.4, 0.9), ni=5, nj=4, nk=3)
g = p.geometries["part"].geometry


def nearest(kind: EntityKind, point: tuple[float, float, float]) -> int:
    """Index of the entity of *kind* closest to *point*."""
    xyz = np.array([point])
    d = [g.project(xyz, ref).distances[0] for ref in g.entities(kind)]
    return int(np.argmin(d))


top = nearest(EntityKind.SURFACE, (0.7, 0.3, 0.5))  # the top face
edge = nearest(EntityKind.CURVE, (0.2, -0.5, 0.5))  # the top edge along x at y = -0.5
corner = nearest(EntityKind.POINT, (-1.0, -0.5, 0.5))  # the corner it starts from
print(f"surface {top}, curve {edge}, point {corner}")
text
surface 2, curve 5, point 1
  • A CAD geometry exposes SURFACE, CURVE and POINT entities, indexed from 0 in geometry.entities(kind). Indices follow the file's topology order, so look them up rather than hard-coding them: here by the entity closest to a probe point.
  • An STL geometry has one BODY and, since ADR 0049, feature CURVE and POINT entities; see Feature curves of an STL.

Associate a face, an edge and a vertex, then snap ​

python
p.associate("box", "k_lo", "part", "surface", [top])  # face -> surface
p.associate("box", "j_lo_k_lo", "part", "curve", [edge])  # edge -> curve
p.associate("box", "v0", "part", "point", [corner])  # vertex -> point
print(p.journal().splitlines()[-1])

p.snap()  # nodes move only now: vertex first, then edges, then faces
n = p.mesh.blocks[0].nodes
print("v0         :", n[0, 0, 0])
print("edge y, z  :", np.unique(n[:, 0, 0, 1:].round(12), axis=0))
print("face z     :", np.unique(n[:, :, 0, 2].round(12)))
dist = g.project(n[:, :, 0].reshape(-1, 3), EntityRef(EntityKind.SURFACE, top)).distances
print(f"k_lo off the surface by at most {dist.max():.1e}")
p.write_geo("snapped")
text
p.associate('box', 'v0', 'part', 'point', (1,))
v0         : [-1.  -0.5  0.5]
edge y, z  : [[-0.5  0.5]]
face z     : [0.5]
k_lo off the surface by at most 6.9e-15
  • The entity is a face name (k_lo), an edge name (j_lo_k_lo) or a vertex v0..v7 in HexBlock.vertices() order; see the block model.
  • kind is "body", "surface", "curve", "point", or "none" to clear. An entity takes geometry of its own dimension or higher: a face a surface or the body, an edge also a curve, a vertex also a point.
  • associate moves no node; snap() does. Where a node belongs to several associated entities, the lowest dimension wins: vertex, then edge, then face. Here v0 goes to the corner (-1, -0.5, 0.5), the rest of the edge to the curve, the rest of the face to z = 0.5.
  • The association stores fingerprints of the entities, not only their indices, so it survives a re-import of a modified CAD file (ADR 0003).
  • snap(exact=True) uses the exact projection on the CAD surfaces instead of their tessellation; on an STL both are the same.
  • snap resolves every project geometry by name ("part" here). On a topology built from a geometry, every block's WALL face (k_lo, with its edges and corners) is already associated with the body, so p.snap() after an edit puts the wall back on it.
  • Seam nodes shared with a neighbour are written through, so SHARED and DIRICHLET seams stay closed (ADR 0040).

Clear or undo an association ​

python
p.associate("box", "v0", "part", "none")  # clear the vertex association
print(p.mesh.blocks[0].vertices()[0].association)
p.undo()  # and bring it back
print(p.mesh.blocks[0].vertices()[0].association is not None)
text
None
True
  • kind="none" clears; undo() restores the previous association.
  • Associations are saved with the project and come back on load, with or without replay (Save, load, replay).
  • Refused with ParameterError, leaving project and journal unchanged: an unknown block, entity, geometry or kind; an index out of range; no index for a surface, curve or point; an index for "body"; a geometry entity of lower dimension than the block entity (a face to a curve).

See also ​

Released under the MIT License.