geometry module#
Convert JITX shapes into Gerber graphic objects and apertures.
Three targets, and which one a shape gets is a property of what the shape is for rather than of its geometry:
a pad becomes an aperture that is flashed, always. 4.10 requires that all pads are flashed and all flashes are pads, so a shape a macro cannot express exactly is composed from primitives or linearized, never turned into a region.
a stroked shape (a routed trace, a legend outline) becomes draws and arcs with a circular aperture, which is what Gerber’s round caps and joins already mean.
a filled shape becomes region contours, an island with its holes.
Mirroring is read off the composed transform and nowhere else. A transform composes as translate * rotate * scale, so a point maps to t + R(r)·S(s)·p; an aperture flashed at t with rotation r therefore holds S(s)·p, which is the whole of the mirror handling and why none of it is hand-rolled here.
- TOLERANCE = 0.0001#
Arc linearization tolerance in mm, for the paths that must linearize.
Two orders of magnitude tighter than from_primitive’s 1e-2 default and two above the 1 nm coordinate grid, which leaves room on both sides: a linearized pad stays well inside the outline primitive’s vertex budget, and the error is invisible at the output resolution.
- exception GeometryError[source]#
Bases:
ValueErrorA shape cannot be expressed in the Gerber model.
- class Placer(total)[source]#
Bases:
objectA shape’s composed transform, decomposed once.
- Parameters:
total (
Transform) – the full transform from the shape’s own frame to the board, already composed. Nothing here adds a side flip of its own: a bottom-side Placement carries its mirror already, and applying another composes two mirrors into none.- Raises:
GeometryError – the transform scales x and y differently, which neither an aperture nor an arc can express.
- placer(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
The placer for a shape under an accumulated transform.
- surface_side(transform, side)[source]#
Which side of the board a surface feature ends up on.
A SurfaceFeature’s side is relative to its placement - Bottom means the opposite side from where the owning landpattern sits - so the answer comes off the composed transform, like every other side question here.
- class Island(outer, holes=())[source]#
Bases:
objectOne filled area: its outer ring and the holes inside it.
The grouping is what any ordering moves, so an island can never be separated from a hole that would erase it.
- to_islands(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
Convert a filled shape to islands with their holes, arcs intact.
- Raises:
GeometryError – the geometry is not a filled shape.
- Return type:
- Parameters:
- to_path(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
Convert a polyline to a placed start point and its segments.
The open-path counterpart of to_islands. to_strokes images the path with an aperture; a drill rout needs the same walk as bare geometry, since XNC states a path and a tool rather than an image.
- to_strokes(shape, aperture, transform=Transform((0, 0), 0, (1, 1)), attrs=())[source]#
Convert a polyline to the draws and arcs that stroke it.
- text_string(shape)[source]#
The string a text shape carries, or None when it is not text.
A query rather than a raise, because the caller’s next question is usually whether the string is a label placeholder, and a shape that is not text simply has no string to inspect.
- text_stroke_width(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
The placed stroke diameter legend text of this shape is drawn with.
Separate from to_text_strokes for the same reason stroke_width is separate from to_strokes: the aperture carries TA.AperFunction as well as a diameter, and what function it has is the caller’s decision.
- Raises:
GeometryError – the shape is not text.
- Return type:
- Parameters:
- to_text_strokes(shape, aperture, transform=Transform((0, 0), 0, (1, 1)), attrs=(), string=None)[source]#
Stroke a text shape into the draws that write it.
- Parameters:
- Return type:
Gerber has no native text (5.7), so a legend string is a run of D01 draws with a small circular aperture. The glyphs come from format.font and the anchor is resolved against that font’s own metrics, so the box the anchor names is the box the ink actually fills.
A glyph that is a dot comes out as a zero-length draw, which is what the spec’s note under 4.8 describes and the only correct object for it: the same image as a flash, but not a flash, because every flash is a pad.
- stroke_width(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
The placed stroke width of a polyline shape.
- pad_aperture(shape, transform=Transform((0, 0), 0, (1, 1)), attrs=(), compat=Compat(bake_macro_rotation=False))[source]#
The aperture and flash point for a pad shape, or None when empty.
The result always flashes. Standard templates serve the shapes they can express exactly, a macro composition serves rotated rectangles, capsules, rounded rectangles and annular pads, and only a genuinely free-form outline is linearized - at TOLERANCE, which is chosen against the outline primitive’s vertex budget rather than independently.
- Parameters:
- Raises:
GeometryError – the shape cannot be expressed as an aperture.
ApertureError – the shape needs more outline vertices than one primitive holds.
- Return type:
- to_shapely(shape, transform=Transform((0, 0), 0, (1, 1)))[source]#
A filled shape as shapely geometry on the output grid.
Snapping happens here rather than at the union, because shapely’s predicates are float-exact: two rectangles abutting at a shared edge intersect, the same pair with a 1e-12 gap do not, and the two answers give different connected components. Deciding adjacency on the grid the file is written on is the only definition that matches the output.
- Return type:
- Parameters:
- ring_contour(points, *, ccw)[source]#
A closed contour from a point ring, wound to the convention.
The ring is expected already placed; this is the entry point for geometry that arrives from shapely rather than from a JITX primitive.