STL export produces a broken mesh when a hole is drilled through a curved wall (0.6.6)
I made a 3D-printed bracket with cadgen: a curved plastic plate with screw holes in
it. When I loaded one version of its exported STL into my slicer (Bambu Studio), it
warned that the mesh was broken. The repo's own dfam-check skill also reported the
bracket's STL as not watertight and couldn't give it a volume.
I cut the part down until I had the smallest shape that still goes wrong: a curved strip with a single hole drilled through it. On that shape, cadgen's STL export writes one triangle on the edge of the hole twice, and leaves about 20 zero-area triangles (slivers flattened into a line) around the same edge. The duplicate is the serious one. It means some edges are shared by more than two triangles, so the mesh no longer describes a proper closed solid:
skills/dfam-checkreportswatertight: falseand can't compute a volume.- Bambu Studio reports
manifold = no.
The CAD shape itself seems fine. If I export the exact same shape with
OpenCascade's mesher instead (build123d.export_stl), the STL comes out with no
defects at all. So the problem looks to be in cadgen's mesh export, not in the model.
This looks similar to #371, which #397 fixed in 0.6.1. The difference is that here the hole goes through a curved face rather than a flat one, and it still happens on 0.6.6.
Environment
| cadgen | 0.6.6 from PyPI (also 0.6.5) |
| build123d | 0.11.1 |
| cadquery-ocp | 7.9.3.1.1 |
| Python | 3.12.14 |
| OS | macOS 27.0, arm64 |
| install | pip install cadgen==0.6.6 into a clean venv |
Reproduction
repro.py builds the smallest shape I found that goes wrong: a strip of curved wall
(90 mm radius, 2.2 mm thick), cut to 130 mm long with square ends, with one Ø3.3 mm
hole drilled through the top of the curve.
from cadgen import build123d as bd
from cadgen import stl
R, T = 90.0, 2.2 # outer radius, wall thickness
L, W = 130.0, 40.0 # length along X (chord), width along Y
HOLE = True
@stl(out="STL/strip.stl")
def strip():
ring = bd.Cylinder(R, 2 * W, rotation=(90, 0, 0)) - bd.Cylinder(R - T, 2 * W, rotation=(90, 0, 0))
body = ring & bd.Pos(0, 0, R / 2) * bd.Box(L, W, R) # top of the ring, ends vertical
if HOLE:
body -= bd.Pos(0, 0, R) * bd.Cylinder(1.65, 10)
return body
if __name__ == "__main__":
strip()To reproduce it, run python repro.py, which writes STL/strip.stl, then check that
file with python check_stl.py STL/strip.stl.
check_stl.py (below) is a short checker that only uses the Python standard
library, so no third-party mesh tool is involved. It reads the STL exactly as written
and counts three kinds of problem: triangles that appear more than once, triangles
with zero area, and edges that aren't shared by exactly two triangles. (In a clean,
closed mesh every edge joins exactly two triangles.)
check_stl.py"""Mesh defects in a binary STL, standard library only.
python check_stl.py FILE.stl
Vertices are welded on exact coordinates, as written.
"""
import collections
import struct
import sys
data = open(sys.argv[1], "rb").read()
n = struct.unpack("<I", data[80:84])[0]
tris = [struct.unpack("<9f", data[84 + i * 50 + 12:84 + i * 50 + 48]) for i in range(n)]
def area(t):
ax, ay, az, bx, by, bz, cx, cy, cz = t
ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
x, y, z = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
return 0.5 * (x * x + y * y + z * z) ** 0.5
ids, edges = {}, collections.Counter()
for t in tris:
v = [ids.setdefault(t[j:j + 3], len(ids)) for j in (0, 3, 6)]
for a, b in ((0, 1), (1, 2), (2, 0)):
edges[tuple(sorted((v[a], v[b])))] += 1
distinct = len({tuple(sorted((t[0:3], t[3:6], t[6:9]))) for t in tris})
uses = collections.Counter(edges.values())
print(f"triangles {n} distinct {distinct} zero-area {sum(area(t) < 1e-9 for t in tris)} "
f"open edges {uses[1]} edges on >2 triangles {sum(c for k, c in uses.items() if k > 2)}")
sys.exit(0 if distinct == n and uses.keys() == {2} else 1)What I get:
triangles 5678 distinct 5677 zero-area 20 open edges 0 edges on >2 triangles 2Of the 5,678 triangles written, only 5,677 are different, because one of them appears twice. There are also 20 zero-area triangles, and 2 edges are shared by more than two triangles. The duplicated triangle sits on the edge of the hole, where it comes out through the inside (concave) face of the curve. Its corners are:
(1.133864, -1.198688, 87.792679)
(1.191288, -1.141636, 87.791924)
(1.209716, -1.12209, 87.791664)To make sure this isn't just my checker, I opened the same file in two other tools:
- The repo's
dfam-checkskill (measure) says the mesh is not watertight (watertight: false) and can't work out its volume (volume_mm3: null). - Bambu Studio 02.08.02 (
--info) saysmanifold = no.
What I'd expect: every triangle different, no zero-area triangles, and every edge
shared by exactly two triangles. That's what I get when I export the same shape with
build123d's own STL export instead of cadgen's
(build123d.export_stl(shape, path, tolerance=0.01, angular_tolerance=0.05)):
triangles 1708 distinct 1708 zero-area 0 open edges 0 edges on >2 triangles 0What changes the result
There are two separate defects in the output, and they respond differently when the part is changed:
- Zero-area triangles: thin slivers whose three corners lie on a straight line, all along the edge of the hole where it meets the curved face.
- The duplicated triangle: the same triangle written twice. This is the one that makes the mesh non-manifold, so it's what dfam-check and Bambu Studio reject.
I changed one thing at a time in repro.py to see what each defect depends on:
- Remove the hole (
HOLE = False): both defects disappear, and the strip exports cleanly as 340 triangles. The hole is what triggers the problem. - Tighten the curve (
R, T = 60.0, 2.2, a 60 mm radius instead of 90): the duplicated triangle goes away, but 19 zero-area triangles remain. - Flatten the curve (
R, T = 200.0, 2.2): it gets worse, with 2 duplicated triangles and 73 zero-area ones. - Keep the whole top half of the ring instead of cutting a 130 mm strip with square ends, so the ends are far from the hole, then drill the hole at angles from 0° to 64° in 2° steps: there's no duplicated triangle at any of them. The angles I also counted slivers at (0°, 5°, 15°, 30°, 38°, 45° and 60°) each still had 10–22 zero-area triangles.
So the zero-area slivers come from the hole meeting a curved face and appear in every case. The duplicated triangle only appears with both the hole and the short strip with square ends, and it depends on the radius.
How it showed up
A 3D-printed guitar bracket: a curved 2.2 mm plate with four countersunk screw holes.
Bambu Studio flagged its STL (78 non-manifold edges on a variant with engraved text), and
dfam-check refuses it. Changing mesh_tolerance/mesh_angular_tolerance shifts the
defects around but none of the settings I tried removed them.
Investigated with Claude Code; the reproduction and every number above were run and checked locally before posting.
Source: earthtojake/text-to-cad