Skip to content

Commit

Permalink
updated to circuit-json and added pcb_trace_id to via (#110)
Browse files Browse the repository at this point in the history
* updated to circuit-json and added pcb_trace_id to via

* updated circuit-json-to-connectivity-map

* updated circuit-to-svg

* as any to some types

the types passed into footprinter function and autorouting-dataset

* added as any to type passed into autorouting dataset function
  • Loading branch information
ShiboSoftwareDev authored Sep 24, 2024
1 parent 7e64120 commit a87e1f8
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 54 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 4 additions & 5 deletions lib/Circuit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { LayerRef } from "circuit-json"
import type { AnyCircuitElement, LayerRef } from "circuit-json"
import type { PrimitiveComponent } from "./components/base-components/PrimitiveComponent"
import type { SoupUtilObjects } from "@tscircuit/soup-util"
import { su } from "@tscircuit/soup-util"
Expand Down Expand Up @@ -87,12 +86,12 @@ export class Circuit {
this._hasRenderedAtleastOnce = true
}

getSoup(): AnySoupElement[] {
getSoup(): AnyCircuitElement[] {
if (!this._hasRenderedAtleastOnce) this.render()
return this.db.toArray()
}

getCircuitJson(): AnySoupElement[] {
getCircuitJson(): AnyCircuitElement[] {
return this.getSoup()
}

Expand All @@ -103,7 +102,7 @@ export class Circuit {
)
})

return circuitToSvg.circuitJsonToPcbSvg(this.getCircuitJson())
return circuitToSvg.convertCircuitJsonToPcbSvg(this.getCircuitJson())
}

async preview(
Expand Down
2 changes: 1 addition & 1 deletion lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class NormalComponent<
if (!footprint) return
if (typeof footprint === "string") {
const fpSoup = fp.string(footprint).soup()
const fpComponents = createComponentsFromSoup(fpSoup)
const fpComponents = createComponentsFromSoup(fpSoup as any) // Remove as any when footprinter gets updated
this.addAll(fpComponents)
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnySoupElement, AnySourceComponent } from "@tscircuit/soup"
import type { Circuit } from "../../Circuit"
import type { AnyCircuitElement, AnySourceComponent } from "circuit-json"
import type { ZodType } from "zod"
import { z } from "zod"
import { symbols, type SchSymbol, type BaseSymbolName } from "schematic-symbols"
Expand Down Expand Up @@ -507,7 +507,7 @@ export abstract class PrimitiveComponent<
return super.renderError(message)
}
// TODO this needs to be cleaned up at some point!
this.root?.db.pcb_error.insert(message)
this.root?.db.pcb_placement_error.insert(message)
}

getString(): string {
Expand Down
2 changes: 1 addition & 1 deletion lib/components/base-components/Renderable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PCBPlacementError, PCBTraceError } from "@tscircuit/soup"
import type { PCBPlacementError, PCBTraceError } from "circuit-json"
import { Component, createElement, type ReactElement } from "react"

export const orderedRenderPhases = [
Expand Down
2 changes: 1 addition & 1 deletion lib/components/normal-components/Capacitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { capacitorProps, ledProps } from "@tscircuit/props"
import { FTYPE, SYMBOL } from "lib/utils/constants"
import { NormalComponent } from "../base-components/NormalComponent"
import type { capacitance, SourceSimpleCapacitorInput } from "@tscircuit/soup"
import type { capacitance, SourceSimpleCapacitorInput } from "circuit-json"
import { Trace } from "../primitive-components/Trace"

type PortNames =
Expand Down
2 changes: 1 addition & 1 deletion lib/components/normal-components/Resistor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resistorProps } from "@tscircuit/props"
import type { PassivePorts, Ftype, BaseSymbolName } from "lib/utils/constants"
import { NormalComponent } from "../base-components/NormalComponent"
import type { SourceSimpleResistorInput } from "@tscircuit/soup"
import type { SourceSimpleResistorInput } from "circuit-json"
import { z } from "zod"
import { Trace } from "../primitive-components/Trace"
import { Net } from "../primitive-components/Net"
Expand Down
2 changes: 1 addition & 1 deletion lib/components/primitive-components/Hole.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { holeProps } from "@tscircuit/props"
import type { PCBHole } from "@tscircuit/soup"
import type { PCBHole } from "circuit-json"

