-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 7 additions & 52 deletions
59
lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-json-to-circuit-json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,18 @@ | ||
import { fromTriangles, scale, applyToPoint } from "transformation-matrix" | ||
|
||
import type { AnyCircuitElement, PcbBoard } from "circuit-json" | ||
import type { DsnPcb } from "../types" | ||
import type { DsnPcb, DsnSession } from "../types" | ||
import { convertPadstacksToSmtPads } from "./convert-padstacks-to-smtpads" | ||
import { convertWiresToPcbTraces } from "./convert-wire-to-trace" | ||
import { pairs } from "lib/utils/pairs" | ||
import { convertDsnPcbToCircuitJson } from "./convert-dsn-pcb-to-circuit-json" | ||
|
||
export function convertDsnJsonToCircuitJson( | ||
dsnPcb: DsnPcb, | ||
dsnPcb: DsnPcb | DsnSession, | ||
): AnyCircuitElement[] { | ||
const elements: AnyCircuitElement[] = [] | ||
|
||
// TODO use pcb.resolution.unit and pcb.resolution.value | ||
const transformUmToMm = scale(1 / 1000) | ||
|
||
// Add the board | ||
// You must use the dsnPcb.boundary to get the center, width and height | ||
const board: PcbBoard = { | ||
type: "pcb_board", | ||
pcb_board_id: "pcb_board_0", | ||
center: { x: 0, y: 0 }, | ||
width: 10, | ||
height: 10, | ||
thickness: 1.4, | ||
num_layers: 4, | ||
} | ||
if (dsnPcb.structure.boundary.path) { | ||
const boundaryPath = pairs(dsnPcb.structure.boundary.path.coordinates) | ||
const maxX = Math.max(...boundaryPath.map(([x]) => x)) | ||
const minX = Math.min(...boundaryPath.map(([x]) => x)) | ||
const maxY = Math.max(...boundaryPath.map(([, y]) => y)) | ||
const minY = Math.min(...boundaryPath.map(([, y]) => y)) | ||
board.center = applyToPoint(transformUmToMm, { | ||
x: (maxX + minX) / 2, | ||
y: (maxY + minY) / 2, | ||
}) | ||
board.width = (maxX - minX) * transformUmToMm.a | ||
board.height = (maxY - minY) * transformUmToMm.a | ||
} else { | ||
throw new Error( | ||
`Couldn't read DSN boundary, add support for dsnPcb.structure.boundary["${Object.keys(dsnPcb.structure.boundary).join(",")}"]`, | ||
) | ||
} | ||
|
||
elements.push(board) | ||
|
||
// Convert padstacks to SMT pads using the transformation matrix | ||
elements.push(...convertPadstacksToSmtPads(dsnPcb, transformUmToMm)) | ||
|
||
// Convert wires to PCB traces using the transformation matrix | ||
if (dsnPcb.wiring && dsnPcb.network) { | ||
elements.push( | ||
...convertWiresToPcbTraces( | ||
dsnPcb.wiring, | ||
dsnPcb.network, | ||
transformUmToMm, | ||
), | ||
) | ||
if (dsnPcb.is_dsn_pcb) { | ||
return convertDsnPcbToCircuitJson(dsnPcb) | ||
} else if (dsnPcb.is_dsn_session) { | ||
return convertDsnSessionToCircuitJson(dsnPcb) | ||
} | ||
|
||
return elements | ||
} |
63 changes: 63 additions & 0 deletions
63
lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-pcb-to-circuit-json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { fromTriangles, scale, applyToPoint } from "transformation-matrix" | ||
|
||
import type { AnyCircuitElement, PcbBoard } from "circuit-json" | ||
import type { DsnPcb } from "../types" | ||
import { convertPadstacksToSmtPads } from "./convert-padstacks-to-smtpads" | ||
import { convertWiresToPcbTraces } from "./convert-wire-to-trace" | ||
import { pairs } from "lib/utils/pairs" | ||
|
||
export function convertDsnPcbToCircuitJson( | ||
dsnPcb: DsnPcb, | ||
): AnyCircuitElement[] { | ||
const elements: AnyCircuitElement[] = [] | ||
|
||
// TODO use pcb.resolution.unit and pcb.resolution.value | ||
const transformUmToMm = scale(1 / 1000) | ||
|
||
// Add the board | ||
// You must use the dsnPcb.boundary to get the center, width and height | ||
const board: PcbBoard = { | ||
type: "pcb_board", | ||
pcb_board_id: "pcb_board_0", | ||
center: { x: 0, y: 0 }, | ||
width: 10, | ||
height: 10, | ||
thickness: 1.4, | ||
num_layers: 4, | ||
} | ||
if (dsnPcb.structure.boundary.path) { | ||
const boundaryPath = pairs(dsnPcb.structure.boundary.path.coordinates) | ||
const maxX = Math.max(...boundaryPath.map(([x]) => x)) | ||
const minX = Math.min(...boundaryPath.map(([x]) => x)) | ||
const maxY = Math.max(...boundaryPath.map(([, y]) => y)) | ||
const minY = Math.min(...boundaryPath.map(([, y]) => y)) | ||
board.center = applyToPoint(transformUmToMm, { | ||
x: (maxX + minX) / 2, | ||
y: (maxY + minY) / 2, | ||
}) | ||
board.width = (maxX - minX) * transformUmToMm.a | ||
board.height = (maxY - minY) * transformUmToMm.a | ||
} else { | ||
throw new Error( | ||
`Couldn't read DSN boundary, add support for dsnPcb.structure.boundary["${Object.keys(dsnPcb.structure.boundary).join(",")}"]`, | ||
) | ||
} | ||
|
||
elements.push(board) | ||
|
||
// Convert padstacks to SMT pads using the transformation matrix | ||
elements.push(...convertPadstacksToSmtPads(dsnPcb, transformUmToMm)) | ||
|
||
// Convert wires to PCB traces using the transformation matrix | ||
if (dsnPcb.wiring && dsnPcb.network) { | ||
elements.push( | ||
...convertWiresToPcbTraces( | ||
dsnPcb.wiring, | ||
dsnPcb.network, | ||
transformUmToMm, | ||
), | ||
) | ||
} | ||
|
||
return elements | ||
} |
29 changes: 29 additions & 0 deletions
29
lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-session-to-circuit-json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { scale, applyToPoint } from "transformation-matrix" | ||
|
||
import type { AnyCircuitElement, PcbBoard } from "circuit-json" | ||
import type { DsnSession } from "../types" | ||
import { convertPadstacksToSmtPads } from "./convert-padstacks-to-smtpads" | ||
import { convertWiresToPcbTraces } from "./convert-wire-to-trace" | ||
import { pairs } from "lib/utils/pairs" | ||
|
||
export function convertDsnSessionToCircuitJson( | ||
dsnSession: DsnSession, | ||
): AnyCircuitElement[] { | ||
const elements: AnyCircuitElement[] = [] | ||
|
||
const transformUmToMm = scale(1 / 1000) | ||
|
||
const board: PcbBoard = { | ||
type: "pcb_board", | ||
pcb_board_id: "pcb_board_0", | ||
center: { x: 0, y: 0 }, | ||
width: 10, | ||
height: 10, | ||
thickness: 1.4, | ||
num_layers: 4, | ||
} | ||
|
||
// TODO rest of the function... | ||
|
||
return elements | ||
} |