lego
レゴ互換ブロック
# レゴ互換ブロック (2x4)
#
# 寸法はすべて LEGO の実寸規格。ピッチ8mm、外壁1.5mm、
# 外寸は「ピッチ×個数 − 0.2」で、この0.2が隣に並べたときの隙間になる。
# ポッチは裏のチューブと外壁の間に挟まって固定される。
@param 2..8 step:1 desc:"ポッチの数 (X方向)"
studs_x = 4
@param 2..8 step:1 desc:"ポッチの数 (Y方向)"
studs_y = 2
@param 1..3 step:1 desc:"高さ (プレート単位。3 = 標準ブロック)"
plates = 3
pitch = 8.0 # ポッチ間隔
plate_h = 3.2 # プレート1枚の高さ。ブロックはこの3枚分
clearance = 0.2 # 隣のブロックとの隙間
stud_r = 2.4 # ポッチ半径
# 外壁の厚み: 下のブロックのポッチが外壁の内面にちょうど接する厚み。
# ポッチ中心から外面までは pitch/2 − clearance/2 なので 4 − 0.1 − 2.4 = 1.5
wall = pitch / 2 - clearance / 2 - stud_r
stud_h = 1.8 # ポッチの高さ
stud_r_f = 0.3 # ポッチ上端のフィレット。嵌めるときの案内になる
tube_or = 3.255 # 裏のチューブ外半径
tube_ir = 2.4 # 同 内半径 (= ポッチ半径。ここでポッチを咥える)
$bw = pitch * studs_x - clearance
$bd = pitch * studs_y - clearance
$bh = plate_h * plates
# 本体。底面を開けて中空にする。shell の肉厚がそのまま壁と天井になる
$body = box $bw $bd $bh
| floor
| faces <Z | shell wall
# 上面のポッチ。grid は原点中心に並ぶので、本体の中心と揃う
$studs = cylinder stud_r stud_h
| floor
| translate 0 0 $bh
| grid studs_x studs_y pitch
# 裏のチューブ。ポッチとポッチの中間 (格子が1つ少ない) に立つ。
# 天井の肉厚の半分まで埋め込む。ちょうど接するだけだと融合せず
# 別ソリッドになり、逆に $bh まで伸ばすと上面が天井と同一平面に
# なって、上面に円形のエッジが残ってしまう
$tube_stock = cylinder tube_or ($bh - wall / 2)
| floor
| grid (studs_x - 1) (studs_y - 1) pitch
$tube_bore = cylinder tube_ir ($bh - wall)
| floor
| grid (studs_x - 1) (studs_y - 1) pitch
# 最後にポッチの上端 (最上面 Z=$bh+stud_h の円8本) だけを丸める。
# >Z は最も高いエッジを選ぶので、本体や天井のエッジは含まれない
$body | union $studs | union $tube_stock | diff $tube_bore
| edges >Z | fillet stud_r_f