@rulecad/smooth
v0.3.0
Published
Autorouted circuit-board tracks pulled tight: wiggles, detours and doubling-backs straightened wherever the straight run keeps its clearance. Only bend points are dropped, so ends, vias and every other track stay put. Works on Circuit JSON (tscircuit) or
Maintainers
Readme
@rulecad/smooth
Autorouted circuit-board tracks, pulled tight. An autorouter searching a grid leaves wiggles, small detours, hooks and
places where a track doubles back on itself. smooth() straightens them:
- Pulling tight: along each track, it joins each point it keeps straight to the farthest point after it that it can reach with clearance, and drops the bend points in between. It repeats until nothing changes.
- Sharp bends: a bend sharper than a right angle (a hook, or a spike out and back) is cut across, as far back as the cut keeps its clearance.
- Never worse: a new straight run must keep its clearance from all other copper, holes and the board's edge, or at least come no closer to anything than the stretch it replaces already did. So a track the router packed in tight can still be tidied, and nothing ends up closer than before.
A track's ends and vias stay exactly where they were, so it still starts and finishes on the same pads and changes layer in the same places. Tracks you don't pass in are never moved, only kept clear of. No dependencies.
Lead-ins: a track may carry on from copper you keep, such as a stub of an earlier board's routing. Pass it as the
track's leads ({ start, end }, points from the stub's root to the track's end). Where the track turns straight back
on it, it is cut back to where the track can leave cleanly, never into its first segment. trimmed then says what's
left of it, so you can shorten that copper to match.
import { smoothCircuit } from '@rulecad/smooth';
// Circuit JSON (tscircuit's board format): pick the tracks that may change
const { json, changed, dropped, trimmed } = smoothCircuit(circuitJson, { movable: (trace) => trace.autorouted, clearance: 0.15, leads });- Clearance: copper to copper is
clearance(default: the board'smin_trace_to_pad_edge_clearance, else 0.1 mm). Holes keepmin_trace_to_hole_edge_clearance(0.2 mm) from their edge. The board's edge keepsmin_board_edge_clearance(0.2 mm), measured to the outline, or to the board's rectangle. - Nets: copper of a track's own net isn't in its way. Nets come from the board's source traces.
- What's in the way: pads (rectangles, turned rectangles, circles, pills, polygons), plated holes and their pads,
vias, mounting holes (with
rulecad_keepout, a screw head's room, from the centre), and every other track. - Runs: a track is pulled tight one run at a time. A run is the points on one layer at one width, so a via or a necked-down stretch stays where it is.
- Result:
jsonis a copy with the changed tracks replaced.routesmaps each changed track'spcb_trace_idto its new route.
For any other board description, smooth(tracks, obstacles, { clearance, outline, edge }) does the geometry:
tracks are { route, net }, and obstacles are { layer, net, shape }. A shape is { circle: { x, y, r } },
{ capsule: { a, b, r } } or { polygon: [{ x, y }, ...] }. The layer may be '*' for every layer, and an
optional clear overrides the clearance.
Finishing: square exits and no acute angles
finishCircuit(json, { movable, clearance, stubs }) finishes a board's tracks the way a fab's engineer asks:
- Square exits: a track leaving a surface-mount pad at an angle is redrawn to leave straight out of the pad's
nearest edge, for a short run past it (
stubs, tried longest first: 0.3, 0.2, 0.12 mm) before it turns, never through a new bend sharper than a right angle. A small part whose tracks pull off its pads at an angle can be drawn up on one end as its solder melts (tombstoning); square exits pull evenly. - Acute corners: where two tracks of a net meet at under 90° (a bend, a junction, at a via or a pad), a short piece of the same copper is laid across the corner, so the notch between them, where etchant pools (an acid trap), becomes two obtuse corners. A corner whose notch is already inside copper of its net (a pad, a via) isn't one.
- Every new piece keeps the board's clearance from all other copper and the edge. What it can't finish is in
left.
It returns { json, exits, fills, left }. A redrawn kept track (rulecad_kept) carries its first shape as
rulecad_squared; fill copper is a track of its own, marked rulecad_fill. unfinished(json) lists what a board still
has: { exits, acute }, for checks.
The straight runs can be at any angle; they aren't snapped to 45°. Check the board again after smoothing, as with any
change to copper. @rulecad/pcb does that itself: with smooth: true, it smooths the router's tracks, and puts back
any track the design checks then find in a new error.
