|
| 1 | +import type { AnyCircuitElement } from "circuit-json" |
| 2 | +import { su } from "@tscircuit/soup-util" |
| 3 | + |
| 4 | +export const generateSymbolTsx = ( |
| 5 | + circuitJson: AnyCircuitElement[], |
| 6 | +): string | null => { |
| 7 | + const schematicArcs = su(circuitJson).schematic_arc.list() |
| 8 | + const schematicLines = su(circuitJson).schematic_line.list() |
| 9 | + |
| 10 | + const elementStrings: string[] = [] |
| 11 | + |
| 12 | + for (const arc of schematicArcs) { |
| 13 | + const center = arc.center ?? { x: 0, y: 0 } |
| 14 | + const radius = arc.radius ?? 0 |
| 15 | + const startAngle = arc.start_angle_degrees ?? 0 |
| 16 | + const endAngle = arc.end_angle_degrees ?? 0 |
| 17 | + const strokeWidth = arc.stroke_width ?? 0.05 |
| 18 | + const color = arc.color ?? "black" |
| 19 | + const isDashed = arc.is_dashed ?? false |
| 20 | + const direction = arc.direction ?? "counterclockwise" |
| 21 | + |
| 22 | + elementStrings.push( |
| 23 | + `<schematicarc |
| 24 | + center={{ x: ${center.x}, y: ${center.y} }} |
| 25 | + radius={${radius}} |
| 26 | + startAngleDegrees={${startAngle}} |
| 27 | + endAngleDegrees={${endAngle}} |
| 28 | + strokeWidth={${strokeWidth}} |
| 29 | + color="${color}" |
| 30 | + isDashed={${isDashed}} |
| 31 | + direction="${direction}" |
| 32 | +/>`, |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + for (const line of schematicLines) { |
| 37 | + const x1 = line.x1 ?? 0 |
| 38 | + const y1 = line.y1 ?? 0 |
| 39 | + const x2 = line.x2 ?? 0 |
| 40 | + const y2 = line.y2 ?? 0 |
| 41 | + const strokeWidth = line.stroke_width ?? 0.05 |
| 42 | + const color = line.color ?? "black" |
| 43 | + const isDashed = line.is_dashed ?? false |
| 44 | + |
| 45 | + elementStrings.push( |
| 46 | + `<schematicline x1={${x1}} y1={${y1}} x2={${x2}} y2={${y2}} strokeWidth={${strokeWidth}} color="${color}" isDashed={${isDashed}}/>`, |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + if (elementStrings.length === 0) { |
| 51 | + return null |
| 52 | + } |
| 53 | + |
| 54 | + return ` |
| 55 | +<symbol> |
| 56 | + ${elementStrings.map((s) => s.split("\n").join("\n ")).join("\n ")} |
| 57 | +</symbol> |
| 58 | + `.trim() |
| 59 | +} |
0 commit comments