Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 12 additions & 43 deletions lib/components/workspace-context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { CircuitJson } from "circuit-json"
import type { ConvertCircuitJsonToLbrnOptions } from "circuit-json-to-lbrn"
import {
convertCircuitJsonToLbrn,
type ConvertCircuitJsonToLbrnOptions,
} from "circuit-json-to-lbrn"
import { KicadToCircuitJsonConverter } from "kicad-to-circuit-json"
import React, {
createContext,
Expand Down Expand Up @@ -144,6 +147,12 @@ const defaultLbrnOptions: ConvertCircuitJsonToLbrnOptions = {
includeSoldermaskCure: true,
mirrorBottomLayer: true,
includeLayers: ["top", "bottom"],
toolingLayerIncludeRefs: [
"test_short_top_left_top_trace",
"test_short_top_right_top_trace",
"test_short_bottom_right_top_trace",
"test_short_bottom_left_top_trace",
],
laserSpotSize: 0.005,
traceMargin: 0.5,
copperCutFillMargin: 0.5,
Expand Down Expand Up @@ -451,12 +460,10 @@ export function WorkspaceProvider({ children }: { children: ReactNode }) {
setError(null)

try {
const { convertCircuitJsonToLbrn } = await import("circuit-json-to-lbrn")

const finalOptions = { ...lbrnOptions, ...options }

const rawXml = await convertCircuitJsonToLbrn(circuitJson, finalOptions)
const xml = formatLbrnXml(rawXml)
const project = await convertCircuitJsonToLbrn(circuitJson, finalOptions)
const xml = project.getString()

setLbrnFileContent({
xml,
Expand Down Expand Up @@ -500,44 +507,6 @@ export function WorkspaceProvider({ children }: { children: ReactNode }) {
)
}

const formatLbrnXml = (value: unknown): string => {
if (typeof value === "string") {
return value
}

if (value && typeof value === "object") {
const candidate = value as { xml?: unknown; outerHTML?: unknown }
if (typeof candidate.xml === "string") {
return candidate.xml
}
if (typeof candidate.outerHTML === "string") {
return candidate.outerHTML
}
}

if (typeof window !== "undefined" && value instanceof XMLDocument) {
return new XMLSerializer().serializeToString(value)
}

if (typeof window !== "undefined" && value instanceof Element) {
return new XMLSerializer().serializeToString(value)
}

if (Array.isArray(value)) {
return value.map(formatLbrnXml).join("")
}

const fallback = String(value)
if (fallback.startsWith("[object ") && fallback.endsWith("]")) {
const inner = fallback.slice(8, -1)
if (inner.trim().startsWith("<?xml")) {
return inner
}
}

return fallback
}

export function useWorkspace() {
const context = useContext(WorkspaceContext)
if (context === undefined) {
Expand Down
30 changes: 30 additions & 0 deletions lib/helpers/generate-lightburn-svg-for-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { generateLightBurnSvg, type LightBurnProject, ShapeBase } from "lbrnts"

const TOOL_1_INDEX = 30
const PREVIEW_INDEX = 24
const PREVIEW_COLOR = "#FF80C0"
const TOOL_1_COLOR = "#F36926"

export const generateLightBurnSvgForPreview = (
project: LightBurnProject,
): string => {
const toolingShapes = project.children.filter(
(child): child is ShapeBase =>
child instanceof ShapeBase && child.cutIndex === TOOL_1_INDEX,
)

// lbrnts 0.0.22 supports native tool layers in the project model but its SVG
// palette stops at C24. Borrow an unused preview color without changing the
// exported LightBurn project.
for (const shape of toolingShapes) {
shape.cutIndex = PREVIEW_INDEX
}

try {
return generateLightBurnSvg(project).replaceAll(PREVIEW_COLOR, TOOL_1_COLOR)
} finally {
for (const shape of toolingShapes) {
shape.cutIndex = TOOL_1_INDEX
}
}
}
1 change: 1 addition & 0 deletions lib/helpers/lightburn-cut-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const LIGHTBURN_COLORS: Record<number, string> = {
22: "#FF8040",
23: "#FFC0FF",
24: "#FF80C0",
30: "#F36926",
}

export type ExistingCutSetting = {
Expand Down
10 changes: 6 additions & 4 deletions lib/hooks/preview-hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { distance, type CircuitJson } from "circuit-json"
import { convertCircuitJsonToLbrn } from "circuit-json-to-lbrn"
import type { ConvertCircuitJsonToLbrnOptions } from "circuit-json-to-lbrn"
import {
convertCircuitJsonToLbrn,
type ConvertCircuitJsonToLbrnOptions,
} from "circuit-json-to-lbrn"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"
import type { PcbSvgOptions } from "circuit-to-svg"
import { generateLightBurnSvg } from "lbrnts"
import {
type RefObject,
useCallback,
Expand All @@ -20,6 +21,7 @@ import {
translate,
} from "transformation-matrix"
import { useMouseMatrixTransform } from "use-mouse-matrix-transform"
import { generateLightBurnSvgForPreview } from "../helpers/generate-lightburn-svg-for-preview"
import { IDENTITY_MATRIX, computeFitTransform } from "../helpers/svg-transform"

const pcbPreviewSvgOptions: PcbSvgOptions = {
Expand Down Expand Up @@ -189,7 +191,7 @@ export function useSvgGeneration({
lbrnOptions,
)

const lbrnSvgResult = generateLightBurnSvg(lbrnProject)
const lbrnSvgResult = generateLightBurnSvgForPreview(lbrnProject)
if (!cancelled) {
setLbrnSvg(String(lbrnSvgResult))
lastLbrnInputs.current = { circuitJson, lbrnOptions }
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"@radix-ui/react-dialog": "^1.1.15",
"@tscircuit/plop": "^0.0.72",
"circuit-json": "^0.0.403",
"circuit-json-to-lbrn": "^0.0.82",
"circuit-json-to-lbrn": "^0.0.86",
"circuit-to-svg": "^0.0.344",
"kicad-to-circuit-json": "^0.0.88",
"kicadts": "^0.0.45",
"lbrnts": "^0.0.19",
"lbrnts": "^0.0.22",
"lucide-react": "^0.562.0",
"react": "^19.1.0",
"react-cosmos": "^7.1.0",
Expand Down
6 changes: 6 additions & 0 deletions tests/conductivity-pads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ test("inserted conductivity pads are included in top and bottom copper cut fill"
includeCopper: true,
includeCopperCutFill: true,
includeLayers: ["top", "bottom"],
toolingLayerIncludeRefs: [
"test_short_top_left_top_trace",
"test_short_top_right_top_trace",
"test_short_bottom_right_top_trace",
"test_short_bottom_left_top_trace",
],
origin: { x: 0, y: 0 },
copperCutFillMargin: 0.5,
})
Expand Down
22 changes: 22 additions & 0 deletions tests/svg.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { expect, test } from "bun:test"
import { LightBurnProject, ShapePath } from "lbrnts"
import { generateLightBurnSvgForPreview } from "../lib/helpers/generate-lightburn-svg-for-preview"

const testSvg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
Expand All @@ -9,3 +11,23 @@ test("svg snapshot example", async () => {
// Subsequent runs will compare against the saved snapshot
await expect(testSvg).toMatchSvgSnapshot(import.meta.path)
})

test("renders native T1 shapes in the preview palette", () => {
const project = new LightBurnProject()
const toolingShape = new ShapePath({
cutIndex: 30,
isClosed: true,
verts: [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 1, y: 1 },
{ x: 0, y: 1 },
{ x: 0, y: 0 },
],
prims: Array.from({ length: 5 }, () => ({ type: 0 })),
})
project.children.push(toolingShape)

expect(generateLightBurnSvgForPreview(project)).toContain("#F36926")
expect(toolingShape.cutIndex).toBe(30)
})
Loading