Skip to content

Commit 3e09ba6

Browse files
authored
Merge pull request #17 from AnasSarkiz/main
2 parents 8190d3f + 81868b1 commit 3e09ba6

1 file changed

Lines changed: 102 additions & 10 deletions

File tree

lib/solvers/GlobalDrcForceImproveSolver/GlobalDrcForceImproveSolver.ts

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,71 @@ const getBoardOutlineGraphics = (srj: SimpleRouteJson): GraphicsObject => {
7171
}
7272
}
7373

74+
const getErrorCenter = (error: Record<string, unknown>) => {
75+
const center = error.center ?? error.pcb_center
76+
if (!center || typeof center !== "object") return undefined
77+
const maybeCenter = center as Record<string, unknown>
78+
return typeof maybeCenter.x === "number" && typeof maybeCenter.y === "number"
79+
? { x: maybeCenter.x, y: maybeCenter.y }
80+
: undefined
81+
}
82+
83+
const getDrcErrorKey = (error: Record<string, unknown>) => {
84+
if (typeof error.pcb_error_id === "string" && error.pcb_error_id.length > 0) {
85+
return `id:${error.pcb_error_id}`
86+
}
87+
88+
const center = getErrorCenter(error)
89+
if (!center) return undefined
90+
91+
const message = typeof error.message === "string" ? error.message : ""
92+
const type =
93+
typeof error.type === "string"
94+
? error.type
95+
: typeof error.error_type === "string"
96+
? error.error_type
97+
: "unknown"
98+
99+
return [
100+
`center:${center.x.toFixed(3)},${center.y.toFixed(3)}`,
101+
`type:${type}`,
102+
`message:${message}`,
103+
].join("|")
104+
}
105+
106+
const createDrcIssueCircles = (
107+
initialSnapshot: DrcSnapshot,
108+
currentSnapshot: DrcSnapshot,
109+
): NonNullable<GraphicsObject["circles"]> => {
110+
const currentIssueKeys = new Set(
111+
currentSnapshot.errors
112+
.map((error) => getDrcErrorKey(error))
113+
.filter((key): key is string => Boolean(key)),
114+
)
115+
116+
return initialSnapshot.errors.flatMap((error) => {
117+
const center = getErrorCenter(error)
118+
const key = getDrcErrorKey(error)
119+
if (!center || !key) return []
120+
121+
const fixed = !currentIssueKeys.has(key)
122+
123+
return [
124+
{
125+
center,
126+
radius: 0.18,
127+
fill: fixed ? "rgba(22, 163, 74, 0.18)" : "rgba(147, 51, 234, 0.2)",
128+
stroke: fixed ? "#16a34a" : "#9333ea",
129+
label: fixed ? "fixed-initial-drc" : "initial-drc",
130+
},
131+
]
132+
})
133+
}
134+
74135
const routesToGraphics = (
75136
srj: SimpleRouteJson,
76137
routes: HighDensityRoute[],
138+
drcIssueCircles: NonNullable<GraphicsObject["circles"]> = [],
77139
): GraphicsObject => {
78140
const boardOutlineGraphics = getBoardOutlineGraphics(srj)
79141

@@ -119,15 +181,18 @@ const routesToGraphics = (
119181
}
120182
}),
121183
),
122-
circles: routes.flatMap((route) =>
123-
route.vias.map((via) => ({
124-
center: via,
125-
radius: route.viaDiameter / 2,
126-
fill: "rgba(15, 23, 42, 0.25)",
127-
stroke: "#0f172a",
128-
label: route.connectionName,
129-
})),
130-
),
184+
circles: [
185+
...routes.flatMap((route) =>
186+
route.vias.map((via) => ({
187+
center: via,
188+
radius: route.viaDiameter / 2,
189+
fill: "rgba(15, 23, 42, 0.25)",
190+
stroke: "#0f172a",
191+
label: route.connectionName,
192+
})),
193+
),
194+
...drcIssueCircles,
195+
],
131196
points: srj.connections.flatMap((connection) =>
132197
connection.pointsToConnect.map((point) => ({
133198
x: point.x,
@@ -158,6 +223,7 @@ export class GlobalDrcForceImproveSolver extends BaseSolver {
158223
private drcCountPlateauChecks = 0
159224
private largeBoardBroadFallbackMisses = 0
160225
private outputSnapshot: DrcSnapshot | undefined
226+
private initialVisualizationSnapshot: DrcSnapshot | undefined
161227

162228
constructor(params: GlobalDrcForceImproveSolverParams) {
163229
super()
@@ -471,8 +537,34 @@ export class GlobalDrcForceImproveSolver extends BaseSolver {
471537
return this.outputHdRoutes
472538
}
473539

540+
private getInitialVisualizationSnapshot() {
541+
this.initialVisualizationSnapshot = getDrcSnapshot(
542+
this.srj,
543+
this.inputHdRoutes,
544+
this.drcEvaluator,
545+
)
546+
return this.initialVisualizationSnapshot
547+
}
548+
549+
private getCurrentVisualizationSnapshot() {
550+
if (this.iterations === 0 && this.outputSnapshot === undefined) {
551+
return this.getInitialVisualizationSnapshot()
552+
}
553+
554+
return (
555+
this.outputSnapshot ??
556+
getDrcSnapshot(this.srj, this.outputHdRoutes, this.drcEvaluator)
557+
)
558+
}
559+
474560
override visualize(): GraphicsObject {
475-
return routesToGraphics(this.srj, this.outputHdRoutes)
561+
const initialSnapshot = this.getInitialVisualizationSnapshot()
562+
const currentSnapshot = this.getCurrentVisualizationSnapshot()
563+
return routesToGraphics(
564+
this.srj,
565+
this.outputHdRoutes,
566+
createDrcIssueCircles(initialSnapshot, currentSnapshot),
567+
)
476568
}
477569

478570
override preview(): GraphicsObject {

0 commit comments

Comments
 (0)