Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,94 @@ type NestedGroupMatchpackProxy = {
size: Point
}

type NestedGroupSchematicElements = {
rootSchematicGroup: SchematicGroup
descendantSchematicGroups: SchematicGroup[]
schematicComponents: SchematicComponent[]
}

/**
* Collect the schematic_group and schematic_component elements owned by a
* nested group node within the active sheet-scoped layout tree (schematic
* world coordinates, mm, +X right, +Y up).
*
* Walking `groupTreeNode` includes components inside deeper descendant groups
* (e.g. schematic groups nested under PCB wrapper groups) while staying scoped
* to the active sheet and stopping at collapsed `showAsSchematicBox` groups.
*/
function collectNestedGroupSchematicElements({
db,
groupTreeNode,
schematicSheetId,
}: {
db: CircuitJsonUtilObjects
groupTreeNode: CircuitJsonTreeNode
schematicSheetId?: SchematicSheetId
}): NestedGroupSchematicElements | null {
const rootSourceGroupId = groupTreeNode.sourceGroup?.source_group_id
if (!rootSourceGroupId) return null

const rootSchematicGroup = db.schematic_group?.getWhere?.({
source_group_id: rootSourceGroupId,
})
if (!rootSchematicGroup) return null

const descendantSchematicGroups: SchematicGroup[] = []
const schematicComponents: SchematicComponent[] = []
const seenSchematicGroupIds = new Set<SchematicGroup["schematic_group_id"]>()
const seenSchematicComponentIds = new Set<
SchematicComponent["schematic_component_id"]
>()

const appendGroupComponents = (schematicGroup: SchematicGroup) => {
const components = db.schematic_component.list({
schematic_group_id: schematicGroup.schematic_group_id,
...(schematicSheetId ? { schematic_sheet_id: schematicSheetId } : {}),
})
for (const component of components) {
if (seenSchematicComponentIds.has(component.schematic_component_id)) {
continue
}
seenSchematicComponentIds.add(component.schematic_component_id)
schematicComponents.push(component)
}
}

const visitGroupNode = (
node: CircuitJsonTreeNode,
schematicGroup: SchematicGroup,
isRootGroup: boolean,
) => {
if (seenSchematicGroupIds.has(schematicGroup.schematic_group_id)) return
seenSchematicGroupIds.add(schematicGroup.schematic_group_id)

if (!isRootGroup) {
descendantSchematicGroups.push(schematicGroup)
}

appendGroupComponents(schematicGroup)

if (schematicGroup.show_as_schematic_box) return

for (const childNode of node.childNodes) {
if (childNode.nodeType !== "group" || !childNode.sourceGroup) continue
const childSchematicGroup = db.schematic_group?.getWhere?.({
source_group_id: childNode.sourceGroup.source_group_id,
})
if (!childSchematicGroup) continue
visitGroupNode(childNode, childSchematicGroup, false)
}
}

visitGroupNode(groupTreeNode, rootSchematicGroup, true)

return {
rootSchematicGroup,
descendantSchematicGroups,
schematicComponents,
}
}

const ROTATION_TO_PLACE_SIDE_ON_TOP: Record<AxisDirection, MatchpackRotation> =
{
"y+": 0,
Expand Down Expand Up @@ -480,22 +568,24 @@ function convertTreeToMatchPackInputProblem(
const groupId = getTreeChildChipId(child, index)!
debug(`[${group.name}] Processing nested group: ${groupId}`)

const schematicGroup = db.schematic_group?.getWhere?.({
source_group_id: child.sourceGroup.source_group_id,
const nestedGroupElements = collectNestedGroupSchematicElements({
db,
groupTreeNode: child,
schematicSheetId: opts.schematicSheetId,
})

debug(
`[${group.name}] Found schematic_group for ${groupId}:`,
schematicGroup,
nestedGroupElements?.rootSchematicGroup,
)

if (schematicGroup) {
if (nestedGroupElements) {
const {
rootSchematicGroup: schematicGroup,
schematicComponents: groupComponents,
} = nestedGroupElements
debug(`[${group.name}] Treating group ${groupId} as composite chip`)

const groupComponents = db.schematic_component.list({
schematic_group_id: schematicGroup.schematic_group_id,
})

debug(
`[${group.name}] Group ${groupId} has ${groupComponents.length} components:`,
groupComponents.map((c: any) => c.source_component_id),
Expand Down Expand Up @@ -577,16 +667,14 @@ function convertTreeToMatchPackInputProblem(
})

if (treeNode?.nodeType === "group" && treeNode.sourceGroup) {
const schematicGroup = db.schematic_group?.getWhere?.({
source_group_id: treeNode.sourceGroup.source_group_id,
const nestedGroupElements = collectNestedGroupSchematicElements({
db,
groupTreeNode: treeNode,
schematicSheetId: opts.schematicSheetId,
})

if (schematicGroup) {
const groupComponents = db.schematic_component.list({
schematic_group_id: schematicGroup.schematic_group_id,
})

for (const comp of groupComponents) {
if (nestedGroupElements) {
for (const comp of nestedGroupElements.schematicComponents) {
const sourcePorts = db.source_port.list({
source_component_id: comp.source_component_id,
})
Expand Down Expand Up @@ -1025,19 +1113,23 @@ export function applySchematicMatchPackLayoutToTree<
}
}
} else if (treeNode.nodeType === "group" && treeNode.sourceGroup) {
const schematicGroup = db.schematic_group?.getWhere?.({
source_group_id: treeNode.sourceGroup.source_group_id,
const nestedGroupElements = collectNestedGroupSchematicElements({
db,
groupTreeNode: treeNode,
schematicSheetId: opts.schematicSheetId,
})

if (schematicGroup) {
if (nestedGroupElements) {
const {
rootSchematicGroup: schematicGroup,
descendantSchematicGroups,
schematicComponents: groupComponents,
} = nestedGroupElements

debug(
`Moving group ${chipId} to (${newCenter.x}, ${newCenter.y}) from (${schematicGroup.center?.x}, ${schematicGroup.center?.y})`,
)

const groupComponents = db.schematic_component.list({
schematic_group_id: schematicGroup.schematic_group_id,
})

debug(
`Group ${chipId} has ${groupComponents.length} components to move`,
)
Expand Down Expand Up @@ -1094,6 +1186,15 @@ export function applySchematicMatchPackLayoutToTree<
}
}

for (const descendantGroup of descendantSchematicGroups) {
if (descendantGroup.center) {
descendantGroup.center = {
x: descendantGroup.center.x + positionDelta.x,
y: descendantGroup.center.y + positionDelta.y,
}
}
}

schematicGroup.center = newCenter
if (matchpackProxy) {
schematicGroup.width = matchpackProxy.size.x
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions tests/groups/group-schematic-matchpack-nested-pcb-groups1.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expect, test } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("schematic auto-layout separates groups nested under separate PCB groups", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width={25} height={15} routingDisabled schAutoLayoutEnabled>
<schematicsheet name="main" sheetIndex={1} />
<group name="Left" pcbX={-6} schAutoLayoutEnabled>
<group name="A" schSheetName="main" schAutoLayoutEnabled>
<resistor
name="R1"
resistance="1k"
footprint="0402"
schSheetName="main"
/>
</group>
</group>
<group name="Right" pcbX={6} schAutoLayoutEnabled>
<group name="B" schSheetName="main" schAutoLayoutEnabled>
<resistor
name="R2"
resistance="1k"
footprint="0402"
schSheetName="main"
/>
</group>
</group>
</board>,
)

await circuit.renderUntilSettled()

const r1Source = circuit.db.source_component.getWhere({ name: "R1" })!
const r2Source = circuit.db.source_component.getWhere({ name: "R2" })!
const r1Sch = circuit.db.schematic_component.getWhere({
source_component_id: r1Source.source_component_id,
})!
const r2Sch = circuit.db.schematic_component.getWhere({
source_component_id: r2Source.source_component_id,
})!
const r1Pcb = circuit.db.pcb_component.getWhere({
source_component_id: r1Source.source_component_id,
})!
const r2Pcb = circuit.db.pcb_component.getWhere({
source_component_id: r2Source.source_component_id,
})!
const groupA = circuit.db.source_group.getWhere({ name: "A" })!
const groupB = circuit.db.source_group.getWhere({ name: "B" })!
const schGroupA = circuit.db.schematic_group.getWhere({
source_group_id: groupA.source_group_id,
})!
const schGroupB = circuit.db.schematic_group.getWhere({
source_group_id: groupB.source_group_id,
})!

expect(
Math.hypot(
r1Sch.center.x - r2Sch.center.x,
r1Sch.center.y - r2Sch.center.y,
),
).toBeGreaterThan(1)
expect(schGroupA.center).toEqual(r1Sch.center)
expect(schGroupB.center).toEqual(r2Sch.center)
expect(r1Pcb.center.x).toBeCloseTo(-6, 3)
expect(r2Pcb.center.x).toBeCloseTo(6, 3)

await expect(circuit).toMatchSchematicSnapshot(import.meta.path)
})
84 changes: 84 additions & 0 deletions tests/groups/group-schematic-matchpack-nested-pcb-groups2.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect, test } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("multi-sheet schematic auto-layout scopes nested PCB groups to each sheet", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width={25} height={15} routingDisabled schAutoLayoutEnabled>
<schematicsheet name="power" sheetIndex={1} />
<schematicsheet name="io" sheetIndex={2} />
<group name="Left" pcbX={-6} schAutoLayoutEnabled>
<group name="A" schSheetName="power" schAutoLayoutEnabled>
<resistor
name="R1"
resistance="1k"
footprint="0402"
schSheetName="power"
/>
<capacitor
name="C1"
capacitance="100nF"
footprint="0402"
schSheetName="power"
/>
<trace from=".R1 > .pin2" to=".C1 > .pin1" />
</group>
<group name="C" schSheetName="io" schAutoLayoutEnabled>
<resistor
name="R3"
resistance="10k"
footprint="0402"
schSheetName="io"
/>
</group>
</group>
<group name="Right" pcbX={6} schAutoLayoutEnabled>
<group name="B" schSheetName="power" schAutoLayoutEnabled>
<resistor
name="R2"
resistance="2.2k"
footprint="0402"
schSheetName="power"
/>
</group>
</group>
</board>,
)

await circuit.renderUntilSettled()

const getSchComp = (name: string) => {
const source = circuit.db.source_component.getWhere({ name })!
return circuit.db.schematic_component.getWhere({
source_component_id: source.source_component_id,
})!
}

const r1 = getSchComp("R1")
const c1 = getSchComp("C1")
const r2 = getSchComp("R2")
const r3 = getSchComp("R3")
const powerSheet = circuit.db.schematic_sheet.getWhere({ name: "power" })!
const ioSheet = circuit.db.schematic_sheet.getWhere({ name: "io" })!

expect(r1.schematic_sheet_id).toBe(powerSheet.schematic_sheet_id)
expect(c1.schematic_sheet_id).toBe(powerSheet.schematic_sheet_id)
expect(r2.schematic_sheet_id).toBe(powerSheet.schematic_sheet_id)
expect(r3.schematic_sheet_id).toBe(ioSheet.schematic_sheet_id)

// R1 and C1 remain separated inside A, and B (R2) does not overlap either of them.
expect(
Math.hypot(r1.center.x - c1.center.x, r1.center.y - c1.center.y),
).toBeGreaterThan(1)
expect(
Math.hypot(r2.center.x - r1.center.x, r2.center.y - r1.center.y),
).toBeGreaterThan(1)
expect(
Math.hypot(r2.center.x - c1.center.x, r2.center.y - c1.center.y),
).toBeGreaterThan(1)
// R3 is the sole component on the "io" sheet and stays unaffected by "power" sheet layout.
expect(r3.center).toEqual({ x: 0, y: 0 })

await expect(circuit).toMatchStackedSchematicSnapshot(import.meta.path)
})
Loading