Skip to content
Open
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
3 changes: 3 additions & 0 deletions lib/analyze-schematic-placement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SchematicBoxInnerLabelCollisionSolver } from "./solvers/SchematicBoxInn
import { SchematicBoxTooWideSolver } from "./solvers/SchematicBoxTooWideSolver/SchematicBoxTooWideSolver"
import { SchematicPinPaddingToEdgeSolver } from "./solvers/SchematicPinPaddingToEdgeSolver/SchematicPinPaddingToEdgeSolver"
import { DiodeResistorAlignmentSolver } from "./solvers/DiodeResistorAlignmentSolver/DiodeResistorAlignmentSolver"
import { NetLabelOrientationSolver } from "./solvers/NetLabelOrientationSolver/NetLabelOrientationSolver"
import type {
SchematicBoxPlacementLineItem,
SchematicPlacementIssue,
Expand Down Expand Up @@ -55,6 +56,8 @@ export class SchematicPlacementAnalysis {
return SchematicPinPaddingToEdgeSolver.issueToString(issue)
case "DiodeResistorNotAligned":
return DiodeResistorAlignmentSolver.issueToString(issue)
case "NetLabelOrientationUnreadable":
return NetLabelOrientationSolver.issueToString(issue)
default:
return ""
}
Expand Down
116 changes: 116 additions & 0 deletions lib/solvers/NetLabelOrientationSolver/NetLabelOrientationSolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { BaseSolver } from "@tscircuit/solver-utils"
import type { CircuitJson, SchematicNetLabel } from "circuit-json"
import type {
SchematicPlacementIssue,
NetLabelOrientationUnreadable,
} from "../../types"
import type { SolverContext } from "../SolverContext"
import { addAttr } from "../../utils/format"

interface NetLabel {
schematicNetLabelId: string
text: string
centerX: number
centerY: number
anchorX?: number
anchorY?: number
anchorSide: "top" | "bottom" | "left" | "right"
angleDeg: number
}

export class NetLabelOrientationSolver extends BaseSolver {
private readonly netLabels: NetLabel[]
private currentIndex = 0
private readonly out: SchematicPlacementIssue[]

constructor({
ctx,
issues: out,
}: {
ctx: SolverContext
issues: SchematicPlacementIssue[]
}) {
super()
this.out = out
this.netLabels = this.computeNetLabels(ctx.circuitJson)
this.solved = this.netLabels.length === 0
}

override _step(): void {
const label = this.netLabels[this.currentIndex++]
if (!label) {
this.solved = true
return
}
this.solved = this.currentIndex >= this.netLabels.length

if (this.isUpsideDown(label.angleDeg)) {
this.out.push({
lineItemType: "NetLabelOrientationUnreadable",
schematicNetLabelId: label.schematicNetLabelId,
text: label.text,
anchorSide: label.anchorSide,
currentAngleDeg: label.angleDeg,
normalizedAngleDeg: this.normalizeAngle(label.angleDeg),
})
}
}

static issueToString(issue: NetLabelOrientationUnreadable): string {
const attrs: string[] = []
addAttr(attrs, "text", issue.text)
addAttr(attrs, "anchorSide", issue.anchorSide)
addAttr(attrs, "currentAngleDeg", issue.currentAngleDeg)
addAttr(attrs, "normalizedAngleDeg", issue.normalizedAngleDeg)
return `<NetLabelOrientationUnreadable ${attrs.join(" ")} />`
}

private isSchematicNetLabel(
el: CircuitJson[number],
): el is SchematicNetLabel {
return el.type === "schematic_net_label"
}

private computeAngleDeg(
center: { x: number; y: number },
anchor: { x: number; y: number },
): number {
return (
Math.atan2(anchor.y - center.y, anchor.x - center.x) * (180 / Math.PI)
)
}

private isUpsideDown(angleDeg: number): boolean {
return angleDeg < -90 || angleDeg > 90
}

private normalizeAngle(angleDeg: number): number {
const shifted = (angleDeg + 90) % 180
const positive = shifted < 0 ? shifted + 180 : shifted
return positive - 90
}

private computeNetLabels(circuitJson: CircuitJson): NetLabel[] {
const labels: NetLabel[] = []

for (const el of circuitJson) {
if (!this.isSchematicNetLabel(el)) continue
if (!el.anchor_position) continue

const angleDeg = this.computeAngleDeg(el.center, el.anchor_position)

labels.push({
schematicNetLabelId: el.schematic_net_label_id,
text: el.text,
centerX: el.center.x,
centerY: el.center.y,
anchorX: el.anchor_position.x,
anchorY: el.anchor_position.y,
anchorSide: el.anchor_side,
angleDeg,
})
}

return labels
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SchematicBoxTooWideSolver } from "../SchematicBoxTooWideSolver/Schemati
import { SchematicPinPaddingToEdgeSolver } from "../SchematicPinPaddingToEdgeSolver/SchematicPinPaddingToEdgeSolver"
import { VerboseNetLabelSolver } from "../VerboseNetLabelSolver/VerboseNetLabelSolver"
import { DiodeResistorAlignmentSolver } from "../DiodeResistorAlignmentSolver/DiodeResistorAlignmentSolver"
import { NetLabelOrientationSolver } from "../NetLabelOrientationSolver/NetLabelOrientationSolver"

type SolverParams = { ctx: SolverContext; issues: SchematicPlacementIssue[] }

Expand Down Expand Up @@ -43,6 +44,13 @@ export class SchematicPlacementPipeline extends BasePipelineSolver<CircuitJson>
{ ctx: p.ctx, issues: p.issues },
],
),
definePipelineStep(
"NetLabelOrientationSolver",
NetLabelOrientationSolver,
(p: SchematicPlacementPipeline): [SolverParams] => [
{ ctx: p.ctx, issues: p.issues },
],
),
definePipelineStep(
"SchematicBoxTooWideSolver",
SchematicBoxTooWideSolver,
Expand Down
10 changes: 10 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ export interface DiodeResistorNotAligned {
message: string
}

export interface NetLabelOrientationUnreadable {
lineItemType: "NetLabelOrientationUnreadable"
schematicNetLabelId: string
text: string
anchorSide: SchematicSide
currentAngleDeg: number
normalizedAngleDeg: number
}

export type SchematicPlacementIssue =
| ComponentOverlap
| SchematicBoxHasALotOfSurroundingWhitespace
Expand All @@ -120,6 +129,7 @@ export type SchematicPlacementIssue =
| SchematicBoxInnerLabelCollision
| SchematicPinPaddingToEdgeTooLarge
| DiodeResistorNotAligned
| NetLabelOrientationUnreadable

export interface SchematicPlacementIssues {
lineItemType: "SchematicPlacementIssues"
Expand Down
37 changes: 37 additions & 0 deletions tests/assets/net-label-orientation-circuits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { CircuitJson } from "circuit-json"

export const unreadableNetLabelCircuitJson: CircuitJson = [
{
type: "source_net",
source_net_id: "net_GND",
name: "GND",
member_source_group_ids: [],
},
{
type: "schematic_net_label",
schematic_net_label_id: "schematic_net_label_GND",
source_net_id: "net_GND",
center: { x: 0, y: 0 },
anchor_position: { x: -1, y: 1 },
anchor_side: "left",
text: "GND",
},
]

export const readableNetLabelCircuitJson: CircuitJson = [
{
type: "source_net",
source_net_id: "net_V3_3",
name: "V3_3",
member_source_group_ids: [],
},
{
type: "schematic_net_label",
schematic_net_label_id: "schematic_net_label_V3_3",
source_net_id: "net_V3_3",
center: { x: 0, y: 0 },
anchor_position: { x: 0, y: -1 },
anchor_side: "bottom",
text: "V3_3",
},
]
4 changes: 3 additions & 1 deletion tests/cases/__snapshots__/diode-resistor-misaligned.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/cases/__snapshots__/led-resistor-alignment-2.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/cases/__snapshots__/led-resistor-alignment.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions tests/cases/__snapshots__/net-label-readable-orientation.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading