Advertisement
Guest User

https://www.reddit.com/r/openscad/comments/1jtndto/help_with_hexagon_honeycomb_project/

a guest
Apr 7th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | Source Code | 0 0
  1. // Parameters
  2. radi = 37 / 2;
  3. thickness = 4; // Thickness of the frame posts
  4. radius = radi + thickness ; // Distance from center to a vertex - divided by 2 cause spud
  5. radialfix = radius / 2; // yeah I should have used diameter...
  6. height = 33; // Height of each prism
  7. base_plate_thickness = .5; // ?? Thickness of the optional base plate fill
  8. stack_height = 3; // Number of hexagons in the vertical stack
  9. inner_hole_radius = radius / 1.5; // Radius of the hexagon hole in the base - divided by 2 cause spud
  10. slant_deg = 500; // Slant in degrees
  11. slant_rad = slant_deg * PI / 180;
  12. slant_test= 20 / PI * 180;
  13. additional_stack_offset = radialfix + radius;
  14. offsetpile = stack_height;
  15.  
  16. // Base module flag
  17. add_base_fill = true; // Set to true to add filled base hexagons
  18. cut_height = height; // Height at which to cut the base hexagons
  19.  
  20. // X-Axis Cut parameters
  21. perform_x_cut = true; // Set to true to perform the X-axis cut
  22. x_cut_width = radius * 6; // Width of the central cut
  23. x_cut_depth = height * 3; // Depth of the cut
  24. x_cut_position = radius; // Position of the cut center
  25.  
  26. // Function to generate hexagon vertices with optional slant
  27. function hexagon_vertices(r, z, slant = 0, h = 0) =
  28. let(slant_y = tan(slant) * h)
  29. [
  30. for (angle = [0:60:300])
  31. z == 0 ?
  32. [r * cos(angle), r * sin(angle), z] :
  33. [r * cos(angle), r * sin(angle) + slant_y, z ]
  34. ];
  35.  
  36. // Create a connecting post between two points
  37. module frame_post(p1, p2, d) {
  38. hull() {
  39. translate(p1) cube(d/1.5);
  40. translate(p2) cube(d/1.5);
  41. }
  42. }
  43.  
  44. module frame_post_l(p1, p2, d, faces = 3) {
  45. hull() {
  46. // First cylinder at position p1 (align it to the corner of the square)
  47. translate([p1[0] + d/3, p1[1] + d/3, p1[2]]) // Shift in the XY plane to align with the corner
  48. cylinder(h = d/1.5, r = d/2, $fn = faces, center = false);
  49. // Second cylinder at position p2 (align it to the corner of the square)
  50. translate([p2[0] + d/3, p2[1] + d/3, p2[2]]) // Shift in the XY plane to align with the corner
  51. cylinder(h = d/1.5, r = d/2, $fn = faces, center = false);
  52. }
  53. }
  54.  
  55. // Solid hexagonal prism - used for base fill
  56. module solid_hex_prism(r, h, slant = 0) {
  57. top_verts = hexagon_vertices(r, h, slant, h);
  58. bottom_verts = hexagon_vertices(r, 0);
  59. // Create faces using polyhedron
  60. faces = [
  61. // Bottom face
  62. [0, 1, 2, 3, 4, 5],
  63. // Top face
  64. [11, 10, 9, 8, 7, 6],
  65. // Side faces
  66. [0, 6, 7, 1],
  67. [1, 7, 8, 2],
  68. [2, 8, 9, 3],
  69. [3, 9, 10, 4],
  70. [4, 10, 11, 5],
  71. [5, 11, 6, 0]
  72. ];
  73.  
  74. points = concat(bottom_verts, top_verts);
  75. polyhedron(points = points, faces = faces, convexity = 10);
  76. }
  77.  
  78. // Hexagonal frame with optional base plate fill
  79. module hexagonal_prism_frame(r, h, d, slant = 0, fill_base=false, inner_hole_r=4, plate_thickness=2) {
  80. top_verts = hexagon_vertices(r, h, slant, h); // slanted top
  81. bottom_verts = hexagon_vertices(r, 0); // flat base
  82.  
  83. // Connect vertical edges
  84. for (i = [0:5]) {
  85. frame_post_l(top_verts[i], bottom_verts[i], d);
  86. }
  87.  
  88. // Connect top and bottom hexagon edges
  89. for (i = [0:5]) {
  90. frame_post(top_verts[i], top_verts[(i+1)%6], d);
  91. frame_post(bottom_verts[i], bottom_verts[(i+1)%6], d);
  92. }
  93.  
  94. // Optional base fill
  95. if (fill_base) {
  96. translate([0, 0, 0])
  97. base_hex_plate(r, inner_hole_r, plate_thickness);
  98. }
  99. }
  100.  
  101. // Flat hexagonal base with central hexagonal hole, level with bottom verts
  102. module base_hex_plate(outer_radius, inner_radius, plate_thickness) {
  103. translate([0, thickness / 2, 0]) // Top aligns with z = 0
  104. difference() {
  105. // Outer solid hexagon
  106. linear_extrude(height = base_plate_thickness)
  107. polygon(points = [
  108. for (angle = [0:60:300])
  109. [outer_radius * cos(angle), outer_radius * sin(angle)]
  110. ]);
  111.  
  112. // Inner hole (hexagon)
  113. translate([0, 0, -0.1]) // Slight offset for clean cut
  114. linear_extrude(height = plate_thickness + 0.2)
  115. polygon(points = [
  116. for (angle = [0:60:300])
  117. [inner_radius * cos(angle), inner_radius * sin(angle)]
  118. ]);
  119. }
  120. }
  121.  
  122. module vertical_stack(r, h, d, n, x_offset, y_offset, slant = 0, fill_base=false, plate_thickness=2, is_first_row=false, stack_index=0) {
  123. spacing_y = 2 * r - d; // Y distance between prism centers so posts align
  124.  
  125. // Generate prism frames
  126. for (i = [0:n-1]) {
  127. y_pos = y_offset + i * spacing_y;
  128. translate([x_offset, y_pos, 0])
  129. hexagonal_prism_frame(r, h, d, slant, fill_base, inner_hole_radius, plate_thickness);
  130. }
  131.  
  132. // --- Base fill behavior split ---
  133. if (add_base_fill) {
  134. // Fill current stack only if its index is odd (1, 3, 5...)
  135. if (stack_index % 2 == 1) {
  136. translate([x_offset, y_offset, thickness / 2]) {
  137. difference() {
  138. solid_hex_prism(r, h, slant);
  139. translate([-r*2, -r*2, cut_height])
  140. cube([r*4, r*4, h]);
  141. }
  142. }
  143. }
  144.  
  145. // Always backfill staggered row behind
  146. if (!is_first_row) {
  147. translate([x_offset, y_offset - spacing_y + thickness / 2, thickness / 2]) {
  148. difference() {
  149. solid_hex_prism(r, h, slant);
  150. translate([-r*2, -r*2, cut_height])
  151. cube([r*4, r*4, h]);
  152. }
  153. }
  154. }
  155. }
  156. }
  157.  
  158. // Stack multiple vertical stacks in alternating honeycomb pattern
  159. module multi_stack(num_stacks, r, h, d, n, slant = 0, fill_base = false, plate_thickness = 2) {
  160. x_spacing = r + r / 2; // Horizontal offset between columns
  161. y_offset_shift = r - d / 2; // Vertical offset for staggered rows
  162. echo ("y-offset:")
  163. echo (y_offset_shift)
  164.  
  165. // Create the entire honeycomb structure
  166. difference() {
  167. union() {
  168. for (i = [0:num_stacks - 1]) {
  169. x_off = i * x_spacing;
  170. // ?? Reversed: stack 0 now gets Y offset (i.e., is a 'half')
  171. y_off = (i % 2 == 1) ? 0 : y_offset_shift;
  172. // ?? Flip is_first_row logic to match
  173. vertical_stack(r, h, d, n, x_off, y_off, slant, fill_base, plate_thickness, i % 2 == 1, i);
  174. }
  175. }
  176.  
  177. // X-cut stays the same
  178. if (perform_x_cut) {
  179. cut_box_width = num_stacks * x_spacing + r * 2;
  180. middle_stack = floor(num_stacks / 2);
  181. middle_x = middle_stack * x_spacing;
  182. translate([middle_x - r * 5, -r * 2, -0.1])
  183. cube([x_cut_width * 20, r * 1.35, x_cut_depth + 0.2]);
  184. }
  185. }
  186. }
  187.  
  188. // --- Render the scene ---
  189. multi_stack(5, radius, height, thickness, stack_height, slant_rad, true, base_plate_thickness);
Tags: OpenSCAD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement