Split a cube into 27, remove the centre and the six face centres, and repeat on each of the 20 cubes left. The limit is the Menger sponge. Each level keeps 20/27 of the volume, so the volume tends to nothing while the surface grows without bound. Cut it through its centre, square to a long diagonal, and the holes appear as six-pointed stars.

A cube riddled with square tunnels

Reference: choose Examples  Menger sponge on the canvas toolbar to load the finished definition.

Build it

Holes for every level

  1. Place three sliders: Size: 27 (9 – 60), Level: 2 (1 – 3) with Decimals 0, and Cut offset: 0 (-20 – 20).

  2. Place a Series (Sets), Level no., with S 1, N 1 and Level → C: 1, 2, … up to the level.

  3. Place four Expression (Maths) components on it: Holes per side 3(x - 1), Hole width y/3x (with Size), Hole pitch y/x (holes per side, Size) and Holes per face x*x.

  4. Place a Series, Hole no., with the holes per face → C: one list per level.

  5. Place two Expression components, First coordinate (x % y - (y - 1)/2)z and Second coordinate (floor(x/y) - (y - 1)/2)*z, with the hole numbers, the holes per side and the pitch, with Graft on *y and z: hole q sits at column q % m and row ⌊q/m⌋ of the level’s grid, centred on the cube.

Bars and the cube

  1. Place a Multiplication (Maths), Size × 1.1 (bar length), and three Center Box (Surface) components for bars along X (length, width, width), Y (width, length, width) and Z (width, width, length), with Graft on the width inputs.

  2. Place an Expression, Bar bottom, x - y/2, of the second coordinate and the width (grafted), and three Move (Transform) components: X bars to (0, first, bottom), Y bars to (first, 0, bottom), Z bars to (first, second, −length/2).

  3. Place a Center Box of Size on all three sides, moved down by half its size, and a Solid Difference (Intersect), Sponge, with the cube → A and the three sets of bars wired into B, with Flatten on B.

The diagonal cut

  1. Place a Multiplication, Size × 4, and a Center Box of that size: a block standing on z = 0.

  2. Place a Rotate (Transform) with A 54.735610317245346 and X (axis) 1, 1, 0: the block’s bottom turns square to the diagonal (1, −1, 1), and the block fills the half beyond it.

  3. Place an Expression, x/sqrt(3), of Cut offset, a Multiplication of that by -1, and a Move by (shift, −shift, shift): along the diagonal.

  4. Place a Solid Difference, Diagonal cut, of the sponge and the block.

  5. Turn Preview off on everything except the cut.

The Menger sponge definition on the canvas
Figure 1. The finished definition
The red preview of the Menger sponge
Figure 2. The preview in FreeCAD’s 3D view

What is happening

A point is removed at some level when two of its three coordinates fall in the middle third of their cell at that level. That is exactly a square bar through the whole cube, along the third axis. So the whole sponge is one cube minus 3 × (1 + 9 + 81 …) bars, one Boolean. A tempting shortcut, a 3 × 3 grid of crosses in one plane, misses most of the second level’s tunnels: along each axis there are 9 tunnels at level 2, not 3.

The geometry tests check the volume, Size³ × (20/27)Level, and that the centred cut keeps exactly half. The angle 54.7356° is the angle between a cube’s edge and its long diagonal, arccos(1/√3).

At Level 2 the sponge updates in about a second and a half. Level 3 has 273 bars and over 7 000 faces, and FreeCAD’s Boolean takes a minute or more.

Things to try

  • Drag Cut offset away from 0 and watch the pattern of holes change as the plane moves along the diagonal.

  • Set Level to 1 for the first step, a cube with a cross of tunnels.

  • Set Level to 3 and wait a minute or more for the next level of detail.