Skip to content
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

feat: move ManualEditFile type from layout to props #114

Closed
Closed
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
60 changes: 60 additions & 0 deletions lib/common/manualEdit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { z } from "zod"
import { point, route_hint_point, type PcbRouteHint } from "circuit-json"

// Types matching layout library exactly
export interface ManualPcbPosition {
selector: string
relative_to?: string
center: { x: number; y: number }
}

export interface ManualTraceHint {
pcb_port_selector: string
offsets: PcbRouteHint[]
}

export interface ManualEditFile {
pcb_placements?: ManualPcbPosition[]
manual_trace_hints?: ManualTraceHint[]
/**
* @deprecated edit events use ids instead of selectors so
* aren't safe
*/
edit_events?: any[] // TODO: Replace with EditEvent[] once @tscircuit/manual-edit-events is available
}

// Schema for manual PCB position with snake_case
export const manualPcbPosition = z.object({
selector: z.string(),
relative_to: z
.string()
.optional()
.default("group_center")
.describe("Can be a selector or 'group_center'"),
center: point,
})

// Schema for manual trace hint with snake_case
export const manualTraceHint = z.object({
pcb_port_selector: z.string(),
offsets: z.array(route_hint_point),
})

// Schema for manual edit file with snake_case
export const manualEditFile = z.object({
pcb_placements: z.array(manualPcbPosition).optional(),
manual_trace_hints: z.array(manualTraceHint).optional(),
edit_events: z.array(z.any()).optional(),
})

// Export types for input validation
export type ManualPcbPositionInput = z.input<typeof manualPcbPosition>
export type ManualTraceHintInput = z.input<typeof manualTraceHint>
export type ManualEditFileInput = z.input<typeof manualEditFile>

// Export the schemas themselves
export {
manualPcbPosition as manualPcbPositionSchema,
manualTraceHint as manualTraceHintSchema,
manualEditFile as manualEditFileSchema,
}
34 changes: 27 additions & 7 deletions lib/components/board.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import type { LayoutBuilder, ManualEditFile } from "@tscircuit/layout"
import type { LayoutBuilder } from "@tscircuit/layout"
import { distance } from "circuit-json"
import type { Distance } from "lib/common/distance"
import { type ManualEditFile } from "lib/common/manualEdit"
import { type Point, point } from "lib/common/point"
import { expectTypesMatch } from "lib/typecheck"
import { z } from "zod"
import { subcircuitGroupProps, type SubcircuitGroupProps } from "./group"
import { commonLayoutProps } from "lib/common/layout"

export interface BoardProps extends Omit<SubcircuitGroupProps, "subcircuit"> {
width?: number | string
height?: number | string
outline?: Point[]
}

export const boardProps = subcircuitGroupProps.extend({
width: distance.optional(),
height: distance.optional(),
outline: z.array(point).optional(),
})
export const boardProps = commonLayoutProps
.merge(
subcircuitGroupProps.pick({
name: true,
children: true,
layout: true,
manualEdits: true,
routingDisabled: true,
defaultTraceWidth: true,
minTraceWidth: true,
pcbRouteCache: true,
autorouter: true,
partsEngine: true,
schAutoLayoutEnabled: true,
}),
)
.extend({
width: distance.optional(),
height: distance.optional(),
outline: z.array(point).optional(),
})

type InferredBoardProps = z.input<typeof boardProps>
expectTypesMatch<BoardProps, InferredBoardProps>(true)
expectTypesMatch<BoardProps, InferredBoardProps>(
"property manualEdits has mismatched types",
)
16 changes: 6 additions & 10 deletions lib/components/group.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { LayoutBuilder, ManualEditFile } from "@tscircuit/layout"
import { length } from "circuit-json"
import type { LayoutBuilder } from "@tscircuit/layout"
import { length, type PcbRouteHint } from "circuit-json"
import type { Distance } from "lib/common/distance"
import {
type CommonLayoutProps,
commonLayoutProps,
type SupplierPartNumbers,
} from "lib/common/layout"
import { type ManualEditFile, manualEditFile } from "lib/common/manualEdit"
import { expectTypesMatch } from "lib/typecheck"
import { z } from "zod"
import type { AnySourceComponent, PcbTrace } from "circuit-json"
Expand Down Expand Up @@ -68,11 +69,6 @@ export interface SubcircuitGroupProps extends BaseGroupProps {

autorouter?: AutorouterProp

/**
* If true, we'll automatically layout the schematic for this group. Must be
* a subcircuit (currently). This is eventually going to be replaced with more
* sophisticated layout options/modes and will be enabled by default.
*/
schAutoLayoutEnabled?: boolean

partsEngine?: PartsEngine
Expand All @@ -95,7 +91,7 @@ export const baseGroupProps = commonLayoutProps.extend({

export const subcircuitGroupProps = baseGroupProps.extend({
layout: z.custom<LayoutBuilder>((v) => true).optional(),
manualEdits: z.custom<ManualEditFile>((v) => true).optional(),
manualEdits: z.lazy(() => manualEditFile).optional(),
schAutoLayoutEnabled: z.boolean().optional(),
routingDisabled: z.boolean().optional(),
defaultTraceWidth: length.optional(),
Expand Down Expand Up @@ -123,7 +119,7 @@ expectTypesMatch<BaseGroupProps, InferredBaseGroupProps>(true)
expectTypesMatch<
SubcircuitGroupPropsWithBool,
InferredSubcircuitGroupPropsWithBool
>(true)
>("property manualEdits has mismatched types")

type InferredGroupProps = z.input<typeof groupProps>
expectTypesMatch<GroupProps, InferredGroupProps>(true)
expectTypesMatch<GroupProps, InferredGroupProps>(false)
4 changes: 3 additions & 1 deletion lib/components/subcircuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export type SubcircuitProps = SubcircuitGroupProps
export const subcircuitProps = subcircuitGroupProps

type InferredSubcircuitProps = z.input<typeof subcircuitProps>
expectTypesMatch<SubcircuitProps, InferredSubcircuitProps>(true)
expectTypesMatch<SubcircuitProps, InferredSubcircuitProps>(
"property manualEdits has mismatched types",
)
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export * from "./common/footprintProp"
export * from "./common/schematicPinDefinitions"
export * from "./common/schematicPinStyle"
export * from "./common/cadModel"
export * from "./common/manualEdit"

export * from "./components/board"
export * from "./components/chip"
Expand Down
Loading