@tscircuit/copper-pour-solver 0.0.33 → 0.0.34

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
@@ -1,37 +1,156 @@
1
1
  # @tscircuit/copper-pour-solver
2
2
 
3
- Solves for copper pour polygons
3
+ Solves PCB copper pour regions from Circuit JSON or from a small geometry input
4
+ format, returning `pcb_copper_pour`-ready B-Rep shapes.
4
5
 
5
- ```tsx
6
- import { CopperPourPipelineSolver } from "@tscircuit/copper-pour-solver"
6
+ ## Install
7
7
 
8
- const solver = new CopperPourPipelineSolver({
9
- // Circuit JSON including pcb_plated_hole, pcb_hole, pcb_smtpad, pcb_trace, pcb_keepout etc.
10
- circuitJson
8
+ ```bash
9
+ bun add @tscircuit/copper-pour-solver
10
+ ```
11
+
12
+ This package expects TypeScript 5 as a peer dependency.
13
+
14
+ ## Basic Usage With Circuit JSON
15
+
16
+ Initialize the geometry runtime once before solving. Then convert Circuit JSON
17
+ into the solver input format, run the solver, and map the returned B-Rep shapes
18
+ back into `pcb_copper_pour` elements.
19
+
20
+ ```ts
21
+ import {
22
+ CopperPourPipelineSolver,
23
+ convertCircuitJsonToInputProblem,
24
+ initializeManifoldGeometry,
25
+ } from "@tscircuit/copper-pour-solver"
26
+
27
+ await initializeManifoldGeometry()
28
+
29
+ const inputProblem = convertCircuitJsonToInputProblem(circuitJson, {
30
+ layer: "top",
31
+ source_net_name: "GND",
32
+ pad_margin: 0.4,
33
+ trace_margin: 0.2,
34
+ board_edge_margin: 0.1,
35
+ cutout_margin: 0.2,
11
36
  })
12
37
 
13
- solver.solve()
38
+ const solver = new CopperPourPipelineSolver(inputProblem)
39
+ const { brep_shapes } = solver.getOutput()
40
+ ```
41
+
42
+ `convertCircuitJsonToInputProblem` reads board bounds or outline, SMT pads,
43
+ plated holes, mechanical holes, vias, traces, and cutouts for the selected layer.
44
+ Pads and traces connected to the selected source net are kept connected to the
45
+ pour; unrelated geometry is subtracted using the configured margins.
46
+
47
+ ## Selecting The Pour Net
14
48
 
15
- solver.getOutput()
16
- // { brepShapes }
49
+ Prefer selecting by source net name or id:
50
+
51
+ ```ts
52
+ const inputProblem = convertCircuitJsonToInputProblem(circuitJson, {
53
+ layer: "top",
54
+ source_net_name: "GND",
55
+ pad_margin: 0.4,
56
+ trace_margin: 0.2,
57
+ })
17
58
  ```
18
59
 
19
- ## B-Rep Shapes
60
+ You can also pass the source net's stable `subcircuit_connectivity_map_key`
61
+ directly:
20
62
 
21
- We use the following representation for 2D b-rep shapes
63
+ ```ts
64
+ const gnd = circuitJson.find(
65
+ (element) => element.type === "source_net" && element.name === "GND",
66
+ )
22
67
 
