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

Fix stub edges on schematic traces #239

Merged
merged 2 commits into from
Nov 5, 2024
Merged
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
14 changes: 14 additions & 0 deletions lib/components/primitive-components/Trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import type { Net } from "./Net"
import type { Port } from "./Port"
import type { TraceHint } from "./TraceHint"
import { getDominantDirection } from "lib/utils/autorouting/getDominantDirection"
import type { Point } from "@tscircuit/math-utils"
import { getStubEdges } from "lib/utils/schematic/getStubEdges"

type PcbRouteObjective =
| RouteHintPoint
Expand Down Expand Up @@ -672,6 +675,17 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
})
}

// The last edges sometimes don't connect to the ports because the
// autorouter is within the "goal box" and doesn't finish the route
// Add a stub to connect the last point to the end port
const lastEdge = edges[edges.length - 1]
const lastEdgePort = portsWithPosition[portsWithPosition.length - 1]
const lastDominantDirection = getDominantDirection(lastEdge)
// Add the connecting edges
edges.push(
...getStubEdges({ lastEdge, lastEdgePort, lastDominantDirection }),
)

const trace = db.schematic_trace.insert({
source_trace_id: this.source_trace_id!,
edges,
Expand Down
23 changes: 23 additions & 0 deletions lib/utils/autorouting/getDominantDirection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Point } from "@tscircuit/math-utils"

export function getDominantDirection(edge: {
from: Point
to: Point
}): "right" | "left" | "up" | "down" {
const delta = {
x: edge.to.x - edge.from.x,
y: edge.to.y - edge.from.y,
}

// Use absolute values to compare magnitude of x and y movement
const absX = Math.abs(delta.x)
const absY = Math.abs(delta.y)

// If horizontal movement is greater than vertical
if (absX > absY) {
return delta.x > 0 ? "right" : "left"
}

// If vertical movement is greater than or equal to horizontal
return delta.y > 0 ? "down" : "up"
}
53 changes: 53 additions & 0 deletions lib/utils/schematic/getStubEdges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Point } from "@tscircuit/math-utils"

type Edge = {
from: Point
to: Point
}

export const getStubEdges = ({
lastEdge,
lastEdgePort,
lastDominantDirection,
}: {
lastEdge: Edge
lastEdgePort: { position: Point }
lastDominantDirection: "left" | "right" | "up" | "down"
}) => {
const edges: Edge[] = []
if (lastEdge && lastEdgePort) {
const intermediatePoint = { x: lastEdge.to.x, y: lastEdge.to.y }

// If the last movement was horizontal (left/right)
if (lastDominantDirection === "left" || lastDominantDirection === "right") {
// First continue horizontally to align with port's x-position
intermediatePoint.x = lastEdgePort.position.x
edges.push({
from: lastEdge.to,
to: { ...intermediatePoint },
})

// Then move vertically to reach port's y-position
edges.push({
from: intermediatePoint,
to: { ...lastEdgePort.position },
})
}
// If the last movement was vertical (up/down)
else {
// First continue vertically to align with port's y-position
intermediatePoint.y = lastEdgePort.position.y
edges.push({
from: lastEdge.to,
to: { ...intermediatePoint },
})

// Then move horizontally to reach port's x-position
edges.push({
from: intermediatePoint,
to: { ...lastEdgePort.position },
})
}
}
return edges
}
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading