by dcowden 2 years, 3 months ago
97 times 1.25s avg. build time

Parametric Lego Brick

This single script can create any rectangular lego brick.

License: ParametricParts-Free Publicly Listed: True Tags: brick,lego
  1. UOM = "mm"
  2. DEFAULT_PRESET = "4x1"
  3. num_bumps_long = IntegerParam(min=0.0,max=50.0,presets={'4x1':4,'4x1-thin':4,'4x2':4,'4x2-thin':4},group="Basics", desc="Number of Lego Bumps Long")
  4. num_bumps_wide = IntegerParam(min=0.0,max=50.0,presets={'4x1':1,'4x1-thin':1,'4x2':2,'4x2-thin':2},group="Basics", desc="Number of Lego Bumps Wide")
  5. thickness = FloatParam(min=0.0,max=50.0,presets={'4x1':9.6,'4x1-thin':3.2,'4x2':9.6,'4x2-thin':3.2},group="Basics", desc="Brick Thickness")
  6. #
  7. # Your build method. It must return a solid object
  8. #
  9. def build():
  10. #lego brick constants
  11. P = 8.0 #nominal pitch
  12. c = 0.1 #clearance on each brick side
  13. H = thickness.value #nominal height of a brick
  14. bumpDiam = 4.8 #the standard bump diameter
  15. t = ( P - ( 2*c) - bumpDiam ) / 2.0 # the nominal thickness of the walls, normally 1.5
  16. lbumps = num_bumps_long.value
  17. wbumps = num_bumps_wide.value
  18. postDiam = P - t #works out to 6.5
  19. total_length = lbumps*P - 2.0*c
  20. total_width = wbumps*P - 2.0*c
  21. s = Workplane("XY").box(total_length,total_width,H) #make the base
  22. s = s.faces("<Z").shell(-1.0* t) #shell inwards not outwards
  23. s = s.faces(">Z").workplane().rarray(P,P,lbumps,wbumps,True).circle(bumpDiam/2.0).extrude(1.8) # make the bumps on the top
  24. #add posts on the bottom. posts are different diameter depending on geometry
  25. #solid studs for 1 bump, tubes for multiple, none for 1x1
  26. tmp = s.faces("<Z").workplane(invert=True)
  27. if lbumps > 1 and wbumps > 1:
  28. tmp = tmp.rarray(P,P,lbumps - 1,wbumps - 1,center=True).circle(postDiam/2.0).circle(bumpDiam/2.0).extrude(H-t)
  29. elif lbumps > 1:
  30. tmp = tmp.rarray(P,P,lbumps - 1,1,center=True).circle(t).extrude(H-t)
  31. elif wbumps > 1:
  32. tmp = tmp.rarray(P,P,1,wbumps -1,center=True).circle(t).extrude(H-t)
  33. return tmp