@tscircuit/circuit-json-util 0.0.113 → 0.0.114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,6 +54,38 @@ It reduces the amount of code to retrieve or join elements from circuit json, it
54
54
  | `computeGapBetweenCopper` | [`lib/compute-gap-between-copper.ts`](./lib/compute-gap-between-copper.ts) | Computes the minimum copper-to-copper gap between two circuit elements by decomposing them into primitive shapes. |
55
55
  | `analyzePcbPin1Location` | [`lib/analyze-pcb-pin1-location.ts`](./lib/analyze-pcb-pin1-location.ts) | Infers a semantic pin 1 location from PCB pad geometry and numeric port hints. |
56
56
  | `categorizeErrorOrWarning` | [`lib/categorize-error-or-warning.ts`](./lib/categorize-error-or-warning.ts) | Categorizes DRC error/warning types into `"netlist"`, `"pin_specification"`, `"placement"`, `"routing"`, `"source"`, or `"unknown"`. |
57
+ | `getPourPolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Converts rectangular, polygon, or BRep pours into copper polygons, preserving cutouts and bulge arcs. |
58
+ | `getSmtPadPolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Converts SMT pad outlines into copper polygons. |
59
+ | `getPlatedHolePolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Converts plated pads into copper polygons with drilled holes excluded. |
60
+ | `getViaPolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Creates an annular copper polygon from a center and outer/hole diameters. |
61
+ | `getTraceSegmentPolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Creates the convex hull of two endpoint disks, with equal or differing widths. |
62
+ | `copperPolygonsTouch` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Tests polygon contact or containment while respecting holes. |
63
+ | `placePolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Rotates a polygon counterclockwise, then translates it to its board position. |
64
+ | `circlePolygon` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Creates a circular polygon with analytic arcs. |
65
+ | `roundedRectangle` | [`lib/copper-geometry.ts`](./lib/copper-geometry.ts) | Creates a positioned and optionally rotated rectangle with rounded corners. |
66
+
67
+ ## Copper geometry
68
+
69
+ These helpers return `Polygon` objects from `@flatten-js/core`. Coordinates are
70
+ PCB board-world points in millimeters, with +X right, +Y up, and counterclockwise
71
+ rotation in degrees. Polygon plated-pad outlines are footprint-local offsets;
72
+ `getPlatedHolePolygon(pad, componentRotation)` places them in board coordinates
73
+ using the pad's rotation, or the supplied component rotation when omitted.
74
+
75
+ ```ts
76
+ import { copperPolygonsTouch, getPlatedHolePolygon, getPourPolygon } from "@tscircuit/circuit-json-util"
77
+
78
+ const touches = copperPolygonsTouch(
79
+ getPourPolygon(pour),
80
+ getPlatedHolePolygon(platedHole),
81
+ )
82
+ ```
83
+
84
+ Callers must check net membership and shared copper layers themselves: these
85
+ helpers operate only on planar geometry. `copperPolygonsTouch` uses a default
86
+ numerical tolerance of `1e-7` mm, not a manufacturing clearance. If callers scale
87
+ the polygons, they must scale that tolerance by the same factor. The helpers do
88
+ not modify their circuit-json inputs or generate routes.
57
89
 
58
90
  ## Standard Usage
59
91
 
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as circuit_json from 'circuit-json';
2
- import { AnyCircuitElementInput, AnyCircuitElement, InsertionDirection, PcbBoard, SchematicComponent, SchematicNetLabel, SchematicTrace, SourceGroup, SourceComponentBase, PcbRenderLayer, PcbPin1Location } from 'circuit-json';
2
+ import { AnyCircuitElementInput, AnyCircuitElement, InsertionDirection, PcbBoard, SchematicComponent, SchematicNetLabel, SchematicTrace, SourceGroup, SourceComponentBase, PcbRenderLayer, PcbPin1Location, Point as Point$2, PcbPlatedHole, PcbCopperPour, PcbSmtPad } from 'circuit-json';
3
3
  import * as zod from 'zod';
4
4
  import { Matrix } from 'transformation-matrix';
5
5
  import * as parsel from 'parsel-js';
6
6
  import { FlexBoxOptions } from '@tscircuit/miniflex';
7
+ import { Polygon as Polygon$1 } from '@flatten-js/core';
7
8
 
8
9
  type CircuitJsonElementInsertInput<CircuitElementType extends AnyCircuitElement["type"], CircuitElement extends {
9
10
  type: CircuitElementType;
@@ -19470,4 +19471,29 @@ type DrcLike = {
19470
19471
  };
19471
19472
  declare const categorizeErrorOrWarning: (value: string | DrcLike) => DrcCategory;
19472
19473
 
19473
- export { type BoardBounds, type CircuitJsonInputUtilObjects, type CircuitJsonOps, type CircuitJsonTreeNode, type CircuitJsonUtilObjects, type CircuitJsonUtilOptions, type DrcCategory, type GetCircuitJsonUtilFn, type GetIndexedCircuitJsonUtilFn, type IndexedCircuitJsonUtilOptions, type PcbBounds, type PcbPin1LocationElement, type SchematicElementBounds, type SchematicElementWithBounds, type Size, type SubtreeOptions, analyzePcbPin1Location, applySelector, applySelectorAST, buildSubtree, categorizeErrorOrWarning, cju, cjuIndexed, computeClearanceBetweenElements, computeGapBetweenCopper, directionToVec, distanceBetweenCircleAndCircle, distanceBetweenCircleAndPolygon, distanceBetweenPolygonAndPolygon, distanceBetweenShapes, findBoundsAndCenter, getBoardBounds, getBoundsOfPcbElements, getCircuitJsonTree, getElementById, getElementId, getElementRenderLayers, getMinimumFlexContainer, getPcbElementBounds, getPcbElementsWithinBounds, getPrimaryId, getReadableNameForElement, getReadableNameForPcbPort, getReadableNameForPcbSmtpad, getReadableNameForPcbTrace, getSchematicElementBounds, getStringFromCircuitJsonTree, oppositeDirection, oppositeSide, repositionPcbComponentTo, repositionPcbGroupTo, repositionSchematicComponentTo, repositionSchematicGroupTo, rotateClockwise, rotateCounterClockwise, rotateDirection, su, transformInsertionDirection, transformPCBElement, transformPCBElements, transformPCBElement as transformPcbElement, transformPCBElements as transformPcbElements, transformSchematicElement, transformSchematicElements, vecToDirection };
19474
+ /** All geometry here is PCB world space in mm: +X right, +Y up, right-handed.
19475
+ * Arguments are points (translation applies); rotations are CCW about +Z.
19476
+ * Curves remain analytic arcs, so small contacts do not depend on tessellation.
19477
+ */
19478
+ declare const placePolygon: (polygon: Polygon$1, center: Point$2, degrees?: number) => Polygon$1;
19479
+ declare const circlePolygon: (center: Point$2, radius: number) => Polygon$1;
19480
+ declare const roundedRectangle: (center: Point$2, width: number, height: number, radius?: number, degrees?: number) => Polygon$1;
19481
+ declare const getPourPolygon: (pour: PcbCopperPour) => Polygon$1;
19482
+ declare const getSmtPadPolygon: (pad: PcbSmtPad) => Polygon$1;
19483
+ declare const getViaPolygon: (center: Point$2, outerDiameter: number, holeDiameter: number) => Polygon$1;
19484
+ /** Polygon plated-hole outlines are footprint-local offsets; all other pad
19485
+ * outlines and hole offsets use the emitted board-world dimensions/rotations.
19486
+ * The polygon placement matches PlatedHole.doInitialPcbComponentRender's local
19487
+ * pad_outline convention, with the owning component's rotation when omitted.
19488
+ */
19489
+ declare const getPlatedHolePolygon: (pad: PcbPlatedHole, componentRotation?: number) => Polygon$1;
19490
+ /** Copper for a straight trace segment in world mm, with round end caps.
19491
+ * Interpolated traces are the convex hull of their endpoint disks.
19492
+ */
19493
+ declare const getTraceSegmentPolygon: (start: Point$2, end: Point$2, startWidth: number, endWidth?: number) => Polygon$1;
19494
+ /** Same-layer contact in board-world mm. Holes are excluded from containment.
19495
+ * 1e-7 mm is numerical tolerance, not an electrical/manufacturing clearance.
19496
+ */
19497
+ declare const copperPolygonsTouch: (a: Polygon$1, b: Polygon$1, tolerance?: number) => boolean;
19498
+
19499
+ export { type BoardBounds, type CircuitJsonInputUtilObjects, type CircuitJsonOps, type CircuitJsonTreeNode, type CircuitJsonUtilObjects, type CircuitJsonUtilOptions, type DrcCategory, type GetCircuitJsonUtilFn, type GetIndexedCircuitJsonUtilFn, type IndexedCircuitJsonUtilOptions, type PcbBounds, type PcbPin1LocationElement, type SchematicElementBounds, type SchematicElementWithBounds, type Size, type SubtreeOptions, analyzePcbPin1Location, applySelector, applySelectorAST, buildSubtree, categorizeErrorOrWarning, circlePolygon, cju, cjuIndexed, computeClearanceBetweenElements, computeGapBetweenCopper, copperPolygonsTouch, directionToVec, distanceBetweenCircleAndCircle, distanceBetweenCircleAndPolygon, distanceBetweenPolygonAndPolygon, distanceBetweenShapes, findBoundsAndCenter, getBoardBounds, getBoundsOfPcbElements, getCircuitJsonTree, getElementById, getElementId, getElementRenderLayers, getMinimumFlexContainer, getPcbElementBounds, getPcbElementsWithinBounds, getPlatedHolePolygon, getPourPolygon, getPrimaryId, getReadableNameForElement, getReadableNameForPcbPort, getReadableNameForPcbSmtpad, getReadableNameForPcbTrace, getSchematicElementBounds, getSmtPadPolygon, getStringFromCircuitJsonTree, getTraceSegmentPolygon, getViaPolygon, oppositeDirection, oppositeSide, placePolygon, repositionPcbComponentTo, repositionPcbGroupTo, repositionSchematicComponentTo, repositionSchematicGroupTo, rotateClockwise, rotateCounterClockwise, rotateDirection, roundedRectangle, su, transformInsertionDirection, transformPCBElement, transformPCBElements, transformPCBElement as transformPcbElement, transformPCBElements as transformPcbElements, transformSchematicElement, transformSchematicElements, vecToDirection };
package/dist/index.js CHANGED
@@ -2770,16 +2770,218 @@ var categorizeErrorOrWarning = (value) => {
2770
2770
  if (SOURCE_TYPES.has(drcType)) return "source";
2771
2771
  return "unknown";
2772
2772
  };
2773
+
2774
+ // lib/copper-geometry.ts
2775
+ import {
2776
+ Arc,
2777
+ Box,
2778
+ Circle,
2779
+ Matrix,
2780
+ ORIENTATION,
2781
+ Point,
2782
+ Polygon,
2783
+ Segment
2784
+ } from "@flatten-js/core";
2785
+ import { compose, rotate, translate as translate5 } from "transformation-matrix";
2786
+ var placePolygon = (polygon, center, degrees = 0) => {
2787
+ const { a, b, c, d, e, f } = compose(
2788
+ translate5(center.x, center.y),
2789
+ rotate(degrees * Math.PI / 180)
2790
+ );
2791
+ return polygon.transform(new Matrix(a, b, c, d, e, f));
2792
+ };
2793
+ var ccw = (polygon) => polygon.orientation() === ORIENTATION.CCW ? polygon : polygon.reverse();
2794
+ var circlePolygon = (center, radius) => ccw(new Polygon(new Circle(new Point(center.x, center.y), radius)));
2795
+ var roundedRectangle = (center, width, height, radius = 0, degrees = 0) => {
2796
+ const x = width / 2;
2797
+ const y = height / 2;
2798
+ const r = Math.max(0, Math.min(radius, x, y));
2799
+ if (r === 0)
2800
+ return placePolygon(
2801
+ ccw(new Polygon(new Box(-x, -y, x, y))),
2802
+ center,
2803
+ degrees
2804
+ );
2805
+ const arcs = [
2806
+ new Arc(new Point(x - r, -y + r), r, -Math.PI / 2, 0, true),
2807
+ new Arc(new Point(x - r, y - r), r, 0, Math.PI / 2, true),
2808
+ new Arc(new Point(-x + r, y - r), r, Math.PI / 2, Math.PI, true),
2809
+ new Arc(new Point(-x + r, -y + r), r, Math.PI, Math.PI * 1.5, true)
2810
+ ];
2811
+ const edges = [];
2812
+ for (let i = 0; i < arcs.length; i++) {
2813
+ const arc = arcs[i];
2814
+ const next = arcs[(i + 1) % arcs.length];
2815
+ edges.push(arc);
2816
+ if (arc.end.distanceTo(next.start)[0] > 1e-12)
2817
+ edges.push(new Segment(arc.end, next.start));
2818
+ }
2819
+ return placePolygon(ccw(new Polygon(edges)), center, degrees);
2820
+ };
2821
+ var ringPolygon = (ring) => {
2822
+ const edges = [];
2823
+ for (let i = 0; i < ring.vertices.length; i++) {
2824
+ const start = ring.vertices[i];
2825
+ const end = ring.vertices[(i + 1) % ring.vertices.length];
2826
+ const dx = end.x - start.x;
2827
+ const dy = end.y - start.y;
2828
+ if (dx === 0 && dy === 0) continue;
2829
+ const bulge = start.bulge ?? 0;
2830
+ if (bulge === 0) {
2831
+ edges.push(
2832
+ new Segment(new Point(start.x, start.y), new Point(end.x, end.y))
2833
+ );
2834
+ } else {
2835
+ const offset = (1 - bulge * bulge) / (4 * bulge);
2836
+ const center = new Point(
2837
+ (start.x + end.x) / 2 - dy * offset,
2838
+ (start.y + end.y) / 2 + dx * offset
2839
+ );
2840
+ edges.push(
2841
+ new Arc(
2842
+ center,
2843
+ Math.hypot(dx, dy) * (1 + bulge * bulge) / (4 * Math.abs(bulge)),
2844
+ Math.atan2(start.y - center.y, start.x - center.x),
2845
+ Math.atan2(end.y - center.y, end.x - center.x),
2846
+ bulge > 0
2847
+ )
2848
+ );
2849
+ }
2850
+ }
2851
+ return ccw(new Polygon(edges));
2852
+ };
2853
+ var subtractHole = (outer, hole) => {
2854
+ for (const face of hole.reverse().faces) outer.addFace(face.shapes);
2855
+ return outer;
2856
+ };
2857
+ var getPourPolygon = (pour) => {
2858
+ if (pour.shape === "rect")
2859
+ return roundedRectangle(
2860
+ pour.center,
2861
+ pour.width,
2862
+ pour.height,
2863
+ 0,
2864
+ pour.rotation
2865
+ );
2866
+ if (pour.shape === "polygon")
2867
+ return ccw(new Polygon(pour.points.map((p) => new Point(p.x, p.y))));
2868
+ const polygon = ringPolygon(pour.brep_shape.outer_ring);
2869
+ for (const ring of pour.brep_shape.inner_rings)
2870
+ subtractHole(polygon, ringPolygon(ring));
2871
+ return polygon;
2872
+ };
2873
+ var getSmtPadPolygon = (pad) => {
2874
+ if (pad.shape === "polygon")
2875
+ return ccw(new Polygon(pad.points.map((p) => new Point(p.x, p.y))));
2876
+ if (pad.shape === "circle") return circlePolygon(pad, pad.radius);
2877
+ const radius = pad.shape === "pill" || pad.shape === "rotated_pill" ? pad.radius : pad.corner_radius ?? pad.rect_border_radius ?? 0;
2878
+ return roundedRectangle(
2879
+ pad,
2880
+ pad.width,
2881
+ pad.height,
2882
+ radius,
2883
+ "ccw_rotation" in pad ? pad.ccw_rotation : 0
2884
+ );
2885
+ };
2886
+ var getViaPolygon = (center, outerDiameter, holeDiameter) => subtractHole(
2887
+ circlePolygon(center, outerDiameter / 2),
2888
+ circlePolygon(center, holeDiameter / 2)
2889
+ );
2890
+ var getPlatedHolePolygon = (pad, componentRotation = 0) => {
2891
+ if (pad.shape === "circle")
2892
+ return getViaPolygon(pad, pad.outer_diameter, pad.hole_diameter);
2893
+ if ("outer_width" in pad) {
2894
+ return subtractHole(
2895
+ roundedRectangle(
2896
+ pad,
2897
+ pad.outer_width,
2898
+ pad.outer_height,
2899
+ Math.min(pad.outer_width, pad.outer_height) / 2,
2900
+ pad.ccw_rotation
2901
+ ),
2902
+ roundedRectangle(
2903
+ pad,
2904
+ pad.hole_width,
2905
+ pad.hole_height,
2906
+ Math.min(pad.hole_width, pad.hole_height) / 2,
2907
+ pad.ccw_rotation
2908
+ )
2909
+ );
2910
+ }
2911
+ const center = { x: pad.x + pad.hole_offset_x, y: pad.y + pad.hole_offset_y };
2912
+ const outer = pad.shape === "hole_with_polygon_pad" ? placePolygon(
2913
+ ccw(new Polygon(pad.pad_outline.map((p) => new Point(p.x, p.y)))),
2914
+ pad,
2915
+ pad.ccw_rotation ?? componentRotation
2916
+ ) : roundedRectangle(
2917
+ pad,
2918
+ pad.rect_pad_width,
2919
+ pad.rect_pad_height,
2920
+ pad.rect_border_radius,
2921
+ "rect_ccw_rotation" in pad ? pad.rect_ccw_rotation : 0
2922
+ );
2923
+ if (pad.shape === "circular_hole_with_rect_pad" || pad.shape === "hole_with_polygon_pad" && pad.hole_shape === "circle") {
2924
+ if (pad.hole_diameter === void 0)
2925
+ throw new Error("Circular plated hole requires hole_diameter");
2926
+ return subtractHole(outer, circlePolygon(center, pad.hole_diameter / 2));
2927
+ }
2928
+ if (!("hole_width" in pad) || pad.hole_width === void 0 || pad.hole_height === void 0)
2929
+ throw new Error("Slotted plated hole requires hole_width and hole_height");
2930
+ return subtractHole(
2931
+ outer,
2932
+ roundedRectangle(
2933
+ center,
2934
+ pad.hole_width,
2935
+ pad.hole_height,
2936
+ Math.min(pad.hole_width, pad.hole_height) / 2,
2937
+ "hole_ccw_rotation" in pad ? pad.hole_ccw_rotation : pad.shape === "hole_with_polygon_pad" ? pad.ccw_rotation ?? componentRotation : 0
2938
+ )
2939
+ );
2940
+ };
2941
+ var getTraceSegmentPolygon = (start, end, startWidth, endWidth = startWidth) => {
2942
+ const length = Math.hypot(end.x - start.x, end.y - start.y);
2943
+ const r1 = startWidth / 2;
2944
+ const r2 = endWidth / 2;
2945
+ if (length <= Math.abs(r2 - r1))
2946
+ return circlePolygon(r1 >= r2 ? start : end, Math.max(r1, r2));
2947
+ const angle = Math.atan2(end.y - start.y, end.x - start.x);
2948
+ const tangent = Math.acos((r1 - r2) / length);
2949
+ const first = new Arc(
2950
+ new Point(start.x, start.y),
2951
+ r1,
2952
+ angle + tangent,
2953
+ angle - tangent,
2954
+ true
2955
+ );
2956
+ const second = new Arc(
2957
+ new Point(end.x, end.y),
2958
+ r2,
2959
+ angle - tangent,
2960
+ angle + tangent,
2961
+ true
2962
+ );
2963
+ return ccw(
2964
+ new Polygon([
2965
+ first,
2966
+ new Segment(first.end, second.start),
2967
+ second,
2968
+ new Segment(second.end, first.start)
2969
+ ])
2970
+ );
2971
+ };
2972
+ var copperPolygonsTouch = (a, b, tolerance = 1e-7) => a.vertices.some((p) => b.contains(p)) || b.vertices.some((p) => a.contains(p)) || a.distanceTo(b)[0] <= tolerance;
2773
2973
  export {
2774
2974
  analyzePcbPin1Location,
2775
2975
  applySelector,
2776
2976
  applySelectorAST,
2777
2977
  buildSubtree,
2778
2978
  categorizeErrorOrWarning,
2979
+ circlePolygon,
2779
2980
  cju_default as cju,
2780
2981
  cju_indexed_default as cjuIndexed,
2781
2982
  computeClearanceBetweenElements,
2782
2983
  computeGapBetweenCopper,
2984
+ copperPolygonsTouch,
2783
2985
  directionToVec,
2784
2986
  distanceBetweenCircleAndCircle,
2785
2987
  distanceBetweenCircleAndPolygon,
@@ -2795,15 +2997,21 @@ export {
2795
2997
  getMinimumFlexContainer,
2796
2998
  getPcbElementBounds,
2797
2999
  getPcbElementsWithinBounds,
3000
+ getPlatedHolePolygon,
3001
+ getPourPolygon,
2798
3002
  getPrimaryId,
2799
3003
  getReadableNameForElement,
2800
3004
  getReadableNameForPcbPort,
2801
3005
  getReadableNameForPcbSmtpad,
2802
3006
  getReadableNameForPcbTrace,
2803
3007
  getSchematicElementBounds,
3008
+ getSmtPadPolygon,
2804
3009
  getStringFromCircuitJsonTree,
3010
+ getTraceSegmentPolygon,
3011
+ getViaPolygon,
2805
3012
  oppositeDirection,
2806
3013
  oppositeSide,
3014
+ placePolygon,
2807
3015
  repositionPcbComponentTo,
2808
3016
  repositionPcbGroupTo,
2809
3017
  repositionSchematicComponentTo,
@@ -2811,6 +3019,7 @@ export {
2811
3019
  rotateClockwise,
2812
3020
  rotateCounterClockwise,
2813
3021
  rotateDirection,
3022
+ roundedRectangle,
2814
3023
  su,
2815
3024
  transformInsertionDirection,
2816
3025
  transformPCBElement,