23
- ```tsx
24
- interface BRepShape {
25
- outerRing: Ring // The outer boundary
26
- innerRings: Ring // The inner cutouts
68
+ const inputProblem = convertCircuitJsonToInputProblem(circuitJson, {
69
+ layer: "top",
70
+ subcircuit_connectivity_map_key: gnd.subcircuit_connectivity_map_key,
71
+ pad_margin: 0.4,
72
+ trace_margin: 0.2,
73
+ })
74
+ ```
75
+
76
+ Do not generate or pass ids from `circuit-json-to-connectivity-map`. The
77
+ converter handles PCB connectivity internally and normalizes it to stable
78
+ `subcircuit_connectivity_map_key` values.
79
+
80
+ ## Manual Input
81
+
82
+ You can skip Circuit JSON conversion and provide the solver input directly.
83
+
84
+ ```ts
85
+ import {
86
+ CopperPourPipelineSolver,
87
+ initializeManifoldGeometry,
88
+ type InputProblem,
89
+ } from "@tscircuit/copper-pour-solver"
90
+
91
+ await initializeManifoldGeometry()
92
+
93
+ const input: InputProblem = {
94
+ regionsForPour: [
95
+ {
96
+ shape: "rect",
97
+ layer: "top",
98
+ bounds: { minX: -10, minY: -5, maxX: 10, maxY: 5 },
99
+ connectivityKey: "net:GND",
100
+ padMargin: 0.4,
101
+ traceMargin: 0.2,
102
+ board_edge_margin: 0.1,
103
+ },
104
+ ],
105
+ pads: [
106
+ {
107
+ shape: "circle",
108
+ padId: "via_1",
109
+ layer: "top",
110
+ connectivityKey: "net:VCC",
111
+ x: 0,
112
+ y: 0,
113
+ radius: 0.5,
114
+ },
115
+ ],
27
116
  }
28
117
 
29
- interface Ring {
30
- cwVertices: PointWithBulge[]
118
+ const output = new CopperPourPipelineSolver(input).getOutput()
119
+ ```
120
+
121
+ Supported input pad shapes are `rect`, `circle`, `pill`, `trace`, and `polygon`.
122
+ Use the same `connectivityKey` as the pour for pads/traces that should connect to
123
+ the copper island; use a different key for blockers that should be cleared.
124
+
125
+ ## Output
126
+
127
+ `getOutput()` returns:
128
+
129
+ ```ts
130
+ interface PipelineOutput {
131
+ brep_shapes: BRepShape[]
31
132
  }
133
+ ```
134
+
135
+ Each B-Rep shape is compatible with Circuit JSON copper pour data:
32
136
 
