circuit-json-to-tscircuit 0.0.7 → 0.0.9

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.
@@ -0,0 +1,111 @@
1
+ // lib/generate-footprint-tsx.tsx
2
+ import { mmStr } from "@tscircuit/mm";
3
+ import { su } from "@tscircuit/soup-util";
4
+ var generateFootprintTsx = (circuitJson) => {
5
+ const holes = su(circuitJson).pcb_hole.list();
6
+ const platedHoles = su(circuitJson).pcb_plated_hole.list();
7
+ const smtPads = su(circuitJson).pcb_smtpad.list();
8
+ const silkscreenPaths = su(circuitJson).pcb_silkscreen_path.list();
9
+ const fabricationNotePaths = su(circuitJson).pcb_fabrication_note_path.list();
10
+ const silkscreenTexts = su(circuitJson).pcb_silkscreen_text.list();
11
+ const elementStrings = [];
12
+ for (const hole of holes) {
13
+ if (hole.hole_shape === "circle") {
14
+ elementStrings.push(
15
+ `<hole pcbX="${mmStr(hole.x)}" pcbY="${mmStr(hole.y)}" diameter="${mmStr(hole.hole_diameter)}" />`
16
+ );
17
+ } else if (hole.hole_shape === "oval") {
18
+ console.warn("Unhandled oval hole in conversion (needs implementation)");
19
+ }
20
+ }
21
+ for (const platedHole of platedHoles) {
22
+ if (platedHole.shape === "oval" || platedHole.shape === "pill") {
23
+ elementStrings.push(
24
+ `<platedhole portHints={${JSON.stringify(platedHole.port_hints)}} pcbX="${mmStr(platedHole.x)}" pcbY="${mmStr(platedHole.y)}" outerHeight="${mmStr(platedHole.outer_height)}" outerWidth="${mmStr(platedHole.outer_width)}" holeHeight="${mmStr(platedHole.hole_height)}" holeWidth="${mmStr(platedHole.hole_width)}" height="${mmStr(platedHole.hole_height)}" shape="${platedHole.shape}" />`
25
+ );
26
+ } else if (platedHole.shape === "circle") {
27
+ elementStrings.push(
28
+ `<platedhole portHints={${JSON.stringify(platedHole.port_hints)}} pcbX="${mmStr(platedHole.x)}" pcbY="${mmStr(platedHole.y)}" outerDiameter="${mmStr(platedHole.outer_diameter)}" holeDiameter="${mmStr(platedHole.hole_diameter)}" shape="circle" />`
29
+ );
30
+ }
31
+ }
32
+ for (const smtPad of smtPads) {
33
+ if (smtPad.shape === "circle") {
34
+ elementStrings.push(
35
+ `<smtpad portHints={${JSON.stringify(smtPad.port_hints)}} pcbX="${mmStr(smtPad.x)}" pcbY="${mmStr(smtPad.y)}" radius="${mmStr(smtPad.radius)}" shape="circle" />`
36
+ );
37
+ } else if (smtPad.shape === "rect") {
38
+ elementStrings.push(
39
+ `<smtpad portHints={${JSON.stringify(smtPad.port_hints)}} pcbX="${mmStr(smtPad.x)}" pcbY="${mmStr(smtPad.y)}" width="${mmStr(smtPad.width)}" height="${mmStr(smtPad.height)}" shape="rect" />`
40
+ );
41
+ }
42
+ }
43
+ for (const silkscreenPath of silkscreenPaths) {
44
+ elementStrings.push(
45
+ `<silkscreenpath route={${JSON.stringify(silkscreenPath.route)}} />`
46
+ );
47
+ }
48
+ for (const fabPath of fabricationNotePaths) {
49
+ elementStrings.push(
50
+ `<silkscreenpath route={${JSON.stringify(fabPath.route)}} />`
51
+ );
52
+ }
53
+ for (const stext of silkscreenTexts) {
54
+ const pcbX = stext.anchor_position?.x ?? 0;
55
+ const pcbY = stext.anchor_position?.y ?? 0;
56
+ const anchorAlignment = stext.anchor_alignment;
57
+ const fontSize = stext.font_size;
58
+ const rawText = String(stext.text ?? "");
59
+ const escapedText = rawText.replace(/"/g, '\\"');
60
+ elementStrings.push(
61
+ `<silkscreentext pcbX={${pcbX}} pcbY={${pcbY}} anchorAlignment="${anchorAlignment}" fontSize={${fontSize}} text="${escapedText}" />`
62
+ );
63
+ }
64
+ return `
65
+ <footprint>
66
+ ${elementStrings.join("\n")}
67
+ </footprint>
68
+ `.trim();
69
+ };
70
+
71
+ // lib/get-component-using-template.ts
72
+ var getComponentUsingTemplate = ({
73
+ pinLabels,
74
+ componentName,
75
+ objUrl,
76
+ circuitJson,
77
+ supplierPartNumbers,
78
+ manufacturerPartNumber
79
+ }) => {
80
+ const footprintTsx = generateFootprintTsx(circuitJson);
81
+ return `
82
+ import { type ChipProps } from "tscircuit"
83
+ ${pinLabels ? `const pinLabels = ${JSON.stringify(pinLabels, null, " ")} as const
84
+ ` : ""}export const ${componentName} = (props: ChipProps${pinLabels ? `<typeof pinLabels>` : ""}) => (
85
+ <chip
86
+ footprint={${footprintTsx}}
87
+ ${pinLabels ? "pinLabels={pinLabels}" : ""}
88
+ ${objUrl ? `cadModel={{
89
+ objUrl: "${objUrl}",
90
+ rotationOffset: { x: 0, y: 0, z: 0 },
91
+ positionOffset: { x: 0, y: 0, z: 0 },
92
+ }}` : ""}
93
+ ${supplierPartNumbers ? `supplierPartNumbers={${JSON.stringify(supplierPartNumbers, null, " ")}}` : ""}
94
+ ${manufacturerPartNumber ? `manufacturerPartNumber="${manufacturerPartNumber}"` : ""}
95
+ {...props}
96
+ />
97
+ )
98
+ `.replace(/\n\s*\n/g, "\n").trim();
99
+ };
100
+
101
+ // lib/index.ts
102
+ var convertCircuitJsonToTscircuit = (circuitJson, opts) => {
103
+ return getComponentUsingTemplate({
104
+ circuitJson,
105
+ ...opts
106
+ });
107
+ };
108
+
109
+ export {
110
+ convertCircuitJsonToTscircuit
111
+ };