@tscircuit/circuit-json-util 0.0.102 → 0.0.104

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.js CHANGED
@@ -884,9 +884,9 @@ var insertionDirectionToVec = (direction) => {
884
884
  return { x: -1, y: 0 };
885
885
  case "from_right":
886
886
  return { x: 1, y: 0 };
887
- case "from_front":
887
+ case "from_top":
888
888
  return { x: 0, y: 1 };
889
- case "from_back":
889
+ case "from_bottom":
890
890
  return { x: 0, y: -1 };
891
891
  }
892
892
  };
@@ -896,11 +896,15 @@ var vecToInsertionDirection = ({
896
896
  }) => {
897
897
  if (x > 0) return "from_right";
898
898
  if (x < 0) return "from_left";
899
- if (y > 0) return "from_front";
900
- return "from_back";
899
+ if (y > 0) return "from_top";
900
+ return "from_bottom";
901
901
  };
902
902
  var transformInsertionDirection = (direction, opts) => {
903
- if (!direction || direction === "from_above") return direction;
903
+ if (!direction) return direction;
904
+ if (direction === "from_above" || direction === "from_below") {
905
+ if (!opts.isFlipped) return direction;
906
+ return direction === "from_above" ? "from_below" : "from_above";
907
+ }
904
908
  let { x, y } = insertionDirectionToVec(direction);
905
909
  let quarterTurns = Math.round(opts.rotationDegrees / 90);
906
910
  while (quarterTurns > 0) {
@@ -1040,7 +1044,19 @@ var transformPCBElement = (elm, matrix) => {
1040
1044
  p.y = tp.y;
1041
1045
  return p;
1042
1046
  });
1043
- } else if (elm.type === "pcb_silkscreen_path" || elm.type === "pcb_trace" || elm.type === "pcb_trace_hint" || elm.type === "pcb_fabrication_note_path" || elm.type === "pcb_note_path") {
1047
+ } else if (elm.type === "pcb_trace") {
1048
+ elm.route = elm.route.map((rp) => {
1049
+ if (!("x" in rp)) {
1050
+ rp.start = applyToPoint(matrix, rp.start);
1051
+ rp.end = applyToPoint(matrix, rp.end);
1052
+ return rp;
1053
+ }
1054
+ const tp = applyToPoint(matrix, rp);
1055
+ rp.x = tp.x;
1056
+ rp.y = tp.y;
1057
+ return rp;
1058
+ });
1059
+ } else if (elm.type === "pcb_silkscreen_path" || elm.type === "pcb_trace_hint" || elm.type === "pcb_fabrication_note_path" || elm.type === "pcb_note_path") {
1044
1060
  elm.route = elm.route.map((rp) => {
1045
1061
  const tp = applyToPoint(matrix, rp);
1046
1062
  rp.x = tp.x;
@@ -1442,10 +1458,13 @@ var getBoundsOfPcbElements = (elements) => {
1442
1458
  }
1443
1459
  } else if (elm.type === "pcb_trace") {
1444
1460
  for (const point of elm.route) {
1445
- minX = Math.min(minX, point.x);
1446
- minY = Math.min(minY, point.y);
1447
- maxX = Math.max(maxX, point.x);
1448
- maxY = Math.max(maxY, point.y);
1461
+ const positions = "x" in point ? [point] : [point.start, point.end];
1462
+ for (const { x, y } of positions) {
1463
+ minX = Math.min(minX, x);
1464
+ minY = Math.min(minY, y);
1465
+ maxX = Math.max(maxX, x);
1466
+ maxY = Math.max(maxY, y);
1467
+ }
1449
1468
  }
1450
1469
  } else if (elm.type === "pcb_courtyard_outline") {
1451
1470
  for (const point of elm.outline) {
@@ -2567,6 +2586,95 @@ var computeClearanceBetweenElements = (elm1, elm2) => {
2567
2586
  return minimumGap;
2568
2587
  };
2569
2588
 
2589
+ // lib/analyze-pcb-pin1-location.ts
2590
+ var PIN1_LOCATION_PARTS = {
2591
+ leftside_top: { side: "leftside", alignment: "top" },
2592
+ leftside_bottom: { side: "leftside", alignment: "bottom" },
2593
+ rightside_top: { side: "rightside", alignment: "top" },
2594
+ rightside_bottom: { side: "rightside", alignment: "bottom" },
2595
+ topside_left: { side: "topside", alignment: "left" },
2596
+ topside_right: { side: "topside", alignment: "right" },
2597
+ bottomside_left: { side: "bottomside", alignment: "left" },
2598
+ bottomside_right: { side: "bottomside", alignment: "right" }
2599
+ };
2600
+ var PIN1_LOCATIONS = Object.keys(PIN1_LOCATION_PARTS);
2601
+ var getPadCenter = (pad) => {
2602
+ if (typeof pad.x === "number" && typeof pad.y === "number") {
2603
+ return { x: pad.x, y: pad.y };
2604
+ }
2605
+ if (pad.points && pad.points.length > 0) {
2606
+ const xs = pad.points.map((point) => point.x);
2607
+ const ys = pad.points.map((point) => point.y);
2608
+ return {
2609
+ x: (Math.min(...xs) + Math.max(...xs)) / 2,
2610
+ y: (Math.min(...ys) + Math.max(...ys)) / 2
2611
+ };
2612
+ }
2613
+ return null;
2614
+ };
2615
+ var getPinNumber = (pad) => {
2616
+ for (const hint of pad.port_hints ?? []) {
2617
+ const match = String(hint).trim().match(/^(?:pin)?(\d+)$/i);
2618
+ if (match) return Number.parseInt(match[1], 10);
2619
+ }
2620
+ return null;
2621
+ };
2622
+ var pinMatchesLocation = (padCenters, pin1Center, pin1Location) => {
2623
+ const xs = padCenters.map((point) => point.x);
2624
+ const ys = padCenters.map((point) => point.y);
2625
+ const minX = Math.min(...xs);
2626
+ const maxX = Math.max(...xs);
2627
+ const minY = Math.min(...ys);
2628
+ const maxY = Math.max(...ys);
2629
+ const centerX = (minX + maxX) / 2;
2630
+ const centerY = (minY + maxY) / 2;
2631
+ const spanX = maxX - minX;
2632
+ const spanY = maxY - minY;
2633
+ const tolerance = Math.max(spanX, spanY, 1) * 1e-6;
2634
+ const { side, alignment } = PIN1_LOCATION_PARTS[pin1Location];
2635
+ const onRequestedSide = side === "leftside" && Math.abs(pin1Center.x - minX) <= tolerance || side === "rightside" && Math.abs(pin1Center.x - maxX) <= tolerance || side === "topside" && Math.abs(pin1Center.y - maxY) <= tolerance || side === "bottomside" && Math.abs(pin1Center.y - minY) <= tolerance;
2636
+ const atRequestedAlignment = alignment === "left" && (spanX <= tolerance || pin1Center.x < centerX - tolerance) || alignment === "right" && (spanX <= tolerance || pin1Center.x > centerX + tolerance) || alignment === "top" && (spanY <= tolerance || pin1Center.y > centerY + tolerance) || alignment === "bottom" && (spanY <= tolerance || pin1Center.y < centerY - tolerance);
2637
+ return onRequestedSide && atRequestedAlignment;
2638
+ };
2639
+ var analyzePcbPin1Location = (elements) => {
2640
+ const pads = elements.filter(
2641
+ (element) => element.type === "pcb_smtpad" || element.type === "pcb_plated_hole"
2642
+ );
2643
+ const numberedPads = pads.map((pad) => ({
2644
+ center: getPadCenter(pad),
2645
+ pinNumber: getPinNumber(pad)
2646
+ }));
2647
+ const pin1Center = numberedPads.find((pad) => pad.pinNumber === 1)?.center;
2648
+ const padCenters = numberedPads.map((pad) => pad.center).filter((point) => point !== null);
2649
+ if (!pin1Center || padCenters.length === 0) return null;
2650
+ const candidates = PIN1_LOCATIONS.filter(
2651
+ (pin1Location) => pinMatchesLocation(padCenters, pin1Center, pin1Location)
2652
+ );
2653
+ if (candidates.length === 1) return candidates[0];
2654
+ if (candidates.length === 0) return null;
2655
+ let nextNumberedPad;
2656
+ for (const pad of numberedPads) {
2657
+ if (!pad.center || pad.pinNumber === null || pad.pinNumber <= 1) continue;
2658
+ if (!nextNumberedPad || pad.pinNumber < nextNumberedPad.pinNumber) {
2659
+ nextNumberedPad = { center: pad.center, pinNumber: pad.pinNumber };
2660
+ }
2661
+ }
2662
+ if (!nextNumberedPad) return null;
2663
+ const xs = padCenters.map((point) => point.x);
2664
+ const ys = padCenters.map((point) => point.y);
2665
+ const span = Math.max(
2666
+ Math.max(...xs) - Math.min(...xs),
2667
+ Math.max(...ys) - Math.min(...ys),
2668
+ 1
2669
+ );
2670
+ const tolerance = span * 1e-6;
2671
+ const topologyCandidates = candidates.filter((pin1Location) => {
2672
+ const { side } = PIN1_LOCATION_PARTS[pin1Location];
2673
+ return side === "leftside" || side === "rightside" ? Math.abs(nextNumberedPad.center.x - pin1Center.x) <= tolerance : Math.abs(nextNumberedPad.center.y - pin1Center.y) <= tolerance;
2674
+ });
2675
+ return topologyCandidates.length === 1 ? topologyCandidates[0] : null;
2676
+ };
2677
+
2570
2678
  // lib/categorize-error-or-warning.ts
2571
2679
  var NETLIST_TYPES = /* @__PURE__ */ new Set(["source_pin_must_be_connected_error"]);
2572
2680
  var PIN_SPECIFICATION_TYPES = /* @__PURE__ */ new Set([
@@ -2597,6 +2705,7 @@ var categorizeErrorOrWarning = (value) => {
2597
2705
  return "unknown";
2598
2706
  };
2599
2707
  export {
2708
+ analyzePcbPin1Location,
2600
2709
  applySelector,
2601
2710
  applySelectorAST,
2602
2711
  buildSubtree,