seveibar/pd-dual-stepper-controller
Bottom-side PCB silkscreen artwork consisting of embedded mirrored TS Circuit SVG logos, with via-clearance-aware lower-board placement.
- Version
- 0.1.0
- License
- unset
- Stars
- 0
mechanical/apply-compact-placement.ts
import {Children,cloneElement,isValidElement,type ReactNode} from "react";
import positions from "./compact-placement.json";
/** Apply board-absolute placement after macro expansion, preserving pin/footprint geometry. */
export function applyCompactPlacement(node: ReactNode, origin={x:0,y:0}): ReactNode {
if (!isValidElement<any>(node)) return node;
const el=node as any;
const props=el.props;
if (node.type === "group" || node.type === "board") {
const next={x:origin.x+parseFloat(props.pcbX??0),y:origin.y+parseFloat(props.pcbY??0)};
return cloneElement(el,{},Children.map(props.children,c=>applyCompactPlacement(c,next)));
}
if (node.type === "silkscreentext" && ["BOOT","RUN","PWR","USB-C"].includes(props.text)) return null;
// Route the short local loops before the global router. Capacitor and shunt
// orientation is chosen so these escapes remain on top copper.
if (node.type === "trace") {
if (["ISEN_A","ISEN_B","ISEN_A_M2","ISEN_B_M2"].includes(props.name))
{
const suffix=props.name.endsWith("_M2") ? "_M2" : "";
const a=props.name.startsWith("ISEN_A");
return cloneElement(el,{pcbPathRelativeTo:`.DRIVER${suffix} > .${a ? "AISEN" : "BISEN"}`,pcbPath:[{x:a ? -1.037553 : 1.137553,y:-4.4434775}],thickness:"0.4mm"});
}
if (props.name === "DVDD1_CAP") return cloneElement(el,{pcbPathRelativeTo:".C_DVDD1 > .pin1",pcbPath:[{x:-1.1,y:-.399848}]});
if (["CURRENT_A_BYPASS","CURRENT_B_BYPASS"].includes(props.name)) {
const phase=props.name.split("_")[1];
return cloneElement(el,{pcbPathRelativeTo:`.C_CURRENT_${phase} > .pin1`,pcbPath:[{x:-.51,y:-2.065}],thickness:"0.3mm"});
}
if (["VM_BULK","VM_BULK_M2"].includes(props.name)) {
const suffix=props.name.endsWith("_M2") ? "_M2" : "";
// Motor 2's bulk connection crosses on bottom so the charge-pump pins
// can escape on top. The length budget includes two 1.6 mm via barrels;
// its high-frequency bypass remains directly beside the supply pin.
const path = suffix ? [
{x:-2.275078,y:-4.1},
{x:-2.275078,y:-4.1,via:true,fromLayer:"top",toLayer:"bottom"},
{x:-2.275078,y:-4.1},{x:-5.7,y:-4.1},
{x:-5.7,y:-4.1,via:true,fromLayer:"bottom",toLayer:"top"},
{x:-5.7,y:-4.1},
] : [{x:-2.275078,y:-4.1},{x:-5.7,y:-4.1}];
return cloneElement(el,{pcbPathRelativeTo:`.DRIVER${suffix} > .VM`,pcbPath:path,thickness:"0.4mm",...(suffix ? {maxLength:"11mm"} : {})});
}
if (["BUZZER_DECOUPLE","PD_VDD_DECOUPLE"].includes(props.name))
return cloneElement(el,{pcbStraightLine:true,thickness:"0.3mm"});
}
const component=["chip","resistor","capacitor","diode","transistor","mosfet","connector","pushbutton","led","inductor"].includes(node.type as string);
const silk=component ? {pcbSx:{...props.pcbSx,"& silkscreentext":{visibility:"hidden"}}} : {};
const p=(positions as Record<string,{pcbX:number;pcbY:number;pcbRotation:number}>)[props.name];
return p ? cloneElement(el,{...silk,...p,pcbX:p.pcbX-origin.x,pcbY:p.pcbY-origin.y}) : component ? cloneElement(el,silk) : node;
}