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

Add schWidth prop with a test #87

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 28 additions & 42 deletions src/lib/builder/component-builder/BugBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import getPortPosition, {
DEFAULT_PIN_SPACING,
getPortArrangementSize,
getPortIndices,
type PortArrangement,
} from "../../utils/get-port-position"
import { associatePcbPortsWithPads } from "../footprint-builder/associate-pcb-ports-with-pads"
import type { ProjectBuilder } from "../project-builder"
import { transformSchematicElements } from "../transform-elements"
import {
ComponentBuilderClass,
type BaseComponentBuilder,
ComponentBuilderClass,
} from "./ComponentBuilder"

const debug = Debug("tscircuit:builder:bug-builder")
Expand All @@ -28,7 +27,7 @@ export interface BugBuilder extends BaseComponentBuilder<BugBuilder> {
properties: Except<
SourceSimpleBugInput,
"type" | "source_component_id" | "ftype" | "name"
> & { name?: string }
> & { name?: string; schWidth?: number }
): BugBuilder
}

Expand All @@ -44,7 +43,12 @@ export class BugBuilderClass
...this.source_properties,
ftype: "simple_bug",
}
this.settable_schematic_properties.push("port_labels", "port_arrangement")
this.settable_schematic_properties.push(
"port_labels",
"port_arrangement",
"pin_spacing",
"schWidth"
)
}

setSourceProperties(props) {
Expand All @@ -55,6 +59,14 @@ export class BugBuilderClass
return this
}

setSchematicProperties(props) {
this.schematic_properties = {
...this.schematic_properties,
...props,
}
return this
}

async build(bc): Promise<Type.AnyElement[]> {
const elements: Type.AnyElement[] = []
const { project_builder } = this
Expand All @@ -73,42 +85,33 @@ export class BugBuilderClass
}
elements.push(source_component)

let port_arrangement: PortArrangement =
this.schematic_properties?.port_arrangement

/** This can be used as a fallback if pinLabels or a port arrangement aren't explicitly given */
const footprintPinLabels = this.footprint.getFootprintPinLabels()
const footprintPinCount = Object.entries(footprintPinLabels).length
const port_arrangement = this.schematic_properties?.port_arrangement

if (!port_arrangement) {
if (footprintPinCount === 0) {
throw new Error(
"port_arrangement/schPortArrangement is required when building a <bug /> without a footprint (footprint has no pins)"
)
}
port_arrangement = {
left_size: Math.floor(footprintPinCount / 2 + 0.500001),
right_size: Math.floor(footprintPinCount / 2),
}
throw new Error("port_arrangement is required when building a <bug />")
}

const pin_spacing =
this.schematic_properties?.pin_spacing ?? DEFAULT_PIN_SPACING

const extended_port_arrangement = {
...port_arrangement,
pin_spacing: this.schematic_properties.pin_spacing,
schWidth: this.source_properties.schPortArrangement.schWidth,
}

const port_arrangement_size = getPortArrangementSize(
extended_port_arrangement
)

const schematic_component: Soup.SchematicComponent = {
type: "schematic_component",
source_component_id,
schematic_component_id,
rotation: this.schematic_rotation ?? 0,
size: {
width: port_arrangement_size.width - pin_spacing,
height: port_arrangement_size.height - pin_spacing,
width: port_arrangement_size.width,
height: port_arrangement_size.height,
},
center: this.schematic_position || { x: 0, y: 0 },
...this.schematic_properties,
Expand All @@ -122,20 +125,14 @@ export class BugBuilderClass
const textElements: SchematicText[] = []

// add ports based on port arrangement and give appropriate labels
let { port_labels } = this.schematic_properties
const { port_labels } = this.schematic_properties
const { total_ports } = port_arrangement_size

if (!port_labels) {
if (footprintPinCount === 0) {
throw new Error(
"port_labels/pinLabels is required when building a <bug /> without a footprint (footprint has no pins)"
)
}

port_labels = footprintPinLabels
debug("inferring port_labels from footprint pin labels")
throw new Error("port_labels is required when building a <bug />")
}

const port_indices = getPortIndices(port_arrangement)
const port_indices = getPortIndices(extended_port_arrangement)
for (const pn of port_indices) {
const portPosition = getPortPosition(extended_port_arrangement, pn)
this.ports.addPort({
Expand All @@ -155,9 +152,7 @@ export class BugBuilderClass
schematic_component_id,
text: port_labels[pn],
anchor: is_left ? "left" : "right",

rotation: 0,

position: {
x: portPosition.x + (is_left ? 0.8 : -0.8) * pin_spacing,
y: portPosition.y,
Expand All @@ -173,7 +168,6 @@ export class BugBuilderClass
text: port_labels[pn],
anchor: "right",
rotation: Math.PI / 2,

position: {
x: portPosition.x,
y: portPosition.y - 0.4,
Expand All @@ -189,7 +183,6 @@ export class BugBuilderClass
text: port_labels[pn],
anchor: "left",
rotation: Math.PI / 2,

position: {
x: portPosition.x,
y: portPosition.y + 0.4,
Expand Down Expand Up @@ -229,13 +222,6 @@ export class BugBuilderClass

associatePcbPortsWithPads(elements)

// TODO use this standard method:
// matchPCBPortsWithFootprintAndMutate({
// footprint_elements,
// pcb_ports: elements.filter((elm) => elm.type === "pcb_port"),
// source_ports: elements.filter((elm) => elm.type === "source_port"),
// } as any)

elements.push(
...this._getCadElements({ source_component_id, pcb_component }, bc)
)
Expand Down
1 change: 1 addition & 0 deletions src/lib/builder/component-builder/remap-prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const remapProp = (prop: string, val: any): [string, any] => {
right_side: val.rightSide,
top_side: val.topSide,
bottom_side: val.bottomSide,
schWidth: val.schWidth,
}),
]
case "pcbX":
Expand Down
11 changes: 8 additions & 3 deletions src/lib/utils/get-port-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type PortArrangement = SideSizes | ExplicitPinMappingArrangement

export type ExtendedPortArrangement = PortArrangement & {
pin_spacing?: number
schWidth?: number
}

export const hasExplicitPinMapping = (
Expand Down Expand Up @@ -162,18 +163,22 @@ export const getPortArrangementSize = (

const total_ports = top_size + right_size + bottom_size + left_size

const width = Math.max(
// MIN_SIDE_DIST is multiplied by the ratio of pin spacing to create more
// square-like bugs
const calculatedWidth = Math.max(
MIN_SIDE_DIST * (pinSpacing / DEFAULT_PIN_SPACING),
(top_size + 1) * pinSpacing,
(bottom_size + 1) * pinSpacing
)

const width =
port_arrangement.schWidth !== undefined
? port_arrangement.schWidth
: calculatedWidth
const height = Math.max(
MIN_SIDE_DIST,
(left_size + 1) * pinSpacing,
(right_size + 1) * pinSpacing
)

return { width, height, total_ports }
}

Expand Down
48 changes: 48 additions & 0 deletions tests/bug-builder/bug-with-width-resizing.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import test from "ava"
import { getTestFixture } from "../fixtures/get-test-fixture"

test("bug with width resizing", async (t) => {
const { logSoup, pb } = await getTestFixture(t)

const soup = await pb
.add("board", (board) =>
board
.setProps({
pcbX: 0,
pcbY: 0,
width: 10,
height: 10,
})
.add("bug", (bug) =>
bug.setProps({
schPinLabels: {
1: "P1",
2: "P2",
3: "P3",
4: "P4",
5: "P5",
6: "P6",
7: "P7",
8: "P8",
},
schPortArrangement: {
leftSize: 4,
rightSize: 4,
schWidth: 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isnt this supposed the be a top level prop? Why is it inside portArrangement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a pretty good PR, but i think it would be better to modify getPortArrangementSize to support taking a schWidth prop, transforming things can make the code more complicated over time since it's not clear where the original width is.

I thought you meant that I needed to add it there.

Copy link
Author

@Slaviiiii Slaviiiii Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the bug with schWidth set to 2

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it looks broken, the ports should be outside of the box

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are like glued - the box changes its width, but they do not move

image

},
pcbRotation: "90deg",
cadModel: {
objUrl:
"https://modelcdn.tscircuit.com/easyeda_models/download?pn=C128415&uuid=dc694c23844346e9981bdbac7bb76421",
},
footprint: "soic8_w7.2mm",
pcbX: 3,
pcbY: 3,
})
)
)
.build()

await logSoup(soup)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for the schWidth

t.pass()
})
Loading