Skip to content

Commit

Permalink
fix: dip width and height unit conversion issue (#187)
Browse files Browse the repository at this point in the history
* fix: dip width and height unit conversion issue

* formatbot: Automatically format code

* variable name update

* variable update

* Center positioning for jscad-electronics

* formatbot: Automatically format code

* test update

---------

Co-authored-by: tscircuitbot <tscircuitbot@users.noreply.github.com>
  • Loading branch information
imrishabh18 and tscircuitbot authored Feb 15, 2025
1 parent 36b3ecd commit 6f4539f
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 32 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@tscircuit/log-soup": "^1.0.2",
"@tscircuit/soup-util": "^0.0.11",
"@tscircuit/soup-util": "^0.0.41",
"@types/bun": "^1.2.2",
"@types/node": "^20.12.13",
"bun-match-svg": "^0.0.6",
Expand Down
52 changes: 33 additions & 19 deletions src/fn/dip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ import type {
PcbFabricationNoteText,
PcbSilkscreenPath,
} from "circuit-json"
import { u_curve } from "../helpers/u-curve"
import { platedhole } from "../helpers/platedhole"
import { type SilkscreenRef, silkscreenRef } from "src/helpers/silkscreenRef"
import { z } from "zod"
import { length } from "circuit-json"
import { platedhole } from "../helpers/platedhole"
import { u_curve } from "../helpers/u-curve"
import type { NowDefined } from "../helpers/zod/now-defined"
import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"

function convertMilToMm(value: string | number): number {
if (typeof value === "string") {
if (value.trim().toLowerCase().endsWith("mil")) {
// Convert mil to mm (1 mil = 0.0254 mm)
const num = Number.parseFloat(value)
return num * 0.0254
}
return Number.parseFloat(value)
}
return Number(value)
}

const lengthInMm = z
.union([z.string(), z.number()])
.transform((val) => convertMilToMm(val))

export const extendDipDef = (newDefaults: { w?: string; p?: string }) =>
z
Expand All @@ -17,30 +32,28 @@ export const extendDipDef = (newDefaults: { w?: string; p?: string }) =>
num_pins: z.number().optional().default(6),
wide: z.boolean().optional(),
narrow: z.boolean().optional(),
w: length.optional(),
p: length.default(length.parse(newDefaults.p ?? "2.54mm")),
id: length.optional(),
od: length.optional(),
w: lengthInMm.optional(),
p: lengthInMm.default(newDefaults.p ?? "2.54mm"),
id: lengthInMm.optional(),
od: lengthInMm.optional(),
})
.transform((v) => {
// Default inner diameter and outer diameter
if (!v.id && !v.od) {
v.id = length.parse("1.0mm")
v.od = length.parse("1.5mm")
v.id = convertMilToMm("1.0mm")
v.od = convertMilToMm("1.5mm")
} else if (!v.id) {
v.id = v.od! * (1.0 / 1.5)
} else if (!v.od) {
v.od = v.id! * (1.5 / 1.0)
}

// Default width (TODO high pin counts should probably be wide?)
if (!v.w) {
if (v.wide) {
v.w = length.parse("600mil")
v.w = convertMilToMm("600mil")
} else if (v.narrow) {
v.w = length.parse("300mil")
v.w = convertMilToMm("300mil")
} else {
v.w = length.parse(newDefaults.w ?? "300mil")
v.w = convertMilToMm(newDefaults.w ?? "300mil")
}
}
return v as NowDefined<typeof v, "w" | "p" | "id" | "od">
Expand Down Expand Up @@ -69,11 +82,12 @@ export const getCcwDipCoords = (
if (isLeft) {
// The y position starts at h/2, then goes down by gap size
// for each pin
return { x: -w / 2, y: h / 2 - (pn - 1) * gs }
} else {
// The y position starts at -h/2, then goes up by gap size
return { x: w / 2, y: -h / 2 + (pn - ph - 1) * gs }
// Adding x padding (0.4) to postion the hole in the center
return { x: -w / 2 - 0.4, y: h / 2 - (pn - 1) * gs }
}
// The y position starts at -h/2, then goes up by gap size
// Adding x padding (0.4) to postion the hole in the center
return { x: w / 2 + 0.4, y: -h / 2 + (pn - ph - 1) * gs }
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/helpers/platedhole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const platedhole = (
od: number | string,
): PcbPlatedHole => {
return {
pcb_plated_hole_id: "",
type: "pcb_plated_hole",
shape: "circle",
x,
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/dip footprint.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

0 comments on commit 6f4539f

Please sign in to comment.