Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Trace Hint Editing (initial) (#48)
Browse files Browse the repository at this point in the history
* Trace Hint Editing (initial)

* update layout dep, fixing manual layout
  • Loading branch information
seveibar authored Jun 14, 2024
1 parent 8261eae commit 73a8de5
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 108 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file modified dev-server-frontend/bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions dev-server-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "tsc && vite build --base /preview && rm -f dist/bundle* && make-vfs --dir dist --outfile ./dist/bundle.ts",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 10",
"preview": "vite preview",
"update-deps": "bun add @tscircuit/pcb-viewer @tscircuit/builder @tscircuit/schematic-viewer"
"update-deps": "bun add @tscircuit/pcb-viewer@latest @tscircuit/builder@latest @tscircuit/schematic-viewer@latest"
},
"dependencies": {
"@headlessui/react": "^1.7.18",
Expand All @@ -30,8 +30,8 @@
"@radix-ui/react-toggle": "^1.0.3",
"@radix-ui/react-toggle-group": "^1.0.4",
"@radix-ui/react-tooltip": "^1.0.7",
"@tscircuit/builder": "^1.5.116",
"@tscircuit/pcb-viewer": "^1.3.11",
"@tscircuit/builder": "^1.5.117",
"@tscircuit/pcb-viewer": "^1.3.14",
"@tscircuit/schematic-viewer": "^1.2.12",
"@tscircuit/table-viewer": "0.0.8",
"axios": "^1.6.7",
Expand Down
206 changes: 156 additions & 50 deletions lib/cmd-fns/dev/start-edit-event-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ import fg from "fast-glob"
import fs from "fs"
import { Project, ts } from "ts-morph"
import * as Path from "path"
import { ManualPcbPosition } from "@tscircuit/builder"
import type { ManualPcbPosition } from "@tscircuit/builder"
import { deriveSelectorFromPcbComponentId } from "./derive-selector-from-pcb-component-id"

// TODO import from builder when builder exports it
type EditEvent = {
edit_event_id: string
pcb_edit_event_type: "edit_component_location"
pcb_component_id: string
original_center: { x: number; y: number }
new_center: { x: number; y: number }
}
import type { EditEvent } from "@tscircuit/manual-edit-events"
import { getManualTraceHintFromEvent, ManualTraceHint } from "@tscircuit/layout"
import JSON5 from "json5"

export const startEditEventWatcher = async (
{
Expand Down Expand Up @@ -142,11 +136,34 @@ export const startEditEventWatcher = async (
const pcb_placements_ts =
object_literal.getPropertyOrThrow("pcb_placements")

if (object_literal.getProperty("edit_events") === undefined) {
object_literal.addPropertyAssignment({
name: "edit_events",
initializer: "[]",
})
}

const edit_events_ts =
object_literal.getPropertyOrThrow("edit_events")

if (
object_literal.getProperty("manual_trace_hints") === undefined
) {
object_literal.addPropertyAssignment({
name: "manual_trace_hints",
initializer: "[]",
})
}
const manual_trace_hints_ts =
object_literal.getPropertyOrThrow("manual_trace_hints")

let pcb_placements: (ManualPcbPosition & {
_edit_event_id?: string
})[]
let in_file_edit_events: EditEvent[]
let manual_trace_hints: ManualTraceHint[]
try {
pcb_placements = JSON.parse(
pcb_placements = JSON5.parse(
pcb_placements_ts.getText().replace(/pcb_placements:\s/, "")
)
} catch (e: any) {
Expand All @@ -157,60 +174,149 @@ export const startEditEventWatcher = async (
)
continue
}
try {
in_file_edit_events = JSON5.parse(
edit_events_ts.getText().replace(/edit_events:\s/, "")
)
} catch (e: any) {
console.log(
kleur.red(
`Error parsing edit_events from manual edits file: ${edit_events_ts.getText()} ${e.toString()}`
)
)
continue
}
try {
manual_trace_hints = JSON5.parse(
manual_trace_hints_ts
.getText()
.replace(/manual_trace_hints:\s/, "")
)
} catch (e: any) {
console.log(
kleur.red(
`Error parsing manual_trace_hints from manual edits file: ${pcb_placements_ts.getText()} ${e.toString()}`
)
)
continue
}

const handled_edit_events = new Set<string>(
pcb_placements
.map((p) => (p as any)._edit_event_id)
.filter(Boolean)
.concat(in_file_edit_events.map((a) => a.edit_event_id))
)

// Add PCB placements from edit events
for (const edit_event of edit_events) {
if (handled_edit_events.has(edit_event.edit_event_id)) continue
for (const incoming_edit_event of edit_events) {
if (handled_edit_events.has(incoming_edit_event.edit_event_id))
continue

if (
incoming_edit_event.pcb_edit_event_type ===
"edit_component_location"
) {
// TODO Figure out a good selector for this pcb_component
let pcb_component_selector: string | null = null
if (incoming_edit_event.pcb_component_id) {
pcb_component_selector = deriveSelectorFromPcbComponentId({
soup: dev_package_example_full.tscircuit_soup,
pcb_component_id: incoming_edit_event.pcb_component_id,
})
}

// TODO we'll need to work past this for edit_event_type=edit_trace
if (!pcb_component_selector) continue

const existing_placement_for_selector = pcb_placements.find(
(pp) => pp.selector === pcb_component_selector
)

// TODO Figure out a good selector for this pcb_component
const selector = deriveSelectorFromPcbComponentId({
soup: dev_package_example_full.tscircuit_soup,
pcb_component_id: edit_event.pcb_component_id,
})
if (!existing_placement_for_selector) {
console.log(
kleur.gray(
` adding PCB placement from edit event for "${pcb_component_selector}"`
)
)

const existing_placement_for_selector = pcb_placements.find(
(pp) => pp.selector === selector
)
pcb_placements.push({
_edit_event_id: incoming_edit_event.edit_event_id,
selector: pcb_component_selector,
center: incoming_edit_event.new_center,
relative_to: "group_center",
})
} else {
existing_placement_for_selector.center =
incoming_edit_event.new_center
}

// Edit the pcb placements object
pcb_placements_ts.replaceWithText(
`pcb_placements: ${JSON.stringify(
pcb_placements,
null,
" "
)}`
)

if (!existing_placement_for_selector) {
console.log(
kleur.gray(
` adding PCB placement from edit event for "${selector}"`
)
// Save the file
fs.writeFileSync(
Path.join(ctx.cwd, manual_edit_file),
ts_manual_edits_file.getFullText()
)
await devServerAxios.post("/api/dev_package_examples/update", {
dev_package_example_id,
edit_events_last_applied_at:
dev_package_example.edit_events_last_updated_at,
})
} else if (
incoming_edit_event.pcb_edit_event_type === "edit_trace_hint"
) {
const new_trace_hint = getManualTraceHintFromEvent(
dev_package_example_full.tscircuit_soup,
incoming_edit_event
)

manual_trace_hints_ts.replaceWithText(
`manual_trace_hints: ${JSON.stringify(
manual_trace_hints
.filter(
(th) =>
th.pcb_port_selector !==
new_trace_hint.pcb_port_selector
)
.concat([new_trace_hint]),
null,
" "
)}`
)

pcb_placements.push({
_edit_event_id: edit_event.edit_event_id,
selector,
center: edit_event.new_center,
relative_to: "group_center",
fs.writeFileSync(
Path.join(ctx.cwd, manual_edit_file),
ts_manual_edits_file.getFullText()
)
await devServerAxios.post("/api/dev_package_examples/update", {
dev_package_example_id,
edit_events_last_applied_at:
dev_package_example.edit_events_last_updated_at,
})
} else {
existing_placement_for_selector.center = edit_event.new_center
// All other events just go to the manual-edits.ts file with
// in the "edit_events" property
edit_events_ts.replaceWithText(
`edit_events: ${JSON.stringify(edit_events, null, " ")}`
)
console.log(edit_events_ts.getFullText())
fs.writeFileSync(
Path.join(ctx.cwd, manual_edit_file),
ts_manual_edits_file.getFullText()
)
await devServerAxios.post("/api/dev_package_examples/update", {
dev_package_example_id,
edit_events_last_applied_at:
dev_package_example.edit_events_last_updated_at,
})
}

// Edit the pcb placements object
pcb_placements_ts.replaceWithText(
`pcb_placements: ${JSON.stringify(pcb_placements, null, " ")}`
)

// Save the file

fs.writeFileSync(
Path.join(ctx.cwd, manual_edit_file),
ts_manual_edits_file.getFullText()
)
await devServerAxios.post("/api/dev_package_examples/update", {
dev_package_example_id,
edit_events_last_applied_at:
dev_package_example.edit_events_last_updated_at,
})
}
}
}
Expand Down
Loading

0 comments on commit 73a8de5

Please sign in to comment.