@@ -3,6 +3,7 @@ import type { AnyCircuitElement, PcbTrace, PcbVia } from "circuit-json"
33import type { Obstacle , SimpleRouteJson , SimplifiedPcbTrace } from "../types"
44import type { HighDensityRoute } from "../types/high-density-types"
55import { getConnectionPointLayers } from "../types/srj-types"
6+ import { getViaLayers } from "./getViaLayers"
67import { mapZToLayerName } from "./mapZToLayerName"
78import type { LayerName } from "./mapZToLayerName"
89import { pointToBoxDistance } from "@tscircuit/math-utils"
@@ -57,6 +58,7 @@ function convertHdRouteToCircuitJsonTraces(
5758 baseId : string ,
5859 connectionName : string ,
5960 width = 0.1 ,
61+ layerCount : number ,
6062) : PcbTrace [ ] {
6163 const traces : PcbTrace [ ] = [ ]
6264
@@ -75,7 +77,7 @@ function convertHdRouteToCircuitJsonTraces(
7577 x : point . x ,
7678 y : point . y ,
7779 width,
78- layer : mapZToLayerName ( point . z , 2 ) ,
80+ layer : mapZToLayerName ( point . z , layerCount ) ,
7981 ...( isFirstPoint && ( point as any ) . pcb_port_id
8082 ? { start_pcb_port_id : ( point as any ) . pcb_port_id }
8183 : { } ) ,
@@ -148,7 +150,7 @@ function convertHdRouteToCircuitJsonTraces(
148150 x : point . x ,
149151 y : point . y ,
150152 width,
151- layer : mapZToLayerName ( point . z , 2 ) ,
153+ layer : mapZToLayerName ( point . z , layerCount ) ,
152154 ...( isFirstPoint && ( point as any ) . pcb_port_id
153155 ? { start_pcb_port_id : ( point as any ) . pcb_port_id }
154156 : { } ) ,
@@ -179,7 +181,7 @@ function convertHdRouteToCircuitJsonTraces(
179181 x : point . x ,
180182 y : point . y ,
181183 width,
182- layer : mapZToLayerName ( point . z , 2 ) ,
184+ layer : mapZToLayerName ( point . z , layerCount ) ,
183185 ...( isLastPoint && ( point as any ) . pcb_port_id
184186 ? { end_pcb_port_id : ( point as any ) . pcb_port_id }
185187 : { } ) ,
@@ -454,6 +456,7 @@ function createPcbPadElements(srj: SimpleRouteJson): AnyCircuitElement[] {
454456 */
455457function extractViasFromRoutes (
456458 routes : SimplifiedPcbTrace [ ] | HighDensityRoute [ ] ,
459+ layerCount : number ,
457460 minViaDiameter = 0.3 ,
458461) : PcbVia [ ] {
459462 const vias : PcbVia [ ] = [ ]
@@ -476,7 +479,7 @@ function extractViasFromRoutes(
476479 y : segment . y ,
477480 outer_diameter : viaDiameter ,
478481 hole_diameter : viaDiameter * 0.5 ,
479- layers : [ segment . from_layer , segment . to_layer ] as LayerName [ ] ,
482+ layers : getViaLayers ( segment , layerCount ) as LayerName [ ] ,
480483 } )
481484 viaLocations . add ( locationKey )
482485 }
@@ -498,8 +501,8 @@ function extractViasFromRoutes(
498501 Math . abs ( prevPoint . x - currPoint . x ) < 0.01 &&
499502 Math . abs ( prevPoint . y - currPoint . y ) < 0.01
500503 ) {
501- const fromLayer = mapZToLayerName ( prevPoint . z , 2 )
502- const toLayer = mapZToLayerName ( currPoint . z , 2 )
504+ const fromLayer = mapZToLayerName ( prevPoint . z , layerCount )
505+ const toLayer = mapZToLayerName ( currPoint . z , layerCount )
503506 const locationKey = `${ currPoint . x } ,${ currPoint . y } ,${ fromLayer } ,${ toLayer } `
504507
505508 if ( ! viaLocations . has ( locationKey ) ) {
@@ -511,7 +514,10 @@ function extractViasFromRoutes(
511514 y : currPoint . y ,
512515 outer_diameter : viaDiameter ,
513516 hole_diameter : viaDiameter * 0.5 ,
514- layers : [ fromLayer , toLayer ] as LayerName [ ] ,
517+ layers : getViaLayers (
518+ { from_layer : fromLayer , to_layer : toLayer } ,
519+ layerCount ,
520+ ) as LayerName [ ] ,
515521 } )
516522 viaLocations . add ( locationKey )
517523 }
@@ -550,7 +556,13 @@ export function convertToCircuitJson(
550556 circuitJson . push ( ...createPcbPadElements ( srjWithPointPairs ) )
551557
552558 // Extract and add vias as independent pcb_via elements
553- circuitJson . push ( ...extractViasFromRoutes ( routes , minViaDiameter ) )
559+ circuitJson . push (
560+ ...extractViasFromRoutes (
561+ routes ,
562+ srjWithPointPairs . layerCount ,
563+ minViaDiameter ,
564+ ) ,
565+ )
554566
555567 // Build a map of connection names to simplify lookups
556568 const connectionMap = new Map < string , string > ( )
@@ -583,6 +595,7 @@ export function convertToCircuitJson(
583595 `trace_${ index } ` ,
584596 connectionMap . get ( connectionName ) || connectionName ,
585597 minTraceWidth ,
598+ srjWithPointPairs . layerCount ,
586599 )
587600 circuitJson . push ( ...( traces as AnyCircuitElement [ ] ) )
588601 } )
0 commit comments