@tscircuit/core 0.0.1155 → 0.0.1157

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.
Files changed (2) hide show
  1. package/dist/index.js +88 -74
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2679,6 +2679,25 @@ function fillCircleWithRects(circle, options = {}) {
2679
2679
 
2680
2680
  // lib/utils/obstacles/getObstaclesFromCircuitJson.ts
2681
2681
  var EVERY_LAYER = ["top", "inner1", "inner2", "bottom"];
2682
+ var QUARTER_TURN_TOLERANCE_DEGREES = 0.01;
2683
+ var getAxisAlignedRectFromRotatedRect = (rotatedRect) => {
2684
+ const normalizedRotation = (rotatedRect.rotation % 360 + 360) % 360;
2685
+ const axisAlignedAngles = [0, 90, 180, 270];
2686
+ for (const angle of axisAlignedAngles) {
2687
+ const angularDistance = Math.min(
2688
+ Math.abs(normalizedRotation - angle),
2689
+ 360 - Math.abs(normalizedRotation - angle)
2690
+ );
2691
+ if (angularDistance > QUARTER_TURN_TOLERANCE_DEGREES) continue;
2692
+ const isVertical = angle === 90 || angle === 270;
2693
+ return {
2694
+ center: rotatedRect.center,
2695
+ width: isVertical ? rotatedRect.height : rotatedRect.width,
2696
+ height: isVertical ? rotatedRect.width : rotatedRect.height
2697
+ };
2698
+ }
2699
+ return null;
2700
+ };
2682
2701
  var getObstaclesFromCircuitJson = (soup, connMap) => {
2683
2702
  const withNetId = (idList) => connMap ? idList.concat(
2684
2703
  idList.map((id) => connMap?.getNetConnectedToId(id)).filter(Boolean)
@@ -2718,8 +2737,9 @@ var getObstaclesFromCircuitJson = (soup, connMap) => {
2718
2737
  height: element.height,
2719
2738
  rotation: element.ccw_rotation
2720
2739
  };
2721
- const approximatingRects = generateApproximatingRects(rotatedRect);
2722
- for (const rect of approximatingRects) {
2740
+ const singleRect = getAxisAlignedRectFromRotatedRect(rotatedRect);
2741
+ const rects = singleRect ? [singleRect] : generateApproximatingRects(rotatedRect);
2742
+ for (const rect of rects) {
2723
2743
  obstacles.push({
2724
2744
  type: "rect",
2725
2745
  layers: [element.layer],
@@ -19276,7 +19296,7 @@ import { identity as identity5 } from "transformation-matrix";
19276
19296
  var package_default = {
19277
19297
  name: "@tscircuit/core",
19278
19298
  type: "module",
19279
- version: "0.0.1154",
19299
+ version: "0.0.1156",
19280
19300
  types: "dist/index.d.ts",
19281
19301
  main: "dist/index.js",
19282
19302
  module: "dist/index.js",
@@ -23074,83 +23094,77 @@ import {
23074
23094
  } from "@tscircuit/props";
23075
23095
  import { unknown_error_finding_part as unknown_error_finding_part2 } from "circuit-json";
23076
23096
 
23077
- // lib/utils/connectors/rewriteToStandardUsbCPortHints.ts
23078
- var PIN_HINT_RE = /^(?:pin)?(\d+)$/i;
23079
- var USB_C_STANDARD_PORT_HINT_ALIASES = {
23080
- GND1: [],
23081
- VBUS1: [],
23082
- SBU2: [],
23083
- CC1: [],
23084
- DM2: ["DN2"],
23085
- DP1: [],
23086
- DM1: ["DN1"],
23087
- DP2: [],
23088
- SBU1: [],
23089
- CC2: [],
23090
- VBUS2: [],
23091
- GND2: [],
23092
- SHELL1: [],
23093
- SHELL2: [],
23094
- SHELL3: [],
23095
- SHELL4: []
23096
- };
23097
- var unique = (hints) => Array.from(new Set(hints));
23098
- var rewriteToStandardUsbCPortHints = (circuitJson) => {
23099
- const aliasToStandardHint = {};
23100
- for (const [standardHint, aliases] of Object.entries(
23101
- USB_C_STANDARD_PORT_HINT_ALIASES
23102
- )) {
23103
- aliasToStandardHint[standardHint] = standardHint;
23104
- for (const alias of aliases) {
23105
- aliasToStandardHint[alias] = standardHint;
23106
- }
23107
- }
23108
- const sourceAliasesByPin = /* @__PURE__ */ new Map();
23109
- for (const elm of circuitJson) {
23110
- if (elm && typeof elm === "object" && elm.type === "source_port" && typeof elm.pin_number === "number") {
23111
- const pin = elm.pin_number;
23112
- const hints = Array.isArray(elm.port_hints) ? elm.port_hints.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h.length > 0 && !PIN_HINT_RE.test(h)) : [];
23113
- if (hints.length > 0) sourceAliasesByPin.set(pin, unique(hints));
23114
- }
23115
- }
23116
- if (sourceAliasesByPin.size === 0) return circuitJson;
23117
- return circuitJson.map((elm) => {
23118
- if (!elm || typeof elm !== "object" || !("port_hints" in elm)) return elm;
23119
- if (!Array.isArray(elm.port_hints)) return elm;
23097
+ // lib/utils/connectors/convertCircuitJsonToUsbCStandardCircuitJson.ts
23098
+ var STANDARD_USB_C_PIN_LABELS = [
23099
+ { label: "GND1", aliases: [] },
23100
+ { label: "VBUS1", aliases: [] },
23101
+ { label: "CC1", aliases: [] },
23102
+ { label: "DP1", aliases: [] },
23103
+ { label: "DM1", aliases: ["DN1"] },
23104
+ { label: "SBU1", aliases: [] },
23105
+ { label: "SBU2", aliases: [] },
23106
+ { label: "DM2", aliases: ["DN2"] },
23107
+ { label: "DP2", aliases: [] },
23108
+ { label: "CC2", aliases: [] },
23109
+ { label: "VBUS2", aliases: [] },
23110
+ { label: "GND2", aliases: [] },
23111
+ { label: "SHELL1", aliases: ["MH1", "EH1", "MOUNT1"] },
23112
+ { label: "SHELL2", aliases: ["MH2", "EH2", "MOUNT2"] },
23113
+ { label: "SHELL3", aliases: ["MH3", "EH3", "MOUNT3"] },
23114
+ { label: "SHELL4", aliases: ["MH4", "EH4", "MOUNT4"] }
23115
+ ];
23116
+ var PIN_NUMBER_HINT_PATTERN = /^(?:pin)?(\d+)$/i;
23117
+ var dedupeHintsPreservingOrder = (hints) => Array.from(new Set(hints));
23118
+ var convertCircuitJsonToUsbCStandardCircuitJson = (partCircuitJson) => {
23119
+ const unassignedPorts = [];
23120
+ for (const elm of partCircuitJson) {
23121
+ if (elm.type !== "source_port") continue;
23122
+ const pinNumber = elm.pin_number;
23123
+ if (typeof pinNumber !== "number") continue;
23124
+ const upperCaseHints = /* @__PURE__ */ new Set();
23125
+ for (const hint of elm.port_hints ?? []) {
23126
+ upperCaseHints.add(hint.trim().toUpperCase());
23127
+ }
23128
+ unassignedPorts.push({ pinKey: `pin${pinNumber}`, upperCaseHints });
23129
+ }
23130
+ const canonicalHintsByPin = {};
23131
+ for (const { label, aliases } of STANDARD_USB_C_PIN_LABELS) {
23132
+ const canonicalAndAliasHintsUpper = [label, ...aliases].map(
23133
+ (s) => s.toUpperCase()
23134
+ );
23135
+ const matchIndex = unassignedPorts.findIndex(
23136
+ (port) => canonicalAndAliasHintsUpper.some((hint) => port.upperCaseHints.has(hint))
23137
+ );
23138
+ if (matchIndex === -1) continue;
23139
+ const { pinKey } = unassignedPorts[matchIndex];
23140
+ canonicalHintsByPin[pinKey] = [label];
23141
+ unassignedPorts.splice(matchIndex, 1);
23142
+ }
23143
+ if (Object.keys(canonicalHintsByPin).length === 0) return partCircuitJson;
23144
+ return partCircuitJson.map((elm) => {
23145
+ if (!("port_hints" in elm) || !Array.isArray(elm.port_hints)) return elm;
23120
23146
  const originalHints = elm.port_hints.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h.length > 0);
23121
23147
  if (originalHints.length === 0) return elm;
23122
- const pinNumbers = /* @__PURE__ */ new Set();
23148
+ const pinKeys = /* @__PURE__ */ new Set();
23123
23149
  if (elm.type === "source_port" && typeof elm.pin_number === "number") {
23124
- pinNumbers.add(elm.pin_number);
23150
+ pinKeys.add(`pin${elm.pin_number}`);
23125
23151
  }
23126
23152
  for (const hint of originalHints) {
23127
- const m = hint.match(PIN_HINT_RE);
23128
- if (m) pinNumbers.add(Number.parseInt(m[1], 10));
23129
- }
23130
- const sourceAliases = [];
23131
- for (const pin of pinNumbers) {
23132
- sourceAliases.push(...sourceAliasesByPin.get(pin) ?? []);
23133
- }
23134
- const standardHints = /* @__PURE__ */ new Set();
23135
- for (const hint of [...originalHints, ...sourceAliases]) {
23136
- const standard = aliasToStandardHint[hint.toUpperCase()];
23137
- if (standard) standardHints.add(standard);
23138
- }
23139
- const addedHints = [];
23140
- for (const standardHint of standardHints) {
23141
- addedHints.push(
23142
- standardHint,
23143
- ...USB_C_STANDARD_PORT_HINT_ALIASES[standardHint]
23144
- );
23153
+ const matchedPin = hint.match(PIN_NUMBER_HINT_PATTERN);
23154
+ if (!matchedPin) continue;
23155
+ pinKeys.add(`pin${Number.parseInt(matchedPin[1], 10)}`);
23145
23156
  }
23146
- const rewrittenPortHints = unique([
23147
- ...originalHints,
23148
- ...sourceAliases,
23149
- ...addedHints
23150
- ]);
23157
+ const canonicalHintsToAdd = [];
23158
+ for (const pinKey of pinKeys) {
23159
+ canonicalHintsToAdd.push(...canonicalHintsByPin[pinKey] ?? []);
23160
+ }
23161
+ if (canonicalHintsToAdd.length === 0) return elm;
23151
23162
  return {
23152
23163
  ...elm,
23153
- port_hints: rewrittenPortHints
23164
+ port_hints: dedupeHintsPreservingOrder([
23165
+ ...originalHints,
23166
+ ...canonicalHintsToAdd
23167
+ ])
23154
23168
  };
23155
23169
  });
23156
23170
  };
@@ -23296,8 +23310,8 @@ var Connector = class extends Chip {
23296
23310
  });
23297
23311
  }
23298
23312
  _addConnectorFootprintFromCircuitJson(standard, circuitJson) {
23299
- const rewrittenCircuitJson = standard === "usb_c" ? rewriteToStandardUsbCPortHints(circuitJson) : circuitJson;
23300
23313
  const props = this._getConnectorProps();
23314
+ const standardizedCircuitJson = standard === "usb_c" ? convertCircuitJsonToUsbCStandardCircuitJson(circuitJson) : circuitJson;
23301
23315
  const fpComponents = createComponentsFromCircuitJson(
23302
23316
  {
23303
23317
  componentName: this.name,
@@ -23306,7 +23320,7 @@ var Connector = class extends Chip {
23306
23320
  pinLabels: props.pinLabels,
23307
23321
  pcbPinLabels: props.pcbPinLabels
23308
23322
  },
23309
- rewrittenCircuitJson
23323
+ standardizedCircuitJson
23310
23324
  );
23311
23325
  this.addAll(fpComponents);
23312
23326
  this._markDirty("InitializePortsFromChildren");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tscircuit/core",
3
3
  "type": "module",
4
- "version": "0.0.1155",
4
+ "version": "0.0.1157",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",