Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions helpers/sp/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,35 @@ def find_bodies(self, pattern, component=None):
results = []
_collect_bodies_recursive(comp, pattern, results)
return results


# ── Standalone body lookups (no DesignContext instance needed) ───────────────
# These are the ONE site for raw `bRepBodies` iteration / name-based body
# identification. Templates call these instead of rolling their own
# `_all_bodies`/`_find_body` loops, so the fragile bits (name collisions after
# mirror/pattern renames, proxy vs native) live in a single hardenable place.

def bodies_in(comp, recursive=False):
"""All bodies in a component. Shallow by default (the component's own
bodies — matches the old per-template `_all_bodies`); pass recursive=True to
walk descendants."""
if recursive:
out = []
_collect_bodies_recursive(comp, "*", out)
return out
return [comp.bRepBodies.item(i) for i in range(comp.bRepBodies.count)]


def find_body(name, comp):
"""Find a body by exact name within `comp` (checks the component's own
bodies first, then descendants). Canonical replacement for local
`_find_body` helpers."""
return _find_body_recursive(comp, name)


def find_bodies(pattern, comp):
"""Bodies whose name matches a glob `pattern` within `comp` (walks
descendants)."""
out = []
_collect_bodies_recursive(comp, pattern, out)
return out
4 changes: 2 additions & 2 deletions woodworking/templates/bed_rail_fastener.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _get_or_import(part_id, step_file):
return None
tmpl_occ = imported[0][0]
tmpl_occ.isLightBulbOn = False
for bi in range(tmpl_occ.component.bRepBodies.count):
tmpl_occ.component.bRepBodies.item(bi).isVisible = False
for b in sp.bodies_in(tmpl_occ.component):
b.isVisible = False
_plate_cache[part_id] = tmpl_occ
hw_mgr._hardware_occurrences.append((tmpl_occ, root))
return hw_mgr._copy_from_template(tmpl_occ, root)
Expand Down
9 changes: 2 additions & 7 deletions woodworking/templates/domino.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,10 +500,5 @@ def four_corners(comp, plane, center, long_axis, long_expr, short_expr,


def _find_body(comp, name):
"""Find body by name in component (non-recursive, fast)."""
for i in range(comp.bRepBodies.count):
b = comp.bRepBodies.item(i)
if b.name == name:
return b
# Fall back to recursive search
return sp.DesignContext().find_body(name, comp)
"""Find body by name in component (delegates to sp.find_body)."""
return sp.find_body(name, comp)
5 changes: 3 additions & 2 deletions woodworking/templates/drawbore.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,6 @@ def blind(comp, tenon_plane, tenon_plane_offset, tenon_origin, tenon_size,


def _all_bodies(comp):
"""Get all bodies in a component."""
return [comp.bRepBodies.item(i) for i in range(comp.bRepBodies.count)]
"""All bodies in a component (delegates to sp.bodies_in — the single site
for raw bRepBodies iteration)."""
return sp.bodies_in(comp)
6 changes: 1 addition & 5 deletions woodworking/templates/scarf_joint.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ def kanawa_tsugi(comp, body_a, body_b, splice_face,
features.append(split_feat)

# Find the two halves (everything except the two timbers)
halves = []
for i in range(comp.bRepBodies.count):
b = comp.bRepBodies.item(i)
if b != body_a and b != body_b:
halves.append(b)
halves = [b for b in sp.bodies_in(comp) if b != body_a and b != body_b]

# Sort: half closer to body_a = half_a
a_center = _body_center(body_a, grain_axis)
Expand Down
8 changes: 2 additions & 6 deletions woodworking/templates/splayed_legs.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,5 @@ def define_stretcher_params(params, name, height_expr,


def _find_body(comp, name):
"""Find body by name in component."""
for i in range(comp.bRepBodies.count):
b = comp.bRepBodies.item(i)
if b.name == name:
return b
return sp.DesignContext().find_body(name, comp)
"""Find body by name in component (delegates to sp.find_body)."""
return sp.find_body(name, comp)
2 changes: 1 addition & 1 deletion woodworking/templates/tusk_tenon.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,4 @@ def _s(v):


def _all_bodies(comp):
return [comp.bRepBodies.item(i) for i in range(comp.bRepBodies.count)]
return sp.bodies_in(comp) # single site for raw bRepBodies iteration