Skip to content

Commit

Permalink
Merge pull request #206 from tscircuit/fix/shifting-port-position
Browse files Browse the repository at this point in the history
Adding the missing trace space between the ports and route
  • Loading branch information
imrishabh18 authored Oct 28, 2024
2 parents 6acfccd + e624604 commit ee7bfe4
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 70 deletions.
3 changes: 2 additions & 1 deletion lib/components/primitive-components/Port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ export class Port extends PrimitiveComponent<typeof portProps> {
return { x: 0, y: 0 }
}

const offsetY = -0.045
const transform = compose(
this.parent!.computeSchematicGlobalTransform(),
translate(-symbol.center.x, -symbol.center.y),
translate(-symbol.center.x, -symbol.center.y + offsetY),
)

return applyToPoint(transform, this.schematicSymbolPortDef)
Expand Down
122 changes: 64 additions & 58 deletions lib/components/primitive-components/Trace.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
import { traceProps } from "@tscircuit/props"
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import type { Port } from "./Port"
import {
MultilayerIjump,
IJumpAutorouter,
autoroute,
getObstaclesFromSoup,
markObstaclesAsConnected,
} from "@tscircuit/infgrid-ijump-astar"
import { traceProps } from "@tscircuit/props"
import type {
AnyCircuitElement,
LayerRef,
PCBTrace,
RouteHintPoint,
SchematicTrace,
SourceTrace,
} from "circuit-json"
import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
import { DirectLineRouter } from "lib/utils/autorouting/DirectLineRouter"
import type {
Obstacle,
SimpleRouteConnection,
SimpleRouteJson,
SimplifiedPcbTrace,
} from "lib/utils/autorouting/SimpleRouteJson"
import { computeObstacleBounds } from "lib/utils/autorouting/computeObstacleBounds"
import { projectPointInDirection } from "lib/utils/projectPointInDirection"
import type { TraceHint } from "./TraceHint"
import {
findPossibleTraceLayerCombinations,
type CandidateTraceLayerCombination,
} from "lib/utils/autorouting/findPossibleTraceLayerCombinations"
import { pairs } from "lib/utils/pairs"
import { findPossibleTraceLayerCombinations } from "lib/utils/autorouting/findPossibleTraceLayerCombinations"
import { mergeRoutes } from "lib/utils/autorouting/mergeRoutes"
import type { Net } from "./Net"
import { getClosest } from "lib/utils/getClosest"
import { z } from "zod"
import { createNetsFromProps } from "lib/utils/components/createNetsFromProps"
import {
isMatchingPathSelector,
isMatchingSelector,
} from "lib/utils/selector-matching"
import { getClosest } from "lib/utils/getClosest"
import { pairs } from "lib/utils/pairs"
import { projectPointInDirection } from "lib/utils/projectPointInDirection"
import { projectPointInOppositeDirection } from "lib/utils/projectPointInOppositeDirection"
import { tryNow } from "lib/utils/try-now"
import { getFullConnectivityMapFromCircuitJson } from "circuit-json-to-connectivity-map"
import type { Renderable } from "../base-components/Renderable"
import { DirectLineRouter } from "lib/utils/autorouting/DirectLineRouter"
import { z } from "zod"
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import type { Net } from "./Net"
import type { Port } from "./Port"
import type { TraceHint } from "./TraceHint"

type PcbRouteObjective =
| RouteHintPoint
Expand Down Expand Up @@ -600,6 +588,7 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
pointsToConnect: [],
}

// Add obstacles from components and ports
for (const elm of db.toArray()) {
if (elm.type === "schematic_component") {
obstacles.push({
Expand All @@ -623,18 +612,26 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
}
}

for (const { port } of ports) {
connection.pointsToConnect.push({
...port._getGlobalSchematicPositionAfterLayout(),
...projectPointInDirection(
port._getGlobalSchematicPositionAfterLayout(),
port.facingDirection!,
0.1501,
),
layer: "top",
})
// Get port positions for later use
const portsWithPosition = ports.map(({ port }) => ({
port,
position: port._getGlobalSchematicPositionAfterLayout(),
schematic_port_id: port.schematic_port_id ?? undefined,
facingDirection: port.facingDirection,
}))

// Ensure there are at least two ports
// Else return insufficient ports to draw a trace
if (portsWithPosition.length < 2) {
return
}

// Add points for autorouter to connect
connection.pointsToConnect = portsWithPosition.map(({ position }) => ({
...position,
layer: "top",
}))

const bounds = computeObstacleBounds(obstacles)

const simpleRouteJsonInput: SimpleRouteJson = {
Expand All @@ -645,21 +642,6 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
layerCount: 1,
}

// For each obstacle, create a schematic_debug_object
if (this.getSubcircuit().props._schDebugObjectsEnabled) {
for (const obstacle of obstacles) {
db.schematic_debug_object.insert({
shape: "rect",
center: obstacle.center,
size: {
width: obstacle.width,
height: obstacle.height,
},
label: "obstacle",
} as any) // TODO issue with discriminated union
}
}

let Autorouter = MultilayerIjump
if (this.getSubcircuit().props._schDirectLineRoutingEnabled) {
Autorouter = DirectLineRouter as any
Expand All @@ -674,25 +656,49 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
if (results.length === 0) return

const [result] = results

const { route } = result

const edges: SchematicTrace["edges"] = []

// Add autorouted path
for (let i = 0; i < route.length - 1; i++) {
const from = route[i]
const to = route[i + 1]

edges.push({
from,
to,
// TODO to_schematic_port_id and from_schematic_port_id
from: route[i],
to: route[i + 1],
})
}

// Add small segments at the ends to connect to ports
const STUB_LENGTH = 0.15

// First port stub
edges.unshift({
from: {
...projectPointInDirection(
route[0],
portsWithPosition[0].facingDirection!,
STUB_LENGTH,
),
},
to: route[0],
from_schematic_port_id: portsWithPosition[0].schematic_port_id!,
})

// Last port stub
edges.push({
from: route[route.length - 1],
to: {
...projectPointInOppositeDirection(
route[route.length - 1],
portsWithPosition[1].facingDirection!,
STUB_LENGTH,
),
},
from_schematic_port_id: portsWithPosition[1].schematic_port_id!,
})

const trace = db.schematic_trace.insert({
source_trace_id: this.source_trace_id!,

edges,
})

Expand Down
8 changes: 4 additions & 4 deletions lib/utils/projectPointInDirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export const projectPointInDirection = (
) => {
switch (direction) {
case "up":
return { x: point.x, y: point.y - distance }
case "down":
return { x: point.x, y: point.y + distance }
case "down":
return { x: point.x, y: point.y - distance }
case "left":
return { x: point.x - distance, y: point.y }
case "right":
return { x: point.x + distance, y: point.y }
case "right":
return { x: point.x - distance, y: point.y }
default:
throw new Error(`Unknown direction "${direction}"`)
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/projectPointInOppositeDirection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const projectPointInOppositeDirection = (
point: { x: number; y: number },
direction: "up" | "down" | "left" | "right",
distance: number,
) => {
switch (direction) {
case "up":
return { x: point.x, y: point.y + distance }
case "down":
return { x: point.x, y: point.y - distance }
case "left":
return { x: point.x + distance, y: point.y }
case "right":
return { x: point.x - distance, y: point.y }
default:
throw new Error(`Unknown direction "${direction}"`)
}
}
Loading

0 comments on commit ee7bfe4

Please sign in to comment.