3dSynth

Tutorials / Custom G-code

Open in the interactive tutorials reader

---
title: Tutorial 24 Writing a Parametric Pattern by Hand
description: Creating complex infill patterns from scratch.
---

Tutorial 24 Writing a Parametric Pattern by Hand

1. Lesson Header

2. Concept Introduction

The Unit Cell.
Complex patterns (Honeycomb, Gyroid, Hilbert Curve) are just simple shapes repeated.
To write them by hand, we don't calculate every point.
We design a Unit Cell—the smallest repeatable block.
Then we use Relative Movement (G91) to stack them like LEGO bricks.

3. Machine State Explanation

Relative vs Absolute (Review).
- G90: Absolute. "Go to X100". (Good for outlines).
- G91: Relative. "Go Right 10mm". (Good for patterns).

The Hexagon Math.
A hexagon has 6 sides.
Internal angle: 120 degrees.
If Side Length = 10mm:
- Move X10 (Flat top).
- Move X5 Y-8.66 (Down-Right).
- Move X-5 Y-8.66 (Down-Left).
- Move X-10 (Flat bottom).
- Move X-5 Y8.66 (Up-Left).
- Move X5 Y8.66 (Up-Right).

4. Command Breakdown

5. Minimal Working Example

The Zig-Zag Unit.
A simple zig-zag is just:
G1 X5 Y5
G1 X5 Y-5
Repeat.

G91
; Unit 1
G1 X5 Y5 E1
G1 X5 Y-5 E1
; Unit 2
G1 X5 Y5 E1
G1 X5 Y-5 E1

6. Visual Representation

Interactive preview is available in the interactive reader.

7. Build Exercise

Task: Create a 3x3 Honeycomb Grid.
1. Design the "Half-Hex" Unit Cell.
- Move 1: Up-Right (X5 Y8.66)
- Move 2: Right (X10)
- Move 3: Down-Right (X5 Y-8.66)
2. Repeat this 3 times to make a row.
3. Return to start of next row.

The "Continuous Line" Constraint.
Printers hate stopping. We want a path that never crosses itself and never lifts the nozzle (Vase Mode style).
This is the "Eulerian Path" problem.
For a honeycomb, we can't do it perfectly without doubling back.
Solution: The "Wiggle" pattern (used in Gyroid).

Let's stick to a simple Hex Grid (with travel moves).
Row 1:
- Hex 1 Top Half.
- Hex 2 Top Half.
- Hex 3 Top Half.
Row 2:
- Hex 3 Bottom Half.
- Hex 2 Bottom Half.
- Hex 1 Bottom Half.

8. Deep Insight Section

Parametric Design.
If you change the Side Length ($L$), all coordinates update.
$X = L * cos(60)$
$Y = L * sin(60)$
In G-code, you have to calculate this manually (or use a script).
But understanding the relationship allows you to scale patterns mentally.

9. Common Failure Modes

  1. Accumulated Error: In Relative Mode (G91), if your math is off by 0.01mm, after 100 repetitions you are off by 1mm.
  2. Travel Scars: If you don't use Retraction (G10) between non-connected hexes, you get stringing.

10. Real-World Application

Custom Infill.
Slicers offer standard infills (Grid, Triangles).
But what if you want a logo-shaped infill? Or a variable-density infill (dense at walls, sparse in center)?
Writing your own G-code generator (in Python) allows this.

11. Final Clean Version

; Lesson 24 - The Honeycomb
G21
G90
M83
G28
G1 Z0.2 F3000

; Move to Start
G1 X50 Y50 ; Center
G91 ; RELATIVE MODE NOW

; --- Row 1 (Top Halves) ---
; Hex 1
G1 X5 Y8.66 E1 ; Up-Right
G1 X10 Y0 E1   ; Top
G1 X5 Y-8.66 E1 ; Down-Right

; Hex 2
G1 X5 Y8.66 E1
G1 X10 Y0 E1
G1 X5 Y-8.66 E1

; Hex 3
G1 X5 Y8.66 E1
G1 X10 Y0 E1
G1 X5 Y-8.66 E1

; --- Return Path (Bottom Halves) ---
; We are now at the right edge. We need to go back.
; Hex 3 Bottom
G1 X-5 Y-8.66 E1 ; Down-Left
G1 X-10 Y0 E1    ; Bottom
G1 X-5 Y8.66 E1  ; Up-Left (Wait, this closes the hex!)

; But we want to move LEFT to the next hex.
; The path logic is tricky.
; Let's just do the simple "Zig Zag" Hex pattern.

; RESET
G90
G1 X50 Y50
G91

; Improved Path:
; / \ / \ / \
; | | | | | |
; \ / \ / \ /

; This is easier. Vertical lines and Zig-Zags.
; Let's build a single "Cell" that is just a vertical wall with a zig-zag.
; (This is getting complex for manual code. Let's simplify to a Triangle Grid).

; --- Triangle Grid (Isogrid) ---
; Unit: Up-Right, Right, Down-Left (Close Triangle).
; Triangle 1
G1 X5 Y8.66 E1
G1 X10 Y0 E1
G1 X-15 Y-8.66 E1 ; Close it (Hypotenuse)

; Move to next start (Right 10)
G0 X10 Y0 ; Travel

; Triangle 2
G1 X5 Y8.66 E1
G1 X10 Y0 E1
G1 X-15 Y-8.66 E1

G90 ; Back to Absolute safety
G28 X0 Y0

12. Stretch Challenge

Challenge: Write a script (Python/JS) that generates G-code for a Hilbert Curve.
The Hilbert Curve is a continuous fractal space-filling curve.
It is the perfect infill pattern because it supports itself and never crosses.
Hint: You need a recursive function.
Manual Challenge: Draw a "Level 2" Hilbert Curve on graph paper and translate it to G-code coordinates manually.
Points: (0,0) -> (0,1) -> (1,1) -> (1,0) -> (2,0) -> ...