npm.io
0.0.3 • Published 14h ago

@tscircuit/infer-cable-insertion-point

Licence
MIT
Version
0.0.3
Deps
0
Size
199 kB
Vulns
0
Weekly
0

@tscircuit/infer-cable-insertion-point

Infer a likely cable insertion center/direction for USB connector footprints from CircuitJson PCB geometry.

Install

bun add @tscircuit/infer-cable-insertion-point

Usage

import { Circuit } from "tscircuit"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"
import {
  addCableInsertNoteRect,
  guessCableInsertCenter,
} from "@tscircuit/infer-cable-insertion-point"

const circuit = new Circuit()
// circuit.add(<ImportedUsbConnector name="U1" />)

await circuit.renderUntilSettled()
const circuitJson = await circuit.getCircuitJson()

const guess = guessCableInsertCenter(circuitJson)
console.log(guess)
// { x, y, side, pcbComponentId }

const { circuitJsonWithNote } = addCableInsertNoteRect(circuitJson)
const pcbSvg = convertCircuitJsonToPcbSvg(circuitJsonWithNote)

addCableInsertNoteRect appends a pcb_note_rect at the inferred insertion point, with dashed stroke and label (cable-insert:<side>), so it is easy to inspect visually in rendered PCB SVGs.

How it works

  • Finds the primary pcb_component in Circuit JSON.
  • Computes footprint bounds using pads, holes, and silkscreen geometry.
  • Compares pad envelope to overall footprint envelope.
  • Chooses the side with the largest non-pad margin (top, bottom, left, or right).
  • Places the insertion center slightly outside that side.

Visual fixtures

The screenshots below come from the generated test fixtures in tests/usb-connectors/__snapshots__. In each image, the green dashed rectangle is the inferred cable insertion center.


TYPE_C_31_M_12

TYPE_C_31_M_17

TYPE_C_31_M_31

TYPE_C_24P_QT_143

TYPE_C_24P_QCHT_143

TYPE_C_24P_GTJB_040

TYPEC_304_ACP16

TYPEC_304A_ACP16G

TYPEC_304J_BCP16

TYPEC_304YSSW_ACP16

TYPE_C_16PIN_2MD_073_

TYPE_C_16PIN_L8_35

TYPE_C_16P_LTH6_8

TYPE_C_16P_QTWT

HX_TYPE_C_16PIN

KH_TYPE_C_16P

USB_TYPE_C_018

USB_TYPE_C_019

USB_TYPE_C_020

USB_TYPE_C_009

U262_061N_4BVC10

U262_061N_4BVC11

U262_161N_4BVC11

U262_241N_4BV64

For the complete fixture set, see all generated snapshots under tests/usb-connectors/__snapshots__.