33
- interface PointWithBulge {
34
- x: number
35
- y: number
137
+ ```ts
138
+ interface BRepShape {
139
+ outer_ring: {
140
+ vertices: Array<{ x: number; y: number; bulge?: number }>
141
+ }
142
+ inner_rings: Array<{
143
+ vertices: Array<{ x: number; y: number; bulge?: number }>
144
+ }>
36
145
  }
37
146
  ```
147
+
148
+ ## Development
149
+
150
+ ```bash
151
+ bun install
152
+ bun run build
153
+ bun test
154
+ bun start
155
+ bun run build:site
156
+ ```
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "$schema": "http://json.schemastore.org/cosmos-config",
3
3
  "plugins": ["react-cosmos-plugin-vite"],
4
- "fixtureFileSuffix": "page"
4
+ "fixtureFileSuffix": "page",
5
+ "exportPath": "cosmos-export"
5
6
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { BasePipelineSolver } from '@tscircuit/solver-utils';
2
2
  import { Bounds, Point } from '@tscircuit/math-utils';
3
- import { BRepShape, AnyCircuitElement, LayerRef, Point as Point$1 } from 'circuit-json';
3
+ import { BRepShape, LayerRef, Point as Point$1, AnyCircuitElement } from 'circuit-json';
4
4
 
5
5
  interface InputPourRegion {
6
6
  shape: "rect";
@@ -65,14 +65,23 @@ declare class CopperPourPipelineSolver extends BasePipelineSolver<InputProblem>
65
65
 
66
66
  declare const initializeManifoldGeometry: () => Promise<void>;
67
67
 
68
- declare const convertCircuitJsonToInputProblem: (circuitJson: AnyCircuitElement[], options: {
68
+ interface ConvertCircuitJsonToInputProblemOptions {
69
69
  layer: LayerRef;
70
- pour_connectivity_key: string;
70
+ source_net_id?: string;
71
+ source_net_name?: string;
72
+ subcircuit_connectivity_map_key?: string;
73
+ /**
74
+ * @deprecated Use subcircuit_connectivity_map_key, source_net_id, or
75
+ * source_net_name. Generated connectivity-map ids are intentionally rejected.
76
+ */
77
+ pour_connectivity_key?: string;
71
78
  pad_margin: number;
72
79
  trace_margin: number;
73
80
  board_edge_margin?: number;
74
81
  cutout_margin?: number;
75
82
  outline?: Point$1[];
76
- }) => InputProblem;
83
+ }
84
+
85
+ declare const convertCircuitJsonToInputProblem: (circuitJson: AnyCircuitElement[], options: ConvertCircuitJsonToInputProblemOptions) => InputProblem;
77
86
 
78
- export { type BaseInputPad, CopperPourPipelineSolver, type InputCircularPad, type InputPad, type InputPillPad, type InputPolygonPad, type InputPourRegion, type InputProblem, type InputRectPad, type InputTracePad, type PipelineOutput, convertCircuitJsonToInputProblem, initializeManifoldGeometry };
87
+ export { type BaseInputPad, type ConvertCircuitJsonToInputProblemOptions, CopperPourPipelineSolver, type InputCircularPad, type InputPad, type InputPillPad, type InputPolygonPad, type InputPourRegion, type InputProblem, type InputRectPad, type InputTracePad, type PipelineOutput, convertCircuitJsonToInputProblem, initializeManifoldGeometry };
package/dist/index.js CHANGED
@@ -537,19 +537,123 @@ var CopperPourPipelineSolver = class extends BasePipelineSolver {
537
537
 
538
538
  // lib/circuit-json/convert-circuit-json-to-input-problem.ts
539
539
  import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map";
540
+
541
+ // lib/circuit-json/buildSubcircuitConnectivityLookup.ts
542
+ import { getElementId } from "@tscircuit/circuit-json-util";
543
+
544
+ // lib/circuit-json/getElementSubcircuitConnectivityKey.ts
545
+ var getElementSubcircuitConnectivityKey = (element) => {
546
+ const key = element.subcircuit_connectivity_map_key;
547
+ return typeof key === "string" && key.length > 0 ? key : void 0;
548
+ };
549
+
550
+ // lib/circuit-json/buildSubcircuitConnectivityLookup.ts
551
+ var buildSubcircuitConnectivityLookup = (circuitJson, connectivityMap) => {
552
+ const idToSubcircuitConnectivityKey = {};
553
+ for (const element of circuitJson) {
554
+ const id = getElementId(element);
555
+ const key = getElementSubcircuitConnectivityKey(element);
556
+ if (id && key) {
557
+ idToSubcircuitConnectivityKey[id] = key;
558
+ }
559
+ }
560
+ const generatedNetIdToSubcircuitConnectivityKey = {};
561
+ for (const [generatedNetId, connectedIds] of Object.entries(
562
+ connectivityMap.netMap
563
+ )) {
564
+ const connectedSubcircuitKeys = new Set(
565
+ connectedIds.map((id) => idToSubcircuitConnectivityKey[id]).filter((key) => Boolean(key))
566
+ );
567
+ if (connectedSubcircuitKeys.size > 1) {
568
+ throw new Error(
569
+ `Multiple subcircuit connectivity keys found for generated connectivity map net "${generatedNetId}": ${Array.from(connectedSubcircuitKeys).join(", ")}`
570
+ );
571
+ }
572
+ const subcircuitKey = connectedSubcircuitKeys.values().next().value;
573
+ if (subcircuitKey) {
574
+ generatedNetIdToSubcircuitConnectivityKey[generatedNetId] = subcircuitKey;
575
+ }
576
+ }
577
+ return {
578
+ knownSubcircuitConnectivityKeys: new Set(
579
+ Object.values(idToSubcircuitConnectivityKey)
580
+ ),
581
+ getSubcircuitConnectivityKeyForId(id) {
582
+ const directKey = idToSubcircuitConnectivityKey[id];
583
+ if (directKey) return directKey;
584
+ const generatedNetId = connectivityMap.getNetConnectedToId(id);
585
+ if (!generatedNetId) return void 0;
586
+ return generatedNetIdToSubcircuitConnectivityKey[generatedNetId];
587
+ }
588
+ };
589
+ };
590
+
591
+ // lib/circuit-json/resolvePourConnectivityKey.ts
592
+ var resolvePourConnectivityKey = (circuitJson, options, knownSubcircuitConnectivityKeys) => {
593
+ if (options.subcircuit_connectivity_map_key) {
594
+ return options.subcircuit_connectivity_map_key;
595
+ }
596
+ if (options.source_net_id) {
597
+ const sourceNet = circuitJson.find(
598
+ (element) => element.type === "source_net" && element.source_net_id === options.source_net_id
599
+ );
600
+ if (!sourceNet) {
601
+ throw new Error(`No source_net found with id "${options.source_net_id}"`);
602
+ }
603
+ if (!sourceNet.subcircuit_connectivity_map_key) {
604
+ throw new Error(
605
+ `source_net "${options.source_net_id}" has no subcircuit_connectivity_map_key`
606
+ );
607
+ }
608
+ return sourceNet.subcircuit_connectivity_map_key;
609
+ }
610
+ if (options.source_net_name) {
611
+ const sourceNet = circuitJson.find(
612
+ (element) => element.type === "source_net" && element.name === options.source_net_name
613
+ );
614
+ if (!sourceNet) {
615
+ throw new Error(
616
+ `No source_net found with name "${options.source_net_name}"`
617
+ );
618
+ }
619
+ if (!sourceNet.subcircuit_connectivity_map_key) {
620
+ throw new Error(
621
+ `source_net "${options.source_net_name}" has no subcircuit_connectivity_map_key`
622
+ );
623
+ }
624
+ return sourceNet.subcircuit_connectivity_map_key;
625
+ }
626
+ if (options.pour_connectivity_key) {
627
+ if (!knownSubcircuitConnectivityKeys.has(options.pour_connectivity_key)) {
628
+ throw new Error(
629
+ `pour_connectivity_key must be a subcircuit_connectivity_map_key. Use subcircuit_connectivity_map_key, source_net_id, or source_net_name instead of a generated connectivity-map id.`
630
+ );
631
+ }
632
+ return options.pour_connectivity_key;
633
+ }
634
+ throw new Error(
635
+ "Copper pour requires source_net_id, source_net_name, or subcircuit_connectivity_map_key"
636
+ );
637
+ };
638
+
639
+ // lib/circuit-json/convert-circuit-json-to-input-problem.ts
540
640
  var convertCircuitJsonToInputProblem = (circuitJson, options) => {
541
641
  const pcb_board = circuitJson.find((e) => e.type === "pcb_board");
542
642
  if (!pcb_board) throw new Error("No pcb_board found in circuit json");
543
643
  const connectivityMap = getFullConnectivityMapFromCircuitJson(circuitJson);
644
+ const { knownSubcircuitConnectivityKeys, getSubcircuitConnectivityKeyForId } = buildSubcircuitConnectivityLookup(circuitJson, connectivityMap);
645
+ const pourConnectivityKey = resolvePourConnectivityKey(
646
+ circuitJson,
647
+ options,
648
+ knownSubcircuitConnectivityKeys
649
+ );
544
650
  const pads = [];
545
651
  for (const elm of circuitJson) {
546
652
  if (elm.type === "pcb_smtpad") {
547
653
  const smtpad = elm;
548
654
  if (smtpad.layer !== options.layer) continue;
549
655
  let connectivityKey;
550
- connectivityKey = connectivityMap.getNetConnectedToId(
551
- smtpad.pcb_smtpad_id
552
- );
656
+ connectivityKey = getSubcircuitConnectivityKeyForId(smtpad.pcb_smtpad_id);
553
657
  if (!connectivityKey) {
554
658
  connectivityKey = `unconnected:${smtpad.pcb_smtpad_id}`;
555
659
  }
@@ -593,7 +697,7 @@ var convertCircuitJsonToInputProblem = (circuitJson, options) => {
593
697
  } else if (elm.type === "pcb_plated_hole") {
594
698
  const platedHole = elm;
595
699
  if (!platedHole.layers.includes(options.layer)) continue;
596
- let connectivityKey = connectivityMap.getNetConnectedToId(
700
+ let connectivityKey = getSubcircuitConnectivityKeyForId(
597
701
  platedHole.pcb_plated_hole_id
598
702
  );
599
703
  if (!connectivityKey) {
@@ -681,7 +785,7 @@ var convertCircuitJsonToInputProblem = (circuitJson, options) => {
681
785
  } else if (elm.type === "pcb_via") {
682
786
  const via = elm;
683
787
  if (!via.layers.includes(options.layer)) continue;
684
- const connectivityKey = connectivityMap.getNetConnectedToId(via.pcb_via_id) ?? `unconnected-via:${via.pcb_via_id}`;
788
+ const connectivityKey = getSubcircuitConnectivityKeyForId(via.pcb_via_id) ?? `unconnected-via:${via.pcb_via_id}`;
685
789
  pads.push({
686
790
  shape: "circle",
687
791
  padId: via.pcb_via_id,
@@ -693,7 +797,7 @@ var convertCircuitJsonToInputProblem = (circuitJson, options) => {
693
797
  });
694
798
  } else if (elm.type === "pcb_trace") {
695
799
  const trace = elm;
696
- const connectivityKey = connectivityMap.getNetConnectedToId(
800
+ const connectivityKey = getSubcircuitConnectivityKeyForId(
697
801
  trace.pcb_trace_id
698
802
  );
699
803
  if (!connectivityKey) continue;
@@ -752,7 +856,7 @@ var convertCircuitJsonToInputProblem = (circuitJson, options) => {
752
856
  layer: options.layer,
753
857
  bounds,
754
858
  outline,
755
- connectivityKey: options.pour_connectivity_key,
859
+ connectivityKey: pourConnectivityKey,
756
860
  padMargin: options.pad_margin,
757
861
  traceMargin: options.trace_margin,
758
862
  board_edge_margin: options.board_edge_margin ?? 0,
@@ -0,0 +1,18 @@
1
+ import type { LayerRef, Point } from "circuit-json"
2
+
3
+ export interface ConvertCircuitJsonToInputProblemOptions {
4
+ layer: LayerRef
5
+ source_net_id?: string
6
+ source_net_name?: string
7
+ subcircuit_connectivity_map_key?: string
8
+ /**
9
+ * @deprecated Use subcircuit_connectivity_map_key, source_net_id, or
10
+ * source_net_name. Generated connectivity-map ids are intentionally rejected.
11
+ */
12
+ pour_connectivity_key?: string
13
+ pad_margin: number
14
+ trace_margin: number
15
+ board_edge_margin?: number
16
+ cutout_margin?: number
17
+ outline?: Point[]
18
+ }
@@ -0,0 +1,54 @@
1
+ import type { AnyCircuitElement } from "circuit-json"
2
+ import type { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
3
+ import { getElementId } from "@tscircuit/circuit-json-util"
4
+ import { getElementSubcircuitConnectivityKey } from "./getElementSubcircuitConnectivityKey"
5
+
6
+ export const buildSubcircuitConnectivityLookup = (
7
+ circuitJson: AnyCircuitElement[],
8
+ connectivityMap: ReturnType<typeof getFullConnectivityMapFromCircuitJson>,
9
+ ) => {
10
+ const idToSubcircuitConnectivityKey: Record<string, string> = {}
11
+
12
+ for (const element of circuitJson) {
13
+ const id = getElementId(element)
14
+ const key = getElementSubcircuitConnectivityKey(element)
15
+ if (id && key) {
16
+ idToSubcircuitConnectivityKey[id] = key
17
+ }
18
+ }
19
+
20
+ const generatedNetIdToSubcircuitConnectivityKey: Record<string, string> = {}
21
+ for (const [generatedNetId, connectedIds] of Object.entries(
22
+ connectivityMap.netMap,
23
+ )) {
24
+ const connectedSubcircuitKeys = new Set(
25
+ connectedIds
26
+ .map((id) => idToSubcircuitConnectivityKey[id])
27
+ .filter((key): key is string => Boolean(key)),
28
+ )
29
+ if (connectedSubcircuitKeys.size > 1) {
30
+ throw new Error(
31
+ `Multiple subcircuit connectivity keys found for generated connectivity map net "${generatedNetId}": ${Array.from(connectedSubcircuitKeys).join(", ")}`,
32
+ )
33
+ }
34
+ const subcircuitKey = connectedSubcircuitKeys.values().next().value
35
+ if (subcircuitKey) {
36
+ generatedNetIdToSubcircuitConnectivityKey[generatedNetId] = subcircuitKey
37
+ }
38
+ }
39
+
40
+ return {
41
+ knownSubcircuitConnectivityKeys: new Set(
42
+ Object.values(idToSubcircuitConnectivityKey),
43
+ ),
44
+ getSubcircuitConnectivityKeyForId(id: string): string | undefined {
45
+ const directKey = idToSubcircuitConnectivityKey[id]
46
+ if (directKey) return directKey
47
+
48
+ const generatedNetId = connectivityMap.getNetConnectedToId(id)
49
+ if (!generatedNetId) return undefined
50
+
51
+ return generatedNetIdToSubcircuitConnectivityKey[generatedNetId]
52
+ },
53
+ }
54
+ }
@@ -1,17 +1,12 @@
1
1
  import type {
2
2
  AnyCircuitElement,
3
- LayerRef,
4
3
  PcbBoard,
5
4
  PcbHole,
6
5
  PcbPlatedHole,
7
- PcbPort,
8
6
  PcbSmtPad,
9
7
  PcbTrace,
10
8
  PcbVia,
11
9
  Point,
12
- SourceNet,
13
- SourcePort,
14
- SourceTrace,
15
10
  } from "circuit-json"
16
11
  import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
17
12
  import type {
@@ -23,18 +18,13 @@ import type {
23
18
  InputRectPad,
24
19
  InputTracePad,
25
20
  } from "lib/types"
21
+ import type { ConvertCircuitJsonToInputProblemOptions } from "./ConvertCircuitJsonToInputProblemOptions"
22
+ import { buildSubcircuitConnectivityLookup } from "./buildSubcircuitConnectivityLookup"
23
+ import { resolvePourConnectivityKey } from "./resolvePourConnectivityKey"
26
24
 
27
25
  export const convertCircuitJsonToInputProblem = (
28
26
  circuitJson: AnyCircuitElement[],
29
- options: {
30
- layer: LayerRef
31
- pour_connectivity_key: string
32
- pad_margin: number
33
- trace_margin: number
34
- board_edge_margin?: number
35
- cutout_margin?: number
36
- outline?: Point[]
37
- },
27
+ options: ConvertCircuitJsonToInputProblemOptions,
38
28
  ): InputProblem => {
39
29
  const pcb_board = circuitJson.find((e) => e.type === "pcb_board") as
40
30
  | PcbBoard
@@ -43,6 +33,13 @@ export const convertCircuitJsonToInputProblem = (
43
33
  if (!pcb_board) throw new Error("No pcb_board found in circuit json")
44
34
 
45
35
  const connectivityMap = getFullConnectivityMapFromCircuitJson(circuitJson)
36
+ const { knownSubcircuitConnectivityKeys, getSubcircuitConnectivityKeyForId } =
37
+ buildSubcircuitConnectivityLookup(circuitJson, connectivityMap)
38
+ const pourConnectivityKey = resolvePourConnectivityKey(
39
+ circuitJson,
40
+ options,
41
+ knownSubcircuitConnectivityKeys,
42
+ )
46
43
 
47
44
  const pads: InputPad[] = []
48
45
 
@@ -52,9 +49,7 @@ export const convertCircuitJsonToInputProblem = (
52
49
  if (smtpad.layer !== options.layer) continue
53
50
 
54
51
  let connectivityKey: string | undefined
55
- connectivityKey = connectivityMap.getNetConnectedToId(
56
- smtpad.pcb_smtpad_id,
57
- )
52
+ connectivityKey = getSubcircuitConnectivityKeyForId(smtpad.pcb_smtpad_id)
58
53
  if (!connectivityKey) {
59
54
  connectivityKey = `unconnected:${smtpad.pcb_smtpad_id}`
60
55
  }
@@ -101,7 +96,7 @@ export const convertCircuitJsonToInputProblem = (
101
96
  const platedHole = elm as PcbPlatedHole
102
97
  if (!platedHole.layers.includes(options.layer)) continue
103
98
 
104
- let connectivityKey = connectivityMap.getNetConnectedToId(
99
+ let connectivityKey = getSubcircuitConnectivityKeyForId(
105
100
  platedHole.pcb_plated_hole_id,
106
101
  )
107
102
  if (!connectivityKey) {
@@ -189,7 +184,7 @@ export const convertCircuitJsonToInputProblem = (
189
184
  if (!via.layers.includes(options.layer)) continue
190
185
 
191
186
  const connectivityKey: string =
192
- connectivityMap.getNetConnectedToId(via.pcb_via_id) ??
187
+ getSubcircuitConnectivityKeyForId(via.pcb_via_id) ??
193
188
  `unconnected-via:${via.pcb_via_id}`
194
189
 
195
190
  pads.push({
@@ -203,7 +198,7 @@ export const convertCircuitJsonToInputProblem = (
203
198
  } as InputCircularPad)
204
199
  } else if (elm.type === "pcb_trace") {
205
200
  const trace = elm as PcbTrace
206
- const connectivityKey = connectivityMap.getNetConnectedToId(
201
+ const connectivityKey = getSubcircuitConnectivityKeyForId(
207
202
  trace.pcb_trace_id,
208
203
  )
209
204
  if (!connectivityKey) continue
@@ -271,7 +266,7 @@ export const convertCircuitJsonToInputProblem = (
271
266
  layer: options.layer,
272
267
  bounds,
273
268
  outline,
274
- connectivityKey: options.pour_connectivity_key,
269
+ connectivityKey: pourConnectivityKey,
275
270
  padMargin: options.pad_margin,
276
271
  traceMargin: options.trace_margin,
277
272
  board_edge_margin: options.board_edge_margin ?? 0,
@@ -0,0 +1,8 @@
1
+ import type { AnyCircuitElement } from "circuit-json"
2
+
3
+ export const getElementSubcircuitConnectivityKey = (
4
+ element: AnyCircuitElement,
5
+ ): string | undefined => {
6
+ const key = (element as any).subcircuit_connectivity_map_key
7
+ return typeof key === "string" && key.length > 0 ? key : undefined
8
+ }
@@ -0,0 +1,61 @@
1
+ import type { AnyCircuitElement, SourceNet } from "circuit-json"
2
+ import type { ConvertCircuitJsonToInputProblemOptions } from "./ConvertCircuitJsonToInputProblemOptions"
3
+
4
+ export const resolvePourConnectivityKey = (
5
+ circuitJson: AnyCircuitElement[],
6
+ options: ConvertCircuitJsonToInputProblemOptions,
7
+ knownSubcircuitConnectivityKeys: Set<string>,
8
+ ): string => {
9
+ if (options.subcircuit_connectivity_map_key) {
10
+ return options.subcircuit_connectivity_map_key
11
+ }
12
+
13
+ if (options.source_net_id) {
14
+ const sourceNet = circuitJson.find(
15
+ (element): element is SourceNet =>
16
+ element.type === "source_net" &&
17
+ element.source_net_id === options.source_net_id,
18
+ )
19
+ if (!sourceNet) {
20
+ throw new Error(`No source_net found with id "${options.source_net_id}"`)
21
+ }
22
+ if (!sourceNet.subcircuit_connectivity_map_key) {
23
+ throw new Error(
24
+ `source_net "${options.source_net_id}" has no subcircuit_connectivity_map_key`,
25
+ )
26
+ }
27
+ return sourceNet.subcircuit_connectivity_map_key
28
+ }
29
+
30
+ if (options.source_net_name) {
31
+ const sourceNet = circuitJson.find(
32
+ (element): element is SourceNet =>
33
+ element.type === "source_net" &&
34
+ element.name === options.source_net_name,
35
+ )
36
+ if (!sourceNet) {
37
+ throw new Error(
38
+ `No source_net found with name "${options.source_net_name}"`,
39
+ )
40
+ }
41
+ if (!sourceNet.subcircuit_connectivity_map_key) {
42
+ throw new Error(
43
+ `source_net "${options.source_net_name}" has no subcircuit_connectivity_map_key`,
44
+ )
45
+ }
46
+ return sourceNet.subcircuit_connectivity_map_key
47
+ }
48
+
49
+ if (options.pour_connectivity_key) {
50
+ if (!knownSubcircuitConnectivityKeys.has(options.pour_connectivity_key)) {
51
+ throw new Error(
52
+ `pour_connectivity_key must be a subcircuit_connectivity_map_key. Use subcircuit_connectivity_map_key, source_net_id, or source_net_name instead of a generated connectivity-map id.`,
53
+ )
54
+ }
55
+ return options.pour_connectivity_key
56
+ }
57
+
58
+ throw new Error(
59
+ "Copper pour requires source_net_id, source_net_name, or subcircuit_connectivity_map_key",
60
+ )
61
+ }
package/lib/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./solvers/CopperPourPipelineSolver"
2
2
  export { initializeManifoldGeometry } from "./solvers/copper-pour/manifold-runtime"
3
+ export type { ConvertCircuitJsonToInputProblemOptions } from "./circuit-json/ConvertCircuitJsonToInputProblemOptions"
3
4
  export * from "./circuit-json/convert-circuit-json-to-input-problem"
4
5
  export * from "./types"
package/package.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "name": "@tscircuit/copper-pour-solver",
3
3
  "main": "dist/index.js",
4
- "version": "0.0.33",
4
+ "version": "0.0.34",
5
5
  "scripts": {
6
6
  "format": "biome format . --write",
7
7
  "format:check": "biome format .",
8
+ "start": "cosmos",
9
+ "cosmos": "react-cosmos",
10
+ "build:site": "cosmos-export",
8
11
  "build": "tsup-node lib/index.ts --dts --format esm"
9
12
  },
10
13
  "type": "module",
11
14
  "devDependencies": {
12
15
  "@biomejs/biome": "^2.2.4",
16
+ "@tscircuit/circuit-json-util": "^0.0.77",
13
17
  "@flatten-js/core": "^1.6.2",
14
18
  "@tscircuit/math-utils": "^0.0.25",
15
19
  "@tscircuit/solver-utils": "^0.0.14",