-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from tscircuit/fabnotes
fabrication note path and text implementation
- Loading branch information
Showing
11 changed files
with
225 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/lib/svg-object-fns/create-svg-objects-from-pcb-fabrication-note-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { PcbSilkscreenPath, PcbFabricationNotePath } from "@tscircuit/soup" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
import type { SvgObject } from "../svg-object" | ||
|
||
export function createSvgObjectsFromPcbFabricationNotePath( | ||
fabNotePath: PcbFabricationNotePath, | ||
transform: Matrix, | ||
): SvgObject[] { | ||
if (!fabNotePath.route || !Array.isArray(fabNotePath.route)) return [] | ||
|
||
let path = fabNotePath.route | ||
.map((point: any, index: number) => { | ||
const [x, y] = applyToPoint(transform, [point.x, point.y]) | ||
return index === 0 ? `M ${x} ${y}` : `L ${x} ${y}` | ||
}) | ||
.join(" ") | ||
|
||
// Close the path if it's not already closed | ||
const firstPoint = fabNotePath.route[0] | ||
const lastPoint = fabNotePath.route[fabNotePath.route.length - 1] | ||
if (firstPoint!.x !== lastPoint!.x || firstPoint!.y !== lastPoint!.y) { | ||
path += " Z" | ||
} | ||
return [ | ||
{ | ||
name: "path", | ||
type: "element", | ||
attributes: { | ||
class: "pcb-fabrication-note-path", | ||
stroke: "rgba(255,255,255,0.5)", | ||
d: path, | ||
"stroke-width": ( | ||
fabNotePath.stroke_width * Math.abs(transform.a) | ||
).toString(), | ||
"data-pcb-component-id": fabNotePath.pcb_component_id, | ||
"data-pcb-fabrication-note-path-id": | ||
fabNotePath.fabrication_note_path_id, | ||
}, | ||
value: "", | ||
children: [], | ||
}, | ||
] | ||
} |
78 changes: 78 additions & 0 deletions
78
src/lib/svg-object-fns/create-svg-objects-from-pcb-fabrication-note-text.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { PcbFabricationNoteText } from "@tscircuit/soup" | ||
import type { INode as SvgObject } from "svgson" | ||
import { toString as matrixToString } from "transformation-matrix" | ||
import { | ||
type Matrix, | ||
applyToPoint, | ||
compose, | ||
rotate, | ||
translate, | ||
} from "transformation-matrix" | ||
|
||
export function createSvgObjectsFromPcbFabricationNoteText( | ||
pcbFabNoteText: PcbFabricationNoteText, | ||
transform: Matrix, | ||
): SvgObject[] { | ||
const { | ||
anchor_position, | ||
anchor_alignment, | ||
text, | ||
font_size = 1, | ||
layer = "top", | ||
} = pcbFabNoteText | ||
|
||
if ( | ||
!anchor_position || | ||
typeof anchor_position.x !== "number" || | ||
typeof anchor_position.y !== "number" | ||
) { | ||
console.error("Invalid anchor_position:", anchor_position) | ||
return [] | ||
} | ||
|
||
const [transformedX, transformedY] = applyToPoint(transform, [ | ||
anchor_position.x, | ||
anchor_position.y, | ||
]) | ||
const transformedFontSize = font_size * Math.abs(transform.a) | ||
|
||
// Remove ${} from text value and handle undefined text | ||
const cleanedText = (text || "").replace(/\$\{|\}/g, "") | ||
if (!cleanedText) { | ||
return [] | ||
} | ||
|
||
// Create a composite transformation | ||
const textTransform = compose( | ||
translate(transformedX, transformedY), // TODO do anchor_alignment | ||
rotate(Math.PI / 180), // Convert degrees to radians | ||
) | ||
|
||
const svgObject: SvgObject = { | ||
name: "text", | ||
type: "element", | ||
attributes: { | ||
x: "0", | ||
y: "0", | ||
"font-family": "Arial, sans-serif", | ||
"font-size": transformedFontSize.toString(), | ||
"text-anchor": "middle", | ||
"dominant-baseline": "central", | ||
transform: matrixToString(textTransform), | ||
class: "pcb-fabrication-note-text", | ||
fill: "rgba(255,255,255,0.5)", | ||
}, | ||
children: [ | ||
{ | ||
type: "text", | ||
value: cleanedText, | ||
name: "", | ||
attributes: {}, | ||
children: [], | ||
}, | ||
], | ||
value: "", | ||
} | ||
|
||
return [svgObject] | ||
} |
41 changes: 41 additions & 0 deletions
41
src/lib/svg-object-fns/create-svg-objects-from-pcb-silkscreen-path.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { PcbSilkscreenPath } from "@tscircuit/soup" | ||
import { applyToPoint, type Matrix } from "transformation-matrix" | ||
import type { SvgObject } from "../svg-object" | ||
|
||
export function createSvgObjectsFromPcbSilkscreenPath( | ||
silkscreenPath: PcbSilkscreenPath, | ||
transform: Matrix, | ||
): SvgObject[] { | ||
if (!silkscreenPath.route || !Array.isArray(silkscreenPath.route)) return [] | ||
|
||
let path = silkscreenPath.route | ||
.map((point: any, index: number) => { | ||
const [x, y] = applyToPoint(transform, [point.x, point.y]) | ||
return index === 0 ? `M ${x} ${y}` : `L ${x} ${y}` | ||
}) | ||
.join(" ") | ||
|
||
// Close the path if it's not already closed | ||
const firstPoint = silkscreenPath.route[0] | ||
const lastPoint = silkscreenPath.route[silkscreenPath.route.length - 1] | ||
if (firstPoint!.x !== lastPoint!.x || firstPoint!.y !== lastPoint!.y) { | ||
path += " Z" | ||
} | ||
return [ | ||
{ | ||
name: "path", | ||
type: "element", | ||
attributes: { | ||
class: `pcb-silkscreen pcb-silkscreen-${silkscreenPath.layer}`, | ||
d: path, | ||
"stroke-width": ( | ||
silkscreenPath.stroke_width * Math.abs(transform.a) | ||
).toString(), | ||
"data-pcb-component-id": silkscreenPath.pcb_component_id, | ||
"data-pcb-silkscreen-path-id": silkscreenPath.pcb_silkscreen_path_id, | ||
}, | ||
value: "", | ||
children: [], | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { INode as SvgObject } from "svgson" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { test, expect } from "bun:test" | ||
import { circuitJsonToPcbSvg } from "src" | ||
import { Circuit } from "@tscircuit/core" | ||
|
||
test("fabrication note path and fabrication note text", () => { | ||
const circuit = new Circuit() | ||
|
||
circuit.add( | ||
<board width="10mm" height="10mm"> | ||
<fabricationnotepath | ||
route={[ | ||
{ x: 0, y: 0 }, | ||
{ x: 10, y: 0 }, | ||
{ | ||
x: 10, | ||
y: 10, | ||
}, | ||
{ | ||
x: 0, | ||
y: 0, | ||
}, | ||
]} | ||
/> | ||
<fabricationnotetext anchorAlignment="bottom_left" text="hello world!" /> | ||
</board>, | ||
) | ||
|
||
const circuitJson = circuit.getCircuitJson() | ||
|
||
const svg = circuitJsonToPcbSvg(circuitJson as any) | ||
|
||
expect(svg).toMatchSvgSnapshot(import.meta.path) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters