Skip to content

Commit

Permalink
refactor all pcb elements
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Sep 20, 2024
1 parent 1ca1a98 commit 53d5fda
Show file tree
Hide file tree
Showing 23 changed files with 784 additions and 251 deletions.
67 changes: 49 additions & 18 deletions scripts/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,55 @@ import fs from "node:fs"

const anthropic = new Anthropic()

const filePath = "./src/pcb/pcb_board.ts"
const fileContents = fs.readFileSync(filePath, "utf8")
const filePaths = [
"./src/pcb/pcb_fabrication_note_path.ts",
"./src/pcb/pcb_component.ts",
"./src/pcb/pcb_port_not_matched_error.ts",
"./src/pcb/pcb_silkscreen_text.ts",
"./src/pcb/pcb_trace_error.ts",
"./src/pcb/pcb_silkscreen_pill.ts",
"./src/pcb/pcb_plated_hole.ts",
"./src/pcb/pcb_fabrication_note_text.ts",
"./src/pcb/pcb_silkscreen_circle.ts",
"./src/pcb/pcb_silkscreen_path.ts",
"./src/pcb/pcb_text.ts",
"./src/pcb/pcb_keepout.ts",
"./src/pcb/pcb_via.ts",
// "./src/pcb/properties/supplier_name.ts",
// "./src/pcb/properties/pcb_route_hints.ts",
// "./src/pcb/properties/layer_ref.ts",
// "./src/pcb/properties/route_hint_point.ts",
"./src/pcb/pcb_silkscreen_oval.ts",
"./src/pcb/pcb_placement_error.ts",
"./src/pcb/pcb_port.ts",
"./src/pcb/pcb_silkscreen_rect.ts",
"./src/pcb/pcb_trace_hint.ts",
"./src/pcb/pcb_smtpad.ts",
"./src/pcb/pcb_silkscreen_line.ts",
"./src/pcb/pcb_hole.ts",
"./src/pcb/pcb_trace.ts",
// "./src/pcb/pcb_board.ts",
]

