-
Notifications
You must be signed in to change notification settings - Fork 136
Add pcb silkscreen graphic support #2415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
techmannih
wants to merge
2
commits into
tscircuit:main
Choose a base branch
from
techmannih:silkscreen-graphic-circuit-json-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
306 changes: 306 additions & 0 deletions
306
lib/components/primitive-components/SilkscreenGraphic.ts
This file contains hidden or 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,306 @@ | ||
| import { | ||
| type SilkscreenGraphicProps as PublicSilkscreenGraphicProps, | ||
| silkscreenGraphicProps as publicSilkscreenGraphicProps, | ||
| } from "@tscircuit/props" | ||
| import { | ||
| asset, | ||
| brep_shape, | ||
| type BRepShape, | ||
| type Ring, | ||
| visible_layer, | ||
| } from "circuit-json" | ||
| import { PrimitiveComponent } from "../base-components/PrimitiveComponent" | ||
| import { applyToPoint } from "transformation-matrix" | ||
| import { z } from "zod" | ||
| import { resolveStaticFileImport } from "lib/utils/resolveStaticFileImport" | ||
| import { svgToBrepShapes } from "lib/utils/svg/svg-to-brep-shapes" | ||
|
|
||
| const internalImportedSilkscreenGraphicProps = z.object({ | ||
| layer: visible_layer.optional(), | ||
| brepShape: brep_shape, | ||
| imageAsset: asset.optional(), | ||
| }) | ||
|
|
||
| const silkscreenGraphicProps = z.union([ | ||
| publicSilkscreenGraphicProps, | ||
| internalImportedSilkscreenGraphicProps, | ||
| ]) | ||
|
|
||
| type ParsedSilkscreenGraphicProps = z.infer<typeof silkscreenGraphicProps> | ||
|
|
||
| const isImageSilkscreenGraphicProps = ( | ||
| props: ParsedSilkscreenGraphicProps, | ||
| ): props is Extract<ParsedSilkscreenGraphicProps, { imageUrl: string }> => | ||
| "imageUrl" in props && typeof props.imageUrl === "string" | ||
|
|
||
| const isImportedBrepSilkscreenGraphicProps = ( | ||
| props: ParsedSilkscreenGraphicProps, | ||
| ): props is z.infer<typeof internalImportedSilkscreenGraphicProps> => | ||
| "brepShape" in props && | ||
| Boolean(props.brepShape) && | ||
| !("imageUrl" in props && typeof props.imageUrl === "string") | ||
|
|
||
| const transformRing = ( | ||
| ring: Ring, | ||
| transform: Parameters<typeof applyToPoint>[0], | ||
| ) => ({ | ||
| vertices: ring.vertices.map((vertex) => { | ||
| const transformed = applyToPoint(transform, { x: vertex.x, y: vertex.y }) | ||
| return { | ||
| ...vertex, | ||
| x: transformed.x, | ||
| y: transformed.y, | ||
| } | ||
| }), | ||
| }) | ||
|
|
||
| const getBoundsFromVertices = ( | ||
| vertices: Array<{ x: number; y: number }>, | ||
| transform: Parameters<typeof applyToPoint>[0], | ||
| ) => { | ||
| if (vertices.length === 0) return { width: 0, height: 0 } | ||
|
|
||
| let minX = Infinity | ||
| let maxX = -Infinity | ||
| let minY = Infinity | ||
| let maxY = -Infinity | ||
|
|
||
| for (const vertex of vertices) { | ||
| const transformed = applyToPoint(transform, vertex) | ||
| minX = Math.min(minX, transformed.x) | ||
| maxX = Math.max(maxX, transformed.x) | ||
| minY = Math.min(minY, transformed.y) | ||
| maxY = Math.max(maxY, transformed.y) | ||
| } | ||
|
|
||
| return { | ||
| width: maxX - minX, | ||
| height: maxY - minY, | ||
| } | ||
| } | ||
|
|
||
| const translateBrepShape = ( | ||
| brepShape: BRepShape, | ||
| deltaX: number, | ||
| deltaY: number, | ||
| ): BRepShape => ({ | ||
| outer_ring: { | ||
| vertices: brepShape.outer_ring.vertices.map((vertex) => ({ | ||
| ...vertex, | ||
| x: vertex.x + deltaX, | ||
| y: vertex.y + deltaY, | ||
| })), | ||
| }, | ||
| inner_rings: brepShape.inner_rings.map((ring) => ({ | ||
| vertices: ring.vertices.map((vertex) => ({ | ||
| ...vertex, | ||
| x: vertex.x + deltaX, | ||
| y: vertex.y + deltaY, | ||
| })), | ||
| })), | ||
| }) | ||
|
|
||
| const getBrepVertices = (brepShape: BRepShape) => [ | ||
| ...brepShape.outer_ring.vertices, | ||
| ...brepShape.inner_rings.flatMap((ring) => ring.vertices), | ||
| ] | ||
|
|
||
| const getBrepCenter = (brepShape: BRepShape) => { | ||
| const vertices = getBrepVertices(brepShape) | ||
| if (vertices.length === 0) return { x: 0, y: 0 } | ||
|
|
||
| let x = 0 | ||
| let y = 0 | ||
| for (const vertex of vertices) { | ||
| x += vertex.x | ||
| y += vertex.y | ||
| } | ||
|
|
||
| return { | ||
| x: x / vertices.length, | ||
| y: y / vertices.length, | ||
| } | ||
| } | ||
|
|
||
| export class SilkscreenGraphic extends PrimitiveComponent< | ||
| typeof silkscreenGraphicProps | ||
| > { | ||
| pcb_silkscreen_graphic_ids: string[] = [] | ||
| isPcbPrimitive = true | ||
| _hasStartedImageLoad = false | ||
|
|
||
| get config() { | ||
| return { | ||
| componentName: "SilkscreenGraphic", | ||
| zodProps: silkscreenGraphicProps, | ||
| } | ||
| } | ||
|
|
||
| doInitialPcbPrimitiveRender(): void { | ||
| if (this.root?.pcbDisabled) return | ||
| const { _parsedProps: props } = this | ||
| const { maybeFlipLayer } = this._getPcbPrimitiveFlippedHelpers() | ||
| const layer = maybeFlipLayer(props.layer ?? "top") as "top" | "bottom" | ||
|
|
||
| if (layer !== "top" && layer !== "bottom") { | ||
| throw new Error( | ||
| `Invalid layer "${layer}" for SilkscreenGraphic. Must be "top" or "bottom".`, | ||
| ) | ||
| } | ||
|
|
||
| if (isImportedBrepSilkscreenGraphicProps(props)) { | ||
| if (this.pcb_silkscreen_graphic_ids.length > 0) return | ||
| this._insertBrepShapes([props.brepShape], layer, props.imageAsset) | ||
| return | ||
| } | ||
|
|
||
| if (!isImageSilkscreenGraphicProps(props)) { | ||
| throw new Error( | ||
| "SilkscreenGraphic must receive either imageUrl/width/height or an internal brepShape", | ||
| ) | ||
| } | ||
|
|
||
| if (this._hasStartedImageLoad) return | ||
| this._hasStartedImageLoad = true | ||
|
|
||
| this._queueAsyncEffect("load-silkscreen-graphic-image", async () => { | ||
| const resolvedUrl = await resolveStaticFileImport( | ||
| props.imageUrl, | ||
| this.root?.platform, | ||
| ) | ||
|
|
||
| const response = await fetch(resolvedUrl) | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to fetch silkscreen graphic "${resolvedUrl}": ${response.status}`, | ||
| ) | ||
| } | ||
|
|
||
| const svgContent = await response.text() | ||
| const brepShapes = svgToBrepShapes(svgContent, { | ||
| width: props.width, | ||
| height: props.height, | ||
| }) | ||
|
|
||
| this._insertBrepShapes(brepShapes, layer, { | ||
| project_relative_path: props.imageUrl, | ||
| url: resolvedUrl, | ||
| mimetype: | ||
| response.headers.get("content-type") || | ||
| (resolvedUrl.toLowerCase().endsWith(".svg") | ||
| ? "image/svg+xml" | ||
| : "application/octet-stream"), | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| private _insertBrepShapes( | ||
| brepShapes: BRepShape[], | ||
| layer: "top" | "bottom", | ||
| imageAsset: z.infer<typeof asset> | undefined, | ||
| ) { | ||
| if (brepShapes.length === 0) { | ||
| throw new Error("SilkscreenGraphic requires at least one BRep shape") | ||
| } | ||
|
|
||
| const { db } = this.root! | ||
| const transform = this._computePcbGlobalTransformBeforeLayout() | ||
| const subcircuit = this.getSubcircuit() | ||
| const group = this.getGroup() | ||
| const pcb_component_id = | ||
| this.parent?.pcb_component_id ?? | ||
| this.getPrimitiveContainer()?.pcb_component_id! | ||
|
|
||
| for (const brepShape of brepShapes) { | ||
| const pcbSilkscreenGraphic = db.pcb_silkscreen_graphic.insert({ | ||
| pcb_component_id, | ||
| pcb_group_id: group?.pcb_group_id ?? undefined, | ||
| subcircuit_id: subcircuit?.subcircuit_id ?? undefined, | ||
| layer, | ||
| shape: "brep", | ||
| image_asset: imageAsset, | ||
| brep_shape: { | ||
| outer_ring: transformRing(brepShape.outer_ring, transform), | ||
| inner_rings: brepShape.inner_rings.map((ring) => | ||
| transformRing(ring, transform), | ||
| ), | ||
| }, | ||
| }) | ||
|
|
||
| this.pcb_silkscreen_graphic_ids.push( | ||
| pcbSilkscreenGraphic.pcb_silkscreen_graphic_id, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| _setPositionFromLayout(newCenter: { x: number; y: number }) { | ||
| const { db } = this.root! | ||
| if (this.pcb_silkscreen_graphic_ids.length === 0) return | ||
|
|
||
| const currentShapes = this.pcb_silkscreen_graphic_ids | ||
| .map((id) => db.pcb_silkscreen_graphic.get(id)) | ||
| .filter(Boolean) | ||
|
|
||
| if (currentShapes.length === 0) return | ||
|
|
||
| const currentCenter = getBrepCenter({ | ||
| outer_ring: { | ||
| vertices: currentShapes.flatMap( | ||
| (shape) => shape!.brep_shape.outer_ring.vertices, | ||
| ), | ||
| }, | ||
| inner_rings: currentShapes.flatMap( | ||
| (shape) => shape!.brep_shape.inner_rings, | ||
| ), | ||
| }) | ||
|
|
||
| for (const graphic of currentShapes) { | ||
| db.pcb_silkscreen_graphic.update(graphic!.pcb_silkscreen_graphic_id, { | ||
| brep_shape: translateBrepShape( | ||
| graphic!.brep_shape, | ||
| newCenter.x - currentCenter.x, | ||
| newCenter.y - currentCenter.y, | ||
| ), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| _moveCircuitJsonElements({ | ||
| deltaX, | ||
| deltaY, | ||
| }: { deltaX: number; deltaY: number }) { | ||
| if (this.root?.pcbDisabled) return | ||
| const { db } = this.root! | ||
| for (const id of this.pcb_silkscreen_graphic_ids) { | ||
| const graphic = db.pcb_silkscreen_graphic.get(id) | ||
| if (!graphic) continue | ||
|
|
||
| db.pcb_silkscreen_graphic.update(id, { | ||
| brep_shape: translateBrepShape(graphic.brep_shape, deltaX, deltaY), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| getPcbSize(): { width: number; height: number } { | ||
| const transform = this._computePcbGlobalTransformBeforeLayout() | ||
|
|
||
| if (isImageSilkscreenGraphicProps(this._parsedProps)) { | ||
| const halfWidth = this._parsedProps.width / 2 | ||
| const halfHeight = this._parsedProps.height / 2 | ||
| return getBoundsFromVertices( | ||
| [ | ||
| { x: -halfWidth, y: -halfHeight }, | ||
| { x: halfWidth, y: -halfHeight }, | ||
| { x: halfWidth, y: halfHeight }, | ||
| { x: -halfWidth, y: halfHeight }, | ||
| ], | ||
| transform, | ||
| ) | ||
| } | ||
|
|
||
| return getBoundsFromVertices( | ||
| getBrepVertices(this._parsedProps.brepShape), | ||
| transform, | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using a fast-xml-parser to parse the imported SVG image so we can extract its shapes, transforms, and fill rules, convert them into paths/polygons, and then generate internal BRep pcb_silkscreen_graphic geometry