Skip to content

Commit

Permalink
Merge pull request #151 from tscircuit/schbox
Browse files Browse the repository at this point in the history
add support for computing schematic transform and placing on svg as data
  • Loading branch information
seveibar authored Dec 19, 2024
2 parents 2bb5dcd + e8b0d20 commit 57bf8af
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions lib/sch/convert-circuit-json-to-schematic-svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fromTriangles,
type Matrix,
fromTwoMovingPoints,
toSVG,
} from "transformation-matrix"
import { drawSchematicGrid } from "./draw-schematic-grid"
import { drawSchematicLabeledPoints } from "./draw-schematic-labeled-points"
Expand Down Expand Up @@ -161,6 +162,7 @@ export function convertCircuitJsonToSchematicSvg(
width: svgWidth.toString(),
height: svgHeight.toString(),
style: `background-color: ${colorMap.schematic.background}`,
"data-real-to-screen-transform": toSVG(transform),
},
children: [
// Add styles
Expand Down
8 changes: 7 additions & 1 deletion lib/sch/get-schematic-bounds-from-circuit-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export function getSchematicBoundsFromCircuitJson(
} else if (item.type === "schematic_text") {
updateBounds(item.position, { width: 0.1, height: 0.1 }, 0)
} else if (item.type === "schematic_voltage_probe") {
updateBounds(item.position, { width: 0.2, height: 0.4 }, 0) // width and hight of the probe (Arrow)
updateBounds(item.position, { width: 0.2, height: 0.4 }, 0) // width and height of the probe (Arrow)
} else if (item.type === "schematic_box") {
updateBounds(
{ x: item.x, y: item.y },
{ width: item.width, height: item.height },
0,
)
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@tscircuit/routing": "^1.3.5",
"@tscircuit/soup-util": "^0.0.41",
"@types/node": "^22.5.5",
"bun-types": "^1.1.40",
"svgson": "^5.3.1",
"transformation-matrix": "^2.16.1"
}
Expand Down
26 changes: 26 additions & 0 deletions tests/sch/transform-attribute.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test, expect } from "bun:test"
import { convertCircuitJsonToSchematicSvg } from "lib"

test("svg should have data-real-to-screen-transform attribute", () => {
const svg = convertCircuitJsonToSchematicSvg([
{
type: "schematic_box",
x: 0,
y: 0,
width: 10,
height: 10,
schematic_component_id: "1",
},
])

// Check that the SVG contains the data-real-to-screen-transform attribute
expect(svg).toContain('data-real-to-screen-transform="')

// Extract the transform value and verify it's a valid matrix string
const match = svg.match(/data-real-to-screen-transform="([^"]+)"/)

// @ts-ignore
expect(match[1]).toMatchInlineSnapshot(
`"matrix(54.5454545455,0,0,-54.5454545455,600,300)"`,
)
})

0 comments on commit 57bf8af

Please sign in to comment.