Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save WilliamLivingood/4361f0ee3dc6236a24bcdba7d2f9d38c to your computer and use it in GitHub Desktop.

Select an option

Save WilliamLivingood/4361f0ee3dc6236a24bcdba7d2f9d38c to your computer and use it in GitHub Desktop.
code_to_make_chain_in_grasshopper
import Rhino.Geometry as rg
import math
Chain = []
try:
N_int = int(round(N))
except:
N_int = 0
try:
style = int(round(S)) if S is not None else 0
except:
style = 0
if N_int < 1:
print("N < 1, nothing to build")
else:
link_len = float(L) if L and L > 0 else 12.0
link_h = float(W) if W and W > 0 else 6.0
wire_r = float(R) if R and R > 0 else 1.0
overlap = float(C) if C and 0.1 < C < 0.9 else 0.6
if link_len <= link_h:
link_len = link_h + 2.0
if wire_r * 2.0 >= link_h:
wire_r = link_h * 0.25
tol = 0.01
a = link_len * 0.5
b = link_h * 0.5
pl = rg.Plane.WorldYZ
el = rg.Ellipse(pl, a, b)
crv_full = el.ToNurbsCurve()
dom = crv_full.Domain
t0, t1 = dom.T0, dom.T1
span = t1 - t0
gap_frac = 0.18
cut_start = t0 + gap_frac * 0.5 * span
cut_end = t1 - gap_frac * 0.5 * span
crv_open = crv_full.Trim(cut_start, cut_end)
if not crv_open:
crv_open = crv_full
pipes_closed = rg.Brep.CreatePipe(
crv_full,
wire_r,
False,
rg.PipeCapMode.Round,
True,
tol,
tol
)
pipes_open = rg.Brep.CreatePipe(
crv_open,
wire_r,
False,
rg.PipeCapMode.Round,
True,
tol,
tol
)
if not pipes_closed or not pipes_open:
print("Pipe failed – check L, W, R")
else:
closed_link = pipes_closed[0]
open_link = pipes_open[0]
links = []
max_step = link_len * 0.9
min_step = wire_r * 2.0
step = min_step + (max_step - min_step) * overlap
for i in range(N_int):
if i == 0 or i == N_int - 1:
base = open_link
else:
base = closed_link
link = base.DuplicateBrep()
cx = i * step
center_pt = rg.Point3d(cx, 0.0, 0.0)
t_move = rg.Transform.Translation(cx, 0.0, 0.0)
link.Transform(t_move)
if i % 2 == 1:
t_rot = rg.Transform.Rotation(
math.pi * 0.5,
rg.Vector3d.YAxis,
center_pt
)
link.Transform(t_rot)
t_global = rg.Transform.Rotation(
math.pi * 0.5,
rg.Vector3d.ZAxis,
center_pt
)
link.Transform(t_global)
scale = 1.0
if style == 1:
pattern = [1.3, 1.3, 1.0, 1.0, 1.0]
scale = pattern[i % len(pattern)]
elif style == 2 and N_int > 1:
t = float(i) / float(N_int - 1)
center_weight = 1.0 - abs(2.0 * t - 1.0)
scale = 0.8 + 0.4 * center_weight
if abs(scale - 1.0) > 1e-6:
s_tf = rg.Transform.Scale(center_pt, scale)
link.Transform(s_tf)
links.append(link)
Chain = links
print("Links created:", len(Chain), "step =", step, "style =", style)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment