@tscircuit/checks 0.0.204 → 0.0.206

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/dist/index.d.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  import * as circuit_json from 'circuit-json';
2
2
  import { AnyCircuitElement, PcbPlacementError, PcbPortNotConnectedError, PcbTraceError, PcbKeepoutOverlapWarning, PcbComponentOutsideBoardError, PcbViaClearanceError, PcbTraceWarning, PcbTraceMissingError, PcbFootprintOverlapError, PcbPadPadClearanceError, PcbCourtyardOverlapError, PcbComponentMissingCourtyardWarning, PcbTraceTooLongError, PcbPadTraceClearanceError, PcbViaTraceClearanceError, SourceComponentMisconfiguredError, SourceComponentPinsUnderspecifiedWarning, SourceNoPowerPinDefinedWarning, SourceNoGroundPinDefinedWarning, SchematicComponentStylingWarning, PcbConnectorNotInAccessibleOrientationWarning, SourceConfusingNetNameWarning, PcbBusLengthSkewError } from 'circuit-json';
3
- import { ConnectivityMap } from 'circuit-json-to-connectivity-map';
3
+ import { ConnectivityMap, PcbConnectivityMap } from 'circuit-json-to-connectivity-map';
4
4
 
5
5
  /** Detect accidental copper contact, rather than enforcing a clearance margin. */
6
- declare function checkCopperPourShorts(circuitJson: AnyCircuitElement[]): PcbPlacementError[];
6
+ declare function checkCopperPourShorts(circuitJson: AnyCircuitElement[], { connMap }?: {
7
+ connMap?: ConnectivityMap;
8
+ }): PcbPlacementError[];
7
9
 
8
- declare function checkEachPcbPortConnectedToPcbTraces(circuitJson: AnyCircuitElement[]): PcbPortNotConnectedError[];
10
+ declare function checkEachPcbPortConnectedToPcbTraces(circuitJson: AnyCircuitElement[], { connMap, pcbConnectivityMap, }?: {
11
+ connMap?: ConnectivityMap;
12
+ pcbConnectivityMap?: PcbConnectivityMap;
13
+ }): PcbPortNotConnectedError[];
9
14
 
10
15
  declare function checkEachPcbTraceNonOverlapping(circuitJson: AnyCircuitElement[], { connMap, minClearance, }?: {
11
16
  connMap?: ConnectivityMap;
@@ -63,9 +68,14 @@ declare function checkSourceTracesMatchPcbTraceThickness(circuitJson: AnyCircuit
63
68
  * pcb_trace associated with it, or its ports physically joined through a
64
69
  * same-net copper pour. Otherwise return an error for that source_trace.
65
70
  */
66
- declare function checkSourceTracesHavePcbTraces(circuitJson: AnyCircuitElement[]): PcbTraceMissingError[];
71
+ declare function checkSourceTracesHavePcbTraces(circuitJson: AnyCircuitElement[], { connMap }?: {
72
+ connMap?: ConnectivityMap;
73
+ }): PcbTraceMissingError[];
67
74
 
68
- declare function checkTracesAreContiguous(circuitJson: AnyCircuitElement[]): PcbTraceError[];
75
+ declare function checkTracesAreContiguous(circuitJson: AnyCircuitElement[], { connMap, pcbConnectivityMap, }?: {
76
+ connMap?: ConnectivityMap;
77
+ pcbConnectivityMap?: PcbConnectivityMap;
78
+ }): PcbTraceError[];
69
79
 
70
80
  /**
71
81
  * Configuration for trace board boundary checking
package/dist/index.js CHANGED
@@ -635,14 +635,16 @@ function convertCircuitJsonToFlattenJs(json, options = {}) {
635
635
  }
636
636
 
637
637
  // lib/check-copper-pour-shorts.ts
638
- import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map";
638
+ import {
639
+ getFullConnectivityMapFromCircuitJson
640
+ } from "circuit-json-to-connectivity-map";
639
641
  function touchesPour(pour, geometry) {
640
642
  if (!pour.box.intersect(geometry.box)) return false;
641
643
  return pour.intersect(geometry).length > 0 || [...geometry.faces].some((face) => pour.contains(face.first.start)) || [...pour.faces].some((face) => geometry.contains(face.first.start));
642
644
  }
643
- function checkCopperPourShorts(circuitJson) {
645
+ function checkCopperPourShorts(circuitJson, { connMap } = {}) {
644
646
  if (!circuitJson.some((e) => e.type === "pcb_copper_pour")) return [];
645
- const connMap = getFullConnectivityMapFromCircuitJson(circuitJson);
647
+ connMap ??= getFullConnectivityMapFromCircuitJson(circuitJson);
646
648
  const { elements: copper } = convertCircuitJsonToFlattenJs(circuitJson, {
647
649
  elementTypes: [
648
650
  "pcb_smtpad",
@@ -706,6 +708,97 @@ function checkCopperPourShorts(circuitJson) {
706
708
  return [...errors.values()];
707
709
  }
708
710
 
711
+ // lib/util/create-indexed-pcb-connectivity-map.ts
712
+ import {
713
+ ConnectivityMap,
714
+ findConnectedNetworks,
715
+ PcbConnectivityMap
716
+ } from "circuit-json-to-connectivity-map";
717
+ import Flatbush2 from "flatbush";
718
+ function createIndexedPcbConnectivityMap(circuitJson) {
719
+ const map = new PcbConnectivityMap();
720
+ map.circuitJson = circuitJson;
721
+ for (const element of circuitJson) {
722
+ if (element.type === "pcb_trace") {
723
+ map.traceIdToElm.set(element.pcb_trace_id, element);
724
+ } else if (element.type === "pcb_port") {
725
+ map.portIdToElm.set(element.pcb_port_id, element);
726
+ }
727
+ }
728
+ const traces = [...map.traceIdToElm.values()];
729
+ const boundsByLayer = /* @__PURE__ */ new Map();
730
+ for (const [traceIndex, trace] of traces.entries()) {
731
+ const boundsForTrace = /* @__PURE__ */ new Map();
732
+ for (let i = 0; i < trace.route.length - 1; i++) {
733
+ const a = trace.route[i];
734
+ const b = trace.route[i + 1];
735
+ if (a.route_type !== "wire" || b.route_type !== "wire" || a.layer !== b.layer)
736
+ continue;
737
+ const bounds = boundsForTrace.get(a.layer) ?? {
738
+ traceIndex,
739
+ minX: Infinity,
740
+ minY: Infinity,
741
+ maxX: -Infinity,
742
+ maxY: -Infinity
743
+ };
744
+ const radius = a.width / 2 + 1e-9;
745
+ bounds.minX = Math.min(bounds.minX, a.x - radius, b.x - radius);
746
+ bounds.minY = Math.min(bounds.minY, a.y - radius, b.y - radius);
747
+ bounds.maxX = Math.max(bounds.maxX, a.x + radius, b.x + radius);
748
+ bounds.maxY = Math.max(bounds.maxY, a.y + radius, b.y + radius);
749
+ boundsForTrace.set(a.layer, bounds);
750
+ }
751
+ for (const [layer, bounds] of boundsForTrace) {
752
+ const entries = boundsByLayer.get(layer) ?? [];
753
+ entries.push(bounds);
754
+ boundsByLayer.set(layer, entries);
755
+ }
756
+ }
757
+ const candidates = traces.map(() => /* @__PURE__ */ new Set());
758
+ for (const bounds of boundsByLayer.values()) {
759
+ const index = new Flatbush2(bounds.length);
760
+ for (const box of bounds) index.add(box.minX, box.minY, box.maxX, box.maxY);
761
+ index.finish();
762
+ for (const box of bounds) {
763
+ for (const hit of index.search(box.minX, box.minY, box.maxX, box.maxY)) {
764
+ const other = bounds[hit].traceIndex;
765
+ if (other > box.traceIndex) candidates[box.traceIndex].add(other);
766
+ }
767
+ }
768
+ }
769
+ const connections = [];
770
+ for (const [i, neighbors] of candidates.entries()) {
771
+ for (const j of [...neighbors].sort((a, b) => a - b)) {
772
+ if (map._arePcbTracesConnected(traces[i], traces[j])) {
773
+ connections.push([traces[i].pcb_trace_id, traces[j].pcb_trace_id]);
774
+ }
775
+ }
776
+ }
777
+ const connectionsByPort = /* @__PURE__ */ new Map();
778
+ for (const trace of traces) {
779
+ for (const point2 of trace.route) {
780
+ if (point2.route_type !== "wire") continue;
781
+ for (const portId of /* @__PURE__ */ new Set([
782
+ point2.start_pcb_port_id,
783
+ point2.end_pcb_port_id
784
+ ])) {
785
+ if (!portId || !map.portIdToElm.has(portId)) continue;
786
+ const entries = connectionsByPort.get(portId) ?? [];
787
+ entries.push(
788
+ point2.start_pcb_port_id === portId ? [portId, trace.pcb_trace_id] : [trace.pcb_trace_id, portId]
789
+ );
790
+ connectionsByPort.set(portId, entries);
791
+ }
792
+ }
793
+ }
794
+ for (const portId of map.portIdToElm.keys()) {
795
+ for (const connection of connectionsByPort.get(portId) ?? [])
796
+ connections.push(connection);
797
+ }
798
+ map.connMap = new ConnectivityMap(findConnectedNetworks(connections));
799
+ return map;
800
+ }
801
+
709
802
  // lib/check-traces-are-contiguous/is-point-in-pad.ts
710
803
  import { pointToSegmentDistance } from "@tscircuit/math-utils";
711
804
 
@@ -1041,8 +1134,7 @@ var addStartAndEndPortIdsIfMissing = (soup) => {
1041
1134
 
1042
1135
  // lib/check-each-pcb-port-connected-to-pcb-trace.ts
1043
1136
  import {
1044
- getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson2,
1045
- PcbConnectivityMap
1137
+ getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson2
1046
1138
  } from "circuit-json-to-connectivity-map";
1047
1139
 
1048
1140
  // lib/util/get-readable-names.ts
@@ -1339,7 +1431,10 @@ function getCopperPourConnectivity(circuitJson, connectivity) {
1339
1431
  }
1340
1432
 
1341
1433
  // lib/check-each-pcb-port-connected-to-pcb-trace.ts
1342
- function checkEachPcbPortConnectedToPcbTraces(circuitJson) {
1434
+ function checkEachPcbPortConnectedToPcbTraces(circuitJson, {
1435
+ connMap,
1436
+ pcbConnectivityMap
1437
+ } = {}) {
1343
1438
  addStartAndEndPortIdsIfMissing(circuitJson);
1344
1439
  const sourceTraces = circuitJson.filter(
1345
1440
  (item) => item.type === "source_trace"
@@ -1351,8 +1446,8 @@ function checkEachPcbPortConnectedToPcbTraces(circuitJson) {
1351
1446
  (item) => item.type === "source_net"
1352
1447
  );
1353
1448
  const errors = [];
1354
- const connectivityMap = getFullConnectivityMapFromCircuitJson2(circuitJson);
1355
- const pcbConnectivityMap = new PcbConnectivityMap(circuitJson);
1449
+ const connectivityMap = connMap ?? getFullConnectivityMapFromCircuitJson2(circuitJson);
1450
+ pcbConnectivityMap ??= createIndexedPcbConnectivityMap(circuitJson);
1356
1451
  let pourConnectivity;
1357
1452
  const getPourConnectivity = () => pourConnectivity ??= getCopperPourConnectivity(
1358
1453
  circuitJson,
@@ -1596,7 +1691,35 @@ var getPolygonShape = (pad) => {
1596
1691
  ]
1597
1692
  };
1598
1693
  };
1694
+ var getRoundedPadCore = (pad) => {
1695
+ if (pad.type !== "pcb_smtpad" || pad.shape !== "rect" && pad.shape !== "rotated_rect" || !pad.corner_radius || pad.corner_radius <= 0)
1696
+ return null;
1697
+ const radius = Math.min(pad.corner_radius, pad.width / 2, pad.height / 2);
1698
+ if (radius === Math.min(pad.width, pad.height) / 2) {
1699
+ const core = {
1700
+ ...pad,
1701
+ shape: "rotated_pill",
1702
+ radius,
1703
+ ccw_rotation: pad.shape === "rotated_rect" ? pad.ccw_rotation : 0
1704
+ };
1705
+ return { core, radius: 0 };
1706
+ }
1707
+ return {
1708
+ core: {
1709
+ ...pad,
1710
+ width: pad.width - 2 * radius,
1711
+ height: pad.height - 2 * radius,
1712
+ corner_radius: 0
1713
+ },
1714
+ radius
1715
+ };
1716
+ };
1599
1717
  var getPadToPadGap = (padA, padB) => {
1718
+ const roundedA = getRoundedPadCore(padA);
1719
+ const roundedB = getRoundedPadCore(padB);
1720
+ if (roundedA || roundedB) {
1721
+ return getPadToPadGap(roundedA?.core ?? padA, roundedB?.core ?? padB) - (roundedA?.radius ?? 0) - (roundedB?.radius ?? 0);
1722
+ }
1600
1723
  if (isPillPad(padA) && isPillPad(padB)) {
1601
1724
  const pillA = getPillCenterLineForPad(padA);
1602
1725
  const pillB = getPillCenterLineForPad(padB);
@@ -1715,6 +1838,9 @@ var getCenterBetweenCopperEdges = ({
1715
1838
  return midpoint(traceEdge, obstacleEdge);
1716
1839
  };
1717
1840
  var getTraceObstacleClearance = (segment, obstacle) => {
1841
+ const rounded = getRoundedPadCore(obstacle);
1842
+ if (rounded) obstacle = rounded.core;
1843
+ const obstacleRadius = rounded?.radius ?? 0;
1718
1844
  const start = { x: segment.x1, y: segment.y1 };
1719
1845
  const end = { x: segment.x2, y: segment.y2 };
1720
1846
  const traceRadius = segment.thickness / 2;
@@ -1753,12 +1879,12 @@ var getTraceObstacleClearance = (segment, obstacle) => {
1753
1879
  getPolygonShape(obstacle).points
1754
1880
  );
1755
1881
  return {
1756
- gap: clearance.distance - traceRadius,
1882
+ gap: clearance.distance - traceRadius - obstacleRadius,
1757
1883
  center: getCenterBetweenCopperEdges({
1758
1884
  tracePoint: clearance.tracePoint,
1759
1885
  obstaclePoint: clearance.obstaclePoint,
1760
1886
  traceRadius,
1761
- obstacleRadius: 0
1887
+ obstacleRadius
1762
1888
  })
1763
1889
  };
1764
1890
  };
@@ -3248,8 +3374,10 @@ function checkSourceTracesMatchPcbTraceThickness(circuitJson) {
3248
3374
  }
3249
3375
 
3250
3376
  // lib/check-source-traces-have-pcb-traces.ts
3251
- import { getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson7 } from "circuit-json-to-connectivity-map";
3252
- function checkSourceTracesHavePcbTraces(circuitJson) {
3377
+ import {
3378
+ getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson7
3379
+ } from "circuit-json-to-connectivity-map";
3380
+ function checkSourceTracesHavePcbTraces(circuitJson, { connMap } = {}) {
3253
3381
  const errors = [];
3254
3382
  const sourceTraces = circuitJson.filter(
3255
3383
  (el) => el.type === "source_trace"
@@ -3263,7 +3391,7 @@ function checkSourceTracesHavePcbTraces(circuitJson) {
3263
3391
  const sourcePortToPcbPort = new Map(
3264
3392
  pcbPorts.map((pcbPort) => [pcbPort.source_port_id, pcbPort])
3265
3393
  );
3266
- const connectivityMap = getFullConnectivityMapFromCircuitJson7(circuitJson);
3394
+ const connectivityMap = connMap ?? getFullConnectivityMapFromCircuitJson7(circuitJson);
3267
3395
  let pourConnectivity;
3268
3396
  const getPourConnectivity = () => pourConnectivity ??= getCopperPourConnectivity(
3269
3397
  circuitJson,
@@ -3311,8 +3439,7 @@ import {
3311
3439
  getReadableNameForPcbTrace
3312
3440
  } from "@tscircuit/circuit-json-util";
3313
3441
  import {
3314
- getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson8,
3315
- PcbConnectivityMap as PcbConnectivityMap2
3442
+ getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson8
3316
3443
  } from "circuit-json-to-connectivity-map";
3317
3444
 
3318
3445
  // lib/check-traces-are-contiguous/via-contact-index.ts
@@ -3576,7 +3703,10 @@ function getMissingConnectionErrorCenter({
3576
3703
  y: (firstPointCenter.y + lastPointCenter.y) / 2
3577
3704
  };
3578
3705
  }
3579
- function checkTracesAreContiguous(circuitJson) {
3706
+ function checkTracesAreContiguous(circuitJson, {
3707
+ connMap,
3708
+ pcbConnectivityMap
3709
+ } = {}) {
3580
3710
  const errors = [];
3581
3711
  const pcbPorts = circuitJson.filter(
3582
3712
  (el) => el.type === "pcb_port"
@@ -3594,8 +3724,8 @@ function checkTracesAreContiguous(circuitJson) {
3594
3724
  (el) => el.type === "pcb_plated_hole"
3595
3725
  );
3596
3726
  const padMap = /* @__PURE__ */ new Map();
3597
- const pcbConnectivityMap = new PcbConnectivityMap2(circuitJson);
3598
- let fullConnectivityMap;
3727
+ pcbConnectivityMap ??= createIndexedPcbConnectivityMap(circuitJson);
3728
+ let fullConnectivityMap = connMap;
3599
3729
  let traceWireSegmentsByNetAndLayer;
3600
3730
  const getFullConnectivityMap = () => {
3601
3731
  fullConnectivityMap ??= getFullConnectivityMapFromCircuitJson8(circuitJson);
@@ -5175,6 +5305,9 @@ function checkSchematicComponentPortsOutsideBody(circuitJson) {
5175
5305
  return warnings;
5176
5306
  }
5177
5307
 
5308
+ // lib/run-all-checks.ts
5309
+ import { getFullConnectivityMapFromCircuitJson as getFullConnectivityMapFromCircuitJson15 } from "circuit-json-to-connectivity-map";
5310
+
5178
5311
  // lib/check-pcb-bus-length-skew.ts
5179
5312
  var checkPcbBusLengthSkew = (circuitJson) => {
5180
5313
  const buses = circuitJson.filter(
@@ -5700,20 +5833,25 @@ async function runAllPinSpecificationChecks(circuitJson) {
5700
5833
  ];
5701
5834
  }
5702
5835
  async function runAllRoutingChecks(circuitJson) {
5836
+ addStartAndEndPortIdsIfMissing(circuitJson);
5837
+ const connectivity = {
5838
+ connMap: getFullConnectivityMapFromCircuitJson15(circuitJson),
5839
+ pcbConnectivityMap: createIndexedPcbConnectivityMap(circuitJson)
5840
+ };
5703
5841
  return [
5704
- ...checkEachPcbPortConnectedToPcbTraces(circuitJson),
5705
- ...checkSourceTracesHavePcbTraces(circuitJson),
5842
+ ...checkEachPcbPortConnectedToPcbTraces(circuitJson, connectivity),
5843
+ ...checkSourceTracesHavePcbTraces(circuitJson, connectivity),
5706
5844
  ...checkPcbTraceLengths(circuitJson),
5707
5845
  ...checkPcbBusLengthSkew(circuitJson),
5708
5846
  ...checkPcbTraceViaCounts(circuitJson),
5709
- ...checkEachPcbTraceNonOverlapping(circuitJson),
5710
- ...checkCopperPourShorts(circuitJson),
5711
- ...checkPadTraceClearance(circuitJson),
5712
- ...checkViaTraceClearance(circuitJson),
5713
- ...checkViaPadClearance(circuitJson),
5714
- ...checkSameNetViaSpacing(circuitJson),
5715
- ...checkDifferentNetViaSpacing(circuitJson),
5716
- ...checkTracesAreContiguous(circuitJson),
5847
+ ...checkEachPcbTraceNonOverlapping(circuitJson, connectivity),
5848
+ ...checkCopperPourShorts(circuitJson, connectivity),
5849
+ ...checkPadTraceClearance(circuitJson, connectivity),
5850
+ ...checkViaTraceClearance(circuitJson, connectivity),
5851
+ ...checkViaPadClearance(circuitJson, connectivity),
5852
+ ...checkSameNetViaSpacing(circuitJson, connectivity),
5853
+ ...checkDifferentNetViaSpacing(circuitJson, connectivity),
5854
+ ...checkTracesAreContiguous(circuitJson, connectivity),
5717
5855
  ...checkPcbTracesOutOfBoard(circuitJson)
5718
5856
  ];
5719
5857
  }