export class Hole extends PrimitiveComponent<typeof holeProps> {
pcb_hole_id: string | null = null
Expand Down
2 changes: 1 addition & 1 deletion lib/components/primitive-components/Keepout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { pcbKeepoutProps } from "@tscircuit/props"
import type { RenderPhaseFn } from "../base-components/Renderable"
import type { PCBKeepout } from "@tscircuit/soup"
import type { PCBKeepout } from "circuit-json"
import { decomposeTSR } from "transformation-matrix"

export class Keepout extends PrimitiveComponent<typeof pcbKeepoutProps> {
Expand Down
6 changes: 3 additions & 3 deletions lib/components/primitive-components/Net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from "zod"
import type { Port } from "./Port"
import type { Trace } from "./Trace"
import { pairs } from "lib/utils/pairs"
import type { AnySoupElement, SourceTrace } from "@tscircuit/soup"
import type { AnyCircuitElement, SourceTrace } from "circuit-json"
import { autoroute } from "@tscircuit/infgrid-ijump-astar"

export const netProps = z.object({
Expand Down Expand Up @@ -134,7 +134,7 @@ export class Net extends PrimitiveComponent<typeof netProps> {
const Aport = A.ports[closestPair[0]]
const Bport = B.ports[closestPair[1]]

const pcbElements: AnySoupElement[] = db
const pcbElements: AnyCircuitElement[] = db
.toArray()
.filter(
(elm) =>
Expand All @@ -156,7 +156,7 @@ export class Net extends PrimitiveComponent<typeof netProps> {
Bport.source_port_id!,
],
} as SourceTrace,
]),
]) as any, // Remove as any when autorouting-dataset has been updated
)

const trace = solution[0]
Expand Down
4 changes: 2 additions & 2 deletions lib/components/primitive-components/PlatedHole.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { platedHoleProps } from "@tscircuit/props"
import type { Port } from "./Port"
import type { PCBPlatedHoleInput } from "@tscircuit/soup"
import type { PCBPlatedHoleInput } from "circuit-json"

export class PlatedHole extends PrimitiveComponent<typeof platedHoleProps> {
pcb_plated_hole_id: string | null = null
Expand Down Expand Up @@ -93,7 +93,7 @@ export class PlatedHole extends PrimitiveComponent<typeof platedHoleProps> {
const pcb_plated_hole = db.pcb_plated_hole.insert({
pcb_component_id,
pcb_port_id: this.matchedPort?.pcb_port_id!,
// @ts-ignore - some issue with @tscircuit/soup union type
// @ts-ignore - some issue with circuit-json union type
outer_diameter: props.outerDiameter,
hole_diameter: props.holeDiameter,
shape: "circle" as const,
Expand Down
2 changes: 1 addition & 1 deletion lib/components/primitive-components/Port.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PCBSMTPad } from "@tscircuit/soup"
import type { PCBSMTPad } from "circuit-json"
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { z } from "zod"
import { getRelativeDirection } from "lib/utils/get-relative-direction"
Expand Down
3 changes: 2 additions & 1 deletion lib/components/primitive-components/SmtPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { smtPadProps } from "@tscircuit/props"
import type { Port } from "./Port"
import type { RenderPhaseFn } from "../base-components/Renderable"
import type { LayerRef, PCBSMTPad } from "@tscircuit/soup"
import type { LayerRef, PCBSMTPad } from "circuit-json"
import {
applyToPoint,
compose,
Expand All @@ -12,6 +12,7 @@ import {
translate,
} from "transformation-matrix"


export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
pcb_smtpad_id: string | null = null

Expand Down
34 changes: 19 additions & 15 deletions lib/components/primitive-components/Trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
markObstaclesAsConnected,
} from "@tscircuit/infgrid-ijump-astar"
import type {
AnySoupElement,
AnyCircuitElement,
LayerRef,
PCBTrace,
RouteHintPoint,
SchematicTrace,
SourceTrace,
} from "@tscircuit/soup"
} from "circuit-json"
import type {
Obstacle,
SimpleRouteConnection,
Expand Down Expand Up @@ -379,14 +379,15 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {

// Cache the PCB obstacles, they'll be needed for each segment between
// ports/hints
const [obstacles, errGettingObstacles] = tryNow(() =>
getObstaclesFromSoup(this.root!.db.toArray()),
const [obstacles, errGettingObstacles] = tryNow(
() => getObstaclesFromSoup(this.root!.db.toArray() as any), // Remove as any when autorouting-dataset gets updated
)

if (errGettingObstacles) {
this.renderError({
type: "pcb_error",
type: "pcb_trace_error",
error_type: "pcb_trace_error",
pcb_trace_error_id: this.pcb_trace_id!,
message: `Error getting obstacles for autorouting: ${errGettingObstacles.message}`,
source_trace_id: this.source_trace_id!,
center: { x: 0, y: 0 },
Expand Down Expand Up @@ -484,10 +485,11 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
traces = ijump.solveAndMapToTraces()
} catch (e: any) {
this.renderError({
type: "pcb_error",
type: "pcb_trace_error",
pcb_trace_error_id: this.source_trace_id!,
error_type: "pcb_trace_error",
message: `error solving route: ${e.message}`,
source_trace_id: this.source_trace_id!,
source_trace_id: this.pcb_trace_id!,
center: { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 },
pcb_port_ids: ports.map((p) => p.pcb_port_id!),
pcb_trace_id: this.pcb_trace_id!,
Expand All @@ -497,8 +499,9 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
if (!traces) return
if (traces.length === 0) {
this.renderError({
type: "pcb_error",
type: "pcb_trace_error",
error_type: "pcb_trace_error",
pcb_trace_error_id: this.pcb_trace_id!,
message: `Could not find a route for ${this}`,
source_trace_id: this.source_trace_id!,
center: { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 },
Expand Down Expand Up @@ -533,9 +536,17 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {

const mergedRoute = mergeRoutes(routes)

const pcb_trace = db.pcb_trace.insert({
route: mergedRoute,
source_trace_id: this.source_trace_id!,
})
this._portsRoutedOnPcb = ports
this.pcb_trace_id = pcb_trace.pcb_trace_id

for (const point of mergedRoute) {
if (point.route_type === "via") {
db.pcb_via.insert({
pcb_trace_id: pcb_trace.pcb_trace_id,
x: point.x,
y: point.y,
hole_diameter: 0.3,
Expand All @@ -546,13 +557,6 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
})
}
}

const pcb_trace = db.pcb_trace.insert({
route: mergedRoute,
source_trace_id: this.source_trace_id!,
})
this._portsRoutedOnPcb = ports
this.pcb_trace_id = pcb_trace.pcb_trace_id
}

doInitialSchematicTraceRender(): void {
Expand Down
2 changes: 1 addition & 1 deletion lib/components/primitive-components/TraceHint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrimitiveComponent } from "lib/components/base-components/PrimitiveComponent"
import { traceHintProps } from "@tscircuit/props"
import type { Port } from "./Port"
import type { RouteHintPoint } from "@tscircuit/soup"
import type { RouteHintPoint } from "circuit-json"
import { applyToPoint } from "transformation-matrix"

export class TraceHint extends PrimitiveComponent<typeof traceHintProps> {
Expand Down
2 changes: 1 addition & 1 deletion lib/soup/underscorifyPinStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SchematicPinStyle } from "@tscircuit/props"
import { schematic_component } from "@tscircuit/soup"
import { schematic_component } from "circuit-json"
import { z } from "zod"

type UnderscorePinStyles = z.input<typeof schematic_component>["pin_styles"]
Expand Down
2 changes: 1 addition & 1 deletion lib/soup/underscorifyPortArrangement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SchematicComponentInput } from "@tscircuit/soup"
import type { SchematicComponentInput } from "circuit-json"
import type { PortArrangement } from "lib/utils/schematic/getAllDimensionsForSchematicBox"

export const underscorifyPortArrangement = (
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/autorouting/mergeRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PCBTrace } from "@tscircuit/soup"
import type { PCBTrace } from "circuit-json"

function pdist(a: any, b: any) {
return Math.hypot(a.x - b.x, a.y - b.y)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AnySourceComponent } from "@tscircuit/soup"
import type { AnySourceComponent } from "circuit-json"
import type { BaseSymbolName } from "schematic-symbols"

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/createComponentsFromSoup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AnySoupElement } from "@tscircuit/soup"
import type { AnyCircuitElement } from "circuit-json"
import type { PrimitiveComponent } from "../components/base-components/PrimitiveComponent"
import { SmtPad } from "lib/components/primitive-components/SmtPad"
import { SilkscreenPath } from "lib/components/primitive-components/SilkscreenPath"
import { PlatedHole } from "lib/components/primitive-components/PlatedHole"
import { Keepout } from "lib/components/primitive-components/Keepout"

export const createComponentsFromSoup = (
soup: AnySoupElement[],
soup: AnyCircuitElement[],
): PrimitiveComponent[] => {
const components: PrimitiveComponent[] = []
for (const elm of soup) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/schematic/getAllDimensionsForSchematicBox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { current } from "@tscircuit/soup"
import { current } from "circuit-json"
import { getSizeOfSidesFromPortArrangement } from "./getSizeOfSidesFromPortArrangement"
import { schematicPortArrangement } from "@tscircuit/props"
import { z } from "zod"
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@types/react": "^18.3.3",
"@types/react-reconciler": "^0.28.8",
"bun-match-svg": "0.0.2",
"circuit-to-svg": "^0.0.36",
"circuit-to-svg": "^0.0.37",
"debug": "^4.3.6",
"howfat": "^0.3.8",
"looks-same": "^9.0.1",
Expand All @@ -35,10 +35,9 @@
"@tscircuit/infgrid-ijump-astar": "^0.0.21",
"@tscircuit/math-utils": "^0.0.4",
"@tscircuit/props": "^0.0.65",
"@tscircuit/soup": "^0.0.73",
"@tscircuit/soup-util": "^0.0.28",
"circuit-json": "^0.0.77",
"circuit-json-to-connectivity-map": "^0.0.15",
"circuit-json": "^0.0.78",
"@tscircuit/soup-util": "^0.0.31",
"circuit-json-to-connectivity-map": "^0.0.17",
"footprinter": "^0.0.44",
"nanoid": "^5.0.7",
"performance-now": "^2.1.0",
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/extend-expect-circuit-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { circuitJsonToPcbSvg, circuitJsonToSchematicSvg } from "circuit-to-svg"
import { circuitJsonToPcbSvg, circuitJsonToSchematicSvg, convertCircuitJsonToPcbSvg, convertCircuitJsonToSchematicSvg } from "circuit-to-svg"
import { it, expect, type CustomMatcher, type MatcherResult } from "bun:test"
import * as fs from "node:fs"
import * as path from "node:path"
import looksSame from "looks-same"
import type { AnySoupElement } from "@tscircuit/soup"
import { Circuit } from "lib/Circuit"
import type { AnyCircuitElement } from "circuit-json"

async function saveSnapshotOfSoup({
soup,
testPath,
mode,
updateSnapshot,
}: {
soup: AnySoupElement[]
soup: AnyCircuitElement[]
testPath: string
mode: "pcb" | "schematic"
updateSnapshot: boolean
Expand All @@ -23,7 +23,7 @@ async function saveSnapshotOfSoup({
const filePath = path.join(snapshotDir, snapshotName)

const svg =
mode === "pcb" ? circuitJsonToPcbSvg(soup) : circuitJsonToSchematicSvg(soup)
mode === "pcb" ? convertCircuitJsonToPcbSvg(soup) : convertCircuitJsonToSchematicSvg(soup)

if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { recursive: true })
Expand Down Expand Up @@ -78,7 +78,7 @@ expect.extend({
): Promise<MatcherResult> {
const soup = await (received instanceof Circuit
? received.getCircuitJson()
: (received as AnySoupElement[]))
: (received as AnyCircuitElement[]))

return saveSnapshotOfSoup({
soup,
Expand All @@ -99,7 +99,7 @@ expect.extend({
soup:
received instanceof Circuit
? received.getSoup()
: (received as AnySoupElement[]),
: (received as AnyCircuitElement[]),
testPath: args[0],
mode: "schematic",
updateSnapshot:
Expand Down

0 comments on commit a87e1f8

Please sign in to comment.