const msg = await anthropic.messages.create({
model: "claude-3-sonnet-20240229",
max_tokens: 4000,
messages: [
{
role: "user",
content: `${refactorTemplate.replace(/\$\{PATHNAME\}/g, filePath).replace(/\$\{FILECONTENTS\}/g, fileContents)}`,
},
],
})
for (const filePath of filePaths) {
const fileContents = fs.readFileSync(filePath, "utf8")

const resText: string = (msg as any).content[0].text
.split("```")[1]
.replace(/^ts\n/, "")
const msg = await anthropic.messages.create({
model: "claude-3-sonnet-20240229",
max_tokens: 4000,
messages: [
{
role: "user",
content: `${refactorTemplate.replace(/\$\{PATHNAME\}/g, filePath).replace(/\$\{FILECONTENTS\}/g, fileContents)}`,
},
],
})

// Replace the file
console.log(`Replacing ${filePath} with ai-refactored version`)
fs.writeFileSync(filePath, resText)
const resText: string = (msg as any).content[0].text
.split("```")[1]
.replace(/^ts\n/, "")

// Replace the file
console.log(`Replacing ${filePath} with ai-refactored version`)
fs.writeFileSync(filePath, resText)
}
18 changes: 10 additions & 8 deletions src/pcb/pcb_component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { z } from "zod"
import { point, type Point } from "../common"
import { layer_ref, type LayerRef } from "./properties/layer_ref"
import { rotation, length, type Rotation, type Length } from "../units"
import { getZodPrefixedIdWithDefault } from "src/common/getZodPrefixedIdWithDefault"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { layer_ref, type LayerRef } from "src/properties/layer_ref"
import { rotation, length, type Rotation, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_component = z
export const pcb_component_zod = z
.object({
type: z.literal("pcb_component"),
pcb_component_id: getZodPrefixedIdWithDefault("pcb_component"),
Expand All @@ -18,9 +17,12 @@ export const pcb_component = z
})
.describe("Defines a component on the PCB")

export type PCBComponentInput = z.input<typeof pcb_component>
type InferredPCBComponent = z.infer<typeof pcb_component>
export type PcbComponentInput = z.input<typeof pcb_component_zod>
type InferredPcbComponent = z.infer<typeof pcb_component_zod>

/**
* Defines a component on the PCB
*/
export interface PcbComponent {
type: "pcb_component"
pcb_component_id: string
Expand All @@ -37,4 +39,4 @@ export interface PcbComponent {
*/
export type PCBComponent = PcbComponent

expectTypesMatch<PcbComponent, InferredPCBComponent>(true)
expectTypesMatch<PcbComponent, InferredPcbComponent>(true)
35 changes: 27 additions & 8 deletions src/pcb/pcb_fabrication_note_path.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from "zod"
import { getZodPrefixedIdWithDefault } from "src/common/getZodPrefixedIdWithDefault"
import { visible_layer } from "./properties/layer_ref"
import { point } from "src/common"
import { length } from "src/units"
import { getZodPrefixedIdWithDefault } from "src/common"
import { visible_layer, type LayerRef } from "src/properties/layer_ref"
import { point, type Point } from "src/common"
import { length, type Length } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_fabrication_note_path = z
.object({
Expand All @@ -20,7 +21,25 @@ export const pcb_fabrication_note_path = z
"Defines a fabrication path on the PCB for fabricators or assemblers",
)

export type PcbFabricationNotePath = z.infer<typeof pcb_fabrication_note_path>
export type PcbFabricationNotePathInput = z.input<
typeof pcb_fabrication_note_path
>
export type PcbFabricationNotePathInput = z.input<typeof pcb_fabrication_note_path>
type InferredPcbFabricationNotePath = z.infer<typeof pcb_fabrication_note_path>

/**
* Defines a fabrication path on the PCB for fabricators or assemblers
*/
export interface PcbFabricationNotePath {
type: "pcb_fabrication_note_path"
pcb_fabrication_note_path_id: string
pcb_component_id: string
layer: LayerRef
route: Point[]
stroke_width: Length
color?: string
}

/**
* @deprecated use PcbFabricationNotePath
*/
export type PCBFabricationNotePath = PcbFabricationNotePath

expectTypesMatch<PcbFabricationNotePath, InferredPcbFabricationNotePath>(true)
36 changes: 29 additions & 7 deletions src/pcb/pcb_fabrication_note_text.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { z } from "zod"
import { visible_layer } from "./properties/layer_ref"
import { point } from "src/common"
import { distance } from "src/units"
import { point, type Point } from "src/common"
import { distance, type Length } from "src/units"
import { visible_layer, type LayerRef } from "src/properties/layer_ref"
import { getZodPrefixedIdWithDefault } from "src/common/getZodPrefixedIdWithDefault"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_fabrication_note_text = z
.object({
Expand All @@ -25,7 +26,28 @@ export const pcb_fabrication_note_text = z
"Defines a fabrication note in text on the PCB, useful for leaving notes for assemblers or fabricators",
)

export type PcbFabricationNoteText = z.infer<typeof pcb_fabrication_note_text>
export type PcbFabricationNoteTextInput = z.input<
typeof pcb_fabrication_note_text
>
export type PcbFabricationNoteTextInput = z.input<typeof pcb_fabrication_note_text>
type InferredPcbFabricationNoteText = z.infer<typeof pcb_fabrication_note_text>

/**
* Defines a fabrication note in text on the PCB, useful for leaving notes for assemblers or fabricators
*/
export interface PcbFabricationNoteText {
type: "pcb_fabrication_note_text"
pcb_fabrication_note_text_id: string
font: "tscircuit2024"
font_size: Length
pcb_component_id: string
text: string
layer: LayerRef
anchor_position: Point
anchor_alignment: "center" | "top_left" | "top_right" | "bottom_left" | "bottom_right"
color?: string
}

/**
* @deprecated use PcbFabricationNoteText
*/
export type PCBFabricationNoteText = PcbFabricationNoteText

expectTypesMatch<PcbFabricationNoteText, InferredPcbFabricationNoteText>(true)
58 changes: 39 additions & 19 deletions src/pcb/pcb_hole.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { z } from "zod"
import { distance } from "../units"
import { distance, type Distance } from "src/units"
import { getZodPrefixedIdWithDefault } from "src/utils/get-zod-prefixed-id-with-default"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_hole = z
.object({
pcb_hole_id: z.string(),
export const pcb_hole = z.union([
z.object({
type: z.literal("pcb_hole"),
hole_shape: z
.enum(["circle", "square", "round"])
Expand All @@ -15,19 +16,38 @@ export const pcb_hole = z
hole_diameter: z.number(),
x: distance,
y: distance,
})
.or(
z.object({
pcb_hole_id: z.string(),
type: z.literal("pcb_hole"),
hole_shape: z.literal("oval"),
hole_width: z.number(),
hole_height: z.number(),
x: distance,
y: distance,
}),
)
.describe("Defines a hole on the PCB")
}),
z.object({
type: z.literal("pcb_hole"),
pcb_hole_id: getZodPrefixedIdWithDefault("pcb_hole"),
hole_shape: z.literal("oval"),
hole_width: z.number(),
hole_height: z.number(),
x: distance,
y: distance,
}),
])

export type PcbHoleInput = z.input<typeof pcb_hole>
type InferredPcbHole = z.infer<typeof pcb_hole>

/**
* Defines a hole on the PCB
*/
export interface PcbHole {
type: "pcb_hole"
pcb_hole_id: string
hole_shape: "round" | "square" | "oval"
hole_diameter?: number
hole_width?: number
hole_height?: number
x: Distance
y: Distance
}

/**
* @deprecated use PcbHole
*/
export type PCBHole = PcbHole

export type PCBHoleInput = z.input<typeof pcb_hole>
export type PCBHole = z.infer<typeof pcb_hole>
expectTypesMatch<PcbHole, InferredPcbHole>(true)
70 changes: 51 additions & 19 deletions src/pcb/pcb_keepout.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
import { z } from "zod"
import { point } from "../common"
import { distance } from "../units"
import { point, type Point, getZodPrefixedIdWithDefault } from "src/common"
import { distance, type Distance } from "src/units"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_keepout = z
.object({
export const pcb_keepout = z.union([
z.object({
type: z.literal("pcb_keepout"),
shape: z.literal("rect"),
pcb_keepout_id: getZodPrefixedIdWithDefault("pcb_keepout"),
center: point,
width: distance,
height: distance,
pcb_keepout_id: z.string(),
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
})
.or(
z.object({
type: z.literal("pcb_keepout"),
shape: z.literal("circle"),
center: point,
radius: distance,
pcb_keepout_id: z.string(),
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
}),
)
}),
z.object({
type: z.literal("pcb_keepout"),
shape: z.literal("circle"),
pcb_keepout_id: getZodPrefixedIdWithDefault("pcb_keepout"),
center: point,
radius: distance,
layers: z.array(z.string()), // Specify layers where the keepout applies
description: z.string().optional(), // Optional description of the keepout
}),
])

export type PcbKeepoutInput = z.input<typeof pcb_keepout>
type InferredPcbKeepout = z.infer<typeof pcb_keepout>

/**
* Defines a keepout area on the PCB, which can be either a rectangle or a circle.
* The keepout area is specified for one or more layers, and an optional description can be provided.
*/
export interface PcbKeepout {
type: "pcb_keepout"
pcb_keepout_id: string
center: Point
layers: string[]
description?: string
}

// Rectangular Keepout
export interface PcbKeepoutRect extends PcbKeepout {
shape: "rect"
width: Distance
height: Distance
}

// Circular Keepout
export interface PcbKeepoutCircle extends PcbKeepout {
shape: "circle"
radius: Distance
}

/**
* @deprecated use PcbKeepout
*/
export type PCBKeepout = PcbKeepout

export type PCBKeepoutInput = z.input<typeof pcb_keepout>
export type PCBKeepout = z.infer<typeof pcb_keepout>
expectTypesMatch<PcbKeepout, InferredPcbKeepout>(true)
27 changes: 22 additions & 5 deletions src/pcb/pcb_placement_error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { z } from "zod"
import { getZodPrefixedIdWithDefault } from "src/common"
import { expectTypesMatch } from "src/utils/expect-types-match"

export const pcb_placement_error = z
.object({
pcb_error_id: z.string(),
type: z.literal("pcb_error"),
error_type: z.literal("pcb_placement_error"),
type: z.literal("pcb_placement_error"),
pcb_placement_error_id: getZodPrefixedIdWithDefault("pcb_placement_error"),
message: z.string(),
})
.describe("Defines a placement error on the PCB")

export type PCBPlacementErrorInput = z.input<typeof pcb_placement_error>
export type PCBPlacementError = z.infer<typeof pcb_placement_error>
export type PcbPlacementErrorInput = z.input<typeof pcb_placement_error>
type InferredPcbPlacementError = z.infer<typeof pcb_placement_error>

/**
* Defines a placement error on the PCB
*/
export interface PcbPlacementError {
type: "pcb_placement_error"
pcb_placement_error_id: string
message: string
}

/**
* @deprecated use PcbPlacementError
*/
export type PCBPlacementError = PcbPlacementError

expectTypesMatch<PcbPlacementError, InferredPcbPlacementError>(true)
Loading

0 comments on commit 53d5fda

Please sign in to comment.